RS485 library - issue handling and interrupts

Posted by Rasihasi on Mon 03 Dec 2012 07:02 PM — 4 posts, 22,200 views.

#0
Hi Nick,

first, thanks for your great RS485 library, I use these lib in my current arduino project to remote control ham radio related gear just like a antenna tuner and a antenna switch. Your lib works fine!

I've two questions please:

1. interrupt

it's possible to use an interrupt to signaling the main process (means the main loop), when bytes are in the rx buffer? Then the main process can make a brake, receive the complete message and returns to his other jobs . You know what I mean?? There is a coding example?

2. issue handling:

what is the best way to inform the main process about an communication problem, so the process can send the message again? At the moment I calculate my own checksum over the transmitted bytes and the slave calculate the checksum again over the received bytes. When both checksums are equal, all is fine, otherwise the master is sending the message again.
There is a more simple way and a code snippet if possible?

Thanks in advance and excuse my english,

Steffen
(Rasihasi)
Australia Forum Administrator #1
1. If you use serial processing to get the incoming data it is already handled by interrupts. You have time to check in your main loop until the Rx buffer fills up (usually 32 bytes).

One way of doing this would be to check in the main loop if enough data has arrived to fill an expected packet, eg.


if (Serial.available () > 10)
  {

  byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
 
  // if received is non-zero process message here 
}



2. You don't need to calculate a checksum because the library does a CRC check. If it fails it returns zero from recvMesg.

So in the above example:


  byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
 
 if (received == 0)
   {
   // request message again
   }
#2
Hi Nick,

wow, a really quick response, thanks a lot :-)

to 1: yes, that's what I'm looking for

to 2: okay, I understand. The master send his message only once and the slave must request the message when the transmission is corrupted, otherwise the slave sends only a short "ACK"nowledge. Good, then I can rebuild my error handling logic now.

Thanks again!

Kind regards,

Rasihasi

Australia Forum Administrator #3
I have made a non-blocking version of the class as described here:

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

This lets you call a function in loop(), and when the packet has arrived you can process it. This might be more suitable if you need to do other things while you are waiting for incoming data.