Arduino serial data split string into array

/*
 * This procedure is suitable for unclassified "111,426" style strings
   * After segmentation, numdata[0]=111,numdata[1]=426
   * And numdata[0] and numdata[1] are integer numbers
*/
#define numdata_length 2
String comdata = "";
int numdata[numdata_length] = {0};
int flag = 0;
void setup() {
  Serial.begin(9600);
  }
void loop() {
int j = 0;
 //Continuously check the serial port buffer in a loop, read the string one by one
while (Serial.available() > 0)
{
  comdata += char(Serial.read());
  delay(2);
  flag = 1;
}
 //If data is received, perform comdata analysis operation, otherwise do nothing
if(flag == 1) {
for(int i = 0; i < comdata.length() ; i++){
  if(comdata[i] == ','){
    j++;
    }
  else{
    numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
    }
  }
comdata = String("");
flag = 0;
for(int i = 0; i < numdata_length; i++){
  Serial.println(numdata[i]);
  numdata[i] = 0;
  }
}
}


Intelligent Recommendation

Arduino serial data visualization method

System: win10 64bits Arduino IDE: 1.8.5 Arduino development board: UNO Arduino can easily operate the sensor to obtain sensor data. After getting the data, we are more concerned about data visualizati...

Python reads arduino serial data

Background: Arduino101 board can measure attitude data: heading (heading angle), pitch (pitch angle), roll (roll angle), use Python to read the serial port data and save it to the computer. Open the s...

esp32 read String or array method in arduino ied compilation environment serial port

** The serial port data can be taken as String or array. This is convenient for taking notes by yourself, for reference only ** 1. Here is the serial2 serial port using esp32. 1.1. Use String.substrin...

Arduino serial port accepts string operations

For the use of the Arduino serial port part, it is somewhat inconvenient. Although the function to be called is officially provided, it is not flexible enough. The following is a summary of the string...

Arduino string comparison, serial print print

Arduino string comparison, serial port income serial port printing, with a Compareto () function. ...

More Recommendation

Split string return array

Array as a query condition for select in This article comes from the CSDN blog, please indicate the source: http://blog.csdn.net/believefym/archive/2009/01/15/3791122.aspx...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top