Modbus TCP sticky packet processing based on LWIP

Source: Internet
Author: User
Tags htons

Recently in the Modbus TCP, encountered a TCP sticky packet problem, because the client sent packets of fewer bytes and speed (10ms/times), resulting in the server side received a few packets!

There are several types of general adhesive packages:

When dealing with TCP sticky packets, many people define a frame's data structure, containing information such as identity, length, and data.

I think Modbus TCP frame structure is very good, can be widely used in power, computer room power monitoring and other fields are not unreasonable.

The following is the Modbus TCP sticky packet problem processing, directly on the code:

1 //Adu and RTU merged into a structure2 structAdu_rtu3         {  4UInt16 Tid;//The transaction identifier. The default is 0x0000. This example is used as RS485 string number: 0x0001->uart0, 0x0002->uart1, 0x0003->uart2, 0x0004->uart35UInt16 Pid;//The protocol identifier. modbus=0x0000.6UInt16 Len;//The number of subsequent bytes. High byte is in front, low byte is behind; includes unit identifier (slave address), function code, data.7Uint8 data[ the];//protocol Data frame. Actual for RTU frames.    Maximum 256 bytes, including slave address, function code, data, and CRC check code. 8         };9 Ten  One /*                                                                                                     A ****************************************************************************************************** - * * Function name: void tcp_adu_process (struct pbuf *p) * * - * * Function Description: Processing the ADU packet from TCP, including TCP sticky packet processing (only for the 1th case above) * * the * * Parameter: *p-------Incoming Data pointer * * - * * return: None * * - ****************************************************************************************************** - */ + voidTcp_adu_process (structPBUF *p) - { +Uint8 buffer[ -]; A uint16 crc_temp, Tid, Tot_len; at     structAdu_rtu *Aru; -      -ARU = p->payload; -Tot_len = p->Len; -      -     //adu data parsing. And sent to the specified serial port. in      while(((HTONS (Aru->len) +6) <= Tot_len) && (Tot_len >0) )//the Adu_rtu frame header is 6 bytes. -         { toTid = HTONS (aru->Tid); +         if((tid>0) && (tid<5) ) -             { thememcpy (buffer, Aru->data, HTONS (Aru->len));//copy the data. *          $Crc_temp = crc16_bit (buffer, HTONS (aru->Len));Panax NotoginsengBuffer[htons (Aru->len)] = (uint8) crc_temp;//fill the CRC check, low byte in front, high byte behind. -Buffer[htons (Aru->len) +1] = (uint8) (crc_temp>>8); the      +             //send data to the specified serial port. AUart_irq_sendbytes (HTONS (Aru->tid)-1, buffer, HTONS (Aru->len) +2, ENABLE); the             } +          -         //find the total length of TCP remaining, the first address of the next packet. $Tot_len-= HTONS (Aru->len) +6; $ARU = (structAdu_rtu *) ((UINT8 *) ARU + HTONS (Aru->len) +6);//convert to a single-byte pointer and then offset the address.  -         }     -}

Modbus TCP sticky packet processing based on LWIP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.