Arduino serial port receiving string
Friends who are used to Arduino serial port transmission know that Serial.read() of Arduino can only read one byte at a time, but sometimes it is very troublesome to communicate with strings. Don't talk nonsense, just go to the complete example:
Only one Arduino is needed for compilation and no external components are required. Use the serial monitor of the Arduino compiler to see the result. What text we type in, what text will be returned below.
String comdata = "";
void setup() { Serial.begin(9600); }
void loop() { while (Serial.available() > 0) { comdata += char(Serial.read()); delay(2); } if (comdata.length() > 0) { Serial.println(comdata); comdata = ""; } }
The code is very simple, comdata is a string type variable. Serial.available() is the amount of data in the current serial buffer pool. Serial.read() is a statement that reads the buffer pool, and can only read one byte at a time. uses String type variables, it is very simple to realize the addition of characters to strings, as well as string output, assignment and other troublesome issues, so very simple codes can handle serial data. Pay special attention to delay(2) when reading the serial port, otherwise the serial port buffer will not have enough time to accept data. Even if the delay is reduced, errors will occur. The specific value can also be determined experimentally.
One more reminder: comdata said it is a string, and also an array. To quote each word, you can use comdata[0], comdata[1]. . . comdata[n]. If we want to take out each byte, we can quote each one.
Effect: What string is input, what is output. Input:
After pressing send:
An example is attached. Inputting 1011101.. in the serial port will cause the D2~Dx pins of Arduino to generate high/low levels. Of course, the data sent once depends on the Arduino pins. Depends on the number, such as Arduino UNO/nano, there are only twelve pins D2~D13. We just hit 12 numbers. For example, 101101011010, if there are characters other than 0 and 1 in the middle, the bit setting is automatically skipped: such as 122202221222. Because 2 is not in the allowable range, just set the value of D2/D6/D10:
String comdata = ""; void setup() { Serial.begin(9600); for(int i = 2; i <= 13; i++) pinMode(i, OUTPUT); }
void loop() { while (Serial.available() > 0) { comdata += int(Serial.read()) - '0'; delay(2); } if(comdata.length() > 0) { for(int i = 0; i < comdata.length(); i++) { if(comdata[i]=='0'||comdata[i]=='1') { digitalWrite(i + 2, comdata[i] - '0'); Serial.print("Pin "); Serial.print(i + 2); Serial.print(" is "); Serial.println(comdata[i]); } } comdata = ""; } }
One more: Advanced version (to be tested) Enter six comma separated numbers such as: 50,20,5,255,20,20 can make Arduino's PWM pins (3,5,6,9,10,11): emit light according to the PWM value. So the number of commas must be 0~255
String comdata = ""; int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0; void setup() { for(int i = 0; i < 6; i++) pinMode(PWMPin[i], OUTPUT); Serial.begin(9600); }
void loop() { int j = 0; while (Serial.available() > 0) { comdata += char(Serial.read()); delay(2); mark = 1; }
if(mark == 1) { Serial.println(comdata); Serial.println(comdata.length()); for(int i = 0; i < comdata.length() ; i++) { if(comdata[i] == ',') { j++; } else { numdata[j] = numdata[j] * 10 + (comdata[i] - '0'); } } comdata = String("");
for(int i = 0; i < 6; i++) { Serial.print("Pin "); Serial.print(PWMPin[i]); Serial.print(" = "); Serial.println(numdata[i]); analogWrite(PWMPin[i], numdata[i]); numdata[i] = 0; } mark = 0; } }
|