简体   繁体   中英

Serial Read - Arduino - Recieving Several Different Mesages

I am a new programmer, so I am having a bit of problem with Serial communication of Arduino.

I am trying to read data from serial Input, sent by a simulation as string and I need to store it as integer to write it with my servo.

I found this https://forum.arduino.cc/t/serial-input-basics-updated/382007 tutorial and example 4 does the job.

However, right now I would like to receive several different messages for different actuators. How can I edit this code to receive several different string messages?

Maybe I was thinking to use different end markers for different messages but could not apply my idea?

Could someone suggest me an idea or way to do this please?

Thanks a lot here is the code.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean newData = false;

//SERVO//
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
////////////////////////

int dataNumber = 0;             // new for this version

void setup() {
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewNumber();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
 if (Serial.available()> 0)  {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
            delay(1);
        }
    }
}

void showNewNumber() {
    if (newData == true) {
        dataNumber = 0;             // new for this version
        dataNumber = atoi(receivedChars);   // new for this version
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.print("Data as Number ... ");    // new for this version
        Serial.println(dataNumber);     // new for this version
        myservo.write(dataNumber);                  // sets the servo position according to the scaled value
        delay(50);
        newData = false;
    }
}

Thanks!

Personally I would introduce a message type to decide what to do with the serial input. At its simplist, this could be a single character that preceeds your number input. This way you can still use the same code to terminate your message - but when you parse the message you can work out which device it would apply to.

I've taken your code, and created 2 servos - servoA and servoB.

The message type is extracted from the start of the message in showNewNumber() . (Note there's no error checking here - it always assumes each message is a single character message type - followed by a non zero length integer. You may want to make this more robust by checking for a minumum string length).

The message type is used to decide which servo to control.

So you could send the message "a25\n" to control servoA - and "b90\n" to control servoB for example.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean newData = false;

//SERVO//
#include <Servo.h>
Servo servoA;  // create servo object to control a servo
Servo servoB;  // create servo object to control a servo
////////////////////////

int dataNumber = 0;             // new for this version

void setup() {
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
    servoA.attach(9);  // attaches the servo on pin 9 to the servo object
    servoB.attach(8);  // attaches the servo on pin 8 to the servo object
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewNumber();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
 if (Serial.available()> 0)  {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
            delay(1);
        }
    }
}

void showNewNumber() {
    if (newData == true) {
        
        char msgType = receivedChars[0]; // 1st character is msg type
      
        dataNumber = 0;             // new for this version
        dataNumber = atoi(receivedChars+1);   // convert rest of msg to integer
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.print("Message Type ... ");
        Serial.println(msgType);
        Serial.print("Data as Number ... ");    // new for this version
        Serial.println(dataNumber);     // new for this version

        switch( msgType ) {
        case 'a':
            servoA.write(dataNumber);                  // sets the servo position according to the scaled value
            break ;
        case 'b':
            servoB.write(dataNumber);
            break ;
        default:
            Serial.print("Unrecognised msg type: ");
            Serial.println(msgType);
        }

        delay(50);
        newData = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM