1
1
package io .sloeber .core .api ;
2
2
3
3
import java .io .IOException ;
4
+ import java .text .MessageFormat ;
4
5
import java .util .ArrayList ;
5
6
import java .util .Arrays ;
6
7
import java .util .List ;
@@ -124,48 +125,65 @@ public void removeListener(MessageConsumer consumer) {
124
125
this .fConsumers .remove (consumer );
125
126
}
126
127
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 ;
143
153
}
144
154
}
145
155
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 ;
155
168
}
156
- this .port = null ;
157
169
}
170
+ return false ;
158
171
}
159
172
160
173
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
+ }
164
179
if (this .fServiceRegistration != null ) {
165
180
this .fServiceRegistration .unregister ();
166
181
}
167
182
}
168
183
184
+ /**
185
+ * @return true if the port is connected/open.
186
+ */
169
187
public boolean IsConnected () {
170
188
return (this .port != null && this .port .isOpen ());
171
189
}
@@ -191,13 +209,15 @@ public void registerService() {
191
209
this , null );
192
210
}
193
211
194
- public void reset () {
212
+ public boolean reset () {
195
213
try {
196
- this .port .close ();
197
- Thread .sleep (100 );
214
+ disconnect ();
198
215
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 ;
200
219
}
220
+ return true ;
201
221
}
202
222
203
223
private synchronized void checkForData () {
0 commit comments