1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Bug 543122: Add additional baud rates for serial

Includes new API to allow, on platforms that support it, setting
and getting arbitrary rates.

Change-Id: I0b1134325f913bb09d1bf0cd902f89e968d80570
This commit is contained in:
Jonah Graham 2020-05-11 12:40:03 -04:00
parent fc7e62940f
commit da0d1d7df6
18 changed files with 404 additions and 83 deletions

View file

@ -44,6 +44,7 @@
</p>
<ol>
<li><a href="#binaryparsers">32-bit Binary parsers with 64-bit replacements</a></li>
<li><a href="#baudrate">BaudRate enum in org.eclipse.cdt.serial</a></li>
</ol>
<hr>
@ -204,6 +205,28 @@
<p>
See <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=562495" target="_blank">Bug 562495</a>.
</p>
<h3>2. <a name="baudrate">BaudRate enum in org.eclipse.cdt.serial</a></h3>
<p>
The BaudRate enum in org.eclipse.cdt.serial package will be removed. The following
APIs will be removed, listed with their replacement.
</p>
<ul>
<li>org.eclipse.cdt.serial.BaudRate, use org.eclipse.cdt.serial.StandardBaudRates to obtain typical baud rate values
</li>
<li>org.eclipse.cdt.serial.SerialPort.setBaudRate(BaudRate), use
org.eclipse.cdt.serial.SerialPort.setBaudRateValue(int) instead</li>
<li>org.eclipse.cdt.serial.SerialPort.getBaudRate(), use org.eclipse.cdt.serial.SerialPort.getBaudRateValue()
instead</li>
<li>org.eclipse.tm.terminal.connector.cdtserial.connector.SerialSettings.getBaudRate(), use
org.eclipse.tm.terminal.connector.cdtserial.connector.SerialSettings.getBaudRateValue() instead</li>
<li>org.eclipse.tm.terminal.connector.cdtserial.connector.SerialSettings.setBaudRate(BaudRate), use
org.eclipse.tm.terminal.connector.cdtserial.connector.SerialSettings.setBaudRateValue(int) instead</li>
</ul>
<p>
See <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=563108" target="_blank">Bug 563108</a>.
</p>
</body>
</html>

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Serial Port
Bundle-SymbolicName: org.eclipse.cdt.native.serial
Bundle-Version: 1.1.500.qualifier
Bundle-Version: 1.2.0.qualifier
Bundle-Vendor: Eclipse CDT
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.cdt.serial

View file

