RS485 protocol error detection

I've been looking into this RS485 protocol library.
I understand the master and slave code but the site mentions that the library contains an error checking protocol. When i look at the source code from the library there are no lines of code that suggest the use of an error check. Would you guys be so kind to enlighten me about the code? (I've included the code below.)

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WConstants.h"
#endif

typedef void (*WriteCallback)  (const byte what);    // send a byte to serial port
typedef int  (*AvailableCallback)  ();    // return number of bytes available
typedef int  (*ReadCallback)  ();    // read a byte from serial port

void sendMsg (WriteCallback fSend, 
              const byte * data, const byte length);
byte recvMsg (AvailableCallback fAvailable, ReadCallback fRead, 
              byte * data, const byte length, 
              unsigned long timeout = 500);

First, each nibble is turned into a byte, where the second four bits are the complement of the first. Thus only the following bytes are valid, and the others are rejected:

0F, 1E, 2D, 3C, 4B, 5A, 69, 78, 87, 96, A5, B4, C3, D2, E1, F0

Second, there is a CRC check at the end of the packet. If the CRC does not match at the receiving end, the packet is rejected.

Now that I look into this thread, you piggy-backed onto an old (August 2013) thread about "RS485 Communication not working using Nick Gammon's RS485 library (Hardware)".

This (new question) is completely irrelevant to the topic of the library not working. Please in future just start a new thread, rather than tacking onto a 2-year old one. The fact that they both are something to do with RS485 is not sufficient justification.

The ones complement i understand, but there seems to be no code that does this? This part i do not understand.

This (new question) is completely irrelevant to the topic of the library not working. Please in future just start a new thread, rather than tacking onto a 2-year old one. The fact that they both are something to do with RS485 is not sufficient justification.

I'm sorry. I read the thread but not close enough it seems!

Generate the CRC or check the CRC? These lines do the check:

          // if we have the ETX this must be the CRC
          if (have_etx)
            {
            if (crc8 (data, input_pos) != current_byte)
              return 0;  // bad crc  
            return input_pos;  // return received length
            }  // end if have ETX already

Where do these lines of code come from? The library i have does not contain these lines? (downloaded the library from http://www.gammon.com.au/Arduino/RS485_protocol.zip)

By using the ones complement, does it mean that a piece of data can't be something like 0xAF? For example i want to send an array that contains some values like: 0x45, 0x68, 0xFA, 0x65.

Where do these lines of code come from? The library i have does not contain these lines?

Yes it does. I just followed that link, unzipped the file, and found them in RS485_protocol.cpp.

By using the ones complement, does it mean that a piece of data can't be something like 0xAF?

Every value can be used. If you send 0xAF it becomes:

0xA5 0xF0

In other words, each byte becomes two bytes. For each nibble (4 bits) the ones complement is used for the other four bits.

I’m so sorry! I never checked this file because the library is called with “RS485_protocol.h” and thought that this meant only the .h file was used.

So to make things clear (for me): The .cpp file is called with the funtions sendMsg and recvMsg through the .h file. These functions use the .cpp file to implement the start, stop, CRC, … For this I only need to insert the .h library in the sketch?

You include the .h file in the sketch, which triggers the IDE to include the .cpp ones as well. The .h file merely specifies which functions are available, the .cpp file implements them.

Thank you Nick! I'm waiting for the components to arrive so I can do some tests!