Splitting a comma separated string through serial (Arduino)

白昼怎懂夜的黑 提交于 2020-12-31 06:34:49

问题


So my arduino is receiving a string from serial, comprising of three values separated by commas, I'm trying to separate these values into three different variables, the rest I can do.

The string looks something like this "1000,1.5,0.9" or "5000,20,0.01"

I would like something like: a - 1000, b - 1.5, c - 0.9

Cheers


回答1:


I presume you are receiving the string that can be split in to three parts. Here's a sample code taken from this thread:

void setup(){
    Serial.begin(9600);
}
void loop(){
    String first  = Serial.readStringUntil(',');
    Serial.read(); //next character is comma, so skip it using this
    String second = Serial.readStringUntil(',');
    Serial.read();
    String third  = Serial.readStringUntil('\0');
    //parse your data here. example:
    //double x = Double.parseDouble(first);
}


来源:https://stackoverflow.com/questions/29504679/splitting-a-comma-separated-string-through-serial-arduino

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!