A reliable data interaction method based on CAN
By using binary tree initialization and data pointer management methods in the CAN bus, the problems of cross and congestion in the CAN bus data interaction are solved, the reliability and real-time nature of data interaction are improved, and the needs of simultaneous communication of multiple devices are met.
Patent Information
- Application Number
- CN202110596945.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-31
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2041-05-31
AI Technical Summary
The existing CAN buses are prone to crossing and congestion during data interaction, which reduces the reliability of data interaction.
By obtaining preset protocol instructions, using structure arrays and for loops to hang protocol instructions on the binary tree for initialization, and based on the received data semaphore, the data received by the CAN interrupt is parsed and cached. Then, the instructions in the binary tree execute the corresponding function, and use the data pointer to write the instruction data to the send queue until the threshold is reached and sent to the CAN bus.
It improves the reliability of data interaction, avoids crossover and congestion, ensures the real-time and efficient CAN bus, and meets the needs of simultaneous communication of multiple devices.
Smart Images

Figure CN113342545B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a reliable data interaction method based on CAN. Background Art
[0002] CAN (Controller Area Network) belongs to the category of fieldbus and is a serial communication network that effectively supports distributed control and real-time control. CAN bus has the characteristics of low cost, high speed, real-time performance and high reliability, and has become one of the national standards for fieldbuses widely used worldwide. Due to its excellent performance, the application scope of CAN bus is no longer limited to the automotive industry, but is widely used in various fields such as automatic control, building automation, and medical equipment. However, in the current data interaction process of CAN, it is easy to cross and congestion, which reduces the reliability of data interaction. Summary of the invention
[0003] The purpose of the present invention is to provide a reliable data interaction method based on CAN to improve the reliability of data interaction.
[0004] To achieve the above object, the present invention provides a reliable data interaction method based on CAN, comprising the following steps:
[0005] Obtain preset protocol instructions, and use a structure array and a for loop to hang the protocol instructions on the binary tree for initialization;
[0006] Based on the received data signal, the data received by the CAN interrupt is parsed and the instruction data is put into the cache queue;
[0007] If the instruction data in the cache queue needs to be parsed, traverse the instructions in the initialized binary tree and execute the corresponding function to release the cache queue memory;
[0008] When the instruction data needs to be sent, the instruction data is written into the sending queue using a data pointer, and when the storage capacity of the sending queue reaches a threshold, the instruction data is sent to the CAN bus.
[0009] Wherein, the method further comprises:
[0010] Detect the response frame position and determine whether a response is required. If a response is required, send the command data to the CAN bus. If a response is not required or the response is completed, continue to query whether there is a received data signal release.
[0011] Wherein, the method further comprises:
[0012] Determine whether the current position data is empty. If not, clear the current position data and traverse the binary tree again;
[0013] If it is empty, re-query whether the cache queue is empty.
[0014] The data pointer includes a write pointer and a read pointer. The write pointer is used to store the instruction data to be sent into the sending queue; the read pointer is used to read the instruction data in the sending queue when sending is required.
[0015] If a response is required, the command data is sent to the CAN bus, including:
[0016] If a response is required, the cache queue is converted into a response message after the protocol. At the same time, a timer is started. When the CAN port receives the response, the extended frame source address, destination address and frame sequence number are compared with the received address in the CAN interrupt to see if they are consistent. If they are consistent, the timer is turned off and the CAN port detection response state is exited.
[0017] The process of obtaining a preset protocol instruction and using a structure array and a for loop to hang the protocol instruction on a binary tree for initialization includes:
[0018] Obtain preset protocol instructions, and use a structure array and a for loop to hang the protocol instructions on the binary tree;
[0019] Determine the value of the instruction value that needs to be hung in the binary tree and the value of the current instruction value. If it is greater than the current instruction value, the structure array is hung on the left side of the binary tree. If it is less than the current value, the structure array is hung on the right side of the binary tree.
[0020] The instruction that needs to be hung in the binary tree is equal to the value of the current instruction, and the structure array is increased by 1 until all the data is hung.
[0021] A reliable data interaction method based on CAN of the present invention obtains preset protocol instructions, uses a structure array and a for loop to hang the protocol instructions on a binary tree for initialization; based on the received data signal quantity, parses the data received by the CAN interruption and puts the instruction data into a cache queue; if the instruction data in the cache queue needs to be parsed, traverses the instructions in the initialized binary tree, and executes the corresponding function to release the cache queue memory; when the instruction data needs to be sent, writes the instruction data into the sending queue using a data pointer, and when the storage capacity of the sending queue reaches a threshold, sends the instruction data to the CAN bus, and can define its instructions according to the communication protocol based on whether to open the response mode according to the demand, with high efficiency, high reliability, good real-time performance and high reliability. A maximum of 255 devices can communicate simultaneously, and each device has a maximum of 511 different instructions, which fully meets the CAN communication in various fields. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0023] Figure 1 It is a schematic diagram of the steps of a reliable data interaction method based on CAN provided by the present invention.
[0024] Figure 2 It is a flow chart of a reliable data interaction method based on CAN provided by the present invention. DETAILED DESCRIPTION
[0025] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present invention, and should not be construed as limiting the present invention.
[0026] In the description of the present invention, it should be understood that the terms "length", "width", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, in the description of the present invention, "multiple" means two or more, unless otherwise clearly and specifically defined.
[0027] See also Figure 1 and Figure 2 The present invention provides a reliable data interaction method based on CAN, comprising the following steps:
[0028] S101, obtaining a preset protocol instruction, and using a structure array and a for loop to hang the protocol instruction on a binary tree for initialization.
[0029] Specifically, data initialization: first initialize the defined protocol instructions, using a structure array, each structure contains its instruction and its corresponding function pointer, and then use a for loop to hang it on the binary tree. Method: Determine the value of the instruction value that needs to be hung in the binary tree and its current instruction value. If it is greater than the current instruction value, the structure array is hung on the left side of the binary tree. If it is less than the current value, the structure array is hung on the right side of the binary tree, and the instruction that needs to be hung in the binary tree is equal to the value of the current instruction, and the structure array +1, until all the data is hung.
[0030] CAN setup initialization: If it is the underlying chip driver code, reuse its corresponding pins as CAN port transmit and receive pins, define its baud rate, interrupt priority, etc.
[0031] S102: Based on the received data signal quantity, the data received by the CAN interruption is parsed and the instruction data is put into a cache queue.
[0032] Specifically, the 32-bit bytes of CAN are divided as follows (i.e., communication protocol):
[0033] Data format 1, canID = Replay + Command + ID + IDFrom + IDTo (Replay 1Bit, Command 8Bit, ID 8Bit, IDFrom 6Bit, IDTO 6Bit)
[0034] (Replay 1Bit represents whether to reply, 0 means this frame needs to be replied, 1 means this frame does not need to be replied
[0035]
[0036] A total of 29 bits, Bit = Replay (1) + Command (8) + ID (8) + IDFrom (6) + IDTO (6), are arranged from high to low.
[0037] After the CAN interrupt receives the signal, it reads the message (reads data) from the CAN mailbox and then releases the Thread1 semaphore. After Thread detects the release of the received data semaphore, it uses a structure to store the CAN signal, specifically: 0~5bit, occupying 6bit, is the destination ID; 6~11bit, occupying 6bits, is the source ID; 12~19bit, occupying 8bits, is the message frame sequence number; 20~27bit, occupying 8bits, is the message frame CMD; 28~31bit, occupying 4bits, is the message frame response requirement (the last three bits need to be set differently in some chips, and the extended frame can be turned on or off. The specific content should be modified according to the chip manual). The received extended frame and data frame are put into the cache queue for parsing. When the write is successful, the cache queue status becomes BUSY. When the write fails, the cache queue status becomes ERROR. When the queue is full, the write status becomes FULL. Then determine whether the message frame needs a response. If necessary, turn the CAN cache queue into a protocol response message and start a timer to record the waiting time for each time (such as 2S, etc., which is set according to actual needs) and the number of waits (usually 3 times, which can be changed according to specific circumstances and needs) through a cumulative method. When the CAN port receives a response, it will make a judgment in the interrupt to compare the extended frame source address, destination address and frame sequence number with the received address to see if they are consistent. If they are consistent, turn off the timer and exit the CAN port to detect the response state.
[0038] S103: If the instruction data in the cache queue needs to be parsed, traverse the instructions in the initialized binary tree and execute corresponding functions to release the cache queue memory.
[0039] Specifically, if the instruction data in the cache queue needs to be parsed, thread 2 starts from the root node of the binary tree and determines whether the parsed instruction value is consistent with the instruction value of the current node. If they are consistent, the function pointed to by the function pointer in the structure array is executed. If they are inconsistent, the size of its instruction value and the node instruction value is determined. If it is greater than the value of the node, it is determined whether the left child node of the binary tree is empty. If it is not empty, it is determined whether its instruction value is consistent with the node instruction value. If it is empty, the traversal is jumped out. (If it is less than the value of the node, it is determined whether the right child node of the binary tree is empty). Repeat the above operation until a node value consistent with it is found or its child node is empty, and then the traversal is jumped out.
[0040] S104: When the instruction data needs to be sent, the instruction data is written into a sending queue using a data pointer, and when the storage capacity of the sending queue reaches a threshold, the instruction data is sent to the CAN bus.
[0041] Specifically, when the command data needs to be sent, it is only necessary to use the data pointer to put the CAN data into the CAN sending queue, and thread 3 queries the sending queue to see if there is data to be sent, that is, the data pointer points to the sending queue. When it is detected that there is data to be sent, the data in the sending queue is sent through the CAN port. After the sending is completed, the sent data cache is cleared, and the data pointer points to the next position in the sending queue. If there is, continue to send, if not, keep querying and waiting. Among them, the data pointer: one is a write pointer, which is a pointer used to store the command data to be sent into the cache queue when the CAN signal needs to be sent, and when the data needs to be written; the other is a read pointer, which is used to read the pointer of the cache queue when it needs to be sent. The cache queue is a circular queue. If the maximum length of the queue has been reached, it points back to the head of the queue.
[0042] The cache queue and the sending queue have three states: ready state, operating state and error. Whenever data is written or read, the queue state will be judged. When the read state is an error state, an alarm will be sent to the CAN bus; if it is in the operating state, it will wait for a set time, and if it is still in the operating state, an alarm will be sent to the CAN bus; if it is in the idle state, the corresponding operation will be performed to change its state to the non-operating state.
[0043] The operation of judging whether the sending queue is full is to judge whether the storage capacity of the sending queue reaches the threshold: divide the current write pointer position by the length of the queue and take the remainder. If it is not equal to the current read pointer position, it means that the sending queue is not full, and the protocol package data (that is, the data to be sent) is copied from the source address to the destination address (queue data element, that is, our CAN protocol data packet) for CAN sending or writing queue operations; if it is equal to the read pointer position, it means that the current sending queue is full. Wait until the sending queue is not full and is in the operating state, and then perform the corresponding operation, and point the read pointer to the first address of the cache queue (the read pointer is pointed to the first address of the cache queue only after the read operation is completed, and the write operation will wait). If it is still full after waiting for the set time, an alarm will be sent to the CAN bus.
[0044] What is disclosed above is only a preferred embodiment of the present invention, and it certainly cannot be used to limit the scope of rights of the present invention. Ordinary technicians in this field can understand that all or part of the processes of the above embodiment and equivalent changes made according to the claims of the present invention still fall within the scope of the invention.
Claims
1. A reliable data interaction method based on CAN, characterized in that: The following steps are involved: Obtain preset protocol instructions, and use a structure array and a for loop to hang the protocol instructions on the binary tree for initialization; Based on the received data signal, the data received by the CAN interrupt is parsed and the instruction data is put into the cache queue; If the instruction data in the cache queue needs to be parsed, traverse the instructions in the initialized binary tree and execute the corresponding function to release the cache queue memory; When the instruction data needs to be sent, the instruction data is written into the sending queue using a data pointer, and when the storage amount of the sending queue reaches a threshold, the instruction data is sent to the CAN bus; Based on the received data signal, the data received by the CAN interrupt is parsed and the instruction data is placed in the cache queue, including: After the CAN interrupt receives the signal, it reads the message from the CAN mailbox and releases the Thread1 semaphore; after Thread detects the release of the received data semaphore, it uses a structure to store the CAN signal, specifically: 0~5bit, occupying 6 bits, is the destination ID; 6~11bit, occupying 6 bits, is the source ID; 12~19bit, occupying 8 bits, is the message frame sequence number; 20~27bit, occupying 8 bits, is the message frame CMD; 28~31bit, occupying 4 bits, is the message frame response requirement; the received extended frame and data frame are put into the cache queue for parsing. When the write is successful, the cache queue status becomes BUSY. When the write fails, the cache queue status becomes ERROR. When the queue is full, the write status becomes FULL. Determine whether the message frame needs a response. If a response is required, turn the CAN cache queue into a protocol response message, start a timer, and record the waiting time and the number of waiting times each time by accumulating them. When the CAN port receives a response, it will make a judgment in the interrupt to compare the extended frame source address, destination address and frame sequence number with the received address to see if they are consistent. If they are consistent, turn off the timer and exit the CAN port detection response state.
2. The reliable data interaction method based on CAN as claimed in claim 1, characterized in that: The method further comprises: Detect the response frame position and determine whether a response is required. If a response is required, send the command data to the CAN bus. If a response is not required or the response is completed, continue to query whether there is a received data signal release.
3. The reliable data interaction method based on CAN as claimed in claim 1, characterized in that: The method further comprises: Determine whether the current position data is empty. If not, clear the current position data and traverse the binary tree again; If it is empty, re-query whether the cache queue is empty.
4. The reliable data interaction method based on CAN as claimed in claim 1, characterized in that: The data pointer includes a write pointer and a read pointer. The write pointer is used to store the instruction data to be sent into the sending queue; the read pointer is used to read the instruction data in the sending queue when sending is required.
5. The reliable data interaction method based on CAN as claimed in claim 2, characterized in that: If a response is required, the command data is sent to the CAN bus, including: If a response is required, the cache queue is converted into a response message after the protocol. At the same time, a timer is started. When the CAN port receives the response, the extended frame source address, destination address and frame sequence number are compared with the received address in the CAN interrupt to see if they are consistent. If they are consistent, the timer is turned off and the CAN port detection response state is exited.
6. The reliable data interaction method based on CAN as claimed in claim 1, characterized in that: Obtain the preset protocol instructions, and use a structure array and a for loop to hang the protocol instructions on the binary tree for initialization, including: Obtain preset protocol instructions, and use a structure array and a for loop to hang the protocol instructions on the binary tree; Determine the value of the instruction value that needs to be hung in the binary tree and the value of the current instruction value. If it is greater than the current instruction value, the structure array is hung on the left side of the binary tree. If it is less than the current value, the structure array is hung on the right side of the binary tree. The instruction that needs to be hung in the binary tree is equal to the value of the current instruction, and the structure array is increased by 1 until all the data is hung.
Citation Information
Patent Citations
Alarm analysis merging method for power secondary system intranet security monitoring platform
CN103607291A
Fiber communication protocol used for distributed controller and realization device thereof
CN104598414A