代码之家  ›  专栏  ›  技术社区  ›  Creagh Duggan

将Arduino串行输入拆分为多个字符串

  •  1
  • Creagh Duggan  · 技术社区  · 6 年前

    您好,提前谢谢您的帮助。 我有一个应用程序,读取速度,转速和齿轮值从BeamNG驱动器和串行打印到一个COM端口。它在循环中工作,使用结束字节分隔值。

    RPM_END_BYTE = '+'; 
    SPEED_END_BYTE = '='; 
    GEAR_END_BYTE = '!'; 
    

    string.toInt(); 转换它。这是程序串行输出的一个循环的样子:

    02500+120=6!
    

    我还使用了下面的代码,当我通过串行监视器输入数字并以“+”结束时,该代码可以工作,但它与我的软件的工作方式不同。

    const byte numChars = 32;
    char receivedChars[numChars];   // an array to store the received data
    #include <LiquidCrystal.h>
    boolean newData = false;
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
    void setup() {
        Serial.begin(9600);
    
        lcd.begin(16, 2);
    }
    
    void loop() {
        recvWithEndMarker();
        showNewData();
    }
    
    void recvWithEndMarker() {
        static byte ndx = 0;
        char endMarker = '+';
        char rc;
    
        while (Serial.available() > 0 && newData == false) {
            rc = Serial.read();
    
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '+'; // terminate the string
                ndx = 0;
                newData = true;
            }
        }
    }
    
    void showNewData() {
        if (newData == true) {
            lcd.setCursor(0, 1);
            lcd.clear();
            lcd.print(receivedChars);
            newData = false;
        }
    

    1 回复  |  直到 5 年前
        1
  •  0
  •   Frenchy    6 年前

    解决方案的一个例子:我已经修改了程序,该程序可以使用带有3个端点标记的“+”

    char endMarker[3] = {'+', '=', '!'};
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        int returnvalue = testifendMarker(rc);
        if (returnvalue < 0 {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
                if (returnvalue == 0){
                     // terminate the string with + do your stuff
                     // maybe use lcd.setCursor(Col, Row) to set the cursor position
                     receivedChars[ndx] = '+';
                }else if (returnvalue == 1){
                     // terminate the string with = do your stuff
                     // maybe use lcd.setCursor(Col, Row) to set the cursor 
                     receivedChars[ndx] = '=';
                }else {
                     // terminate the string with ! do your stuff
                     // maybe use lcd.setCursor(Col, Row) to set the cursor 
                     receivedChars[ndx] = '!';
                }
                //display the result on lcd
                lcd.print(receivedChars);// you just display 
                ndx = 0;
                // newdata = true; put this boolean to true terminate the loop while
        }
    }   
    
    int testifendMarker(char c) {
        for (int i=0; i < 3; i++){
                if (endMarker[i] == c){
                    return i;
                }
        }
    
        return -1;
    }