Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Prevent arduino from attempting to echo data before myserial is intia…

…lized
  • Loading branch information...
commit d0fd95cecaff855127b803ed6f15f5a08b4c238e 1 parent 972a30d
Kent Horvath authored
32 src/arduino/arduino_rs232_commander/arduino_rs232_commander.ino
@@ -14,15 +14,19 @@ byte buffer[4]; //current command buffer
14 14 unsigned short int buffer_position; //current state of buffer position
15 15 byte pending_command; //interpreted and executed on confirmCommand()
16 16 byte request_confirmation_sent; //did the arduino request confirmation of a pending command?
  17 +byte myserial_is_initialized;
  18 +unsigned int myserial_baudrate;
17 19
18 20 void setup()
19 21 {
20 22 //Initialize variables
21 23
22   - byte buffer[4] = {0x00, 0x00, 0x00, 0x00}; //current command buffer
23   - unsigned short int buffer_position = 0; //current state of buffer position
24   - byte pending_command = 0x00; //interpreted and executed on confirmCommand()
25   - byte request_confirmation_sent = 0; //did the arduino request confirmation of a pending command?
  24 + byte buffer[] = {0x00, 0x00, 0x00, 0x00}; //current command buffer
  25 + buffer_position = 0; //current state of buffer position
  26 + pending_command = 0x00; //interpreted and executed on confirmCommand()
  27 + request_confirmation_sent = 0; //did the arduino request confirmation of a pending command?
  28 + myserial_is_initialized = 0;
  29 + myserial_baudrate = 0;
26 30
27 31 //Init arduino built-in serial
28 32 Serial.begin(9600);
@@ -40,7 +44,8 @@ void setup()
40 44
41 45 void setSoftwareSerialBaudrate(unsigned int baudrate)
42 46 {
43   - mySerial.begin(baudrate);
  47 + mySerial.begin(baudrate);
  48 + myserial_is_initialized = 1;
44 49 }
45 50
46 51 void loop()
@@ -61,7 +66,7 @@ void loop()
61 66 if (read_byte == ':') read_colon_into_buffer();
62 67 else if (buffer_position != 0) read_non_colon_into_buffer(read_byte);
63 68 else {
64   - mySerial.write(read_byte);
  69 + if(myserial_is_initialized) mySerial.write(read_byte);
65 70 }
66 71 }
67 72 }
@@ -90,7 +95,7 @@ void interpret_buffered_command()
90 95 {
91 96 if(buffer[0]==':' && buffer[2]==';' && buffer[3]==0x0D)
92 97 {
93   - if(pending_command != 0x00 && buffer[1] != '!')
  98 + if(myserial_is_initialized && pending_command != 0x00 && buffer[1] != '!')
94 99 {
95 100 //What happens if two commands get sent without a confirmation in between? Assume pending command was meant for myserial and write it out.
96 101 mySerial.write(':');
@@ -150,34 +155,45 @@ void confirm_command()
150 155 switch(pending_command){
151 156 case '0':
152 157 setSoftwareSerialBaudrate(1200);
  158 + myserial_baudrate = 1200;
153 159 break;
154 160 case '1':
155 161 setSoftwareSerialBaudrate(2400);
  162 + myserial_baudrate = 2400;
156 163 break;
157 164 case '2':
158 165 setSoftwareSerialBaudrate(4800);
  166 + myserial_baudrate = 4800;
159 167 break;
160 168 case '3':
161 169 setSoftwareSerialBaudrate(9600);
  170 + myserial_baudrate = 9600;
162 171 break;
163 172 case '4':
164 173 setSoftwareSerialBaudrate(14400);
  174 + myserial_baudrate = 14400;
165 175 break;
166 176 case '5':
167 177 setSoftwareSerialBaudrate(19200);
  178 + myserial_baudrate = 19200;
168 179 break;
169 180 case '6':
170 181 setSoftwareSerialBaudrate(38400);
  182 + myserial_baudrate = 38400;
171 183 break;
172 184 case '7':
173 185 setSoftwareSerialBaudrate(57600);
  186 + myserial_baudrate = 57600;
174 187 break;
175 188 case '8':
176 189 setSoftwareSerialBaudrate(115200);
  190 + myserial_baudrate = 115200;
177 191 break;
178 192 case '#':
179 193 reset_arduino();
180 194 break;
  195 + case '?':
  196 + Serial.print(myserial_baudrate);
181 197 }
182 198
183 199 pending_command = 0x00;
@@ -194,7 +210,7 @@ void flush_buffer()
194 210 {
195 211 //Write out everything in the buffer to mySerial
196 212 for(int i = 0; i < buffer_position; i++){
197   - mySerial.write(buffer[i]);
  213 + if(myserial_is_initialized) mySerial.write(buffer[i]);
198 214 }
199 215
200 216 //Set the buffer position back to 0

0 comments on commit d0fd95c

Please sign in to comment.
Something went wrong with that request. Please try again.