A basic library for RS485 communication

Dependencies:   BufferedSerial

Dependents:   mbed_blinko

Embed: (wiki syntax)

« Back to documentation index

RS485 Class Reference

RS485 Library. More...

#include <RS485.h>

Public Member Functions

 RS485 (PinName tx, PinName rx, PinName dere)
 Create a BufferedSerial port, connected to the specified transmit and receive pins.
void sendComplemented (const byte what)
 sendComplemented byte send a byte complemented, repeated only values sent would be (in hex): 0F, 1E, 2D, 3C, 4B, 5A, 69, 78, 87, 96, A5, B4, C3, D2, E1, F0 the byte to complement
void sendMsg (const byte *data, const byte length)
 send message cyclic redundancy check
byte recvMsg (byte *data, const byte length, unsigned long timeout)
 receive message reads serial port and populates data

Static Public Member Functions

static byte crc8 (const byte *addr, byte len)
 calculate 8-bit CRC cyclic redundancy check

Detailed Description

RS485 Library.

Can send from 1 to 255 bytes from one node to another with:

Packet start indicator (STX) Each data byte is doubled and inverted to check validity Packet end indicator (ETX) Packet CRC (checksum)

Using MAX485 modules or the MAX485 CSA+

Example:

  #include "mbed.h" 
  #include <RS485.h>
  Serial pc(USBTX, USBRX); 
  RS485 RS485(PC_10,PC_11,PB_3); // Tx, Rx , !RE and DE MAX485 pin
 
  DigitalOut ho(PB_3); // this pin should be connected to !RE and DE
  typedef uint8_t byte;
 
  byte regvalue[9];
  byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data
  int main()
  {
    pc.printf("main\n");
    while(1) {
       pc.printf("Starting\n");
       ho = 1;                  // Enable sending on MAX485
       RS485.sendMsg(data,sizeof(data));
       wait_ms(600);            // Must wait for all the data to be sent   
       ho = 0;                  // Enable receiving on MAX485
       pc.printf("Getting data\n");
       if(RS485.readable() >0){
           memset(regvalue,0,sizeof(regvalue));
           wait_ms(200);
           RS485.recvMsg(regvalue,sizeof(data),500);
           wait_ms(200);
           for (int count = 0; count < 9; count++) {
               pc.printf("%X - ", regvalue[count]);
           }
       }else printf("No Data\n");
       printf("Done\n");
       wait_ms(1000);
    }
  } 

Definition at line 82 of file RS485.h.


Constructor & Destructor Documentation

RS485 ( PinName  tx,
PinName  rx,
PinName  dere 
)

Create a BufferedSerial port, connected to the specified transmit and receive pins.

Parameters:
txTransmit pin
rxReceive pin
dereEnable pin, this pin should be connected to !RE and DE
Note:
uses BufferedSerial

Definition at line 12 of file RS485.cpp.


Member Function Documentation

byte crc8 ( const byte *  addr,
byte  len 
) [static]

calculate 8-bit CRC cyclic redundancy check

Parameters:
addrbyte pointer of information to use (typical an byte array)
lenlength of byte of information were converting
Returns:
the CRC byte

Definition at line 19 of file RS485.cpp.

byte recvMsg ( byte *  data,
const byte  length,
unsigned long  timeout 
)

receive message reads serial port and populates data

Parameters:
databuffer to receive into
lengthlength of the data
timeoutclock_mseconds before timing out
Returns:
the number of bytes received

Definition at line 62 of file RS485.cpp.

void sendComplemented ( const byte  what )

sendComplemented byte send a byte complemented, repeated only values sent would be (in hex): 0F, 1E, 2D, 3C, 4B, 5A, 69, 78, 87, 96, A5, B4, C3, D2, E1, F0 the byte to complement

Definition at line 37 of file RS485.cpp.

void sendMsg ( const byte *  data,
const byte  length 
)

send message cyclic redundancy check

Parameters:
datathe data to be sent through RS485
lengthlength of the data
Note:
puts STX at start, ETX at end, and add CRC

Definition at line 50 of file RS485.cpp.