Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Arduino串行输入拆分为多个字符串_Arduino_Beamng - Fatal编程技术网

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

将Arduino串行输入拆分为多个字符串,arduino,beamng,Arduino,Beamng,您好,提前谢谢您的帮助。 我有一个应用程序,读取速度,转速和齿轮值从BeamNG驱动器和串行打印到一个COM端口。它在循环中工作,使用结束字节分隔值 RPM_END_BYTE = '+'; SPEED_END_BYTE = '='; GEAR_END_BYTE = '!'; 使用这些结束字节,我想知道如何在arduino中将输入拆分为三个字符串。我可以直接将它们转换为整数,也可以使用string.toInt()来转换它。这是程序串行输出的一个循环的样子: 02500+120=6! 我已

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

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

02500+120=6!
我已经在我的电脑上设置了一个虚拟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;
    }
const byte numChars=32;
char receivedChars[numChars];//存储接收到的数据的数组
#包括
布尔newData=false;
液晶液晶显示器(7,8,9,10,11,12);
无效设置(){
Serial.begin(9600);
lcd.begin(16,2);
}
void循环(){
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker(){
静态字节ndx=0;
char endMarker='+';
char-rc;
while(Serial.available()>0&&newData==false){
rc=Serial.read();
如果(rc!=endMarker){
接收到的数据chars[ndx]=rc;
ndx++;
如果(ndx>=numChars){
ndx=numChars-1;
}
}
否则{
receivedChars[ndx]='+';//终止字符串
ndx=0;
newData=true;
}
}
}
void showNewData(){
if(newData==true){
lcd.setCursor(0,1);
lcd.clear();
lcd.打印(已接收到打印);
newData=false;
}

非常感谢任何能提供帮助的人。如果已经有人问过类似的问题,我很抱歉,但我找不到。解决方案的一个例子是:我已经修改了程序,该程序可以使用带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;
}      
char-endMarker[3]={'+','=','!'};
while(Serial.available()>0&&newData==false){
rc=Serial.read();
int returnvalue=标记(rc);
如果(返回值<0{
接收到的数据chars[ndx]=rc;
ndx++;
如果(ndx>=numChars){
ndx=numChars-1;
}
}
否则{
如果(返回值==0){
//使用+do your stuff终止字符串
//可以使用lcd.setCursor(Col,Row)设置光标位置
接收到的数据chars[ndx]='+';
}else if(returnvalue==1){
//用=完成你的工作结束字符串
//可以使用lcd.setCursor(Col,Row)来设置光标
接收到的命令[ndx]='=';
}否则{
//用!完成你的任务
//可以使用lcd.setCursor(Col,Row)来设置光标
receivedChars[ndx]='!';
}
//在lcd上显示结果
lcd.print(receivedChars);//您只需显示
ndx=0;
//newdata=true;将此布尔值设为true,在
}
}   
整型标记(字符c){
对于(int i=0;i<3;i++){
if(endMarker[i]==c){
返回i;
}
}
返回-1;
}      

我很快就可以测试了,非常感谢!我已经使用了这段代码,并使用“receivedChars[ndx]='='”;“with+,=,!在缩进位置终止字符串,然后将receivedChars打印到lcd屏幕上;但是,一旦有一个结果(rpm)已经打印,程序停止。我的代码在这里。你介意告诉我我做错了什么吗?谢谢。这很正常。你修改了程序的逻辑,我已经适应了你的程序。我修改了解决方案,我在行newdata=truethankyou上添加了注释。当使用串行监视器时,你的程序工作正常但是,输入数字;当我使用BeamNG详细信息发送器时,它不起作用。这使我相信这是我的程序的问题,而不是arduino代码的问题。非常感谢您的贡献!如果一切正常,我将发布任何更新和工作代码。