use of io.sloeber.core.api.Serial in project arduino-eclipse-plugin by Sloeber.
the class PlotterView method dispose.
@Override
public void dispose() {
// As we have a listener we need to remove the listener
if ((this.myPlotterListener != null) && (this.mySerial != null)) {
Serial tempSerial = this.mySerial;
this.mySerial = null;
tempSerial.removeListener(this.myPlotterListener);
this.myPlotterListener.dispose();
this.myPlotterListener = null;
this.myPlotter.dispose();
this.myPlotter = null;
}
super.dispose();
}
use of io.sloeber.core.api.Serial in project arduino-eclipse-plugin by Sloeber.
the class SerialMonitor method createPartControl.
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
@Override
public void createPartControl(Composite parent1) {
parent = parent1;
parent1.setLayout(new GridLayout());
GridLayout layout = new GridLayout(5, false);
layout.marginHeight = 0;
layout.marginWidth = 0;
Composite top = new Composite(parent1, SWT.NONE);
top.setLayout(layout);
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
serialPorts = new ComboViewer(top, SWT.READ_ONLY | SWT.DROP_DOWN);
GridData minSizeGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
minSizeGridData.widthHint = 150;
serialPorts.getControl().setLayoutData(minSizeGridData);
serialPorts.setContentProvider(new IStructuredContentProvider() {
@Override
public void dispose() {
// no need to do something here
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// no need to do something here
}
@Override
public Object[] getElements(Object inputElement) {
@SuppressWarnings("unchecked") Map<Serial, SerialListener> items = (Map<Serial, SerialListener>) inputElement;
return items.keySet().toArray();
}
});
serialPorts.setLabelProvider(new LabelProvider());
serialPorts.setInput(serialConnections);
serialPorts.addSelectionChangedListener(new ComPortChanged(this));
sendString = new Text(top, SWT.SINGLE | SWT.BORDER);
sendString.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
lineTerminator = new ComboViewer(top, SWT.READ_ONLY | SWT.DROP_DOWN);
lineTerminator.setContentProvider(new ArrayContentProvider());
lineTerminator.setLabelProvider(new LabelProvider());
lineTerminator.setInput(SerialManager.listLineEndings());
lineTerminator.getCombo().select(MyPreferences.getLastUsedSerialLineEnd());
lineTerminator.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
MyPreferences.setLastUsedSerialLineEnd(lineTerminator.getCombo().getSelectionIndex());
}
});
send = new Button(top, SWT.BUTTON1);
send.setText(Messages.serialMonitorSend);
send.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
int index = lineTerminator.getCombo().getSelectionIndex();
GetSelectedSerial().write(sendString.getText(), SerialManager.getLineEnding(index));
sendString.setText(new String());
sendString.setFocus();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// nothing needs to be done here
}
});
send.setEnabled(false);
reset = new Button(top, SWT.BUTTON1);
reset.setText(Messages.serialMonitorReset);
reset.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
GetSelectedSerial().reset();
sendString.setFocus();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// nothing needs to be done here
}
});
reset.setEnabled(false);
// register the combo as a Selection Provider
getSite().setSelectionProvider(serialPorts);
monitorOutput = new StyledText(top, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
monitorOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1));
monitorOutput.setEditable(false);
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
ITheme currentTheme = themeManager.getCurrentTheme();
FontRegistry fontRegistry = currentTheme.getFontRegistry();
// $NON-NLS-1$
monitorOutput.setFont(fontRegistry.get("io.sloeber.serial.fontDefinition"));
monitorOutput.setText(Messages.serialMonitorNoInput + newLine);
monitorOutput.addMouseListener(new MouseListener() {
@Override
public void mouseUp(MouseEvent e) {
// ignore
}
@Override
public void mouseDown(MouseEvent e) {
// If right button get selected text save it and start external tool
if (e.button == 3) {
String selectedText = monitorOutput.getSelectionText();
if (!selectedText.isEmpty()) {
IProject selectedProject = ProjectExplorerListener.getSelectedProject();
if (selectedProject != null) {
try {
ICConfigurationDescription activeCfg = CoreModel.getDefault().getProjectDescription(selectedProject).getActiveConfiguration();
String activeConfigName = activeCfg.getName();
IPath buildFolder = selectedProject.findMember(activeConfigName).getLocation();
// $NON-NLS-1$
File dumpFile = buildFolder.append("serialdump.txt").toFile();
FileUtils.writeStringToFile(dumpFile, selectedText, Charset.defaultCharset());
} catch (Exception e1) {
// ignore
e1.printStackTrace();
}
}
}
}
}
@Override
public void mouseDoubleClick(MouseEvent e) {
// ignore
}
});
parent.getShell().setDefaultButton(send);
makeActions();
contributeToActionBars();
}
use of io.sloeber.core.api.Serial in project arduino-eclipse-plugin by Sloeber.
the class SerialMonitor method connectSerial.
/**
* Connect to a serial port and sets the listener
*
* @param comPort
* the name of the com port to connect to
* @param baudRate
* the baud rate to connect to the com port
*/
public void connectSerial(String comPort, int baudRate) {
if (serialConnections.size() < MY_MAX_SERIAL_PORTS) {
int colorindex = -1;
for (int idx = 0; idx < serialPortAllocated.length; idx++) {
if (!serialPortAllocated[idx]) {
colorindex = idx;
break;
}
}
if (colorindex < 0) {
log(new Status(IStatus.ERROR, PLUGIN_ID, Messages.serialMonitorNoMoreSerialPortsSupported, null));
}
Serial newSerial = new Serial(comPort, baudRate);
if (newSerial.IsConnected()) {
newSerial.registerService();
SerialListener theListener = new SerialListener(this, colorindex);
newSerial.addListener(theListener);
theListener.event(newLine + Messages.serialMonitorConnectedTo.replace(Messages.PORT, comPort).replace(Messages.BAUD, Integer.toString(baudRate)) + newLine);
serialConnections.put(newSerial, theListener);
SerialPortsUpdated();
// Only mark the serial port as allocated now everything is done with no errors.
serialPortAllocated[colorindex] = true;
return;
}
} else {
log(new Status(IStatus.ERROR, PLUGIN_ID, Messages.serialMonitorNoMoreSerialPortsSupported, null));
}
}
use of io.sloeber.core.api.Serial in project arduino-eclipse-plugin by Sloeber.
the class SerialMonitor method disConnectSerialPort.
public void disConnectSerialPort(String comPort) {
Serial newSerial = GetSerial(comPort);
if (newSerial != null) {
SerialListener theListener = serialConnections.get(newSerial);
serialPortAllocated[theListener.getColorIndex()] = false;
serialConnections.remove(newSerial);
newSerial.removeListener(theListener);
int idx = theListener.getColorIndex();
serialPortAllocated[idx] = false;
if (lineBuffer[idx].length() > 0) {
// Flush any leftover data.
String str = lineBuffer[idx].toString();
str += newLine;
ReportSerialActivity(str, idx);
// Clear the leftover data out.
lineBuffer[idx].setLength(0);
}
newSerial.dispose();
theListener.dispose();
}
SerialPortsUpdated();
}
use of io.sloeber.core.api.Serial in project arduino-eclipse-plugin by Sloeber.
the class ArduinoSerial method makeArduinoUploadready.
/**
* reset the arduino
*
* This method takes into account all the setting to be able to reset all
* different types of arduino If RXTXDisabled is set the method only return
* the parameter Comport
*
* @param project
* The project related to the com port to reset
* @param comPort
* The name of the com port to reset
* @return The com port to upload to
*/
public static String makeArduinoUploadready(MessageConsoleStream console, IProject project, String configName, BoardDescriptor boardDescriptor) {
boolean use_1200bps_touch = Common.getBuildEnvironmentVariable(project, configName, Const.ENV_KEY_UPLOAD_USE_1200BPS_TOUCH, Const.FALSE).equalsIgnoreCase(Const.TRUE);
boolean bWaitForUploadPort = Common.getBuildEnvironmentVariable(project, configName, Const.ENV_KEY_WAIT_FOR_UPLOAD_PORT, Const.FALSE).equalsIgnoreCase(Const.TRUE);
String comPort = boardDescriptor.getUploadPort();
String boardName = boardDescriptor.getBoardName();
boolean bResetPortForUpload = Common.getBuildEnvironmentVariable(project, configName, Const.ENV_KEY_RESET_BEFORE_UPLOAD, Const.TRUE).equalsIgnoreCase(Const.TRUE);
/*
* Teensy uses halfkay protocol and does not require a reset in
* boards.txt use Const.ENV_KEY_RESET_BEFORE_UPLOAD=FALSE to disable a
* reset
*/
if (!bResetPortForUpload || "teensyloader".equalsIgnoreCase(boardDescriptor.getuploadTool())) {
// $NON-NLS-1$
return comPort;
}
/*
* if the com port can not be found and no specific com port reset
* method is specified assume it is a network port and do not try to
* reset
*/
List<String> originalPorts = Serial.list();
if (!originalPorts.contains(comPort) && !use_1200bps_touch && !bWaitForUploadPort) {
console.println(Messages.ArduinoSerial_comport_not_found);
return comPort;
}
if (use_1200bps_touch) {
// Get the list of the current com serial ports
console.println(Messages.ArduinoSerial_Using_1200bps_touch + comPort);
if (!reset_Arduino_by_baud_rate(comPort, 1200, 400)) /* || */
{
console.println(Messages.ArduinoSerial_reset_failed);
} else {
if (boardName.startsWith("Digistump DigiX")) {
// switch-in after the reset
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// ignore error
}
}
if (bWaitForUploadPort) {
String newComport = wait_for_com_Port_to_appear(console, originalPorts, comPort);
console.println(Messages.ArduinoSerial_Using_comport + newComport + Messages.ArduinoSerial_From_Now_Onwards);
console.println(Messages.ArduinoSerial_Ending_reset);
return newComport;
}
}
console.println(Messages.ArduinoSerial_Continuing_to_use + comPort);
console.println(Messages.ArduinoSerial_Ending_reset);
return comPort;
}
// connect to the serial port
console.println(Messages.ArduinoSerial_reset_dtr_toggle);
Serial serialPort;
try {
serialPort = new Serial(comPort, 9600);
} catch (Exception e) {
e.printStackTrace();
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, Messages.ArduinoSerial_exception_while_opening_seral_port + comPort, e));
console.println(Messages.ArduinoSerial_exception_while_opening_seral_port + comPort);
console.println(Messages.ArduinoSerial_Continuing_to_use + comPort);
console.println(Messages.ArduinoSerial_Ending_reset);
return comPort;
}
if (!serialPort.IsConnected()) {
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, Messages.ArduinoSerial_unable_to_open_serial_port + comPort, null));
console.println(Messages.ArduinoSerial_exception_while_opening_seral_port + comPort);
console.println(Messages.ArduinoSerial_Continuing_to_use + comPort);
console.println(Messages.ArduinoSerial_Ending_reset);
return comPort;
}
console.println(Messages.ArduinoSerial_23);
ToggleDTR(serialPort, 100);
serialPort.dispose();
console.println(Messages.ArduinoSerial_Continuing_to_use + comPort);
console.println(Messages.ArduinoSerial_Ending_reset);
return comPort;
}
Aggregations