Search code examples
linuxserial-portipopenwrt

Forward Bytes from Serial Port over IP using C/C++ on OpenWRT Linux


I am receiving packets of 35 Bytes on my Serial Port and would simply like to forward/tunnel them via Ethernet. I was wondering if anyone had any useful codesnippets. I know this can be done using netcat, but I was hoping for a simple C/C++ Program to be run as a background process. I am running a OpenWrt Linux with very limited Flash. Thanks in advance!


Solution

  • You can make a small program, that opens the serial port for reading and a socket for the outgoing communication.

    In a loop, read from the serial port, and what you read you write to the socket.

    In pseudo-ish code:

    int ser = open("/dev/ttyS0");
    int sock = socket_connect();
    
    for (;;)
    {
        ssize_t r = read(ser, data, MAXLEN);
        if (r > 0)
            write(sock, data, r);
    }