FreeRTOS + TCP for modbus tcp on RA6M4

Hi, I am trying to develop an app to run a modbus tcp server on Renesas RA6M4 and send data using this protocol.
The MCU software package provides FreeRTOS+TCP stack to connect it over TCP/IP but when adding the Modbus library (freemodbus) I don’t know how to proceed in order to send data.
I don’t see how can I build a modbus server after adding freertos+tcp stack.
Can you please help me?

Is this the official website? - it looks like they have a FreeRTOS port already, albeit using lwIP you should be able to see where lwIP is used to send and receive:

If you can link to the porting documentation for that library then we could suggest how to create an implementation using FreeRTOS+TCP.

Yes it’s the official website. And you’re right they use lwIP stack for this library.
here is the porting documentation:
So it means that I have to change to lwip ? because in renesas RA6M4 they have only freertos+tcp to connect over TCP.

You don’t need to switch to lwIP. It’s just another networking stack. It should be possible to just replace the lwIP networking calls with the corresponding FreeRTOS+TCP API.

You mean that every call or header in this library about lwip , I should replace it with freertos+tcp stuff ? is it feasible or is there something more clear

Well, if I got you right you want to develop your own TCP/modbus server. The networking part shouldn’t be that difficult and therefore not too hard to port to FreeRTOS+TCP.
And if you don’t have much experience with networking yet, it’s a good and valuable chance to learn something about it :slight_smile:

1 Like

The linked documentation tells you which ports exist, not how to port to a different TCP/IP stack. Somewhere in the code lwIP will get initialised - you need to replace that with the FreeRTOS+TCP equivalent. Likewise, where lwIP creates a connection, or sends, or receives, you need to replace the lwIP calls with the FreeRTOS+TCP equivalent. Hopefully the use of lwIP will be constrained to a single “porting” file - where you can change the implementation of the functions in that file to use FreeRTOS+TCP.

1 Like

I did program a TCP/modbus before. What I remember was the high frequency of sending and receiving messages.

I found one example where modbus uses lwIP, it is not too much code.

Where lwIP talks about PCB’s (Protocol Control Block) and netconn’s, FreeRTOS+TCP uses standard BSD/Posix sockets.

This modbus implementation uses callback functions, or application hooks. This one for example, it sets the reception handler to prvxMBTCPPortReceive():

	tcp_recv( pxPCB, prvxMBTCPPortReceive );


And where data is written:

    if( tcp_write( pxPCBClient, pucMBTCPFrame, ( u16_t ) usTCPLength, NETCONN_COPY ) == ERR_OK )

you can call the FreeRTOS_send() function.

This is not necessary:

    /* Make sure data gets sent immediately. */
    ( void )tcp_output( pxPCBClient );

because the packet has most probably already been sent when FreeRTOS_send() returns. That depends on the task priorities, and on how busy the IP-stack is.

If you have further questions you can ask them in this thread. Please also check these pages.

1 Like

Thank you.
I did the porting myself with FreeRTOS+TCP stack and it worked!!

1 Like

That is very good, congratulations!

1 Like