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".
Committer:
hudakz
Date:
Thu Aug 25 10:37:39 2016 +0000
Revision:
2:e38442f3cb74
Parent:
1:d39ee3c772e8
Improved reading serial data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:5f21653e7308 1 /*
hudakz 0:5f21653e7308 2 * TCP/IP to Serial Converter.
hudakz 0:5f21653e7308 3 *
hudakz 0:5f21653e7308 4 * Created: 2015-04-10
hudakz 0:5f21653e7308 5 * Author: Zoltan Hudak
hudakz 0:5f21653e7308 6 *
hudakz 0:5f21653e7308 7 * ENC28J60 modul is connected to the mbed board to create a TCP/IP server.
hudakz 1:d39ee3c772e8 8 * The server is listening for clients at IP address 192.168.1.181, port 10001 (you can adapt it to your needs).
hudakz 2:e38442f3cb74 9 * Binary data received from a client is transmitted over serial device and
hudakz 1:d39ee3c772e8 10 * in turn data received from the serial device is sent back to the TCP/IP client.
hudakz 1:d39ee3c772e8 11 *
hudakz 2:e38442f3cb74 12 * How to use the converter:
hudakz 1:d39ee3c772e8 13 * Compile and save it to your mbed board.
hudakz 1:d39ee3c772e8 14 * Connect a ENC28J60 module to the mber board according to the pin assignment below.
hudakz 1:d39ee3c772e8 15 * Connect a serial device to the mbed serial port. Please notice that in case your serial device
hudakz 1:d39ee3c772e8 16 * is using an RS-232 connection a "serial TTL to serial RS-232" converter is needed!
hudakz 1:d39ee3c772e8 17 * To test or use this "TCP/IP to Serial Converter", create a virtual serial port on your PC as follows:
hudakz 1:d39ee3c772e8 18 * See "Using HW VSP powered by HW group" at <http://www.HW-group.com>
hudakz 1:d39ee3c772e8 19 * and download their free "HW VPS3 Virtual Serial Port" program from <http://www.hw-group.com/products/hw_vsp/index_en.html>
hudakz 1:d39ee3c772e8 20 * Install and run the application and go to tab "Virtual Serial Port".
hudakz 1:d39ee3c772e8 21 * Create a new virtual serial port, for example with name COM200, IP Address 192.168.1.181 and Port number 10001.
hudakz 1:d39ee3c772e8 22 * From now on, whatever data you send to COM200 on your PC will be redirected via Ethernet to this "TCP/IP to serial Converter"
hudakz 1:d39ee3c772e8 23 * which will pass it to the connected remote serial device.
hudakz 1:d39ee3c772e8 24 * In turn, the response from the remote serial device will be sent back to COM200 on your PC.
hudakz 1:d39ee3c772e8 25 *
hudakz 0:5f21653e7308 26 */
hudakz 0:5f21653e7308 27
hudakz 0:5f21653e7308 28 #include "mbed.h"
hudakz 0:5f21653e7308 29 #include "UIPEthernet.h"
hudakz 0:5f21653e7308 30
hudakz 0:5f21653e7308 31 const int BAUD = 115200; // serial bit rate (change to match the speed of the connected serial device)
hudakz 2:e38442f3cb74 32 uint8_t rxBuf[512]; // serial Rx buffer (adjust the size to your needs)
hudakz 2:e38442f3cb74 33 uint8_t txBuf[512]; // serial Tx buffer (adjust the size to your needs)
hudakz 1:d39ee3c772e8 34 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 1:d39ee3c772e8 35 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 };
hudakz 1:d39ee3c772e8 36 // IP address must be also unique and compatible with your network. Change as appropriate.
hudakz 1:d39ee3c772e8 37 const IPAddress MY_IP(192, 168, 1, 181);
hudakz 1:d39ee3c772e8 38 // TCP/IP port (select a port)
hudakz 1:d39ee3c772e8 39 const uint16_t MY_PORT = 10001;
hudakz 1:d39ee3c772e8 40
hudakz 0:5f21653e7308 41
hudakz 0:5f21653e7308 42 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:5f21653e7308 43 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:5f21653e7308 44 #if defined(TARGET_LPC1768)
hudakz 1:d39ee3c772e8 45 Serial serial(p9, p10); // serial: tx, rx
hudakz 1:d39ee3c772e8 46 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // spi: mosi, miso, sck, cs
hudakz 2:e38442f3cb74 47 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \
hudakz 2:e38442f3cb74 48 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \
hudakz 2:e38442f3cb74 49 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \
hudakz 2:e38442f3cb74 50 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
hudakz 2:e38442f3cb74 51 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
hudakz 2:e38442f3cb74 52 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
hudakz 2:e38442f3cb74 53 || defined(TARGET_NRF51822) \
hudakz 2:e38442f3cb74 54 || defined(TARGET_RZ_A1H)
hudakz 1:d39ee3c772e8 55 Serial serial(PA_2, PA_3); // serial: tx, rx
hudakz 2:e38442f3cb74 56 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // spi: mosi, miso, sck, cs
hudakz 2:e38442f3cb74 57 #else
hudakz 0:5f21653e7308 58 // If your board/plaform is not present yet then uncomment
hudakz 0:5f21653e7308 59 // the following three lines and replace TARGET_YOUR_BOARD and other pins as appropriate.
hudakz 0:5f21653e7308 60
hudakz 0:5f21653e7308 61 //#elif defined(TARGET_YOUR_BOARD)
hudakz 1:d39ee3c772e8 62 //Serial serial(SERIAL_TX, SERIAL_RX); // serial: tx, rx
hudakz 1:d39ee3c772e8 63 //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // spi: mosi, miso, sck, cs
hudakz 0:5f21653e7308 64 #endif
hudakz 0:5f21653e7308 65
hudakz 0:5f21653e7308 66 EthernetServer myServer = EthernetServer(MY_PORT); // create server
hudakz 1:d39ee3c772e8 67 volatile int rxLength = 0; // number of serial bytes received
hudakz 0:5f21653e7308 68
hudakz 0:5f21653e7308 69 /**
hudakz 0:5f21653e7308 70 * @brief Reads data from serial port
hudakz 0:5f21653e7308 71 * @note Called on arrival of new serial data
hudakz 0:5f21653e7308 72 * @param
hudakz 0:5f21653e7308 73 * @retval
hudakz 0:5f21653e7308 74 */
hudakz 0:5f21653e7308 75 void onSerial(void) {
hudakz 2:e38442f3cb74 76 while(serial.readable())
hudakz 2:e38442f3cb74 77 rxBuf[rxLength++] = serial.getc(); // read serial data
hudakz 0:5f21653e7308 78 }
hudakz 0:5f21653e7308 79
hudakz 0:5f21653e7308 80 /**
hudakz 0:5f21653e7308 81 * @brief Main
hudakz 0:5f21653e7308 82 * @note
hudakz 0:5f21653e7308 83 * @param
hudakz 0:5f21653e7308 84 * @retval
hudakz 0:5f21653e7308 85 */
hudakz 0:5f21653e7308 86 int main(void) {
hudakz 0:5f21653e7308 87 serial.attach(&onSerial);
hudakz 1:d39ee3c772e8 88
hudakz 0:5f21653e7308 89 while(1) {
hudakz 0:5f21653e7308 90 EthernetClient client = myServer.available();
hudakz 1:d39ee3c772e8 91 if(client) { // check for client connected
hudakz 1:d39ee3c772e8 92 size_t txLength = client.available();
hudakz 1:d39ee3c772e8 93 if(txLength > 0) {
hudakz 1:d39ee3c772e8 94 rxLength = 0; // clear rx length
hudakz 1:d39ee3c772e8 95 txLength = client.read(txBuf, txLength); // read data received from the TCP/IP client
hudakz 1:d39ee3c772e8 96 for(int i = 0; i < txLength; i++) // send through serial
hudakz 1:d39ee3c772e8 97 serial.putc(txBuf[i]);
hudakz 2:e38442f3cb74 98 wait_ms(50); // receive serial data from device
hudakz 2:e38442f3cb74 99 client.write(rxBuf, rxLength); // send data to client over ethernet
hudakz 0:5f21653e7308 100 }
hudakz 0:5f21653e7308 101 }
hudakz 0:5f21653e7308 102 }
hudakz 0:5f21653e7308 103 }