Skip to content

Commit 8df41a7

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 8df41a7

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

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

+53-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sloeber.core.api;
22

33
import java.io.IOException;
4+
import java.text.MessageFormat;
45
import java.util.ArrayList;
56
import java.util.Arrays;
67
import java.util.List;
@@ -124,48 +125,65 @@ public void removeListener(MessageConsumer consumer) {
124125
this.fConsumers.remove(consumer);
125126
}
126127

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

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-
}
156+
/**
157+
* @return true if the system was connected and is now disconnected.
158+
*/
159+
public boolean disconnect() {
160+
if (IsConnected()) {
161+
try {
162+
this.port.close();
163+
this.port = null;
164+
return true;
165+
} catch (Exception e) {
166+
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, "Serial port close failed", e)); //$NON-NLS-1$
167+
return false;
155168
}
156-
this.port = null;
157169
}
170+
return false;
158171
}
159172

160173
public void dispose() {
161-
notifyConsumersOfEvent("Disconnect of port " + this.port.getPortName() + " executed"); //$NON-NLS-1$ //$NON-NLS-2$
162-
disconnect();
163-
174+
if (IsConnected()) {
175+
String name = this.port.getPortName();
176+
disconnect();
177+
notifyConsumersOfEvent(MessageFormat.format("Disconnect of port {0} executed", name)); //$NON-NLS-1$
178+
}
164179
if (this.fServiceRegistration != null) {
165180
this.fServiceRegistration.unregister();
166181
}
167182
}
168183

184+
/**
185+
* @return true if the port is connected/open.
186+
*/
169187
public boolean IsConnected() {
170188
return (this.port != null && this.port.isOpen());
171189
}
@@ -191,13 +209,15 @@ public void registerService() {
191209
this, null);
192210
}
193211

194-
public void reset() {
212+
public boolean reset() {
195213
try {
196-
this.port.close();
197-
Thread.sleep(100);
214+
disconnect();
198215
connect();
199-
} catch (Exception e) {// JABA is not going to add code
216+
} catch (Exception e) {
217+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "Serial port reset failed", e)); //$NON-NLS-1$
218+
return false;
200219
}
220+
return true;
201221
}
202222

203223
private synchronized void checkForData() {

0 commit comments

Comments
 (0)