18
18
import it .baeyens .arduino .common .Const ;
19
19
20
20
public class ArduinoSerial {
21
+
22
+ private ArduinoSerial () {}
23
+
21
24
/**
22
25
* This method resets arduino based on setting the baud rate. Used for due,
23
26
* Leonardo and others
@@ -53,36 +56,36 @@ public static boolean reset_Arduino_by_baud_rate(String ComPort, int baudrate, l
53
56
* Waits for a serial port to appear. It is assumed that the default comport
54
57
* is not available on the system
55
58
*
56
- * @param OriginalPorts
59
+ * @param originalPorts
57
60
* The ports available on the system
58
61
* @param defaultComPort
59
62
* The port to return if no new com port is found
60
63
* @return the new comport if found else the defaultComPort
61
64
*/
62
- public static String wait_for_com_Port_to_appear (MessageConsoleStream console , Vector <String > OriginalPorts ,
65
+ public static String wait_for_com_Port_to_appear (MessageConsoleStream console , Vector <String > originalPorts ,
63
66
String defaultComPort ) {
64
67
65
68
Vector <String > NewPorts ;
66
69
Vector <String > NewPortsCopy ;
67
70
68
71
// wait for port to disappear and appear
69
- int NumTries = 0 ;
70
- int MaxTries = 40 ; // wait for max 10 seconds as arduino does
72
+ int numTries = 0 ;
73
+ int maxTries = 40 ; // wait for max 10 seconds as arduino does
71
74
int delayMs = 250 ;
72
- int PrefNewPortsCopySize = -10 ;
75
+ int prefNewPortsCopySize = -10 ;
73
76
do {
74
77
75
78
NewPorts = Serial .list ();
76
79
77
80
NewPortsCopy = new Vector <>(NewPorts );
78
- for (int i = 0 ; i < OriginalPorts .size (); i ++) {
79
- NewPortsCopy .remove (OriginalPorts .get (i ));
81
+ for (int i = 0 ; i < originalPorts .size (); i ++) {
82
+ NewPortsCopy .remove (originalPorts .get (i ));
80
83
}
81
84
82
85
/* dump the serial ports to the console */
83
86
console .print ("PORTS {" ); //$NON-NLS-1$
84
- for (int i = 0 ; i < OriginalPorts .size (); i ++) {
85
- console .print (' ' + OriginalPorts .get (i ) + ',' );
87
+ for (int i = 0 ; i < originalPorts .size (); i ++) {
88
+ console .print (' ' + originalPorts .get (i ) + ',' );
86
89
}
87
90
console .print ("} / {" ); //$NON-NLS-1$
88
91
for (int i = 0 ; i < NewPorts .size (); i ++) {
@@ -97,18 +100,18 @@ public static String wait_for_com_Port_to_appear(MessageConsoleStream console, V
97
100
98
101
// code to capture the case: the com port reappears with a name that
99
102
// was in the original list
100
- int NewPortsCopySize = NewPorts .size ();
101
- if ((NewPortsCopy .size () == 0 ) && (NewPortsCopySize == PrefNewPortsCopySize + 1 )) {
103
+ int newPortsCopySize = NewPorts .size ();
104
+ if ((NewPortsCopy .isEmpty () ) && (newPortsCopySize == prefNewPortsCopySize + 1 )) {
102
105
console .println (Messages .ArduinoSerial_Comport_Appeared_and_disappeared );
103
106
return defaultComPort ;
104
107
}
105
- PrefNewPortsCopySize = NewPortsCopySize ;
108
+ prefNewPortsCopySize = newPortsCopySize ;
106
109
107
- if (NumTries ++ > MaxTries ) {
110
+ if (numTries ++ > maxTries ) {
108
111
console .println (Messages .ArduinoSerial_Comport_is_not_behaving_as_expected );
109
112
return defaultComPort ;
110
113
}
111
- if (NewPortsCopy .size () == 0 ) // wait a while before we do the next
114
+ if (NewPortsCopy .isEmpty () ) // wait a while before we do the next
112
115
// try
113
116
{
114
117
try {
@@ -118,10 +121,10 @@ public static String wait_for_com_Port_to_appear(MessageConsoleStream console, V
118
121
// code
119
122
}
120
123
}
121
- } while (NewPortsCopy .size () == 0 );
124
+ } while (NewPortsCopy .isEmpty () );
122
125
123
126
console .println (
124
- Messages .ArduinoSerial_Comport_reset_took + (NumTries * delayMs ) + Messages .ArduinoSerial_miliseconds );
127
+ Messages .ArduinoSerial_Comport_reset_took + (numTries * delayMs ) + Messages .ArduinoSerial_miliseconds );
125
128
return NewPortsCopy .get (0 );
126
129
}
127
130
@@ -158,38 +161,33 @@ public static boolean ToggleDTR(Serial serialPort, long delay) {
158
161
*
159
162
* @param project
160
163
* The project related to the com port to reset
161
- * @param ComPort
164
+ * @param comPort
162
165
* The name of the com port to reset
163
166
* @return The com port to upload to
164
167
*/
165
168
public static String makeArduinoUploadready (MessageConsoleStream console , IProject project , String configName ,
166
- String ComPort ) {
167
- // ArduinoProperties arduinoProperties = new ArduinoProperties(project);
169
+ String comPort ) {
168
170
boolean use_1200bps_touch = Common
169
171
.getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_UPLOAD_USE_1200BPS_TOUCH , Const .FALSE )
170
172
.equalsIgnoreCase (Const .TRUE );
171
- // boolean bDisableFlushing = Common
172
- // .getBuildEnvironmentVariable(project, configName,
173
- // Const.ENV_KEY_upload_disable_flushing, Const.FALSE)
174
- // .equalsIgnoreCase(Const.TRUE);
175
- boolean bwait_for_upload_port = Common
173
+ boolean bWaitForUploadPort = Common
176
174
.getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_WAIT_FOR_UPLOAD_PORT , Const .FALSE )
177
175
.equalsIgnoreCase (Const .TRUE );
178
176
String boardName = Common .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_JANTJE_BOARD_NAME ,
179
177
Const .EMPTY_STRING );
180
- String upload_protocol = Common .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_UPLOAD_PROTOCOL ,
178
+ String uploadProtocol = Common .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_UPLOAD_PROTOCOL ,
181
179
Const .EMPTY_STRING );
182
180
/* Teensy uses halfkay protocol and does not require a reset */
183
- if (upload_protocol .equalsIgnoreCase ("halfkay" )) { //$NON-NLS-1$
184
- return ComPort ;
181
+ if (uploadProtocol .equalsIgnoreCase ("halfkay" )) { //$NON-NLS-1$
182
+ return comPort ;
185
183
}
186
184
/* end of Teensy and halfkay */
187
185
if (use_1200bps_touch ) {
188
186
// Get the list of the current com serial ports
189
187
console .println (Messages .ArduinoSerial_Using_12000bps_touch );
190
- Vector <String > OriginalPorts = Serial .list ();
188
+ Vector <String > originalPorts = Serial .list ();
191
189
192
- if (!reset_Arduino_by_baud_rate (ComPort , 1200 , 300 ) /* || */ ) {
190
+ if (!reset_Arduino_by_baud_rate (comPort , 1200 , 300 ) /* || */ ) {
193
191
console .println (Messages .ArduinoSerial_reset_failed );
194
192
195
193
} else {
@@ -202,58 +200,49 @@ public static String makeArduinoUploadready(MessageConsoleStream console, IProje
202
200
// ignore error
203
201
}
204
202
}
205
- if (bwait_for_upload_port ) {
206
- String NewComport = wait_for_com_Port_to_appear (console , OriginalPorts , ComPort );
207
- console .println (Messages .ArduinoSerial_Using_comport + NewComport
203
+ if (bWaitForUploadPort ) {
204
+ String newComport = wait_for_com_Port_to_appear (console , originalPorts , comPort );
205
+ console .println (Messages .ArduinoSerial_Using_comport + newComport
208
206
+ Messages .ArduinoSerial_From_Now_Onwards );
209
207
console .println (Messages .ArduinoSerial_Ending_reset );
210
- return NewComport ;
208
+ return newComport ;
211
209
}
212
210
}
213
- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
211
+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
214
212
console .println (Messages .ArduinoSerial_Ending_reset );
215
- return ComPort ;
213
+ return comPort ;
216
214
}
217
215
218
216
// connect to the serial port
219
217
console .println (Messages .ArduinoSerial_reset_dtr_toggle );
220
218
Serial serialPort ;
221
219
try {
222
- serialPort = new Serial (ComPort , 9600 );
220
+ serialPort = new Serial (comPort , 9600 );
223
221
} catch (Exception e ) {
224
222
e .printStackTrace ();
225
223
Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID ,
226
- Messages .ArduinoSerial_exception_while_opening_seral_port + ComPort , e ));
227
- console .println (Messages .ArduinoSerial_exception_while_opening_seral_port + ComPort );
228
- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
224
+ Messages .ArduinoSerial_exception_while_opening_seral_port + comPort , e ));
225
+ console .println (Messages .ArduinoSerial_exception_while_opening_seral_port + comPort );
226
+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
229
227
console .println (Messages .ArduinoSerial_Ending_reset );
230
- return ComPort ;
231
- // throw new RunnerException(e.getMessage());
228
+ return comPort ;
232
229
}
233
230
if (!serialPort .IsConnected ()) {
234
231
Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID ,
235
- Messages .ArduinoSerial_unable_to_open_serial_port + ComPort , null ));
236
- console .println (Messages .ArduinoSerial_exception_while_opening_seral_port + ComPort );
237
- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
232
+ Messages .ArduinoSerial_unable_to_open_serial_port + comPort , null ));
233
+ console .println (Messages .ArduinoSerial_exception_while_opening_seral_port + comPort );
234
+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
238
235
console .println (Messages .ArduinoSerial_Ending_reset );
239
- return ComPort ;
236
+ return comPort ;
240
237
}
241
238
242
- // if (!bDisableFlushing) {
243
- // // Cleanup the serial buffer
244
- // console.println(Messages.ArduinoSerial_Flushing_buffer);
245
- // flushSerialBuffer(serialPort);// I wonder is this code on the right
246
- // // place (I mean before the reset?;
247
- // // shouldn't it be after?)
248
- // }
249
- // reset arduino
250
239
console .println (Messages .ArduinoSerial_23 );
251
240
ToggleDTR (serialPort , 100 );
252
241
253
242
serialPort .dispose ();
254
- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
243
+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
255
244
console .println (Messages .ArduinoSerial_Ending_reset );
256
- return ComPort ;
245
+ return comPort ;
257
246
258
247
}
259
248
}
0 commit comments