Announcement

Collapse
No announcement yet.

Arduino - RS-232 Expansion Board

Collapse
X
Collapse
  •  

  • Arduino - RS-232 Expansion Board

    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 are part of of PhpocExpansion library for Arduino. The library reference is available here

    This tutorial explains how Arduino exchanges data with Serial Device via RS-232 using RS-232 expansion board.


    Application





    Testing

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




    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 RS-232 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 RS-232 expansion board PES-2606 on PHPoC Shield or PHPoC WiFi Shield.
    • Connect Arduino to PC via usb cable.
    • Connect RS232-to-USB cable 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 "ExpansionSerialRS232" 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 RS232-to-USB cable.
    • Config serial parameters on TeraTerm
    • Open "Serial Monitor" tool on Arduino IDE.
    • Send some data from TeraTerm to RS-232 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 RS-232 expansion boards.
    • Sends back the received data to RS-232 expansion boards and prints the received to Serial Monitor.




    At the beginning
    • We need to create a ExpansionSerial object:
      Code:
      	ExpansionSerial rs232(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: RS-232 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.
    • rs232.begin(F("115200N81N")) function is used to set baud-rate, parity, data bit, stop bit and flow control to 115200, none, 8 bit, 1 bit and no flow control, respectively.


    In loop() function

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


    Data is read from the receiving buffer of RS-232 expansion board and save to application buffer, and then write to the sending buffer of RS-232 expansion board.



    Firstly, we get the size of received data in receiving buffer of RS-232 expansion board.
    Code:
    // get the size of the received data in the receiving buffer of RS-232 expansion board
    int rxlen = rs232.available();

    If data is available in the receiving buffer of RS-232 expansion board, get the size of free space in the sending buffer of RS-232 expansion board.
    Code:
    // read and write length
    int rwlen;
    
    // get the size of free space in the sending buffer of RS-232 expansion board
    int txfree = rs232.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 RS-232 expansion board. If we write an amount of data that exceed the free space of the sending buffer of RS-232 expansion board, the sending buffer of RS-232 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 RS-232 expansion board.
    Code:
    // check to make sure that write data does not exceed free space in the sending buffer of RS-232 expansion board
    rwlen = min(rwlen, txfree);

    Reading data from the receiving buffer of RS-232 expansion board and save to application buffer.
    Code:
    // receive data from the receiving buffer of RS-232 expansion board and save it to the application buffer
    rwlen = rs232.readBytes(rwbuf, rwlen);

    Sending data from the application buffer to the sending buffer of RS-232 expansion board
    Code:
    // send data from the application buffer to the sending buffer of RS-232 expansion board
    rs232.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
    Last edited by support; 12-14-2022, 07:16 AM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by 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, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by 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, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by 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, 02:43 PM
    • Arduino - Stepper Motor Controller
      by 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, 02:41 PM
    • Arduino - DC Motor Controller
      by 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, 02:40 PM
    • Arduino - Digital Input Board
      by 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, 02:39 PM
    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