RS 485 Non Blocking Library Nick Gammon

Hi everyone,
Please can someone guide me that how can a master send a message to multiple slaves using RS 485 non blocking library by Nick Gammon.

here is the link

As I am trying to understand the code but I am unable find a variable where we could define the slave to which we want to send the message. As in the blocking code the message includes i.e
Both Blocking and non blocking libraries are in the above link,

byte msg [] = {
1, // device 1 (slave) address
3 // turn light on command received
};

please help me.

  // assemble message
  byte msg [] = { 
     1,    // device 1
     2,    // turn light on
     level // to what level
  };

The first byte of the message says which slave is supposed to deal with that message. There are two ways to send messages to multiple slaves, depending on whether multiple means all o more than one.

If it means all, decide what slave number means all, and send the message to that number. Each slave would be programmed to react to its own number and the one that means all.

If it means more than one (but not all), you send multiple messages, each to a specific slave.

Hi Paul,

Thank you for your reply.

I understand the point where the first byte represents the address of the slave but i cannot understand how to represent this in the non blocking library.

this the link for the library. At the bottom of the page there is non blocking library.

and this is the example code given there for sending and receiving data but i cannot seem to understand the address part here.

Sending Code Example:

#include <RS485_non_blocking.h>

size_t fWrite (const byte what)
{
  return Serial.write (what);  
}

RS485 myChannel (NULL, NULL, fWrite, 0);

void setup ()
{
  Serial.begin (115200);
  myChannel.begin ();
}  // end of setup

const byte msg [] = "Hello world";

void loop ()
{
  myChannel.sendMsg (msg, sizeof (msg));
  delay (1000);   
}  // end of loop

Receiving Code Example

#include <RS485_non_blocking.h>

 int fAvailable ()
   {
   return Serial.available ();  
   }
 
 int fRead ()
   {
   return Serial.read ();  
   }
 

RS485 myChannel (fRead, fAvailable, NULL, 20);

void setup ()
  {
  Serial.begin (115200);
  myChannel.begin ();
  }  // end of setup

void loop ()
  {
  if (myChannel.update ())
    {
    Serial.write (myChannel.getData (), myChannel.getLength ()); 
    Serial.println ();
    }
  }  // end of loop

You are sending an array of bytes in both cases. In one case, a protocol has been defined. In the other case, it hasn't. There is nothing magic about the array of bytes in either case.

If you want, in the non blocking case, to say that the 7th byte represents an address, feel free to do that. Or the 24th. Or, even the 1st.

I don't understand what the confusion is. All slaves are listening, so you could do this:

  // assemble message
  byte msg [] = {
     0,    // 0 = all slaves
     2,    // turn light on
     level // to what level
  };

They all get the message, so instead of all but one ignoring it, they all act upon it.

I don't understand what the confusion is.

In your blocking version, you define a protocol.
In your non-blocking version, you don't.

OP appears to have gotten the impression that there was some magic to the blocking vs. non-blocking versions of the data being sent.

Ah yes, I was leaving that as an exercise for the reader. Plus I didn't want to give the impression that the struct in question was the only way of using the library.

Thank you Paul and Nick for your replies and sorry for the trouble.

Does this library have advantages over EasyTransfer?
In the examples, the code is very similar. Which is better to use?
Can I transmit int and String in one struct?

You can't send a String because that is allocated on the heap. You can however send a struct with a C-style string inside it.

Do I need to perform additional steps to ensure the quality of the transmission (CRC checking, delivery verification, etc.), or is it all happening inside the library?

The library does a CRC check, however it does not do delivery verification. There isn't a handshaking protocol as such, it merely guarantees that if a packet is received it is a valid packet. You might need to send some sort of acknowledgement back (eg. received packet 5) and if the sender doesn't get that, it sends it again after a certain delay.

Thanks.