serial port - Arduino: How store values in a char array in one line then clear/empty a char array? -
i attempting write code store command text between { , } serial terminal. far have:
byte index = 0; // index array; store character char cmdarr[10]; boolean startofline = false; boolean endofline = false; void setup() { serial.begin(38400); } void serialevent() { while (serial.available()) { char cmd = (char)serial.read(); if (cmd == '{') { startofline = true; } if (cmd == '}') { endofline = true; //cmdarr[index] = '\0'; //null terminate c string; i'm not sure if needed } if (startofline && cmd != '{' && cmd != '}') { cmdarr[index++] = cmd; } if (startofline && endofline) { int i; (i = 0; < 10; i++) { //do command } startofline = false; endofline = false; break; } } } void loop() { } i able iterate through cmdarr print out array values. right now, example, command getting stored so: char cmdarr[10] = {'p', 'h', 'c', '\0'}; possible store command char cmdarr[10] = {"phc"};? also, how clear/empty command ready next command?
new code seems working. resorted using string since seemed easiest approach:
string cmddata; //store complete command on 1 line send sensor board. boolean startofline = false; boolean endofline = false; void setup() { serial.begin(38400); serial3.begin(38400); } void serialevent() { while (serial.available()) { char cmd = (char)serial.read(); if (cmd == '{') { startofline = true; } if (cmd == '}') { endofline = true; } if (startofline && cmd != '{' && cmd != '}') { //serial.print("send command"); cmddata += cmd; } if (startofline && endofline) { startofline = false; endofline = false; cmddata += '\r'; serial3.print(cmddata); cmddata = ""; } } } void serialevent3() { char cmd3 = (char)serial3.read(); serial.print(cmd3); } void loop() { }
the cmdarr contiguous block of memory. in sense data stored there "phc\0". should able use cmdarr pointer able print data string instead of accessing characters 1 one (you can access characters 1 one pointers though). in solution don't think need clear array ever. enough set index 0 , make sure when wrote characters terminate string \0 (what doing). strings in c 0 terminated. if want process contents 1 one in loop need exit loop if encounter zero.
Comments
Post a Comment