Multi-frame CAN communication method and device for PLC and IO module
By reconstructing the 11-bit CAN ID function bits, the problems of data source confusion and frame sequence number verification in the traditional CAN protocol are solved, realizing the reliability and simplicity of multi-frame transmission, which is suitable for industrial control scenarios.
Patent Information
- Application Number
- CN202511532109.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-24
- Publication Date
- 2026-02-24
AI Technical Summary
In the traditional CAN protocol, the CAN ID is only used to identify the module or master control address, which makes it impossible for the PLC to distinguish the data source of multiple IO modules. Furthermore, there is no frame sequence number identification when transmitting multiple frames, which easily leads to frame loss or frame out of order, increasing communication redundancy and complexity.
By reconstructing the 11-bit CAN ID function bits, including frame sequence number, module address, packet end bit, and master control flag bit, the source of multi-frame transmission can be traced, the sequence number can be verified, and the end can be directly determined. This adopts a hardware layer solution and does not require application layer protocol parsing.
It effectively prevents frame loss and data mixing, improves communication reliability, reduces communication redundancy, simplifies logic processes, and meets the strong real-time and high reliability requirements of industrial control.
Smart Images

Figure CN121567503A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of industrial control communication technology, and in particular to a multi-frame CAN communication method and apparatus for PLC and IO modules. Background Technology
[0002] Currently, in the traditional CAN protocol, the CAN ID is only used to identify the module or master control address. When multiple I / O modules send data to the PLC, they use the same CAN ID represented by the master control address. During PLC framing, data from multiple I / O modules may be mixed in, making it impossible for the master control to distinguish the data source. Furthermore, existing CAN bus multi-frame transmissions lack frame sequence number identification, making it impossible for the receiving end to verify frame continuity, leading to frame loss or out-of-order frame issues. When the last frame is 8 bytes, an additional 0-length end frame needs to be sent to determine the end of transmission, increasing communication redundancy and complexity. Summary of the Invention
[0003] Therefore, it is necessary to provide a multi-frame CAN communication method and apparatus for PLC and IO modules to address the above-mentioned technical problems.
[0004] Firstly, a multi-frame CAN communication method for a PLC and an I / O module is provided. The method is applied to a CAN communication system, which includes a PLC or an I / O module. The PLC is connected to the I / O module. Both the PLC and the I / O module can act as either the initiating device or the receiving device according to communication requirements. The transmitting end and the receiving end are respectively a PLC or an I / O module. The method includes: The sending end acquires the message to be sent and divides it into frames based on the total data length of the message to be sent, thus obtaining multiple frames of data; Based on the module address of the message to be sent, whether it is an end frame, the frame sequence number, and the master control flag, the sending end determines the CAN ID of each frame of the message to be sent, and continuously sends each frame of the message to be sent according to the frame sequence number via the CAN bus. The receiving end filters the CAN ID of the current frame data according to the matching requirements configured in the filter, and receives the current frame data that has been successfully filtered. The receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, it performs frame data anomaly processing. If the current frame data is correct or after the exception handling is completed, the receiving end executes the step of filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and sends the data processing instruction to the application layer, so that the application layer processes multiple frames of data.
[0005] Secondly, a multi-frame CAN communication device for a PLC and an I / O module is provided. The device is applied to a CAN communication system, which includes a PLC or an I / O module. The PLC is connected to the I / O module. Both the PLC and the I / O module can act as either the initiating device or the receiving device according to communication requirements. The transmitting end and the receiving end are respectively a PLC or an I / O module. The device includes: The acquisition module is used by the sending end to acquire the message to be sent and to divide the message into frames based on the total length of the data to be sent, thereby obtaining multiple frames of data. The determining module is used to determine the CAN ID of each frame of the message to be sent based on the module address, whether it is an end frame, the frame sequence number, and the master control flag of the message to be sent, and to continuously send each frame of the message to be sent according to the frame sequence number through the CAN bus. The filtering module is used by the receiving end to filter the CANID of the current frame data according to the matching requirements configured by the filter, and to receive the current frame data that has been successfully filtered. The determination module is used by the receiving end to determine whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, frame data anomaly processing is performed. The execution module is configured to, if the current frame data is error-free or after exception handling is completed, execute the steps of the receiving end filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and send the data processing instruction to the application layer so that the application layer processes multiple frames of data.
[0006] Thirdly, a multi-frame CAN communication system for PLC and IO modules is provided, the multi-frame CAN communication system comprising: the multi-frame CAN communication method for PLC and IO modules as described in the first aspect and the multi-frame CAN communication device for PLC and IO modules as described in the second aspect.
[0007] Fourthly, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program executable on the processor, and the processor executes the computer program to implement the steps of the method described in the first aspect.
[0008] Fifthly, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the steps of the method described in the first aspect.
[0009] This application provides a multi-frame CAN communication method for PLC and IO modules. The technical solution provided by the embodiments of this application brings at least the following beneficial effects: The module address, frame sequence number, and end frame flag are directly carried through the CAN ID, enabling frame source detection, frame continuity verification, and transmission completion determination without application layer protocols. This provides better anti-interference performance than schemes relying on protocol layer parsing. Module address isolation: The PLC and IO modules distinguish the communication direction through bit 10, eliminating the need for separate master control address settings. All message frames sent and received carry the IO module address identifier, significantly improving hardware-level reliability. Transmission completion determination only requires detecting bit 9, eliminating the need to process zero-length end frames. Source and sequence number verification are completed at the link layer, reducing application layer overhead and simplifying the logic. Module address identification and frame sequence number verification effectively prevent frame loss and data mixing, and effectively prevent the entire CAN bus communication from stopping due to a fault in a single IO module.
[0010] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and do not limit this application. Attached Figure Description
[0011] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0012] Figure 1 A schematic diagram of the structure of a multi-frame CAN communication system for a PLC and IO modules provided in this application embodiment; Figure 2 A schematic diagram of a multi-frame data transmission layered architecture and frame mapping provided in an embodiment of this application; Figure 3 A flowchart illustrating a multi-frame CAN communication method for a PLC and I / O module provided in this application embodiment; Figure 4 A flowchart illustrating an example of a sending process provided in this application embodiment; Figure 5 A schematic diagram of an 11-bit CAN ID function provided in an embodiment of this application; Figure 6 A flowchart illustrating an example of a receiving process provided in this application embodiment; Figure 7 A schematic diagram of the structure of a multi-frame CAN communication device for a PLC and IO module provided in an embodiment of this application; Figure 8This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation
[0013] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0014] The multi-frame CAN communication method provided in this application can be applied to multi-frame CAN communication systems for PLCs and I / O modules. For example... Figure 1 As shown, the multi-frame CAN communication system of the PLC and IO module includes a CAN bus master transceiver 101 and a CAN bus slave transceiver 102, which are connected via a CAN bus. Both the CAN bus master transceiver 101 and the CAN bus slave transceiver 102 are physical layer devices. Figure 2 As shown, the physical layer consists of the CAN interface and transceiver. The data in the link layer is in the complete CAN standard frame format, including the Start of Frame (SOF), Arbitration Segment (containing the CAN ID), Control Segment (containing the frame data length), Data Segment (containing the valid data of the frame), CRC Segment, ACK Segment, and End of Frame (EOF). Each CAN data frame's data segment carries one frame data block, and all frame data blocks are transmitted sequentially from 0 to N according to their frame sequence numbers, for a total of N+1 frames. After receiving and verifying all frames, the receiving end submits the reassembled complete data as application layer data for application layer processing. The CAN bus master transceiver 101 can be a PLC or multiple I / O modules, and the CAN bus slave transceiver 102 can be multiple I / O modules or a PLC. Both the PLC and I / O modules can act as communication initiators or receivers depending on the communication requirements.
[0015] The process of CAN communication between the CAN bus master transceiver 101 and the CAN bus slave transceiver 102 is as follows: The transmitting end acquires the message to be sent and divides it into frames based on the total length of the message data, resulting in multiple frames. Based on the module address of the message, whether it is the end frame, the frame sequence number, and the master control flag, the transmitting end determines the CAN ID of each frame of the message and continuously transmits each frame of the message according to its frame sequence number via the CAN bus.
[0016] The receiving end filters the CAN ID of the current frame data according to the matching requirements configured in the filter, and receives the successfully filtered current frame data. The receiving end determines whether the current frame data is erroneous based on its CAN ID. If erroneous, it performs frame data error handling. If the current frame data is error-free or after error handling is completed, the receiving end continues the process of filtering the current frame data according to the CAN ID configured in the filter and receiving the successfully filtered current frame data until the end frame is received. Finally, it sends a data processing instruction to the application layer, enabling the application layer to process multiple frames of data.
[0017] The following will describe in detail, with reference to specific implementation methods, a multi-frame CAN communication method for PLC and IO modules provided in this application embodiment. Figure 3 A flowchart of the multi-frame CAN communication method for PLC and IO modules provided in the embodiments of this application is shown below. Figure 3 As shown, the specific steps are as follows: Step 301: The sending end obtains the message to be sent and divides it into frames based on the total length of the message to be sent, thus obtaining multiple frames of data.
[0018] In implementation, in the traditional CAN protocol, the CAN ID is only used to identify the module or master control address. When multiple I / O modules send data to the PLC, they use the same CAN ID represented by the master control address. When the PLC frames the data, data from multiple I / O modules may be mixed in, causing the PLC to be unable to distinguish the data source. Simultaneously, in existing CAN bus multi-frame transmissions, there is no frame sequence number identifier, and the receiving end cannot verify frame continuity, leading to frame loss or out-of-order frame problems. When the last frame is 8 bytes, an additional 0-length end frame needs to be sent to determine the end of transmission, which increases communication redundancy and complexity. Therefore, this application proposes an optimized CAN ID scheme. By reconstructing the 11-bit CAN ID function bits, it achieves traceable source, verifiable sequence number, and direct end determination for multi-frame transmission, solving the frame mixing, frame loss, and redundancy problems of traditional schemes. It is also compatible with CAN FD extensions, adapting to the high real-time and high reliability requirements of industrial control. Therefore, when the sending end wants to send information, it needs to first obtain the message to be sent, and then frame the message into multiple frames.
[0019] Specifically, the steps for performing frame division based on the total length of the data to be sent to obtain multiple frames of data are as follows: divide the total data length by the maximum data length of each frame. If there is a remainder after division, the number of frames is the quotient plus one; if there is no remainder, the number of frames is the quotient, thus obtaining the number of frames of data.
[0020] In implementation, the framing rule for dividing the message to be sent is to divide the data of the message to be sent into frames according to the maximum data length of each frame. The maximum data length of each frame can be 8 bytes. The total data length is divided by 8. If there is a remainder after division, the number of frames is the quotient plus one; otherwise, the number of frames is the quotient, resulting in the number of frames. For example, if the total length of the message to be sent is 100 bytes, the quotient of 100 divided by 8 is 12 with a remainder of 4, then the number of frames is 13 (12+1). Therefore, the message to be sent can be divided into 13 frames, where the first 12 frames have 8 bytes of valid data, and the last frame has 4 bytes of valid data.
[0021] Step 302: Based on the module address of the message to be sent, whether it is an end frame, the frame sequence number, and the master control flag, the sending end determines the CAN ID of each frame of the message to be sent, and continuously sends each frame of the message to be sent through the CAN bus according to the frame sequence number.
[0022] In implementation, this application redefines the 11-bit CAN ID function bits and addresses the pain points of multi-frame transmission at the hardware ID layer. The 11-bit CAN ID function bits can be divided into frame sequence number identifier bits, module address bits, packet end bits, and master control flag bits. The frame sequence number identifier bit is used to identify the sequence number of the frame data and verify frame continuity; the module address bit is used to indicate the data source; the packet end bit is used to determine whether the frame data is the end frame; and the master control flag bit indicates the transmission direction from the PLC to the IO module or from the IO module to the PLC. Therefore, the transmitting end can determine the CAN ID of each frame of the message to be transmitted based on the module address, whether it is the end frame, the frame sequence number, and the master control flag. After each frame is transmitted, the transmitting end waits for the CAN bus to be idle (determined by the CAN controller status register) to avoid bus conflicts and ensure reasonable arbitration priority. The transmitting end continuously transmits each frame of the message to be transmitted via the CAN bus according to the frame sequence number. This ensures that the receiving end can promptly detect reception anomalies when receiving each frame, preventing them from flowing into the upper-level application layer for parsing and processing, thus reducing processing efficiency.
[0023] like Figure 4 As shown, after starting, the transmitting end calculates the number of frames: total length / 8. If the remainder is not 0, the number of frames is incremented by 1. Then, it generates a CAN ID for each frame: IO module address, frame sequence number, C / L bit (end of packet bit), and master control flag bit. The transmitting end sends the current frame data and checks if the bus is idle. If idle, it checks if it is the last frame. If so, the transmission is complete. If not, it continues generating the CAN ID for each frame: IO module address, frame sequence number, C / L bit (end of packet bit), and master control flag bit, until the last frame is sent. In this way, the transmitting end completes the entire process of sending the message to be sent.
[0024] Specifically, the process of executing step 302 is as follows: Step 1: For any frame, starting from the first frame, the transmitting end determines the frame sequence number according to the sorting order, and determines the frame sequence number of the CAN ID based on the frame sequence number of the current frame data. When the frame sequence number reaches the sequence number threshold, the order is reordered. The frame sequence number of the CAN ID is the value represented by bits 2 to 0 in the CAN ID.
[0025] In implementation, after dividing the message to be sent into multiple frames, starting from the first frame, the frame number of each frame is determined according to the sorting order. Based on the current frame's frame number, the CAN ID's frame number is determined. Sequential numbering ensures continuity, allowing subsequent verification of frame data correctness. Furthermore, the frame numbers are cyclically ordered, reordering when a threshold is reached. The frame numbers start from 0, the threshold is 7, and the sequence number cycles from 0→1→…→7→0. The CAN ID's frame number is represented by bits 2 to 0 of the CAN ID.
[0026] Step 2: Determine the module address of the CAN ID based on the module address of the transmitting end. The module address is the value represented by bits 8 to 3 in the CAN ID.
[0027] In implementation, the module address of the CAN ID is determined based on the module address of the transmitting end. When the transmitting end is an I / O module, the module address indicates the source of the frame data; when the receiving end is an I / O module, the module address indicates the module address of the receiving I / O module. This allows for verification based on the module address when receiving frame data; frames with different addresses are considered erroneous. The module address is represented by bits 8 to 3 of the CAN ID.
[0028] Step 3: For any frame, if the current frame is a continuous frame, set the packet end bit of the CAN ID to 0; if the current frame data is the end frame, set the packet end bit to 1; the packet end bit is bit 9 of the CAN ID.
[0029] In implementation, the message to be sent is divided into multiple frames, and the frame numbers are sequentially ordered. These frames contain consecutive frames and a final frame, with the final frame indicating that the message has been completely sent. Therefore, it is necessary to distinguish between consecutive and final frames for the receiving end to determine whether the transmitted data has been received completely. If received completely, a "completion event" is issued, and the buffered data is submitted to the application layer. The packet end bit of the CAN ID can be used to determine whether a frame is consecutive or final. If the current frame is consecutive, the packet end bit of the CAN ID is set to 0; if it is final, it is set to 1. The packet end bit is bit 9 (C / L flag) of the CAN ID. In this way, the last frame can be identified by the C / L flag (bit 9=1), eliminating the need to send an additional 0-length final frame.
[0030] Step 4: If the sender of the message to be sent is a PLC and the receiver is an IO module, then the master control flag of the CAN ID is set to 0. If the sender is an IO module and the receiver is a PLC, then the master control flag of the CAN ID is set to 1. The master control flag is bit 10 of the CAN ID.
[0031] In implementation, data transmission between the PLC and the I / O module can be achieved via CAN bus communication. In this application, the transmitting end can be either a PLC or an I / O module; similarly, the receiving end can also be either a PLC or an I / O module. Therefore, a master control flag is present in the CAN ID. If the transmitting end of the message to be sent is a PLC and the receiving end is an I / O module, the master control flag of the CAN ID is set to 0; if the transmitting end is an I / O module and the receiving end is a PLC, the master control flag of the CAN ID is set to 1. The master control flag is bit 10 of the CAN ID.
[0032] Furthermore, the CAN ID11 bit allocation function can be adjusted, for example, the functions of bit 10 and bit 9 can be swapped (the end bit and data transmission direction bit can be swapped); bits 0-3 are used for frame sequence numbers (supporting a 0-15 cycle), the receiver's verification logic needs to be adjusted to judge the continuity of 0-15, and the IO module address is compressed to bits 4-8 (supporting 32 modules), without changing the verification method of the end bit and direction bit. Adjustments can be made according to the actual situation, and no restrictions are imposed here.
[0033] Table 1 shows the 11 function bits of the CAN ID. like Figure 5As shown, bit 10 of the 11-bit CAN ID is the master control flag bit, where 1 indicates that the frame data is sent from the IO module to the PLC, and 0 indicates that the frame is sent from the PLC to the IO module. Bit 9 (C / L bit) is the packet end bit, where 1 indicates the end of the frame, and 0 indicates consecutive frames. Bits 3-8 represent the module address (0-63), used to identify the IO module to which the data belongs to prevent confusion. Bits 0-2 represent the frame sequence number (0-7 cycle), used to verify the continuity of frames, with the frame sequence number cycling from 0-1-2…7-0.
[0034] Step 303: The receiving end filters the CAN ID of the current frame data according to the matching requirements configured in the filter, and receives the current frame data that has been successfully filtered.
[0035] In implementation, the matching requirements for the receive filter configuration differ when the receiving end is a PLC or an I / O module. When the receiving end is a PLC, the filter of the CAN controller's receive channel is configured to only allow data frames with bit 10=1 (frame data sent from the I / O module to the PLC), the receive channel CAN ID is set to 0x400, and the corresponding receive channel filter is set to 0x400. When the receiving end is an I / O module, the filter of the CAN controller is configured to only allow data frames with bit 10=0 and whose module identifier is the same as its own address, the receive channel CAN ID is set to (((uint16)mod_addr&0x3F)<<3), and the corresponding receive channel filter is set to 0x5F8. Therefore, the receiving end filters the CAN ID of the current frame data according to the matching requirements of the filter configuration and receives the successfully filtered current frame data. The receiver acquires frame data via interrupt; that is, after receiving the current frame data, it first verifies the current frame data and then pauses receiving the next frame data.
[0036] Specifically, the process of filtering the CAN IDs of the current frame data according to the matching requirements configured in the filter is as follows: Step 5: If the receiving end is a PLC, determine whether the value of the master control flag bit in the CAN ID of the current frame data is 1. If it is, the filtering is successful; the matching requirement is that the value of the master control flag bit is 1.
[0037] In implementation, when the receiving end is a PLC, only data frames with bit 10=1 (frame data sent from the IO module to the PLC) are allowed to be received. Therefore, the receiving end (PLC) checks whether the value of the master control flag bit in the CAN ID of the current frame data is 1. If it is, the filtering is successful. The matching requirement is that the value of the master control flag bit is 1, i.e., bit 10=1.
[0038] Step 6: If the receiving end is an IO module, determine whether the value of the master control flag in the CAN ID of the current frame data is 0, and whether the value of the module address field is the same as its own address. If so, the filtering is successful. The matching requirement is that the value of the master control flag is 0, and the value of the module address field is the same as its own address.
[0039] In implementation, when the receiving end is an I / O module, only data frames with bit 10=0 and the module address field value being the same as its own address are allowed to be received. Therefore, the receiving end (I / O module) determines whether the master control flag bit value in the CAN ID of the current frame data is 0 and whether the module address field value is the same as its own address. If so, the filtering is successful; the matching requirement is that the master control flag bit value is 0 and the module address field value is the same as its own address.
[0040] Step 304: The receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, frame data error processing is performed.
[0041] In practice, after receiving the current frame data, the receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, it is processed as faulty frame data. If the current frame data is correct, the receiving end continues to acquire the next frame data.
[0042] Specifically, before executing step 304, the following steps are required: Step A: Before receiving the first frame of data, the receiving end initializes the initial state of the state machine, the next expected frame number, and the expected processing module address.
[0043] In implementation, before receiving the first frame of data, the receiving end initializes the initial state of the state machine, the next expected frame sequence number, and the expected processing module address, and waits for the first frame of data. The state machine is implemented using a `can_frame_t` structure, with the initial state `status=0`, the next expected frame sequence number `next_sn`=0, and the expected processing module address `cur_id`=0.
[0044] Step B: When the first frame of data is received, if the frame number of the first frame is the same as the next expected frame number, the receiving end will change the initial state to normal reception state, increment the next expected frame number by one, and determine the value of the CAN ID module address field of the first frame data as the expected processing module address.
[0045] In implementation, upon receiving the first frame of data, it is determined whether the frame sequence number of the first frame is the same as the next expected frame sequence number. If the frame sequence number of the first frame is 0, the next expected frame sequence number is also 0, meaning the frame sequence number of the first frame and the next expected frame sequence number are the same. If the frame sequence number of the first frame and the next expected frame sequence number are the same, the receiving end modifies the initial state to normal reception state, i.e., status=1, and increments the next expected frame sequence number by one. Furthermore, the value of the CAN ID module address field of the first frame data is determined as the expected processing module address cur_id.
[0046] Specifically, the process by which the receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data is as follows: Step C: The receiving end parses the target module address bit, target frame sequence number identifier bit, and target packet end bit of the CAN ID of the current frame data.
[0047] In practice, after receiving the current frame data, the receiving end parses the target module address bit, target frame sequence number bit, and target packet end bit of the CAN ID of the current frame data. Subsequently, it determines whether the current frame data is erroneous by checking the target module address bit, target frame sequence number bit, and target packet end bit.
[0048] Step D: The receiving end performs module address verification between the target module address and the preset expected processing module address. If the target module address and the expected processing module address are inconsistent, it is determined that the current frame data is incorrect, and the normal reception state is changed to the module address inconsistency state.
[0049] In implementation, the receiving end performs module address verification between the target module address of the current frame data and the preset expected processing module address. This involves comparing the target module address of the current frame data with the preset expected processing module address. If the target module address and the expected processing module address do not match, the current frame data is determined to be erroneous, and the normal reception status is changed to a module address mismatch status. The preset expected processing module address is the module address of the first frame data. This allows the receiver to determine whether subsequent received frame data, after receiving the first frame data, is the frame data for the message to be sent. The normal reception status (status=1) is changed to the module address mismatch status (status=6).
[0050] Step E: If the target module address and the expected processing module address are the same, then check whether the frame sequence number of the current frame data and the next expected frame sequence number are the same. If they are not the same, the receiver determines that the current frame data is erroneous and changes the normal reception state to the frame sequence number error state.
[0051] In implementation, if the target module address of the received current frame data matches the expected processing module address, a frame sequence number check can be performed on the current frame data. This check verifies whether the frame sequence number of the current frame data is the same as the next expected frame sequence number. If they are different, the receiver determines that the current frame data is erroneous and changes the normal reception status to a frame sequence number error status. The normal reception status is 1, and the status is changed to 3.
[0052] Step F: If the frame sequence number is the same as the next expected frame sequence number, determine whether the current frame is the end frame based on the target packet end bit. If it is, the receiver changes the normal reception state to the reception completed state; otherwise, increment the next expected frame sequence number by one.
[0053] In implementation, if the sequence number of the current frame data is the same as the sequence number of the next expected frame, it indicates that the current frame data is continuous with the previous frame data (the first frame data), and the received information to be sent is correct. Then, it is necessary to determine whether the current frame data is the end frame. If it is the end frame, it means that the complete message to be sent has been received. If it is not the end frame, then it is necessary to continue receiving the next frame data of the message to be sent. This can be determined based on the end bit of the target packet. If it is, the receiving end changes the normal reception status (status=1) to the reception completion status (status=2); otherwise, the sequence number of the next expected frame is incremented by one.
[0054] In step G, if the length of the received data is greater than or equal to the buffer capacity, the receiving end determines that the current frame data is erroneous and changes the normal reception state to the buffer full state.
[0055] In implementation, the receiving end also needs to determine whether the buffer capacity is sufficient to receive the current frame data. This can be done by comparing the length of the received data with the buffer capacity. If the length of the received data is greater than or equal to the buffer capacity, the receiving end determines that the current frame data is erroneous and changes the normal reception status to the buffer full status. The normal reception status is 1, and the status is changed to 5 when the buffer is full.
[0056] Step H: Set the timeout counter. In the normal reception state, the timeout counter is reset after each frame is received. If the timeout counter of the receiving end overflows, the receiving end determines that the current frame data is incorrect and changes the normal reception state to the reception timeout state.
[0057] In implementation, a timeout counter needs to be set when receiving frame data. The counter is reset after each frame is received; that is, it resets after each frame is received from the normal receiving state. If the next frame is not received within the timeout period (i.e., the timeout counter overflows), the current frame data is considered erroneous, and the state machine is changed from normal receiving state to receiving timeout state. The timeout counter is set to 30-100ms, dynamically adjusted according to the bus load, with a default of 50ms. In the normal receiving state, status=1, and the counter is reset after each frame is received. If a subsequent frame is not received within the timeout period, status=4. This receiving timeout mechanism effectively prevents the entire CAN bus communication from stopping due to a failure of a certain I / O module.
[0058] Specifically, if the current frame data is incorrect, the specific process for handling frame data errors is as follows: Step a: If the state machine is in a module address mismatch state, the receiving end discards the current frame data, waits to receive new current frame data, and changes the module address mismatch state to a normal receiving state.
[0059] In implementation, when the state machine is in a state of inconsistent module addresses, the current frame data is processed for frame data anomaly. The receiving end discards the current frame data and waits for the receiving end to resend the new current frame data. It is also necessary to change the inconsistent module address state to a normal receiving state and wait for the correct frame.
[0060] Step b: If the state machine is in the frame sequence number error state, the receiving end discards the current frame data, records the error, and modifies the frame sequence number error state back to the initial state.
[0061] In implementation, when the state machine is in the frame sequence number error state, the current frame data is processed for frame data error. The receiving end discards the current frame data, records the error, modifies the frame sequence number error state to the initial state, and notifies the upper layer to process the error.
[0062] In step c, if the state machine is in a full cache state, the receiver sends a space release instruction to the application layer and changes the full cache state back to the initial state.
[0063] In implementation, when the state machine buffer is full, frame data exception handling is performed on the current frame data. The receiver stops receiving and marks the buffer as insufficient. Then, the receiver sends a space release instruction to the application layer, modifies the full buffer state to the initial state, and resumes receiving messages.
[0064] In step d, if the state machine is in the receive timeout state, the receiver sends a receive timeout instruction to the application layer and modifies the receive timeout state back to the initial state.
[0065] In implementation, when the state machine is in a receive timeout state, frame data anomaly processing is performed on the current frame data. The receiver stops receiving, sends a receive timeout instruction to the application layer, and resets the receive parameters, thus changing the receive timeout state back to the initial state.
[0066] Table 2 is the state machine transition table, as shown in Table 2: Define the receiver state machine (implemented using the can_frame_t structure): status=0 (initialization): Wait for the first frame, reset cur_id (current module address) and next_sn (next expected sequence number).
[0067] status=1 (normal reception): continuously verify the frame sequence number and module address, and update next_sn (+1 loop).
[0068] status=2 (Receive Complete): Bit 9=1 was detected, and the application layer was notified to process the complete data.
[0069] status=3-6 (abnormal status): including sequence number error, timeout, cache full, address mismatch, triggering error handling.
[0070] As shown in Figure 6, before receiving the first frame, the states are initialized: status=0, next_sn=0, and cur_id=0. Here, next_sn is the expected sequence number of the next frame to be received (cyclically from 0 to 7), used to verify frame continuity. cur_id is the address of the module currently receiving data, used to verify the data source. status is the current state of the state machine, driving subsequent processing logic (such as data storage and error marking). If the first frame is received and the sequence number matches, cur_id is set to the module address of the first frame, next_sn is incremented by 1, and status=1. Subsequent frame data is then received, and the frame sequence number and address are continuously verified. 1) If an end frame is received, notify the application layer to process the data, set status=2, and then reset the receive parameters, i.e., status=0, next_sn=0, and cur_id=0. 2) If the sequence number of a subsequent frame is different from next_sn, determine that the sequence numbers are discontinuous, discard the subsequent frame, record an exception, set status=3, and then reset the receive parameters, i.e., status=0, next_sn=0, and cur_id=0. 3) If the module address of a subsequent frame is different from cur_id, determine that the module address does not match, discard the subsequent frame, and continue waiting, set status=6, waiting for a correct frame. 4) If the length of the received frame is greater than or equal to the buffer capacity (1024 bytes), notify the application layer to release the space, status=5, and reset the receive parameters after the application layer releases the space, i.e., status=0, next_sn=0, and cur_id=0. 6) If the timeout counter overflows (30-100ms), notify the application layer that the reception has timed out, status=4, and then reset the receive parameters, i.e., status=0, next_sn=0, and cur_id=0.
[0071] Step 305: If the current frame data is correct or after the exception handling is completed, the receiving end executes the step of filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and sends the data processing instruction to the application layer so that the application layer processes multiple frames of data.
[0072] In implementation, after receiving frame data, the receiving end first checks the received current frame data and then pauses or interrupts the reception of new frame data. Only after confirming that the current frame data is error-free will it continue receiving the next frame data. If the current frame data is error-free or after exception handling is completed, the receiving end can continue to execute the steps of filtering the CAN ID of the current frame data according to the matching requirements configured in the filter, and receiving the successfully filtered current frame data. This continues until the end frame is detected, indicating that the entire message sequence transmission is complete. Instructions for processing data are then sent to the application layer, enabling the application layer to process multiple frames of data.
[0073] Specific implementation method: 1. Hardware configuration: PLC CAN controller receiving channel: Configure the CAN controller's receiving channel filter to only allow the reception of data frames with bit 10=1 (frames sent from the IO module to the main controller). Its receiving channel CAN ID is set to 0x400, and the corresponding receiving channel filter is set to 0x400. Main controller CAN controller transmitting channel: Configure the PLC CAN controller's transmitting channel CAN ID (i.e., the CAN ID of the transmitted data frame) as ((((uint16)cl&0x1)<<9) |(((uint16)mod_addr&0x3F)<<3) |((uint16)sn&0x7)), where cl indicates whether the transmitted message frame is the last frame (1 for the last frame, 0 for other frames), mod_addr indicates the address of the IO module to which it is sent, and sn indicates the frame sequence number of the transmitted message frame (0-7 cycle).
[0074] IO Module CAN Controller Receive Channel: Configure the CAN controller's receive filter to only allow the reception of data frames where bit 10=0 and the module identifier is the same as its own address. The CAN ID of the receive channel is set to (((uint16)mod_addr&0x3F)<<3), and the corresponding receive channel filter is set to 0x5F8. IO Module CAN Controller Transmit Channel: Configure the CAN controller's transmit channel CAN ID (i.e., the CAN ID of the transmitted message frame) to (0x400|(((uint16)cl&0x1)<<9) |(((uint16)mod_addr&0x3F)<<3) |((uint16)sn&0x7)), where cl indicates whether the transmitted message frame is the last frame (1 for the last frame, 0 for other frames), mod_addr indicates the IO module address that sent the message frame, and sn indicates the frame sequence number of the transmitted message frame (cyclically from 0 to 7).
[0075] 2. Bus parameters: Baud rate set to 1Mbps (commonly used in industry), sampling points 75% (compliant with ISO 11898 standard).
[0076] 3. Implementation steps: Transmitter: Calculate the number of frames based on the data length → Generate a CAN ID for each frame (including module address, frame sequence number, and C / L flag) → Send frame-by-frame data and wait for the bus to become idle. Receiver: Obtain data frames via interrupt → Parse the CAN ID (module address + frame sequence number + C / L flag) and verify frame continuity and module address consistency → Update the state machine → Complete / handle exceptions.
[0077] Application scenario example: In oil and gas field pipeline control, the PLC needs to receive sensor data from 20 IO modules in real time (128 bytes / frame per module). After adopting this solution, the frame mixing rate is reduced from 3% in the traditional solution to 0, and the transmission delay is reduced by 20%.
[0078] In existing technologies, the solutions used in current industrial control PLC systems for multi-frame transmission via CAN bus have significant problems: 1. CAN ID only identifies the I / O module or master control address. When multiple modules send data to the master control, all I / O modules use the same master control address, making it impossible for the receiving end to distinguish the data source. 2. Frames lack sequence numbers, making it impossible to verify frame continuity, which can easily lead to problems such as data from other I / O modules being mixed in during frame assembly and frame loss. 3. The receiving end needs to receive the length field via CAN (e.g., frames less than 8 bytes or 0-byte frames). When the last frame is 8 bytes, an additional 0-byte end frame needs to be sent, increasing communication redundancy and logical complexity. For example, existing solutions: 1. Rely on "message header + data block" encapsulation (e.g., 15 FF XX XX YY YY) to distinguish frame types, adding 6 bytes of message header overhead and reducing bus bandwidth utilization; 2. Low efficiency in data frame error detection: information such as frame source and sequence number depends on protocol layer parsing. If the message header is tampered with (e.g., malicious injection of error packet identifiers), errors can easily occur.
[0079] This application provides a multi-frame CAN communication method for PLC and IO modules. By reconstructing the 11-bit CAN ID function bits, it achieves traceability of the source of multi-frame transmission, verifiable sequence numbers, and direct determination of the end of transmission, solving the problems of frame mixing, frame loss, and redundancy in traditional solutions. It is also compatible with CAN FD extensions, adapting to the high real-time and high reliability requirements of industrial control. It eliminates protocol layer encapsulation, abandoning upper-layer message encapsulation, and directly controls multi-frame transmission and reception through the CAN ID field, reducing bus overhead and improving transmission efficiency. It has higher adaptability to industrial control scenarios. Combined with a state machine and timeout mechanism, it quickly detects and handles anomalies such as address conflicts, frame loss, and timeouts, ensuring the real-time performance of industrial control. It is suitable for PLC-IO, industrial control equipment, and other applications requiring "strong traceability, low latency, and high reliability." Compatibility extension: The 11-bit ID design can be directly extended to CAN FD (32-bit ID), only requiring the retention of function bit definitions, adapting to higher bandwidth requirements.
[0080] It should be understood that, although Figures 3 to 6 The steps in the flowchart are shown sequentially as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order in which these steps are executed, and they can be performed in other orders. Figures 3 to 6At least some of the steps in the process may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but may be executed at different times. The execution order of these steps or stages is not necessarily sequential, but may be executed in turn or alternately with other steps or at least some of the steps or stages in other steps.
[0081] It is understood that the same / similar parts between the various embodiments of the methods described above in this specification can be referred to each other. Each embodiment focuses on the differences from other embodiments, and relevant parts can be referred to the description of other method embodiments.
[0082] This application also provides a multi-frame CAN communication device for PLC and IO modules, such as... Figure 7 As shown, the device includes: The acquisition module 701 is used by the sending end to acquire the message to be sent and to divide the message into frames based on the total length of the data to be sent to obtain multiple frames of data. The determining module 702 is used to determine the CAN ID of each frame of the message to be sent based on the module address, whether it is an end frame, the frame sequence number and the master control flag of the message to be sent, and to continuously send each frame of the message to be sent according to the frame sequence number through the CAN bus. The filtering module 703 is used by the receiving end to filter the CANID of the current frame data according to the matching requirements configured by the filter, and to receive the successfully filtered current frame data. The determination module 704 is used by the receiving end to determine whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, frame data anomaly processing is performed. The execution module 705 is configured to, if the current frame data is correct or after the exception handling is completed, execute the step of the receiving end filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and send the data processing instruction to the application layer so that the application layer processes multiple frames of data.
[0083] As an optional implementation, the acquisition module 701 is specifically used for: Divide the total length of the data by the maximum length of each frame. If there is a remainder after division, the number of frames is the quotient plus one; if there is no remainder, the number of frames is the quotient, thus obtaining the number of frames of data.
[0084] As an optional implementation, the determining module 702 is specifically used for: For any given frame, starting from the first frame, the transmitting end determines the frame sequence number according to the sorting order, and determines the frame sequence number of the CAN ID based on the frame sequence number of the current frame data. When the frame sequence number reaches the sequence number threshold, the order is reordered. The frame sequence number of the CAN ID is the value represented by bits 2 to 0 in the CAN ID. Based on the module address of the transmitting end, the module address of the CAN ID is determined, wherein the module address bits are the values represented by bits 8 to 3 in the CAN ID; For any frame, if the current frame is a continuous frame, the packet end bit of the CAN ID is set to 0; if the current frame data is the end frame, the packet end bit is set to 1; the packet end bit is bit 9. If the sending end of the message to be sent is a PLC and the receiving end is an IO module, then the master control flag of the CAN ID is set to 0; if the sending end is an IO module and the receiving end is a PLC, then the master control flag of the CAN ID is set to 1; the master control flag is bit 10 of the CAN ID.
[0085] As an optional implementation, the screening module 703 is specifically used for: If the receiving end is a PLC, then determine whether the value of the master control flag bit in the CAN ID of the current frame data is 1. If it is, the filtering is successful; the matching requirement is that the value of the master control flag bit is 1. If the receiving end is an IO module, it is determined whether the value of the master control flag in the CAN ID of the current frame data is 0, and whether the value of the module address field is the same as the address value of the receiving end itself. If so, the filtering is successful. The matching requirement is that the value of the master control flag is 0, and the value of the module address field is the same as the address value of the receiving end itself.
[0086] As an optional implementation, the device further includes: An initialization module is used to initialize the initial state of the state machine, the next expected frame number, and the expected processing module address of the receiving end before receiving the first frame of data. The modification module is used to, upon receiving the first frame of data, if the frame sequence number of the first frame is the same as the next expected frame sequence number, modify the initial state to a normal receiving state, increment the next expected frame sequence number by one, and determine the value of the CAN ID module address field of the first frame of data as the expected processing module address.
[0087] As an optional implementation, the determination module 704 is specifically used for: The receiving end parses the target module address bit, target frame sequence number identifier bit, and target packet end bit of the CAN ID of the current frame data; The receiving end performs module address verification between the target module address and the preset expected processing module address. If the target module address and the expected processing module address are inconsistent, it is determined that the current frame data is incorrect, and the normal reception state is changed to the module address inconsistency state. If the target module address and the expected processing module address are the same, then it is detected whether the frame sequence number of the current frame data and the next expected frame sequence number are the same. If they are not the same, the receiving end determines that the current frame data is erroneous and changes the normal receiving state to the frame sequence number error state. If the frame sequence number is the same as the next expected frame sequence number, then the current frame is determined to be the end frame based on the target packet end bit. If it is, the receiving end changes the normal receiving state to the receiving completed state. Otherwise, the next expected frame sequence number is incremented by one. If the length of the received data is greater than or equal to the buffer capacity, the receiving end determines that the current frame data is erroneous and changes the normal receiving state to the buffer full state. Set a timeout counter. The timeout counter is reset after each frame is received in the normal reception state. If the timeout counter of the receiving end overflows, the receiving end determines that the current frame data is incorrect and changes the normal reception state to the reception timeout state.
[0088] As an optional implementation, the determination module 704 is specifically used for: If the state machine is in a module address mismatch state, the receiving end discards the current frame data, waits to receive new current frame data, and modifies the module address mismatch state to a normal receiving state. If the state machine is in the frame sequence number error state, the receiving end discards the current frame data, records the error, and modifies the frame sequence number error state back to the initial state. If the state machine is in a full cache state, the receiving end sends a space release instruction to the application layer and modifies the full cache state to the initial state; If the state machine is in a receive timeout state, the receiving end sends a receive timeout instruction to the application layer and modifies the receive timeout state back to the initial state.
[0089] This application provides a multi-frame CAN communication device for PLC and IO modules. By reconstructing the 11-bit CAN ID function bits, it achieves traceable source, verifiable sequence number, and direct termination determination for multi-frame transmission, solving the problems of frame mixing, frame loss, and redundancy in traditional solutions. It is also compatible with CAN FD extensions, adapting to the high real-time and high reliability requirements of industrial control. It features protocol-layer-free transmission, abandoning upper-layer message encapsulation and directly controlling multi-frame transmission and reception through the CAN ID field, reducing bus overhead and improving transmission efficiency. It has higher adaptability to industrial control scenarios. Combined with a state machine and timeout mechanism, it quickly detects and handles anomalies such as address conflicts, frame loss, and timeouts, ensuring the real-time performance of industrial control. It is suitable for PLC-IO, industrial control equipment, and other applications requiring "strong traceability, low latency, and high reliability." Compatibility extension: The 11-bit ID design can be directly extended to CAN FD (32-bit ID), requiring only the function bit definition to adapt to higher bandwidth requirements.
[0090] Specific limitations regarding the multi-frame CAN communication device for PLC and I / O modules can be found in the above description of the multi-frame CAN communication method, and will not be repeated here. Each module in the aforementioned multi-frame CAN communication device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in the computer device, or stored in software in the computer device's memory, so that the processor can call and execute the corresponding operations of each module.
[0091] In one embodiment, a computer device is provided, such as Figure 8 As shown, it includes a memory and a processor. The memory stores a computer program that can run on the processor. When the processor executes the computer program, it implements the multi-frame CAN communication method steps of the PLC and IO modules described above.
[0092] In one embodiment, a computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the multi-frame CAN communication method for the PLC and IO modules described above.
[0093] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0094] It should also be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for display, data used for analysis, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties.
[0095] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0096] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0097] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A multi-frame CAN communication method for PLC and IO modules, characterized in that, The method is applied to a CAN communication system, which includes a PLC or an I / O module. The PLC is connected to the I / O module. Both the PLC and the I / O module can act as either the initiating device or the receiving device according to communication requirements. The transmitting end and the receiving end are respectively a PLC or an I / O module. The method includes: The sending end acquires the message to be sent and divides it into frames based on the total data length of the message to be sent, thus obtaining multiple frames of data; Based on the module address of the message to be sent, whether it is an end frame, the frame sequence number, and the master control flag, the sending end determines the CAN ID of each frame of the message to be sent, and continuously sends each frame of the message to be sent according to the frame sequence number via the CAN bus. The receiving end filters the CAN ID of the current frame data according to the matching requirements configured in the filter, and receives the current frame data that has been successfully filtered. The receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, it performs frame data anomaly processing. If the current frame data is correct or after the exception handling is completed, the receiving end executes the step of filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and sends the data processing instruction to the application layer, so that the application layer processes multiple frames of data.
2. The method according to claim 1, characterized in that, The process of dividing the data into frames based on the total length of the message to be sent, resulting in multiple frames of data, includes: Divide the total length of the data by the maximum length of each frame. If there is a remainder after division, the number of frames is the quotient plus one; if there is no remainder, the number of frames is the quotient, thus obtaining the number of frames of data.
3. The method according to claim 1, characterized in that, The sending end determines the CAN ID of each frame of the message to be sent based on the module address, whether it is an end frame, the frame sequence number, and the master control flag, including: For any given frame, starting from the first frame, the transmitting end determines the frame sequence number according to the sorting order, and determines the frame sequence number of the CAN ID based on the frame sequence number of the current frame data. When the frame sequence number reaches the sequence number threshold, the order is reordered. The frame sequence number of the CAN ID is the value represented by bits 2 to 0 in the CAN ID. Based on the module address of the transmitting end, determine the module address of the CAN ID, where the module address is the value represented by bits 8 to 3 in the CAN ID; For any given frame, if the current frame is a continuous frame, the packet end bit of the CAN ID is set to 0; if the current frame data is the end frame, the packet end bit is set to 1; the packet end bit is bit 9 of the CAN ID. If the sending end of the message to be sent is a PLC and the receiving end is an IO module, then the master control flag of the CAN ID is set to 0; if the sending end is an IO module and the receiving end is a PLC, then the master control flag of the CAN ID is set to 1; the master control flag is bit 10 of the CAN ID.
4. The method according to claim 1, characterized in that, The receiving end filters the CAN ID of the current frame data according to the matching requirements configured in the filter, including: If the receiving end is a PLC, then determine whether the value of the master control flag bit in the CAN ID of the current frame data is 1. If it is, the filtering is successful; the matching requirement is that the value of the master control flag bit is 1. If the receiving end is an IO module, it is determined whether the value of the master control flag in the CAN ID of the current frame data is 0, and whether the value of the module address field is the same as the address value of the receiving end itself. If so, the filtering is successful. The matching requirement is that the value of the master control flag is 0, and the value of the module address field is the same as the address value of the receiving end itself.
5. The method according to claim 1, characterized in that, The method further includes: Before receiving the first frame of data, the receiving end initializes the initial state of the state machine, the next expected frame number, and the expected processing module address; Upon receiving the first frame of data, if the frame number of the first frame is the same as the next expected frame number, the receiving end will change the initial state to normal receiving state, increment the next expected frame number by one, and determine the value of the CAN ID module address field of the first frame data as the expected processing module address.
6. The method according to claim 5, characterized in that, The receiving end determines whether the current frame data is erroneous based on the CAN ID of the current frame data, including: The receiving end parses the target module address bit, target frame sequence number identifier bit, and target packet end bit of the CAN ID of the current frame data; The receiving end performs module address verification between the target module address and the preset expected processing module address. If the target module address and the expected processing module address are inconsistent, it is determined that the current frame data is incorrect, and the normal reception state is changed to the module address inconsistency state. If the target module address and the expected processing module address are the same, then it is detected whether the frame sequence number of the current frame data and the next expected frame sequence number are the same. If they are not the same, the receiving end determines that the current frame data is erroneous and changes the normal receiving state to the frame sequence number error state. If the frame sequence number is the same as the next expected frame sequence number, then the current frame is determined to be the end frame based on the target packet end bit. If it is, the receiving end changes the normal receiving state to the receiving completed state. Otherwise, the next expected frame sequence number is incremented by one. If the length of the received data is greater than or equal to the buffer capacity, the receiving end determines that the current frame data is erroneous and changes the normal receiving state to the buffer full state. Set a timeout counter. The timeout counter is reset after each frame is received in the normal reception state. If the timeout counter of the receiving end overflows, the receiving end determines that the current frame data is incorrect and changes the normal reception state to the reception timeout state.
7. The method according to claim 6, characterized in that, If the current frame data is incorrect, then frame data anomaly handling is performed, including: If the state machine is in a module address mismatch state, the receiving end discards the current frame data, waits to receive new current frame data, and modifies the module address mismatch state to a normal receiving state. If the state machine is in the frame sequence number error state, the receiving end discards the current frame data, records the error, and modifies the frame sequence number error state back to the initial state. If the state machine is in a full cache state, the receiving end sends a space release instruction to the application layer and modifies the full cache state to the initial state; If the state machine is in a receive timeout state, the receiving end sends a receive timeout instruction to the application layer and modifies the receive timeout state back to the initial state.
8. A multi-frame CAN communication device for PLC and IO modules, characterized in that, The device is applied to a CAN communication system, which includes a PLC or an I / O module. The PLC is connected to the I / O module. Both the PLC and the I / O module can act as either the initiating device or the receiving device according to communication requirements. The transmitting end and the receiving end are respectively a PLC or an I / O module. The device includes: The acquisition module is used by the sending end to acquire the message to be sent and to divide the message into frames based on the total length of the data to be sent, thereby obtaining multiple frames of data. The determining module is used to determine the CAN ID of each frame of the message to be sent based on the module address, whether it is an end frame, the frame sequence number, and the master control flag of the message to be sent, and to continuously send each frame of the message to be sent according to the frame sequence number through the CAN bus. The filtering module is used by the receiving end to filter the CAN ID of the current frame data according to the matching requirements configured by the filter, and to receive the current frame data that has been successfully filtered. The determination module is used by the receiving end to determine whether the current frame data is erroneous based on the CAN ID of the current frame data. If the current frame data is erroneous, frame data anomaly processing is performed. The execution module is configured to, if the current frame data is error-free or after exception handling is completed, execute the steps of the receiving end filtering the CAN ID of the current frame data according to the matching requirements configured by the filter, and receiving the successfully filtered current frame data, until the end frame is received, and send the data processing instruction to the application layer so that the application layer processes multiple frames of data.
9. The apparatus according to claim 8, characterized in that, The acquisition module is specifically used for: Divide the total length of the data by the maximum length of each frame. If there is a remainder after division, the number of frames is the quotient plus one; if there is no remainder, the number of frames is the quotient, thus obtaining the number of frames of data.
10. The apparatus according to claim 8, characterized in that, The determining module is specifically used for: For any given frame, starting from the first frame, the transmitting end determines the frame sequence number according to the sorting order, and determines the frame sequence number of the CAN ID based on the frame sequence number of the current frame data. When the frame sequence number reaches the sequence number threshold, the order is reordered. The frame sequence number of the CAN ID is the value represented by bits 2 to 0 in the CAN ID. Based on the module address of the transmitting end, determine the module address of the CAN ID, where the module address is the value represented by bits 8 to 3 in the CAN ID; For any given frame, if the current frame is a continuous frame, the packet end bit of the CAN ID is set to 0; if the current frame data is the end frame, the packet end bit is set to 1; the packet end bit is bit 9 of the CAN ID. If the sending end of the message to be sent is a PLC and the receiving end is an IO module, then the master control flag of the CAN ID is set to 0; if the sending end is an IO module and the receiving end is a PLC, then the master control flag of the CAN ID is set to 1; the master control flag is bit 10 of the CAN ID.