RS485 serial communication, passing multiple values

Hi Guys,

I am very new to coding with Arduino and I am experimenting with sending multiple ints from an Uno to a nano.

Backstory I'm trying to create a rov with two joysticks and an Uno at the top of a 20M cable and parsing those values onto a nano with a servo and a few escs at the bottom. using two max485 boards

I have been able to send one value of a pot down the link using the code below but I am struggling to understand how to send and receive two or more.

Any help would be appreciated

master.ino (556 Bytes)

slave.ino (965 Bytes)

looks like you're sending an integer value as an ascii string (e.g "123"), 3 ascii characters vs a single 8-bit byte, 0x7b.

parseInt() reads a number of ascii decimal digit characters ('0' - '9') and returns a values after a timeout period expires.

the timeout is on way to delimit a message. Another approach is to recognize a line termination chars (e.g. linefeed '\n')

you could format an ascii string with multiple values separated with a space and terminated, read an ascii string up to the line terminated and then translate the values in the string to two integer values using sscanf()

// ---------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
}

// ---------------------------------------------------------
void
process (
    int  a,
    int  b )
{
    Serial.print   (__func__);
    Serial.print   (" ");
    Serial.print   (a);
    Serial.print   (" ");
    Serial.println (b);
}

// ---------------------------------------------------------
void
loop (void)
{
    static char s [80];
    static int  idx = 0;

    while (Serial.available ())  {
        char c = Serial.read ();
        s [idx++] = c;

        if ('\n' == c)  {
            s [idx] = 0;
            idx = 0;

            int a, b;
            sscanf (s, "%d %d", &a, & b);

            process (a, b);
        }
    }
}

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.