Hardware component

Arduino UNO × 1
PHPoC Shield 2 for Arduino × 1
PHPoC Smart RS232 Board × 1

Why should we?

In the era of IoT, do you still own serial devices?

The answer is yes for me. I have a Pan-tilt camera which only supports RS485 interface, and I am not going to throw it away any time soon . There are tons of serial devices over there in the real world, not to mention that most of industrial devices still use serial interfaces. Serial communication is simple, cost-efficient, reliable, and can come to the rescue whenever you need to get the console access to the device.

But, let me remind you, it is 2019. Everything is going to be Internet-connected! Isn't it great if we can add network capability to those serial devices, so that in case of need, we can connect and access them from wherever we want.

Here I will show you how I made a simple Ethernet to RS232 converter by using Arduino Uno and PHPoC Shield. Any TCP data bypass Ethernet connection can be transformed to serial data, and vice versa.

System Architecture

How can we?

For serial interface, here I use a Smart RS232 Board. Along with a PHPoC Shield 2, they are stacked on an Arduino Uno.

A PHPoC shield supports both Wi-Fi and Ethernet connectivity, but you can only choose either of them for network connection. In my project I set up an Ethernet connection for PHPoC Shield. And with a small change in hardware setup, it can be used as Wi-Fi/Ethernet to RS232/RS422/RS485 converter. Maybe with a little more effort, you can even make a Wi-Fi/Ethernet Serial gateway, since an Arduino board can carry up to 14 smart expansion boards, and by combining with a PHPoC shield, it can support up to 4 TCP connections simultaneously.

Arduino Uno + PHPoC Shield 2 + Smart RS232 Board

With cables

In my project, I made the Arduino board with PHPoC shield as a TCP client, so that it can connect to a TCP server via Ethernet network. Well, TBH, you can just call my project "TCP to Serial converter" as well.

Each Smart RS232 Board has a RS-232 port, and is addressed by ID, which can set by manually setting DIP switches. In this project, the board are addressed as 1.

Basically, the Arduino code does read the incoming data from serial, then send it to TCP server, as well as read incoming data from TCP server and forward it to serial.

For the demonstration, you can take a look at the screenshot below:

Arduino Code

#include <Phpoc.h>
#include <PhpocExpansion.h>

#define BUFFER_SIZE 100
#define TCP_PEER_ADDR ""   // enter your tcp server addr here
#define TCP_PEER_PORT 1470    // enter your tcp server port here

char peer_addr[] = TCP_PEER_ADDR;
int peer_port = TCP_PEER_PORT;
PhpocClient tcp_client;

byte expansionId = 1; // based on the DIP switches you set
// https://www.phpoc.com/support/manual/pes-2607_user_manual/contents.php?id=layout
ExpansionSerial rs232(expansionId);

byte tcp_buf[BUFFER_SIZE];
byte serial_buf[BUFFER_SIZE];
int rwlen;

void setup() {

  // initialize PHPoC [WiFi] Shield:
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);

  // connect to TCP server
  if(tcp_client.connect(peer_addr, peer_port))
    // if connected
    Serial.println("Connected to server");
  else // if not connected
    Serial.println("connection failed");

  // set baudrate, parity, data bit, stop bit and flow control to  115200, none,
  // 8 bit, 1 bit and no flow control, respectively.


void loop() {

  int tcp_rxlen = tcp_client.available();
  int serial_txfree = rs232.availableForWrite();
  if (tcp_rxlen) {
    if (serial_txfree > tcp_rxlen)
    rwlen = (tcp_rxlen <= BUFFER_SIZE) ? tcp_rxlen : BUFFER_SIZE;

    // read the incoming bytes from TCP communication
    tcp_client.readBytes(tcp_buf, rwlen);
    // send these received data via serial interface
    rs232.write(tcp_buf, rwlen);

  int serial_rxlen = rs232.available();
  int tcp_txfree = tcp_client.availableForWrite();
  if(serial_rxlen) {
  if (tcp_txfree > serial_rxlen)
  rwlen = (serial_rxlen <= BUFFER_SIZE) ? serial_rxlen : BUFFER_SIZE;
  // read the incoming bytes from serial communication
  rs232.readBytes(serial_buf, rwlen);
  // send these received data via TCP interface
    tcp_client.write(serial_buf, rwlen);

    // if the server's disconnected, stop the tcp_client:
    // do nothing forevermore: