debevv / nanoMODBUS

A compact MODBUS RTU/TCP C library for embedded/microcontrollers
MIT License
234 stars 47 forks source link

NMBS_ERROR_TRANSPORT after disconnect #43

Closed JonasReich92 closed 4 months ago

JonasReich92 commented 5 months ago

Hello, i am using STM32F407 with freeRTOS and LwIP. I wanted to use ModbusTCP server, so I use the Linux Example (server-tcp.c).

I do only a few changes to use it as a Task.

/**
 * Create an ModbusTCP Task
 */
void ModbusTCPTaskRun(void)
{
    osThreadDef(ModbusTCPTask, StartModbusTCPTask, osPriorityHigh, 0, 1024);
    ModbusTCPHandle = osThreadCreate(osThread(ModbusTCPTask), NULL);

}

and "int main" to StartModbusTCPTask

int StartModbusTCPTask(void)
{
    char* address = "192.168.0.50";
    char* port = "502";
.
.
.

Problem: I can connect the Server with "ModbusPoll" and get the right Register Data (Dummy Data). I can connect the Server with a second instance of "ModbusPoll" and get the right Register Data, too. But when i make a disconnect, I cannot connect again. During the Debugging i can see that in the function read_fd_linux() the function disconnect(arg) is called. This looks ok. But there is no ping working and i cannot connect again. It looks there is a Problem with the Sockets.

Do you have this issue, too? Or any Idea about that Problem?

debevv commented 5 months ago

Hi, yes the problem was definitely the server not being able to accept new connection after closing the first one. I fixed the linux TCP server example, can you try it and tell me if it fixed your problem?

JonasReich92 commented 5 months ago

Hi, thank you.

I solve the issue in the meantime with a few defines on my lwip stack:

define SO_REUSE_RXTOALL 1

define LWIP_SO_RCVBUF 1

define LWIP_TCP_KEEPALIVE 1

define SO_REUSE 1

It looks like your changes make's it better. But an other Problems occures in this context. How many connection can be established at the same time? Because I can open two connections at the same time, the third one can be connected too but there is always a Read/Write Error on more than two connections. image

debevv commented 5 months ago

I'm using modpoll to test the server (https://www.modbusdriver.com/modpoll.html). There should not be any limit on the number of connections now, in fact I tried to open about 6 connections, closing one, reopening another one and everything seems to work. Maybe your problem lies within lwip? I used it in the past and I remember having some (understandable) limitations related to the memory usage/buffer sizes, including the maximum number of open connections, so maybe you can start investigating there?

JonasReich92 commented 5 months ago

Yes, i think this problem is on my side. Thank you.