0

The RS485 master-slave communication library on Nick Gammon's page http://www.gammon.com.au/Arduino/RS485_protocol.zip is working for me OK, but on the same hardware wiring is not working for the non-blocking library http://www.gammon.com.au/Arduino/RS485_non_blocking.zip.

I can not find information about the connection of MAX485 on Arduino for non-block version. Is MAX485/RS485 from HW serial (ie Rx=0 and Tx=1)?

If so, how can a slave on the same line at the same time receive from master the message "hello world" and print it to the serial monitor? Thank you for the explanation.

When I added the SoftwareSerial library into Nick's example for non-blocking, so it works, but is it still a non-blocking?

//Master:
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 4; 
SoftwareSerial mySerial(2,3); //RX = 2 TX = 3

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

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

void setup () {
  Serial.begin (9600);
  mySerial.begin (28800);
  myChannel.begin ();  

  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable 
}  // end of setup

const byte msg [] = "Hello world";

void loop () {
  digitalWrite (ENABLE_PIN, HIGH);  // enable sending 
  myChannel.sendMsg (msg, sizeof (msg));
  digitalWrite (ENABLE_PIN, LOW);  // disable sending
  delay (1000);   
}  // end of loop

//Slave:
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 4; 
SoftwareSerial mySerial(2,3); //RX = 2 TX = 3

int fAvailable () {
  return mySerial.available ();  
}

int fRead() {
  return mySerial.read ();
}

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

void setup() {
  Serial.begin(9600);
  mySerial.begin(28800);
  myChannel.begin();
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
}  // end of setup

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

Regards, Vava (Sorry for my English... :-)

  • While tangential to your question, you should verity that the sendMsg() method blocks until the message has been fully emitted (and not merely hardware or software buffered), as if it does not your immediate disabling of the transmit enable pin after that call will truncate your message. – Chris Stratton Dec 2 '15 at 1:45
2

Reference page: http://www.gammon.com.au/forum/?id=11428


The blocking library is intended where you want to wait for incoming data, and are prepared to do nothing else while it arrives. There is also a timeout parameter which lets the code proceed if nothing happens for a specified number of milliseconds.

The non-blocking library is intended for where you need to do other things (like control motors, or read switches) at the same time you are receiving data. For that situation you call an update function periodically which assembles the incoming packet, byte-by-byte if necessary.

When the data is fully received the update returns true, and you can process the packet.


If so, how can a slave on the same line at the same time receive from master the message "hello world" and print it to the serial monitor?

As I stated above in neither case do you receive and print "at the same time", however the non-blocking one does not simply wait until all the data has arrived.


Example code from the linked page:

void loop ()
  {
  if (myChannel.update ())  // returns true when packet fully received
    {
    Serial.write (myChannel.getData (), myChannel.getLength ()); 
    Serial.println ();
    }

  // do other things while waiting

  }  // end of loop

If in example for non-blocking libraries is the same wiring as in the example of blocking library, where is (in non-blocking example) definition of RX(=2) and TX(=3) pins?

The hardware is the same.


there is limitation for use - only pins which supports change interrupts.

Of course. If the underlying protocol is SoftwareSerial then the usual limitations apply (that is for the Rx pin).

  • Thank You for the explanation. Let me have a 2 small supplementary questions - 1) If in example for non-blocking libraries is the same wiring as in the example of blocking library, where is (in non-blocking example) definition of RX(=2) and TX(=3) pins? 2) In description of SoftwareSerial (arduino.cc/en/Reference/SoftwareSerial), there is limitation for use - only pins which supports change interrupts. Is the same limitation for RS485 non-blocking library ? – vava Dec 2 '15 at 10:42
  • See amended reply. – Nick Gammon Dec 2 '15 at 22:01
0

The hardware connection should be the same, as blocking vs non-blocking is referring to how you handle the incoming data packets.

Don't quote me on this, but generally, blocking vs non-blocking refers to the use of delay() or similar, which will block anything else from running while that function is being executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.