A method for sending and receiving automotive CAN messages
By using the structure array buffer and buffer management module in the automotive CAN message sending and receiving buffering method, effective management of messages and adjustment of sending cycles is realized, equipment lag caused by differences in received messages and sending messages is solved, and equipment stability and usage range are improved.
Patent Information
- Application Number
- CN202310067615.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-01-30
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2043-01-30
AI Technical Summary
In the prior art, when the rate difference between the receiving and sending messages is too large and there are requirements for the periodicity of sending messages, it is easy to cause equipment lag and program lag.
Using a method of sending and receiving and buffering for automotive CAN messages, the structure array buffer and buffer management module is used to manage the packet ID, message content, message times and message cycle. When receiving a message, this method determines whether the message ID is a new message or an old message by searching the index, and updates the message cycle and sending time count values, and sends the message according to the sending cycle.
This method can effectively reduce the load of the receiving device and reduce the performance requirements of the device. It is suitable for scenarios with different message sending cycles, avoiding device lag and program lag.
Smart Images

Figure CN116074153B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of vehicle communication, and in particular relates to a method for sending and receiving buffer of automobile CAN messages. Background Art
[0002] As the Internet of Things is gaining more and more attention, mobile phones, WeChat applets and other handheld devices are required to be able to interpret CAN message content. However, with the increase in automotive electronic components and the improvement in intelligence, the load rate of the CAN bus system is getting higher and higher, and the CAN baud rate is getting faster and faster. Due to the uneven performance of handheld devices, a large number of messages need to be sent and received in a short time during the transmission process, which will cause device jams and program jams.
[0003] Chinese invention patent: A method and system for receiving CAN messages at a high load rate (application number: CN202010779668.6) introduces a method for high-load reception of CAN messages. By filtering and screening CAN messages, CAN messages are stored in a ring queue. This method can solve a small amount of message buffering, but when the rate of receiving messages and sending messages differs too much and there is a requirement for the periodicity of sending messages, the ring queue is difficult to solve. Summary of the invention
[0004] The technical problem to be solved by the present invention is to overcome the shortcomings of the prior art and provide a method for sending and receiving automobile CAN messages, which is used to solve the technical problem in the prior art that when the rate of receiving messages and sending messages differs too much and there is a requirement for the periodicity of sending messages, it is easy to cause device jamming and program freezing.
[0005] The technical solution of the present invention to solve the above technical problem is as follows: a method for sending and receiving buffer of automobile CAN messages, comprising:
[0006] A buffer, wherein the buffer is a structure array, and the structure array includes a message ID, message content, message number and message cycle;
[0007] A buffer management module, the buffer management module is used to manage the structure array and update the structure array content;
[0008] When the CAN receives an interrupt service function, the received message enters the buffer management module;
[0009] Step (1): In the buffer management module, firstly, the structure array is searched by searching the index to determine whether the message ID is a new message ID. If it is a new message ID, the process proceeds to step (2); otherwise, the process proceeds to step (3);
[0010] Step (2): If it is determined that the message ID is a new message and the buffer is not full, proceed to step (4) to update the message period;
[0011] Step (3): If it is determined that the message ID is an old message that has been stored before, proceed to step (4) to update the message cycle;
[0012] Step (4): In the 1S timer, the difference between the number of messages received in the previous second and the number of messages received at the current time is calculated to calculate the message period and update it into the data buffer;
[0013] Step (5): setting the sending time count value in the counter;
[0014] Step (6): Send the message according to the sending cycle.
[0015] The array elements in the structure array are addressed by indexes, and the buffer structure array also includes two indexes: a storage index and a search index;
[0016] Step (101): Start from 0 by searching the index. If the search index and the stored index are consistent, it is determined to be a new message and proceed to step (102). Otherwise, proceed to step (103).
[0017] Step (102): read the array element marked by the fetch index in the receiving array, and update the stored index;
[0018] Step (103): Read the array element marked by the retrieved index in the receiving array and store the index unchanged.
[0019] When the step (2) identifies a new message:
[0020] When storing a new message ID, determine whether the index value is equal to the array length. If the index value is less than the array length, increase the stored index by one, update the message ID and message content, and increase the number of messages received by one. If the index value is equal to the array length, set the stored index to zero.
[0021] When the step (3) identifies a previously stored message:
[0022] The previously stored message information is searched by searching the index. If the message received by the CAN receiver is the message ID that has been stored before, the message content is updated and the message count is increased by one.
[0023] The step (5) sets the sending frequency of the message time count value in the counter to once in n milliseconds, where n is a positive integer, and performs time accumulation;
[0024] And a flag bit is set in the counter, and the flag bit is a time node.
[0025] The step (6) can send a message according to the message sending cycle, search all message cycles, and when the time accumulated value of the counter multiplied by the sending frequency coincides with any message cycle, send the message and enable the message sending interrupt. When the CAN controller sends the message successfully, the number of times the message is sent is reduced by one, and the time accumulated value exceeds the flag bit and needs to be cleared.
[0026] The beneficial effects of the present invention are:
[0027] 1. The performance requirements for the device receiving the message are relatively low, and an MCU with a smaller hardware buffer of the CAN controller can be selected to reduce the cost of use. ;
[0028] 2. The message sending cycle can be customized to be enlarged or reduced, which reduces the load on the receiving device and has a wider range of uses. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0030] Figure 1 A flow chart of a method for sending and receiving buffer of automobile CAN messages provided by the present invention;
[0031] Figure 2 A flow chart of receiving a message in a method for sending and receiving a buffer of a CAN message for an automobile provided by the present invention;
[0032] Figure 3 The present invention provides a flow chart of sending messages in a method for sending and receiving buffers of automobile CAN messages. DETAILED DESCRIPTION
[0033] The following embodiments of the technical solution of the present invention are described in detail in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and are therefore only used as examples, and cannot be used to limit the protection scope of the present invention.
[0034] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in this application should have the common meanings understood by those skilled in the art to which the present invention belongs.
[0035] Example
[0036] like Figure 1 , Figure 2 and Figure 3As shown, a method for sending and receiving automobile CAN messages provided by the present invention includes:
[0037] A buffer, wherein the buffer is a structure array, and the structure array includes a message ID, message content, message number and message cycle;
[0038] A buffer management module, the buffer management module is used to manage the structure array and update the structure array content;
[0039] When the CAN receives an interrupt service function, the received message enters the buffer management module;
[0040] Step (1): In the buffer management module, firstly, the structure array is searched by searching the index to determine whether the message ID is a new message ID. If it is a new message ID, the process proceeds to step (2); otherwise, the process proceeds to step (3);
[0041] Step (2): If it is determined that the message ID is a new message and the buffer is not full, proceed to step (4) to update the message period;
[0042] Step (3): If it is determined that the message ID is an old message that has been stored before, proceed to step (4) to update the message cycle;
[0043] Step (4): In the 1S timer, the difference between the number of messages received in the previous second and the number of messages received at the current time is calculated to calculate the message period and update it into the data buffer;
[0044] Step (5): setting the sending time count value in the counter;
[0045] Step (6): Send the message according to the sending cycle.
[0046] The array elements in the structure array are addressed by indexes, and the buffer structure array also includes two indexes: a storage index and a search index;
[0047] Step (101): Start from 0 by searching the index. If the search index and the stored index are consistent, it is determined to be a new message and proceed to step (102). Otherwise, proceed to step (103).
[0048] Step (102): read the array element marked by the fetch index in the receiving array, and update the stored index;
[0049] Step (103): Read the array element marked by the retrieved index in the receiving array and store the index unchanged.
[0050] When the step (2) identifies a new message:
[0051] When storing a new message ID, determine whether the index value is equal to the array length. If the index value is less than the array length, increase the stored index by one, update the message ID and message content, and increase the number of messages received by one. If the index value is equal to the array length, set the stored index to zero.
[0052] When the step (3) identifies a previously stored message:
[0053] The previously stored message information is searched by searching the index. If the message received by the CAN receiver is the message ID that has been stored before, the message content is updated and the message count is increased by one.
[0054] The step (5) sets the sending frequency of the message time count value in the counter to once in n milliseconds, where n is a positive integer, and accumulates the time;
[0055] And a flag bit is set in the counter, and the flag bit is a time node.
[0056] The step (6) can send a message according to the message sending cycle, search all message cycles, and when the time accumulated value of the counter multiplied by the sending frequency coincides with any message cycle, send the message and enable the message sending interrupt. When the CAN controller sends the message successfully, the number of times the message is sent is reduced by one, and the time accumulated value exceeds the flag bit and needs to be cleared.
[0057] When the message time count value is sent at a frequency of 10ms / time, the time is accumulated; if the time accumulation value is 15, that is, 10ms / time multiplied by 15, a time period of 150ms is obtained. When one of the message cycles is 150ms, it can be determined to be consistent and the message can be sent. In this way, the messages can be sent in the order in which they are received.
[0058] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for sending and receiving automotive CAN messages, characterized in that: include: A buffer, wherein the buffer is a structure array, and the structure array includes a message ID, message content, message number and message cycle; A buffer management module, the buffer management module is used to manage the structure array and update the structure array content; When the CAN receives an interrupt service function, the received message enters the buffer management module; Step (1): In the buffer management module, firstly, the structure array is searched by searching the index to determine whether the message ID is a new message ID. If it is a new message ID, the process proceeds to step (2); otherwise, the process proceeds to step (3); Step (2): If it is determined that the message ID is a new message and the buffer is not full, proceed to step (4) to update the message period; Step (3): If it is determined that the message ID is an old message that has been stored before, proceed to step (4) to update the message cycle; Step (4): In the 1S timer, the difference between the number of messages received in the previous second and the number of messages received at the current time is calculated to calculate the message period and update it into the data buffer; Step (5): setting the sending time count value in the counter; Step (6): Send the message according to the sending cycle; The array elements in the structure array are addressed by indexes, and the buffer structure array also includes two indexes: a storage index and a search index; Step (101): Start from 0 by searching the index. If the search index and the stored index are consistent, it is determined to be a new message and proceed to step (102). Otherwise, proceed to step (103). Step (102): read the array element marked by the fetch index in the receiving array, and update the stored index; Step (103): read the array element marked by the retrieved index in the receiving array, and store the index unchanged; When the step (2) identifies a new message: When a new message ID is stored, determine whether the index value is equal to the array length. If the index value is less than the array length, add one to the stored index, update the message ID and message content, and increase the number of received messages by one. If the index value is equal to the length of the array, the stored index is set to zero; When the step (3) identifies a previously stored message: Search the previously stored message information by searching the index. If the message received by CAN is the message ID that has been stored before, update the message content and increase the message count by one. The step (5) sets the sending frequency of the message time count value in the counter to once in n milliseconds, where n is a positive integer, and performs time accumulation; And set a flag bit in the counter, the flag bit is a time node; The step (6) can send a message according to the message sending cycle, search all message cycles, and when the time accumulated value of the counter multiplied by the sending frequency coincides with any message cycle, send the message and enable the message sending interrupt. When the CAN controller sends the message successfully, the number of times the message is sent is reduced by one, and the time accumulated value exceeds the flag bit and needs to be cleared.
Citation Information
Patent Citations
CAN message high-load-rate receiving method and system
CN112118125A
Message transceiving method and system for car CAN bus gateway
CN106534368A
Message sending method and device, electronic equipment and storage medium
CN115333675A