@@ -124,48 +124,62 @@ public void removeListener(MessageConsumer consumer) {
124
124
this .fConsumers .remove (consumer );
125
125
}
126
126
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 ;
143
152
}
144
153
}
145
154
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 ;
155
167
}
156
- this .port = null ;
157
168
}
169
+ return false ;
158
170
}
159
171
160
172
public void dispose () {
161
- notifyConsumersOfEvent ("Disconnect of port " + this .port .getPortName () + " executed" ); //$NON-NLS-1$ //$NON-NLS-2$
162
173
disconnect ();
163
-
174
+ notifyConsumersOfEvent ( "Disconnect of port " + this . port . getPortName () + " executed" ); //$NON-NLS-1$ //$NON-NLS-2$
164
175
if (this .fServiceRegistration != null ) {
165
176
this .fServiceRegistration .unregister ();
166
177
}
167
178
}
168
179
180
+ /**
181
+ * @return true if the port is connected/open.
182
+ */
169
183
public boolean IsConnected () {
170
184
return (this .port != null && this .port .isOpen ());
171
185
}
@@ -191,13 +205,15 @@ public void registerService() {
191
205
this , null );
192
206
}
193
207
194
- public void reset () {
208
+ public boolean reset () {
195
209
try {
196
- this .port .close ();
197
- Thread .sleep (100 );
210
+ disconnect ();
198
211
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 ;
200
215
}
216
+ return true ;
201
217
}
202
218
203
219
private synchronized void checkForData () {
0 commit comments