@ -27,6 +27,9 @@
#include <strings.h>
#include <errno.h>
#include <sys/ioctl.h>
#ifndef __APPLE__
#include <linux/serial.h>
#endif
#else
#define WIN32_LEAN_AND_MEAN
#define UNICODE
@ -36,6 +39,29 @@
#define FUNC(x) Java_org_eclipse_cdt_serial_SerialPort_ ## x
/**
* Use this method to throw an exception when open fails after the OS open
* stage. This method obtains the last error from OS to include in the
* IOException
*/
#ifndef __MINGW32__
static void closeAndthrowIOException(int fd, JNIEnv *env, const char *msg)
#else
static void closeAndthrowIOException(HANDLE handle, JNIEnv *env, const char *msg)
#endif
{
char buff[256];
#ifndef __MINGW32__
sprintf(buff, "%s: %s", msg, strerror(errno));
close(fd);
#else
sprintf_s(buff, sizeof(buff), "%s (%d)", msg, GetLastError());
CloseHandle(handle);
#endif
jclass cls = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, cls, buff);
}
static void throwIOException(JNIEnv *env, const char *msg)
{
char buff[256];
@ -69,6 +95,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
tcgetattr(fd, &options);
options.c_cflag |= (CLOCAL | CREAD);
#ifndef __APPLE__
speed_t baud;
switch (baudRate) {
case 110:
@ -104,12 +131,82 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
case 115200:
baud = B115200;
break;
case 230400:
baud = B230400;
break;
case 460800:
baud = B460800;
break;
case 500000:
baud = B500000;
break;
case 576000:
baud = B576000;
break;
case 921600:
baud = B921600;
break;
case 1000000:
baud = B1000000;
break;
case 1152000:
baud = B1152000;
break;
case 1500000:
baud = B1500000;
break;
case 2000000:
baud = B2000000;
break;
case 2500000:
baud = B2500000;
break;
case 3000000:
baud = B3000000;
break;
case 3500000:
baud = B3500000;
break;
case 4000000:
baud = B4000000;
break;
default:
baud = B115200;
baud = B0;
break;
}
// Set baud rate
cfsetispeed(&options, baud);
cfsetospeed(&options, baud);
if (baud == B0) {
// Use custom linux baud rates if possible: https://bugs.eclipse.org/bugs/show_bug.cgi?id=543122#c8
struct serial_struct serial_options;
options.c_cflag |= B38400;
if (ioctl(fd, TIOCGSERIAL, &serial_options) != 0) {
closeAndthrowIOException(fd, env, "Failed to use custom baud rate. Error using TIOCGSERIAL");
return -1;
}
serial_options.custom_divisor = serial_options.baud_base / baudRate;
if (serial_options.custom_divisor == 0) {
serial_options.custom_divisor = 1;
}
serial_options.flags &= ~ASYNC_SPD_MASK;
serial_options.flags |= ASYNC_SPD_CUST;
if (ioctl(fd, TIOCSSERIAL, &serial_options) != 0) {
closeAndthrowIOException(fd, env, "Failed to use custom baud rate. Error using TIOCSSERIAL");
return -1;
}
} else {
// Set baud rate
cfsetispeed(&options, baud);
cfsetospeed(&options, baud);
}
#else
// On OSX speed_t is simply the baud rate: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/cfsetispeed.3.html
cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);
#endif
// set data size
options.c_cflag &= ~CSIZE;
@ -191,7 +288,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
DCB dcb = { 0 };
if (!GetCommState(handle, &dcb)) {
throwIOException(env, "Error getting DCB");
closeAndthrowIOException(handle, env, "Error getting DCB");
return -1;
}
@ -223,7 +320,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
}
if (!SetCommState(handle, &dcb)) {
throwIOException(env, "Error setting DCB");
closeAndthrowIOException(handle, env, "Error setting DCB");
return -1;
}
@ -232,7 +329,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 200;
if (!SetCommTimeouts(handle, &timeouts)) {
throwIOException(env, "Error setting timeouts");
closeAndthrowIOException(handle, env, "Error setting timeouts");
return -1;
}

View file

@ -5,7 +5,7 @@
are made available under the terms of the Eclipse Public License 2.0
which accompanies this distribution, and is available at
https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
@ -23,7 +23,7 @@
<relativePath>../../pom.xml</relativePath>
</parent>
<version>1.1.500-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<artifactId>org.eclipse.cdt.native.serial</artifactId>
<packaging>eclipse-plugin</packaging>

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software Systems and others.
* Copyright (c) 2015, 2020 QNX Software Systems and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@ -13,60 +13,135 @@
*******************************************************************************/
package org.eclipse.cdt.serial;
import java.util.Arrays;
import java.util.Optional;
/**
* Standard BaudRates that are generally supported by serial driver.
* @since 1.0
* @deprecated Baud Rates are not a fixed set. Instead use {@link StandardBaudRates} for
* typical values and use an int to represent baud rates. This deprecation goes
* along with {@link SerialPort#setBaudRate(BaudRate)'s deprecation. Use
* SerialPort#setBaudRateValue(int) instead.
*/
@Deprecated
public enum BaudRate {
B110(110), B300(300), B600(600), B1200(1200), B2400(2400), B4800(4800), B9600(9600), B14400(14400), B19200(19200),
B38400(38400), B57600(57600), B115200(115200);
B110, //
B300, //
B600, //
B1200, //
B2400, //
B4800, //
B9600,
/**
* 14,400 is not standard on Linux and requires custom baud rate support.
*/
B14400, //
B19200, //
B38400, //
B57600, //
B115200,
/**
* @since 1.2
*/
B230400,
/**
* @since 1.2
*/
B460800,
/**
* @since 1.2
*/
B500000,
/**
* @since 1.2
*/
B576000,
/**
* @since 1.2
*/
B921600,
/**
* @since 1.2
*/
B1000000,
/**
* @since 1.2
*/
B1152000,
/**
* @since 1.2
*/
B1500000,
/**
* @since 1.2
*/
B2000000,
/**
* @since 1.2
*/
B2500000,
/**
* @since 1.2
*/
B3000000,
/**
* @since 1.2
*/
B3500000,
/**
* @since 1.2
*/
B4000000;
private final int rate;
private BaudRate(int rate) {
this.rate = rate;
BaudRate() {
this.rate = Integer.parseInt(toString().substring(1));
}
public int getRate() {
return rate;
}
private static final String[] strings = { "110", //$NON-NLS-1$
"300", //$NON-NLS-1$
"600", //$NON-NLS-1$
"1200", //$NON-NLS-1$
"2400", //$NON-NLS-1$
"4800", //$NON-NLS-1$
"9600", //$NON-NLS-1$
"14400", //$NON-NLS-1$
"19200", //$NON-NLS-1$
"38400", //$NON-NLS-1$
"57600", //$NON-NLS-1$
"115200" //$NON-NLS-1$
};
public static String[] getStrings() {
return strings;
private String getSpeedString() {
return toString().substring(1);
}
private static final BaudRate[] rates = { B110, B300, B600, B1200, B2400, B4800, B9600, B14400, B19200, B38400,
B57600, B115200 };
public static String[] getStrings() {
return Arrays.asList(values()).stream().map(BaudRate::getSpeedString).toArray(String[]::new);
}
public static BaudRate fromStringIndex(int rate) {
return rates[rate];
if (rate < values().length && rate >= 0) {
return values()[rate];
}
return getDefault();
}
public static int getStringIndex(BaudRate rate) {
for (int i = 0; i < rates.length; ++i) {
if (rate.equals(rates[i])) {
return i;
}
}
return getStringIndex(getDefault());
return rate.ordinal();
}
/**
* This method allows some amount of translation between new API that uses ints
* for baud rate and those that use BaudRate. It attempts to get the closest
* value.
*
* @since 1.2
*/
public static BaudRate getClosest(int baudRate) {
Optional<BaudRate> reduce = Arrays.asList(BaudRate.values()).stream().reduce((result, current) -> {
if (Math.abs(baudRate - current.getRate()) < Math.abs(baudRate - result.getRate()))
return current;
else
return result;
});
return reduce.get();
}
public static BaudRate getDefault() {
return B115200;
}
}

View file

@ -38,7 +38,7 @@ public class SerialPort {
private boolean isOpen;
private boolean isPaused;
private Object pauseMutex = new Object();
private BaudRate baudRate = BaudRate.B115200;
private int baudRate = StandardBaudRates.getDefault();
private ByteSize byteSize = ByteSize.B8;
private Parity parity = Parity.None;
private StopBits stopBits = StopBits.S1;
@ -325,7 +325,7 @@ public class SerialPort {
}
public synchronized void open() throws IOException {
handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
handle = open0(portName, baudRate, byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
isOpen = true;
synchronized (openPorts) {
@ -393,20 +393,51 @@ public class SerialPort {
return;
}
isPaused = false;
handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
handle = open0(portName, baudRate, byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
isOpen = true;
pauseMutex.notifyAll();
}
}
/**
*
* @param rate
* @throws IOException
* @deprecated Use {@link #setBaudRateValue(int)}
*/
@Deprecated
public void setBaudRate(BaudRate rate) throws IOException {
if (isOpen) {
throw new IOException(PORT_OPEN);
}
this.baudRate = rate.getRate();
}
/**
* @since 1.2
*/
public void setBaudRateValue(int rate) throws IOException {
if (isOpen) {
throw new IOException(PORT_OPEN);
}
this.baudRate = rate;
}
/**
* @return the baud rate or closest match. Will only
* return same value as {@link #setBaudRate(BaudRate)},
* may not match value passed {@link #setBaudRateValue(int)}
* @deprecated Use {@link #getBaudRateValue()}
*/
@Deprecated
public BaudRate getBaudRate() {
return BaudRate.getClosest(baudRate);
}
/**
* @since 1.2
*/
public int getBaudRateValue() {
return baudRate;
}

View file

@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2020 Kichwa Coders Canada Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
= *******************************************************************************/
package org.eclipse.cdt.serial;
/**
* Define the set of baud rates that are standard are generally supported.
*
* @since 1.2
*/
public final class StandardBaudRates {
/**
* Return an array of the standard values for baud rates.
*
* Note: Especially on Linux these values are special as they can be set
* without requiring operations on the serial port that are not universally
* supported.
*
* The contents of this array may be changed from time to time and therefore
* the order of the elements and length of this array should not be used
* for anything. In particular, if storing a baud rate preference, store the
* integer value of that preference, not the index in this table.
*
* @return array of standard values
*/
public static int[] asArray() {
// This list comes from what linux supports without custom rates.
return new int[] { 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000,
576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000 };
}
/**
* Return an array of the standard values for baud rates, as strings for
* display in the UI.
* @see #asArray()
*/
public static String[] asStringArray() {
int[] rates = asArray();
String[] rateStrings = new String[rates.length];
for (int i = 0; i < rateStrings.length; i++) {
rateStrings[i] = Integer.toString(rates[i]);
}
return rateStrings;
}
/**
* Return the default speed used by the {@link SerialPort}
*/
public static int getDefault() {
return 115200;
}
}

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.tm.terminal.connector.cdtserial;singleton:=true
Bundle-Version: 4.6.0.qualifier
Bundle-Version: 4.7.0.qualifier
Bundle-Activator: org.eclipse.tm.terminal.connector.cdtserial.activator.Activator
Bundle-Vendor: %providerName
Require-Bundle: org.eclipse.core.expressions;bundle-version="3.4.400",

View file

@ -19,11 +19,13 @@ import java.util.HashSet;
import java.util.Set;
import org.eclipse.cdt.serial.SerialPort;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tm.internal.terminal.provisional.api.ISettingsStore;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl;
import org.eclipse.tm.internal.terminal.provisional.api.TerminalState;
import org.eclipse.tm.internal.terminal.provisional.api.provider.TerminalConnectorImpl;
import org.eclipse.tm.terminal.connector.cdtserial.activator.Activator;
import org.eclipse.tm.terminal.connector.cdtserial.nls.Messages;
public class SerialConnector extends TerminalConnectorImpl {
@ -71,13 +73,20 @@ public class SerialConnector extends TerminalConnectorImpl {
serialPort = new SerialPort(settings.getPortName());
try {
serialPort.setBaudRate(settings.getBaudRate());
serialPort.setBaudRateValue(settings.getBaudRateValue());
serialPort.setByteSize(settings.getByteSize());
serialPort.setParity(settings.getParity());
serialPort.setStopBits(settings.getStopBits());
serialPort.open();
} catch (IOException e) {
Activator.log(e);
String error = NLS.bind(Messages.SerialConnector_FailedToOpen, settings.getPortName(),
e.getLocalizedMessage());
try {
control.getRemoteToTerminalOutputStream().write(error.getBytes());
} catch (IOException e1) {
Activator.log(e);
}
control.setState(TerminalState.CLOSED);
return;
}

View file

@ -15,6 +15,7 @@ package org.eclipse.tm.terminal.connector.cdtserial.connector;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.StandardBaudRates;
import org.eclipse.cdt.serial.StopBits;
import org.eclipse.tm.internal.terminal.provisional.api.ISettingsStore;
@ -27,7 +28,7 @@ public class SerialSettings {
public static final String STOP_BITS_ATTR = "cdtserial.stopBits"; //$NON-NLS-1$
private String portName;
private BaudRate baudRate;
private int baudRate;
private ByteSize byteSize;
private Parity parity;
private StopBits stopBits;
@ -39,16 +40,13 @@ public class SerialSettings {
portName = store.get(PORT_NAME_ATTR, ""); //$NON-NLS-1$
String baudRateStr = store.get(BAUD_RATE_ATTR, ""); //$NON-NLS-1$
if (baudRateStr.isEmpty()) {
baudRate = BaudRate.getDefault();
} else {
String[] rates = BaudRate.getStrings();
for (int i = 0; i < rates.length; ++i) {
if (baudRateStr.equals(rates[i])) {
baudRate = BaudRate.fromStringIndex(i);
break;
}
}
baudRate = 0;
try {
baudRate = Integer.parseInt(baudRateStr);
} catch (NumberFormatException e) {
}
if (baudRate <= 0) {
baudRate = StandardBaudRates.getDefault();
}
String byteSizeStr = store.get(BYTE_SIZE_ATTR, ""); //$NON-NLS-1$
@ -96,7 +94,7 @@ public class SerialSettings {
*/
public void save(ISettingsStore store) {
store.put(PORT_NAME_ATTR, portName);
store.put(BAUD_RATE_ATTR, BaudRate.getStrings()[BaudRate.getStringIndex(baudRate)]);
store.put(BAUD_RATE_ATTR, Integer.toString(baudRate));
store.put(BYTE_SIZE_ATTR, ByteSize.getStrings()[ByteSize.getStringIndex(byteSize)]);
store.put(PARITY_ATTR, Parity.getStrings()[Parity.getStringIndex(parity)]);
store.put(STOP_BITS_ATTR, StopBits.getStrings()[StopBits.getStringIndex(stopBits)]);
@ -110,11 +108,33 @@ public class SerialSettings {
this.portName = portName;
}
/**
* @deprecated Use {@link #getBaudRateValue()}
*/
@Deprecated
public BaudRate getBaudRate() {
return BaudRate.getClosest(baudRate);
}
/**
* @since 4.7
*/
public int getBaudRateValue() {
return baudRate;
}
/**
* @deprecated Use {@link #setBaudRate(int)}
*/
@Deprecated
public void setBaudRate(BaudRate baudRate) {
this.baudRate = baudRate.getRate();
}
/**
* @since 4.7
*/
public void setBaudRateValue(int baudRate) {
this.baudRate = baudRate;
}

View file

@ -14,7 +14,6 @@ package org.eclipse.tm.terminal.connector.cdtserial.controls;
import java.util.Map;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.StopBits;
@ -62,7 +61,7 @@ public class SerialConfigPanel extends AbstractExtendedConfigurationPanel {
page.saveSettings();
data.put(SerialSettings.PORT_NAME_ATTR, settings.getPortName());
data.put(SerialSettings.BAUD_RATE_ATTR, settings.getBaudRate());
data.put(SerialSettings.BAUD_RATE_ATTR, settings.getBaudRateValue());
data.put(SerialSettings.BYTE_SIZE_ATTR, settings.getByteSize());
data.put(SerialSettings.PARITY_ATTR, settings.getParity());
data.put(SerialSettings.STOP_BITS_ATTR, settings.getStopBits());
@ -79,7 +78,10 @@ public class SerialConfigPanel extends AbstractExtendedConfigurationPanel {
}
settings.setPortName((String) data.get(SerialSettings.PORT_NAME_ATTR));
settings.setBaudRate((BaudRate) data.get(SerialSettings.BAUD_RATE_ATTR));
Object object = data.get(SerialSettings.BAUD_RATE_ATTR);
if (object != null) {
settings.setBaudRateValue((int) object);
}
settings.setByteSize((ByteSize) data.get(SerialSettings.BYTE_SIZE_ATTR));
settings.setParity((Parity) data.get(SerialSettings.PARITY_ATTR));
settings.setStopBits((StopBits) data.get(SerialSettings.STOP_BITS_ATTR));

View file

@ -14,10 +14,10 @@ package org.eclipse.tm.terminal.connector.cdtserial.controls;
import java.io.IOException;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.SerialPort;
import org.eclipse.cdt.serial.StandardBaudRates;
import org.eclipse.cdt.serial.StopBits;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
@ -50,7 +50,7 @@ public class SerialSettingsPage extends AbstractSettingsPage {
private Combo stopBitsCombo;
private String portName;
private BaudRate baudRate;
private int baudRate;
private ByteSize byteSize;
private Parity parity;
private StopBits stopBits;
@ -65,17 +65,15 @@ public class SerialSettingsPage extends AbstractSettingsPage {
portName = dialogSettings.get(SerialSettings.PORT_NAME_ATTR);
String baudRateStr = dialogSettings.get(SerialSettings.BAUD_RATE_ATTR);
if (baudRateStr == null || baudRateStr.isEmpty()) {
baudRate = BaudRate.getDefault();
} else {
String[] rates = BaudRate.getStrings();
for (int i = 0; i < rates.length; ++i) {
if (baudRateStr.equals(rates[i])) {
baudRate = BaudRate.fromStringIndex(i);
break;
}
if (baudRateStr != null && !baudRateStr.isEmpty()) {
try {
baudRate = Integer.parseInt(baudRateStr);
} catch (NumberFormatException e) {
}
}
if (baudRate <= 0) {
baudRate = StandardBaudRates.getDefault();
}
String byteSizeStr = dialogSettings.get(SerialSettings.BYTE_SIZE_ATTR);
if (byteSizeStr == null || byteSizeStr.isEmpty()) {
@ -152,11 +150,9 @@ public class SerialSettingsPage extends AbstractSettingsPage {
Label baudRateLabel = new Label(comp, SWT.NONE);
baudRateLabel.setText(Messages.SerialTerminalSettingsPage_BaudRate);
baudRateCombo = new Combo(comp, SWT.READ_ONLY);
baudRateCombo = new Combo(comp, SWT.DROP_DOWN);
baudRateCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
for (String baudRateStr : BaudRate.getStrings()) {
baudRateCombo.add(baudRateStr);
}
baudRateCombo.setItems(StandardBaudRates.asStringArray());
Label byteSizeLabel = new Label(comp, SWT.NONE);
byteSizeLabel.setText(Messages.SerialTerminalSettingsPage_DataSize);
@ -212,11 +208,11 @@ public class SerialSettingsPage extends AbstractSettingsPage {
portCombo.select(0);
}
BaudRate baudRate = settings.getBaudRate();
if (baudRate == null) {
int baudRate = settings.getBaudRateValue();
if (baudRate <= 0) {
baudRate = this.baudRate;
}
baudRateCombo.select(BaudRate.getStringIndex(baudRate));
baudRateCombo.setText(Integer.toString(baudRate));
ByteSize byteSize = settings.getByteSize();
if (byteSize == null) {
@ -240,13 +236,21 @@ public class SerialSettingsPage extends AbstractSettingsPage {
@Override
public void saveSettings() {
settings.setPortName(portCombo.getText());
settings.setBaudRate(BaudRate.fromStringIndex(baudRateCombo.getSelectionIndex()));
int baudRateValue = 0;
try {
baudRateValue = Integer.parseInt(baudRateCombo.getText());
} catch (NumberFormatException e) {
}
if (baudRateValue <= 0) {
baudRateValue = StandardBaudRates.getDefault();
}
settings.setBaudRateValue(baudRateValue);
settings.setByteSize(ByteSize.fromStringIndex(byteSizeCombo.getSelectionIndex()));
settings.setParity(Parity.fromStringIndex(parityCombo.getSelectionIndex()));
settings.setStopBits(StopBits.fromStringIndex(stopBitsCombo.getSelectionIndex()));
dialogSettings.put(SerialSettings.PORT_NAME_ATTR, portCombo.getText());
dialogSettings.put(SerialSettings.BAUD_RATE_ATTR, BaudRate.getStrings()[baudRateCombo.getSelectionIndex()]);
dialogSettings.put(SerialSettings.BAUD_RATE_ATTR, Integer.toString(baudRateValue));
dialogSettings.put(SerialSettings.BYTE_SIZE_ATTR, ByteSize.getStrings()[byteSizeCombo.getSelectionIndex()]);
dialogSettings.put(SerialSettings.PARITY_ATTR, Parity.getStrings()[parityCombo.getSelectionIndex()]);
dialogSettings.put(SerialSettings.STOP_BITS_ATTR, StopBits.getStrings()[stopBitsCombo.getSelectionIndex()]);

View file

@ -14,7 +14,6 @@ package org.eclipse.tm.terminal.connector.cdtserial.launcher;
import java.util.Map;
import org.eclipse.cdt.serial.BaudRate;
import org.eclipse.cdt.serial.ByteSize;
import org.eclipse.cdt.serial.Parity;
import org.eclipse.cdt.serial.StopBits;
@ -58,7 +57,7 @@ public class SerialLauncherDelegate extends AbstractLauncherDelegate {
// Extract the properties
SerialSettings settings = new SerialSettings();
settings.setPortName((String) properties.get(SerialSettings.PORT_NAME_ATTR));
settings.setBaudRate((BaudRate) properties.get(SerialSettings.BAUD_RATE_ATTR));
settings.setBaudRateValue((Integer) properties.get(SerialSettings.BAUD_RATE_ATTR));
settings.setByteSize((ByteSize) properties.get(SerialSettings.BYTE_SIZE_ATTR));
settings.setParity((Parity) properties.get(SerialSettings.PARITY_ATTR));
settings.setStopBits((StopBits) properties.get(SerialSettings.STOP_BITS_ATTR));

View file

@ -37,5 +37,6 @@ public class Messages extends NLS {
public static String SerialTerminalSettingsPage_Parity;
public static String SerialTerminalSettingsPage_SerialPort;
public static String SerialTerminalSettingsPage_StopBits;
public static String SerialConnector_FailedToOpen;
}

View file

@ -16,3 +16,4 @@ SerialTerminalSettingsPage_DataSize=Data size:
SerialTerminalSettingsPage_Parity=Parity:
SerialTerminalSettingsPage_SerialPort=Serial port:
SerialTerminalSettingsPage_StopBits=Stop bits:
SerialConnector_FailedToOpen=Failed to open port {0} with selected settings.\r\nThe error from the serial driver:\r\n{1}