cancel
Showing results for 
Search instead for 
Did you mean: 

Need to know how to use packet buffers to send response packets, once the response packet is built.

VSomasekhar
Associate II

This is regarding the issue I am currently handling. I am using STM3240G evaluation board to run a Modbus server having a TCP Raw socket-based echo server interface which receives Modbus request packets, sent from ModScan32 tool.                                                                                                                                                                The request packet is received without any errors on port 502, by the Modbus server running on the evaluation board. I am building a response Modbus packet as an array of unsigned char type, and type casting it to (void *) and assigning it to the payload member of a pbuf structure.                                                                                                  I am assigning this pbuf structure pointer to the pbuf structure pointer member of the tcp echo server(es) structure. But the Modbus response packet is not received at the ModScan32 tool. My main doubt is regarding the values initialized in the pbuf structure.               

My questions are:

What are the valid values for the pbuf structure members when TCP Raw socket interface is used to send a response message?

When I return the same received pbuf, the response packets are received in the ModScan32 tool. But when I change the payload in this pbuf, the received packet data is not changing - why? I am using the tcp echo server but want to return response data in the response packet, which is not happening. This is the exact problem I want a resolution for.

In other words, I just want to send a Modbus response packet from the evaluation board to the ModScan32 tool. The request Modbus packet is received in the Board, and I can display the structure fields in the IDE. Once this is done, I'll adapt this method to my application. But the payload field in the pbuf structure is not getting updated, even if I assign the send_buffer to the payload field. If sample code is available, I can understand the root cause better and be able to resolve this issue myself. 

It is also possible that the send_buffer is built incorrectly, without taking the byte orientation into consideration. I have set endian-ness to BIG_ENDIAN. 

Since the response is for a specific request, tcp echo server structure (es) has to be the same for both request and response. If I can send a Modbus packet from the Board to the ModScan32 tool, I can adapt this to my task. Please help in this regard. If any more details are required, I'll provide. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
STea
ST Employee

Hello @VSomasekhar ,

It seems that you are having problems with the transmission buffer management I will recommend you start by having a look in the example in STM32Cube_FW_F4_V1.28.0\Projects\STM324xG_EVAL\Applications\LwIP\LwIP_TCP_Echo_Client which can be obtained from Github or from the ST website.
this description of pbuf_alloc() function should help you answer your question:
* Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
*
* The actual memory allocated for the pbuf is determined by the
* layer at which the pbuf is allocated and the requested size
* (from the size parameter).
*
* @param layer header size
* @param length size of the pbuf's payload
* @param type this parameter decides how and where the pbuf
* should be allocated as follows:
*
* - PBUF_RAM: buffer memory for pbuf is allocated as one large
* chunk. This includes protocol headers as well.
* - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
* protocol headers. Additional headers must be prepended
* by allocating another pbuf and chain in to the front of
* the ROM pbuf. It is assumed that the memory used is really
* similar to ROM in that it is immutable and will not be
* changed. Memory which is dynamic should generally not
* be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
* - PBUF_REF: no buffer memory is allocated for the pbuf, even for
* protocol headers. It is assumed that the pbuf is only
* being used in a single thread. If the pbuf gets queued,
* then pbuf_take should be called to copy the buffer.
* - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
* the pbuf pool that is allocated during pbuf_init().
*
* @return the allocated pbuf. If multiple pbufs where allocated, this
* is the first pbuf of a pbuf chain.
example usage:

/* allocate pbuf */
es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL);
/* copy data to pbuf */
pbuf_take(es->p_tx, (char*)data, strlen((char*)data));



Regards


In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
STea
ST Employee

Hello @VSomasekhar ,

It seems that you are having problems with the transmission buffer management I will recommend you start by having a look in the example in STM32Cube_FW_F4_V1.28.0\Projects\STM324xG_EVAL\Applications\LwIP\LwIP_TCP_Echo_Client which can be obtained from Github or from the ST website.
this description of pbuf_alloc() function should help you answer your question:
* Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
*
* The actual memory allocated for the pbuf is determined by the
* layer at which the pbuf is allocated and the requested size
* (from the size parameter).
*
* @param layer header size
* @param length size of the pbuf's payload
* @param type this parameter decides how and where the pbuf
* should be allocated as follows:
*
* - PBUF_RAM: buffer memory for pbuf is allocated as one large
* chunk. This includes protocol headers as well.
* - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
* protocol headers. Additional headers must be prepended
* by allocating another pbuf and chain in to the front of
* the ROM pbuf. It is assumed that the memory used is really
* similar to ROM in that it is immutable and will not be
* changed. Memory which is dynamic should generally not
* be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
* - PBUF_REF: no buffer memory is allocated for the pbuf, even for
* protocol headers. It is assumed that the pbuf is only
* being used in a single thread. If the pbuf gets queued,
* then pbuf_take should be called to copy the buffer.
* - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
* the pbuf pool that is allocated during pbuf_init().
*
* @return the allocated pbuf. If multiple pbufs where allocated, this
* is the first pbuf of a pbuf chain.
example usage:

/* allocate pbuf */
es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL);
/* copy data to pbuf */
pbuf_take(es->p_tx, (char*)data, strlen((char*)data));



Regards


In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
STea
ST Employee

Hello @VSomasekhar ,

Any update on this question?
Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.