TCP/IP to Serial Converter (remote serial port). For more details see [https://developer.mbed.org/users/hudakz/code/Serial_over_Ethernet/wiki/Homepage]

Dependencies:   UIPEthernet mbed mbed-STM32F103C8T6

TCP/IP to Serial Converter

  • Enables communication with TTL serial device over Ethernet LAN connection.
  • You can considerably extend an existing serial connection without modifying the legacy serial communication software.

How it Works

Serial data sent by an application running on your PC to your PC's virtual serial port (see below how to create one) is automatically transmitted through Ethernet LAN connection to a "TCP/IP to Serial Converter". The converter then sends the data received over Ethernet to the connected TTL serial device through serial connection. In turn, the response from the serial device is sent back to your PC by the "TCP/IP to Serial Converter" over Ethernet connection and the application can read through the virtual serial port without knowing that it actually arrived over Ethernet .

/media/uploads/hudakz/serial_over_ethernet.png

How to build one

To create a "TCP/IP to Serial Converter" an inexpensive ENC28J60 Ethernet module is connected to the mbed board and set-up as TCP/IP server. The server is listening for data coming from clients for example at IP Address 192.168.1.181, Port Number 10001 (adapt to your needs). Binary data received from a client over the Ethernet is transmitted to the connected TTL serial device and in turn, data received from the serial device is sent back to the TCP/IP client via Ethernet.

  • Compile the program and save the binary to your mbed board.
  • Connect an ENC28J60 Ethernet module to the mbed board according to the pin assignment defined in main.cpp.
  • Connect the ENC28J60 module to your Ethernet network.
  • Connect a serial device to the mbed's serial port. Please notice that in case your serial device is using a regular RS-232 connection an additional "Serial TTL to regular RS-232 converter" is needed!

How to test and use the Converter

  • Create a virtual serial port on your PC as follows:
    See Using HW VSP powered by HW group and download their HW VSP3 Virtual Serial Port for free.
  • Install and run the HW VSP. Proceed to the Virtual Serial Port tab and create a new virtual serial port (for example COM200). Assign it the same IP address and port number as you specified in main.cpp (IP address 192.168.1.181 and port number 10001).

    /media/uploads/hudakz/virtualserialport.jpg

  • You can now close the HW VSP application and verify, through the Windows Device Manager, that a new COM200 port is available on the system. From now on, all serial data sent by any application running on your PC to your PC's serial port COM200 will be redirected trough the Ethernet LAN to the "TCP/IP to Serial Converter". Then mbed's serial port will act on behalf of COM200 and transmit the data to the connected serial device. In turn, the response from the serial device will be sent back trough the Ethernet LAN to your PC and the application can read through the (virtual) serial port COM200.
  • Simple test:. Connect the serial Tx pin on the mbed board to the Rx pin. On your PC, run your favorite terminal application (HyperTerminal, TeraTerm, PuTTY etc.) and open serial port COM200. Set the same bit rate as you did in main.cpp. Then type in some message and send it to serial port COM200. In turn, you should receive the same data echoed by the "TCP/IP to Serial Converter".

main.cpp

Committer:
hudakz
Date:
2015-04-10
Revision:
0:5f21653e7308
Child:
1:d39ee3c772e8

File content as of revision 0:5f21653e7308:

/*
 * TCP/IP to Serial Converter.
 *
 *  Created: 2015-04-10
 *   Author: Zoltan Hudak
 *
 *  ENC28J60 modul is connected to the mbed board to create a TCP/IP server.
 *  The server is listening for clients at port 1001 (you can select another one if you like).
 *  Binary data received from a client is transmitted to a serial device and
 *  data received from the serial device is in turn sent back to the TCP/IP client. 
 * 
 */

#include "mbed.h"
#include "UIPEthernet.h"

const int           BAUD = 115200;  // serial bit rate (change to match the speed of the connected serial device)
uint8_t             rxBuf[1024];    // serial rx buffer (reduce the size if you don't need it)
uint8_t             txBuf[1024];    // serial tx buffer (reduce the size if you don't need it)
volatile int        rxCounter = 0;  // serial rx counter

// UIPEthernet is the name of a global instance of UIPEthernetClass.
// Do not change the name! It is used within the UIPEthernet library.
#if defined(TARGET_LPC1768)
Serial              serial(p9, p10);                        // serial port
UIPEthernetClass    UIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
#elif defined(TARGET_NUCLEO_F103RB)
Serial              serial(PA_2, PA_3);                     // serial port
UIPEthernetClass    UIPEthernet(PB_5, PB_4, PB_3, PB_6);    // mosi, miso, sck, cs

// If your board/plaform is not present yet then uncomment
// the following three lines and replace TARGET_YOUR_BOARD and other pins as appropriate.

//#elif defined(TARGET_YOUR_BOARD)
//Serial              serial(SERIAL_TX, SERIAL_RX);                         // serial port
//UIPEthernetClass    UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);     // mosi, miso, sck, cs
#endif

// MAC number must be unique within the connected network. Modify as appropriate.
const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 };

// IP address must be also unique and compatible with your network. Change as appropriate.
const IPAddress     MY_IP(192, 168, 1, 181);

// TCP/IP port (select a port) 
const uint16_t      MY_PORT = 10001;

EthernetServer      myServer = EthernetServer(MY_PORT); // create server

/**
 * @brief   Reads data from serial port
 * @note    Called on arrival of new serial data
 * @param
 * @retval
 */
void onSerial(void) {
    rxBuf[rxCounter++] = serial.getc();   // read serial data
}

/**
 * @brief   Main
 * @note
 * @param
 * @retval
 */
int main(void) {
    UIPEthernet.begin(MY_MAC, MY_IP);
    myServer.begin();
    serial.baud(BAUD);
    serial.attach(&onSerial);
    
    while(1) {
        EthernetClient  client = myServer.available();
        if(client) {                                // check for connected client
            size_t  size = client.available();
            if(size > 0) {
                rxCounter = 0;                      // clear rx counter
                size = client.read(txBuf, size);    // read data received from the TCP/IP client
                for(int txCounter = 0; txCounter < size; txCounter++)
                    serial.putc(txBuf[txCounter]);  // send through serial
                wait_ms(50);                        // tune (increase) this if you are loosing some data
                client.write(rxBuf, rxCounter);     // send back to client
            }
        }
    }
}