Skip to content

Commit 00e3cbc

Browse files
committed
Use CDT Serial class #1261
The Serial class was rewritten to use the CDT serial. However, the CDT Serial class is missing some methods compared to jssc. 1. There is no STOP_BITS 1.5 2. There is no serial listener, only an InputStream 3. There is no setRTS 4. There is no setDTR
1 parent 71bf385 commit 00e3cbc

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

io.sloeber.core/src/io/sloeber/core/api/Serial.java

+48-32
Original file line numberDiff line numberDiff line change
@@ -124,48 +124,62 @@ public void removeListener(MessageConsumer consumer) {
124124
this.fConsumers.remove(consumer);
125125
}
126126

127-
public void connect() {
128-
if (this.port == null) {
129-
int count = 0;
130-
try {
131-
this.port = new SerialPort(this.portName);
132-
this.port.setBaudRateValue(this.rate);
133-
this.port.setParity(this.parity);
134-
this.port.setStopBits(this.stopbits);
135-
this.port.setByteSize(this.databits);
136-
this.port.open();
137-
startMonitor();
138-
} catch (IOException e) {
139-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Error opening serial port " + this.portName, //$NON-NLS-1$
140-
e));
141-
this.port = null;
142-
}
127+
/**
128+
* Only connects if the port is not currently connected.
129+
*
130+
* @return true if the port was not connected/open and is now connected/open.
131+
* @see #IsConnected()
132+
*/
133+
public boolean connect() {
134+
if (IsConnected()) {
135+
return false;
136+
}
137+
int count = 0;
138+
try {
139+
this.port = new SerialPort(this.portName);
140+
this.port.setBaudRateValue(this.rate);
141+
this.port.setParity(this.parity);
142+
this.port.setStopBits(this.stopbits);
143+
this.port.setByteSize(this.databits);
144+
this.port.open();
145+
startMonitor();
146+
return true;
147+
} catch (IOException e) {
148+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Error opening serial port " + this.portName, //$NON-NLS-1$
149+
e));
150+
this.port = null;
151+
return false;
143152
}
144153
}
145154

146-
public void disconnect() {
147-
if (this.port != null) {
148-
149-
if (this.port.isOpen()) {
150-
try {
151-
this.port.close();
152-
} catch (Exception e) {
153-
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, "Serial port close failed", e)); //$NON-NLS-1$
154-
}
155+
/**
156+
* @return true if the system was connected and is now disconnected.
157+
*/
158+
public boolean disconnect() {
159+
if (IsConnected()) {
160+
try {
161+
this.port.close();
162+
this.port = null;
163+
return true;
164+
} catch (Exception e) {
165+
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, "Serial port close failed", e)); //$NON-NLS-1$
166+
return false;
155167
}
156-
this.port = null;
157168
}
169+
return false;
158170
}
159171

160172
public void dispose() {
161-
notifyConsumersOfEvent("Disconnect of port " + this.port.getPortName() + " executed"); //$NON-NLS-1$ //$NON-NLS-2$
162173
disconnect();
163-
174+
notifyConsumersOfEvent("Disconnect of port " + this.port.getPortName() + " executed"); //$NON-NLS-1$ //$NON-NLS-2$
164175
if (this.fServiceRegistration != null) {
165176
this.fServiceRegistration.unregister();
166177
}
167178
}
168179

180+
/**
181+
* @return true if the port is connected/open.
182+
*/
169183
public boolean IsConnected() {
170184
return (this.port != null && this.port.isOpen());
171185
}
@@ -191,13 +205,15 @@ public void registerService() {
191205
this, null);
192206
}
193207

194-
public void reset() {
208+
public boolean reset() {
195209
try {
196-
this.port.close();
197-
Thread.sleep(100);
210+
disconnect();
198211
connect();
199-
} catch (Exception e) {// JABA is not going to add code
212+
} catch (Exception e) {
213+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Serial port reset failed", e)); //$NON-NLS-1$
214+
return false;
200215
}
216+
return true;
201217
}
202218

203219
private synchronized void checkForData() {

0 commit comments

Comments
 (0)