TCP to serial and vice versa

Hello,

I am trying to make simple project where I send data to serial port and it will show it on TCP and other way around, when I type something in TCP server it should show up on serial.

Board: Arduino Leonardo ETH (genuine) - connected to TP-link router via cable
Computer 1 (windows 10, using Hercules for serial monitoring and RS232 to USB adapter)
Computer 2 (Kali linux, and using terminal with netcat to connect via netcat) - this computer is connected to TP-link router via wifi

Everything seems to work expect: When I type let’s say number “1” in serial terminal i get output on TCP “g” when i type “2” in serial terminal i get output of “3” in TCP

When i type “1” in TCP i get serial output “g=” when i type “2” in TCP i get serial out “3=”

Here is the code, i cannot figure out whats wrong …

#include <SPI.h>
#include <Ethernet.h>

// Arduino MAC aadress
// Arduino IP aadress
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x3C, 0x74
};
IPAddress ip(192, 168, 1, 100);

#define UART_TCP_PORT 3300
#define TR_BAUD_RATE 115200
EthernetServer server(UART_TCP_PORT);

void setup() {
  while (!Serial) { ; }
    // initialize CS pin
    Ethernet.init(10);
    // initialize the ethernet device
    Ethernet.begin(mac, ip);
    

    // Open serial communication
    SERIAL_PORT_HARDWARE.begin(TR_BAUD_RATE);

    // start listening for clients
    server.begin();
}

void loop() {
    // wait for a TCP client:
    EthernetClient client = server.available();

    // when the TCP client is connected
    if (client) {
        // and sends the byte
        if (client.available() > 0) {
            // read the bytes incoming from the TCP client and send it to UART
            SERIAL_PORT_HARDWARE.write(client.read());
        }
    }

    // when TR module sends the byte, read it and send it to TCP client
    if (SERIAL_PORT_HARDWARE.available()) {
        server.write(SERIAL_PORT_HARDWARE.read());
    }
    
}

I'm not sure about the character issues. Some thoughts:

  1. Verify you're using the right baud rate on the Windows PC serial
  2. Check netcat's connection mode isn't doing anything funky/unexpected. Posting the command you're using to connect would be a good idea.

Aside from this, I note your code is checking for a new client each time around loop() and then ditching it, which I think is probably not what you want. I'd have thought you want to establish a client, and thereafter hold onto that client instance and shuffle bytes between it and the serial port.

Thanks for your input, I will try putty or something like that instead of netcat just to be sure. The netcat command I use is “netcat 192.168.1.100 3300”

Baudrate is corret, othewise I dont get any output on serial.

Right, that netcat command should be creating a TCP connection, so that looks fine to me.

I'd try fixing up the issues in loop() since everything else is checking out.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.