1818import it .baeyens .arduino .common .Const ;
1919
2020public class ArduinoSerial {
21+
22+ private ArduinoSerial () {}
23+
2124 /**
2225 * This method resets arduino based on setting the baud rate. Used for due,
2326 * Leonardo and others
@@ -53,36 +56,36 @@ public static boolean reset_Arduino_by_baud_rate(String ComPort, int baudrate, l
5356 * Waits for a serial port to appear. It is assumed that the default comport
5457 * is not available on the system
5558 *
56- * @param OriginalPorts
59+ * @param originalPorts
5760 * The ports available on the system
5861 * @param defaultComPort
5962 * The port to return if no new com port is found
6063 * @return the new comport if found else the defaultComPort
6164 */
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 ,
6366 String defaultComPort ) {
6467
6568 Vector <String > NewPorts ;
6669 Vector <String > NewPortsCopy ;
6770
6871 // 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
7174 int delayMs = 250 ;
72- int PrefNewPortsCopySize = -10 ;
75+ int prefNewPortsCopySize = -10 ;
7376 do {
7477
7578 NewPorts = Serial .list ();
7679
7780 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 ));
8083 }
8184
8285 /* dump the serial ports to the console */
8386 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 ) + ',' );
8689 }
8790 console .print ("} / {" ); //$NON-NLS-1$
8891 for (int i = 0 ; i < NewPorts .size (); i ++) {
@@ -97,18 +100,18 @@ public static String wait_for_com_Port_to_appear(MessageConsoleStream console, V
97100
98101 // code to capture the case: the com port reappears with a name that
99102 // 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 )) {
102105 console .println (Messages .ArduinoSerial_Comport_Appeared_and_disappeared );
103106 return defaultComPort ;
104107 }
105- PrefNewPortsCopySize = NewPortsCopySize ;
108+ prefNewPortsCopySize = newPortsCopySize ;
106109
107- if (NumTries ++ > MaxTries ) {
110+ if (numTries ++ > maxTries ) {
108111 console .println (Messages .ArduinoSerial_Comport_is_not_behaving_as_expected );
109112 return defaultComPort ;
110113 }
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
112115 // try
113116 {
114117 try {
@@ -118,10 +121,10 @@ public static String wait_for_com_Port_to_appear(MessageConsoleStream console, V
118121 // code
119122 }
120123 }
121- } while (NewPortsCopy .size () == 0 );
124+ } while (NewPortsCopy .isEmpty () );
122125
123126 console .println (
124- Messages .ArduinoSerial_Comport_reset_took + (NumTries * delayMs ) + Messages .ArduinoSerial_miliseconds );
127+ Messages .ArduinoSerial_Comport_reset_took + (numTries * delayMs ) + Messages .ArduinoSerial_miliseconds );
125128 return NewPortsCopy .get (0 );
126129 }
127130
@@ -158,38 +161,33 @@ public static boolean ToggleDTR(Serial serialPort, long delay) {
158161 *
159162 * @param project
160163 * The project related to the com port to reset
161- * @param ComPort
164+ * @param comPort
162165 * The name of the com port to reset
163166 * @return The com port to upload to
164167 */
165168 public static String makeArduinoUploadready (MessageConsoleStream console , IProject project , String configName ,
166- String ComPort ) {
167- // ArduinoProperties arduinoProperties = new ArduinoProperties(project);
169+ String comPort ) {
168170 boolean use_1200bps_touch = Common
169171 .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_UPLOAD_USE_1200BPS_TOUCH , Const .FALSE )
170172 .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
176174 .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_WAIT_FOR_UPLOAD_PORT , Const .FALSE )
177175 .equalsIgnoreCase (Const .TRUE );
178176 String boardName = Common .getBuildEnvironmentVariable (project , configName , Const .ENV_KEY_JANTJE_BOARD_NAME ,
179177 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 ,
181179 Const .EMPTY_STRING );
182180 /* 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 ;
185183 }
186184 /* end of Teensy and halfkay */
187185 if (use_1200bps_touch ) {
188186 // Get the list of the current com serial ports
189187 console .println (Messages .ArduinoSerial_Using_12000bps_touch );
190- Vector <String > OriginalPorts = Serial .list ();
188+ Vector <String > originalPorts = Serial .list ();
191189
192- if (!reset_Arduino_by_baud_rate (ComPort , 1200 , 300 ) /* || */ ) {
190+ if (!reset_Arduino_by_baud_rate (comPort , 1200 , 300 ) /* || */ ) {
193191 console .println (Messages .ArduinoSerial_reset_failed );
194192
195193 } else {
@@ -202,58 +200,49 @@ public static String makeArduinoUploadready(MessageConsoleStream console, IProje
202200 // ignore error
203201 }
204202 }
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
208206 + Messages .ArduinoSerial_From_Now_Onwards );
209207 console .println (Messages .ArduinoSerial_Ending_reset );
210- return NewComport ;
208+ return newComport ;
211209 }
212210 }
213- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
211+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
214212 console .println (Messages .ArduinoSerial_Ending_reset );
215- return ComPort ;
213+ return comPort ;
216214 }
217215
218216 // connect to the serial port
219217 console .println (Messages .ArduinoSerial_reset_dtr_toggle );
220218 Serial serialPort ;
221219 try {
222- serialPort = new Serial (ComPort , 9600 );
220+ serialPort = new Serial (comPort , 9600 );
223221 } catch (Exception e ) {
224222 e .printStackTrace ();
225223 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 );
229227 console .println (Messages .ArduinoSerial_Ending_reset );
230- return ComPort ;
231- // throw new RunnerException(e.getMessage());
228+ return comPort ;
232229 }
233230 if (!serialPort .IsConnected ()) {
234231 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 );
238235 console .println (Messages .ArduinoSerial_Ending_reset );
239- return ComPort ;
236+ return comPort ;
240237 }
241238
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
250239 console .println (Messages .ArduinoSerial_23 );
251240 ToggleDTR (serialPort , 100 );
252241
253242 serialPort .dispose ();
254- console .println (Messages .ArduinoSerial_Continuing_to_use + ComPort );
243+ console .println (Messages .ArduinoSerial_Continuing_to_use + comPort );
255244 console .println (Messages .ArduinoSerial_Ending_reset );
256- return ComPort ;
245+ return comPort ;
257246
258247 }
259248}
0 commit comments