Announcement

Collapse
No announcement yet.

Arduino - RS-485 Expansion Board

Collapse
X
Collapse

  • Arduino - RS-485 Expansion Board

    PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
    Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
    Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

    Library and examples for the RS422/RS485 expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here

    The examples for the RS422/RS485 expansion board includes two examples: one for exchanging data between Arduino and Serial device via RS-422, the other one is for exchanging data between Arduino and Serial device via RS-485.

    This tutorial explains how Arduino exchanges data with Serial Device via RS-485 using RS422/RS485 expansion board.


    Application





    Testing

    RS422/RS485 Expansion Board is designed to let Arduino exchange data with serial device via RS-422 or RS-485. For testing, we can use PC or laptop as a serial device. Although most of PC and laptop does not have RS422/RS485 COM port, we still can do the test by using USB-to-RS485 converter cable (or USB-RS232 cable + RS232-to-RS485 converter).




    Hardware Required

    Note that: in the test, we use two cables: one is USB cable that connects PC to Arduino, the other cable is RS232-to-USB cable that connects PC to RS422/RS485 expansion board for testing. These cables connect to PC with different COM ports.
    You can check these COM Port by accessing "Device Manager" on your PC. It looks like below image.



    Note that the COM port number is depending on each PC.



    Wiring
    • Stack PHPoC Shield or PHPoC WiFi Shield on Arduino.
    • Stack RS422/RS485 expansion board PES-2607 on PHPoC Shield or PHPoC WiFi Shield.
    • Connect Arduino to PC via usb cable.
    • Connect USB-to-RS485 converter to expansion board and your PC.






    Install Arduino IDE

    If you have not install Arduino IDE yet, please download and install Arduino IDE .





    Install Library
    • Run Arduino IDE.
    • Navigate to Sketch > Include Library > Manage Libraries

    • Search "Phpoc Expansion" on search bar of the Library Manager.

    • Select the PhpocExpansion library and press the [Install] button.

    • Restart Arduino IDE for the next step.



    Source Code
    • Run Arduino IDE.
    • Open "ExpansionSerialRS422" example.
    • Compile the example code and uploads to Arduino by clicking "Upload" button on Arduino IDE.
    • Run a Serial software such as TeraTerm, connect to COM port corresponding to RS485-to-USB cable.
    • Config serial parameters on TeraTerm
    • Open "Serial Monitor" tool on Arduino IDE.
    • Send some data from TeraTerm to RS422/RS485 Expansion Board by typing some characters in TeraTerm.
    • We can see that, data is shown on both TeraTerm and Serial Monitor.
      Note that: By default, TeraTerm does NOT display the data that it sends, it only displays data that it receives from serial.
      Therefore, data that is displayed in screen is echo data from Arduino

    Code Explanation

    This code does:
    • Reads data from RS422/RS485 expansion boards.
    • Sends back the received data to RS422/RS485 expansion boards and prints the received to Serial Monitor.




    At the beginning
    • We need to create a ExpansionSerial object:
      Code:
      	ExpansionSerial rs485(expansionId);
      Where expansionId is address of expansion board that are set via DIP switch on expansion board. By using the different expansionId, we can stack multiple expansion board on a single Arduino Uno or Mega.
      You can change it and map it to the below to know the value.

    • We need to create a buffer to keep the received data:
      Code:
      	byte rwbuf[BUFFER_SIZE];



    Note that: RS422/RS485 expansion board is one of the expansion boards. List of expansion for Arduino is available here



    In setup() function
    • Phpoc.begin() and Expansion.begin() functions must be used to initial PHPoC [WiFi] Shield and expansion board, repectively.
    • Serial.println(in0.getName()) function is used optionally to get name of expansion board and print it to serial.
    • rs485.begin(F("115200N81T")) function is used to set baud-rate, parity, data bit, stop bit and flow control to 115200, none, 8 bit, 1 bit and TxDE control, respectively.


    In loop() function

    There are some functions are used.
    • rs485.available(): get the size of received data in the receiving buffer of RS422/RS485 expansion board.
    • rs485.availableForWrite(): get the size of free space in the sending buffer of RS422/RS485 expansion board.
    • rs485.readBytes(rwbuf, rwlen): receive data from the receiving buffer of RS422/RS485 expansion board and save it to the application buffer.
    • rs485.write(rwbuf, rwlen): send data from the application buffer to the sending buffer of RS422/RS485 expansion board.


    Data is read from the receiving buffer of RS422/RS485 expansion board and save to application buffer, and then write to the sending buffer of RS422/RS485 expansion board.



    Firstly, we get the size of received data in receiving buffer of RS422/RS485 expansion board.
    Code:
    // get the size of the received data in the receiving buffer of RS422/RS485 expansion board
    int rxlen = rs485.available();

    If data is available in the receiving buffer of RS422/RS485 expansion board, get the size of free space in the sending buffer of RS422/RS485 expansion board.
    Code:
    // read and write length
    int rwlen;
    
    // get the size of free space in the sending buffer of RS422/RS485 expansion board
    int txfree = rs485.availableForWrite();

    If we read an amount of data that exceed capacity of application buffer, application buffer will be overflowed and data is lost.
    Therefore, before reading data, we need to make sure that the the reading data does not exceed the application buffer capacity.
    Code:
    // check to make sure that read data does not exceed size of application buffer
    rwlen = min(rxlen, BUFFER_SIZE);

    All data that stored in application buffer will be written to the sending buffer of of RS422/RS485 expansion board. If we write an amount of data that exceed the free space of the sending buffer of RS422/RS485 expansion board, the sending buffer of RS422/RS485 expansion board will be overflowed and data is lost. Therefore, before reading data and store data to application buffer, we need to make sure that the the reading data does not exceed the free space of the sending buffer of RS422/RS485 expansion board.
    Code:
    // check to make sure that write data does not exceed free space in the sending buffer of RS422/RS485 expansion board
    rwlen = min(rwlen, txfree);

    Reading data from the receiving buffer of RS422/RS485 expansion board and save to application buffer.
    Code:
    // receive data from the receiving buffer of RS422/RS485 expansion board and save it to the application buffer
    rwlen = rs485.readBytes(rwbuf, rwlen);

    Sending data from the application buffer to the sending buffer of RS422/RS485 expansion board
    Code:
    // send data from the application buffer to the sending buffer of RS422/RS485 expansion board
    rs485.write(rwbuf, rwlen);

    Printing data to Serial Monitor of Arduino IDE
    Code:
    // print data to serial monitor of Arduino IDE
    Serial.write(rwbuf, rwlen);

    See Also


    References
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 03:45 PM
    • Arduino - RS-422 Expansion Board
      support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 03:44 PM
    • Arduino - RS-232 Expansion Board
      support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 03:43 PM
    • Arduino - Stepper Motor Controller
      support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 03:41 PM
    • Arduino - DC Motor Controller
      support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 03:40 PM
    • Arduino - Digital Input Board
      support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 03:39 PM
    Working...
    X