disassembling incoming serial data

I have an Arduino that is sending a , a character, then a numeric value, to another Arduino. That Arduino is displaying that data in the serial monitor, per the attached picture. I want to use the as an indicator that the next value is a variable name, and the value that follows is the value to put in that variable. A println indicates the end of the packet.

I need guidance on how to look for the $, and stuff the indicated variable with the value that follows.

TX Code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

int value=0;

void setup()  
{
// set the data rate for the hardware serial port
  Serial.begin(9600);
// set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  
}

void loop() // run over and over
{
  Serial.print(value);
    uSend("L", value);
    uSend("M", value+5);
    value = value+1;
    delay(1000);
}


// Function to send serial data packet to other Aduino
  
  int uSend(char* x, int y){
    
  mySerial.print("$");
  mySerial.print(x);
  mySerial.println(y, DEC);
 
  
  }

RX Code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

int value=0;

void setup()  
{
// set the data rate for the hardware serial port
  Serial.begin(9600);
// set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
      
}

packetSerialRx.PNG

Will the variable that is to be set to the incoming value have an arbitrary name or will it be one of a number of variables whose name(s) are known in advance ?

The latter should not be difficult as long as you have a delimiter between the various portions of the incoming message, for instance a comma. If so, read the incoming bytes into a string (note the lowercase, not a String) until you reach the linefeed, then parse the string to find the name of the variable and its value and set that variable with the value.

Advice on reading the incoming bytes into a string http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11425&page=1 Use strtok() to split the string and atoi() to convert the string value to an integer. Then a series of ifs to test the variable name and to carry out the assignment.

Sounds like you need something like this:

void loop() {
  static int name;
  static int value;
  static int state = 0;

  if (mySerial.available()) {
    int c = mySerial.read();

    if (c == '

) {      state = 1;    }    else if (state  == 1) { // Have the $      name = c;     value = mySerial.readNumber();      state=2;    }    else if (state == 2) {// Have the name and value      Serial.print("Name=");      Serial.write((char) name);      Serial.print("  Value=");      Serial.println(value);      state = 0; // Go back to waiting for $    }  } } ```

Bob, The variable name is known in advance. John, you might want to try that code, it does not work.

There are code examples available/posted that use < and > as packet start and end delimiters. If you probably can modify the examples to use your desired delimiters.

There are code examples available/posted that use < and > as packet start and end delimiters.

Like so:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

I'm receiving the data and assembling it into a variable, and able to print that variable. Now I just need to parse the data into the individual variables and data. I'll post the code later. Lightning took out my phone and internet Sunday, and it should be back up later today.