【问题标题】:Splitting of Arduino Serial input into multiple strings将 Arduino 串行输入拆分为多个字符串
【发布时间】:2018-12-15 17:34:08
【问题描述】:

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

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

使用这些结束字节,我想知道如何在 arduino 中将输入拆分为三个字符串。我可以将它们直接转换为整数,也可以使用string.toInt(); 进行转换。这是程序串行输出的一个循环的样子:

02500+120=6!

我已经在我的 PC 上设置了一个虚拟 COM 端口来检查软件是否正常工作,但我似乎无法弄清楚如何拆分输入。

我还使用了以下代码,当我通过串行监视器输入数字并以“+”结尾时,它可以工作,但它不适用于我的软件。

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;
    }

非常感谢任何可以提供帮助的人。如果已经有人问过类似的问题,但我找不到,我深表歉意。

【问题讨论】:

    标签: arduino beamng


    【解决方案1】:

    一个解决方案的例子:我已经调整了带有 '+' 和 3 个 endMarkers 的程序

    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;
    }      
    

    【讨论】:

    • 我很快就能测试了,非常感谢!
    • 我已使用此代码并使用“receivedChars[ndx] = '=';”终止了字符串与 +,=,!但是,您的缩进在哪里,然后将 receivedChars 打印到液晶屏幕上;一旦打印了一个结果 (rpm),程序就会停止。我的代码在这里pastebin.com/MFaTUgy3 你介意告诉我我做错了什么吗?谢谢。
    • 正常你修改了程序的逻辑,我已经适应了你的程序。我修改了解决方案,我在 newdata = true 上添加了评论
    • 谢谢,但是,当串行监视器用于输入数字时,您的程序完美地发挥了它的作用;当我使用 BeamNG 详细信息发送器时,它不起作用。这让我相信这是我的程序有问题,而不是 arduino 代码。非常感谢您的贡献!如果一切正常,我会发布任何更新和工作代码。
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2011-06-14
    • 2016-12-31
    相关资源
    最近更新 更多