CAN communication method, device and equipment

By strictly managing cache status and bus restart in CAN communication, the data loss problem caused by inter-frame competition is solved, and the orderliness and reliability of data transmission is achieved. It is suitable for intelligent building air conditioning systems, automotive networks and industrial automation scenarios.

CN120301840APending Publication Date: 2025-07-11GUANGDONG TCL INTELLIGENT HEATING & VENTILATING EQUIP CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510615566.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-13
Publication Date
2025-07-11

AI Technical Summary

Technical Problem

The CAN communication mechanism frequently competes between frames in continuous data transmission scenarios, resulting in the loss of some data frames.

Method used

By obtaining the software cache and hardware cache status when data is received, and data frames are stored and sent when the software cache is idle and the hardware cache is sent as a sending completion status, combined with the historical reception time, sending time and resend times, the cache status and bus restart are dynamically managed to ensure the orderliness and reliability of data transmission.

Benefits of technology

It effectively avoids low-priority frame delay or interruption caused by frequent inter-frame competition, reduces the risk of data frame loss, and improves the stability and integrity of data transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120301840A_ABST
    Figure CN120301840A_ABST
Patent Text Reader

Abstract

The invention discloses a CAN (Controller Area Network) communication method, a CAN communication device and CAN communication equipment, which are characterized in that the states of a software cache and a hardware cache are acquired when first single-frame data are received, and the first single-frame data are stored and sent only when the state of the software cache is an idle state and the state of the hardware cache is a sending completion state, so that the orderliness and reliability of data transmission are realized. According to the embodiment of the invention, in a continuous data transmission scene, the cache state is strictly checked to ensure that the new data frame is processed only when the cache resource is available, and a single-frame transmission mechanism that the new frame is transmitted after the hardware cache finishes the transmission of the second single-frame data is confirmed is adopted, so that the data transmission efficiency is improved. The problem of low-priority frame delay or interruption caused by frequent inter-frame competition can be effectively avoided, and the risk of partial data frame loss caused by competition failure is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technologies, and in particular, to a CAN communication method, apparatus, and device. Background Art

[0002] The Controller Area Network (CAN) communication mechanism can achieve real-time transmission of control instructions and status data. However, the CAN communication mechanism has a problem of frame loss caused by frame competition in actual applications. When multiple nodes attempt to send data simultaneously, the arbitration mechanism based on priority may cause low-priority frames to be delayed or interrupted. In a continuous data transmission scenario, if frame competition is frequent and fails, some data frames may be lost. Summary of the Invention

[0003] Based on this, it is necessary to provide a CAN communication method, apparatus, and device to solve the problem that in a continuous data transmission scenario of CAN communication, if frame competition is frequent and fails, some data frames may be lost.

[0004] In a first aspect, an embodiment of the present application provides a CAN communication method, and the method includes:

[0005] When receiving a first single-frame data, obtain the software cache status of the software cache and the hardware cache status of the hardware cache; wherein, the first single-frame data is a frame of data to be currently sent.

[0006] If the software cache status is an idle state and the hardware cache status is a transmission completion state, store the first single-frame data in the software cache and send the first single-frame data through the hardware cache via the CAN bus; wherein, the transmission completion state is used to indicate that the hardware cache has completed the transmission of a second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

[0007] In some embodiments of the present application, after storing the first single-frame data in the software cache, the method further includes:

[0008] If the software cache status is an occupied state, send the first single-frame data in the software cache to the hardware cache, set the software cache status to an idle state, and set the hardware cache status to an uncompleted transmission state.

[0009] After sending the first single-frame data through the hardware cache via the CAN bus, the method further includes:

[0010] If the hardware cache status is updated from the uncompleted transmission state to the transmission completion state, determine that the first single-frame data has been sent successfully.

[0011] In some embodiments of the present application, before sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to the idle state, and setting the hardware cache status to the not-sent-completed state, the method further includes:

[0012] Obtain the historical reception duration of the hardware cache; wherein, the historical reception duration is used to indicate the duration from the time when the hardware cache received the second single-frame data to the current moment;

[0013] If the historical reception duration is less than or equal to a preset duration threshold, return to execute the step of obtaining the software cache status of the software cache and the hardware cache status of the hardware cache;

[0014] If the historical reception duration is greater than the preset duration threshold, execute the steps of sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to the idle state, and setting the hardware cache status to the not-sent-completed state.

[0015] In some embodiments of the present application, after storing the first single-frame data in the software cache, the method further includes: backing up the first single-frame data in the software cache;

[0016] After sending the first single-frame data through the hardware cache via the CAN bus, the method further includes:

[0017] If the hardware cache status is the not-sent-completed state, obtain the sending duration and the retransmission times of the hardware cache; wherein, the sending duration is used to indicate the duration for the current hardware cache to send the first single-frame data via the CAN bus, and the retransmission times are used for the total number of times the hardware cache re-sends the first single-frame data via the CAN bus;

[0018] If the sending duration is greater than a preset sending duration threshold, and the retransmission times are less than or equal to a preset sending times threshold, then set the sending duration to zero, and increment the retransmission times by 1;

[0019] Store the backed-up first single-frame data in the software cache, back up the first single-frame data in the software cache, and return to execute the step of sending the first single-frame data through the hardware cache via the CAN bus.

[0020] In some embodiments of the present application, after obtaining the sending duration and the retransmission times of the hardware cache, the method further includes:

[0021] If the sending duration is greater than a preset sending duration threshold and the number of retransmissions is greater than a preset number of retransmission threshold, then set the CAN bus state of the CAN bus to the closed state, set the restart CAN flag of the CAN bus to the to-be-restarted flag, and reset the number of retransmissions to zero;

[0022] Restart the CAN bus. After the CAN bus state is updated to the start state, return to execute the step of obtaining the software cache state of the software cache and the hardware cache state of the hardware cache;

[0023] If the CAN bus state is the open state and the restart CAN flag is the to-be-restarted flag, then store the backed-up first single-frame data into the software cache, back up the first single-frame data in the software cache, and return to execute the step of sending the first single-frame data through the hardware cache via the CAN bus.

[0024] In some embodiments of the present application, after obtaining the software cache state of the software cache and the hardware cache state of the hardware cache, the method further includes:

[0025] If the software cache state is the idle state or the occupied state and the hardware cache state is the not-sent-completed state, then execute the step of obtaining the sending duration and the number of retransmissions of the hardware cache.

[0026] In some embodiments of the present application, after obtaining the software cache state of the software cache and the hardware cache state of the hardware cache, the method further includes:

[0027] If the software cache state is the occupied state and the hardware cache state is the sent-completed state, then send the single-frame data stored in the software cache through the hardware cache via the CAN bus.

[0028] In some embodiments of the present application, before receiving the single-frame data of the first frame, the method includes:

[0029] Set the software cache state to the idle state, set the hardware cache state to the sent-completed state, set the restart CAN flag to the not-to-be-restarted flag, and set the CAN bus state to the open state.

[0030] In a second aspect, an embodiment of the present application further provides a CAN communication device, where the CAN communication device includes:

[0031] A state acquisition module, configured to obtain the software cache state of the software cache and the hardware cache state of the hardware cache when receiving the first single-frame data; where the first single-frame data is a frame of data to be currently sent;

[0032] A data sending module, which is configured to store the first single-frame data into the software cache and send the first single-frame data via the CAN bus through the hardware cache if the software cache status is idle and the hardware cache status is the sending completed status; wherein, the sending completed status is used to indicate that the hardware cache has completed the sending of the second single-frame data, and the second single-frame data is the previous frame of the first single-frame data to be sent.

[0033] In a third aspect, an embodiment of the present application further provides a terminal device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the steps in the above CAN communication method are implemented.

[0034] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above CAN communication method are implemented.

[0035] In a fifth aspect, an embodiment of the present application further provides a computer program product or a computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the methods provided in various optional implementation manners of the embodiment of the present application.

[0036] The present invention provides a CAN communication method, device, and equipment. By obtaining the software cache and hardware cache status when receiving the first single-frame data, and storing and sending the first single-frame data only when the software cache status is idle and the hardware cache status is the sending completed status, the orderliness and reliability of data transmission are realized. In the scenario of continuous data sending, the present invention strictly checks the cache status, ensures that new data frames are processed only when the cache resources are available, and through the single-frame sending mechanism that confirms that the hardware cache has completed the sending of the second single-frame data before sending a new frame, it can effectively avoid the problems of low-priority frame delay or interruption caused by frequent frame competition, and reduce the risk of partial data frame loss caused by competition failure. Description of the Drawings

[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained according to these drawings.

[0038] Among them:

[0039] Figure 1 Schematic flowchart of the CAN communication method provided in the first embodiment of the present application;

[0040] Figure 2 Schematic flowchart of the CAN communication method provided in the second embodiment of the present application;

[0041] Figure 3 Schematic overall flowchart of the CAN communication method;

[0042] Figure 4 Schematic flowchart of the CAN communication method provided in the third embodiment of the present application;

[0043] Figure 5 Schematic structural diagram of the CAN communication device;

[0044] Figure 6 Structural block diagram of the terminal device. Detailed implementation manners

[0045] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0046] Terms such as "first" and "second" in the specification and claims of the present application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally further includes steps or units not listed, or optionally further includes other steps or units inherent to these processes, methods, products or devices.

[0047] Referring to "embodiment" herein means that a specific feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the present application. The phrase appears in various places in the specification and does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment mutually exclusive with other embodiments. Those skilled in the art will explicitly and implicitly understand that the embodiments described herein can be combined with other embodiments.

[0048] The CAN communication mechanism can achieve real-time transmission of control instructions and status data, and is widely used in scenarios that require high real-time performance and reliability.

[0049] For example, in the air conditioning system of a smart building, the demand for high-precision temperature control requires core components such as electric compressors and variable-frequency fans to transmit a large amount of control instructions and status data in real time, such as compressor speed, ambient temperature, power consumption, and fault codes. To ensure efficient data transmission, the CAN protocol is often selected at the initial design stage.

[0050] Or in the automotive field, CAN communication is widely used in in-vehicle networks, such as engine control units, braking systems, body control modules, etc., which need to transmit data such as throttle position, vehicle speed, tire pressure, and diagnostic information in real time to ensure the safety and performance of the vehicle.

[0051] Or, in an industrial automation scenario, such as the robot control system on a production line, the CAN protocol is used to coordinate the transmission of instructions and status data between multi-axis motion controllers and sensors to ensure high-precision and synchronous operation.

[0052] However, the CAN communication mechanism has the problem of frame loss caused by frame competition in practical applications. When multiple nodes attempt to send data simultaneously, the arbitration mechanism based on priority may cause low-priority frames to be delayed or interrupted. In a continuous data transmission scenario, if frame competition is frequent and fails, some data frames may be lost.

[0053] For the above problems, please refer to Figure 1 , Figure 1 which is a schematic flowchart of the CAN communication method provided by the first embodiment of this application. Although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order from that shown in the drawings. Specifically, the specific process of the CAN communication method provided by this first embodiment is as follows:

[0054] S101, when receiving the first single-frame data, obtain the software cache status of the software cache and the hardware cache status of the hardware cache.

[0055] Among them, the first single-frame data is the data of one frame to be sent currently. Software cache refers to the software-layer buffer in the CAN communication system for temporarily storing data to be sent, which is usually managed by the operating system, driver or application program. Software cache status refers to the current working state of the software cache, including the idle state and the occupied state. The idle state means that the software cache currently stores no CAN data and can receive new data; the occupied state means that the software cache already stores CAN data and cannot receive new data temporarily. Hardware cache refers to the hardware-layer buffer in the CAN controller for directly interacting with the CAN bus, which is responsible for loading the data frame onto the CAN bus for physical transmission. Hardware cache status refers to the current working state of the hardware cache, including the untransmitted complete state and the transmitted complete state. The untransmitted complete state means that the hardware cache is transmitting the data frame or the data frame has not been completely transmitted onto the CAN bus; the transmitted complete state means that the hardware cache has completed the transmission of the second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

[0056] In some embodiments of the present application, when the user inputs a control instruction or the system periodically queries status data, the instruction or status data will be split into multiple frames of data and sent in sequence. And the currently received single-frame data to be sent is the first single-frame data.

[0057] In some embodiments of the present application, by querying the flag bit or status register of the software cache, it is judged whether it is in the idle state or the occupied state. For example, check whether the flag bit of the software cache is "0" (corresponding to the idle state) or "1" (corresponding to the occupied state).

[0058] In some embodiments of the present application, by reading the status register of the CAN controller, it is judged whether the hardware cache is in the transmitted complete state or the untransmitted complete state. For example, read the transmitted complete flag bit of the CAN controller to confirm whether it is "0" (corresponding to the untransmitted complete state) or "1" (corresponding to the transmitted complete state).

[0059] S102, if the software cache status is the idle state and the hardware cache status is the transmitted complete state, then store the first single-frame data into the software cache and send the first single-frame data through the hardware cache via the CAN bus.

[0060] If the software cache status is the idle state and the hardware cache status is the transmitted complete state, it means that the second single-frame data has completed single-frame transmission and will not affect the transmission of the current first single-frame data. At this time, store the first single-frame data into the software cache and send the first single-frame data through the hardware cache via the CAN bus.

[0061] In some embodiments of the present application, if the software cache status is the idle state and the hardware cache status is the transmission completed state, the first single-frame data in the software cache is loaded into the hardware cache. The hardware cache formats the data frame into the standard frame structure required by the CAN protocol, and then sends the formatted first single-frame data to the CAN bus. Further, after the transmission is completed, the software cache status can be set to the idle state and the hardware cache status can be set to the transmission completed state to prepare for the transmission of the next frame of data.

[0062] To facilitate the description of the solution, the following takes the CAN communication in the air-conditioning scenario as an example for illustration:

[0063] In the air-conditioning system of an intelligent building, the central controller needs to communicate with multiple target devices (such as variable-frequency compressors, fans, temperature sensors) in real time through the CAN bus. For example, when the building management center issues an instruction to adjust the conference room temperature to 22 °C, the central controller splits the instruction into multiple frames of data (such as each frame corresponding to the temperature setting value, timestamp, device ID, etc.), and sends them to the target devices through the CAN bus. When the central controller receives the first single-frame data (such as a frame of data corresponding to the temperature setting value), it obtains the software cache status and the hardware cache status by querying the flag bit and the CAN controller register. Then, after confirming that the software cache is in the idle state and the hardware cache is in the transmission completed state, it stores the first single-frame data in the software cache, and then loads it into the hardware cache. The hardware cache formats it and sends it to the target device through the bus for adjustment (such as sending it to the compressor to adjust its frequency), and updates the cache status.

[0064] In the above embodiment, by obtaining the software cache and hardware cache status when receiving the first single-frame data, and storing and sending the first single-frame data only when the software cache status is the idle state and the hardware cache status is the transmission completed state, the orderliness and reliability of data transmission are achieved. In the scenario of continuous data transmission, by strictly checking the cache status, this embodiment ensures that new data frames are processed only when the cache resources are available, and through the single-frame transmission mechanism of confirming that the hardware cache has completed the transmission of the second single-frame data before sending a new frame, it can effectively avoid the problem of low-priority frame delay or interruption caused by frequent frame competition, and reduce the risk of partial data frame loss caused by competition failure.

[0065] Please refer to Figure 2 , Figure 2 which is the flowchart of the CAN communication method provided in the second embodiment of the present application. Although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order from that shown in the drawings. Specifically, the specific process of the CAN communication method provided in this second embodiment is as follows:

[0066] S201. Before receiving the single-frame data of the first frame, set the software cache status to the idle state, set the hardware cache status to the transmission completed state, set the restart CAN flag to the non-restart pending flag, and set the CAN bus status to the enabled state.

[0067] Among them, the restart CAN flag is a control flag used to indicate whether the CAN communication module needs to be restarted, including the non-restart pending flag and the restart pending flag. The non-restart pending flag indicates that the CAN bus is operating normally and does not need to be restarted, and its flag bit can be correspondingly represented as 0; the restart pending flag indicates that the CAN bus needs to be restarted to restore normal function, and its flag bit can be correspondingly represented as 1. The CAN bus status refers to the current working state of the CAN bus, including the enabled state and the disabled state. The disabled state indicates that the bus is not activated and data cannot be transmitted, and its flag bit can be correspondingly represented as OFF; the enabled state indicates that the CAN bus is activated and data transmission can be performed, and its flag bit can be correspondingly represented as ON.

[0068] In some embodiments of the present application, as Figure 3 shown, Figure 3 is the overall flow schematic diagram of the CAN communication method. This step corresponds to the "initialization" step therein. Before receiving the single-frame data of the first frame, the software cache can be initialized to the idle state by writing the flag bit of the software cache (such as setting the flag bit to "0"). The hardware cache is initialized to the transmission completed state by configuring the status register of the CAN controller (such as setting the transmission flag to "1"). The restart CAN flag is set to the non-restart pending flag by updating the control register of the CAN module (such as setting the restart flag to "0"). The CAN bus status is set to the enabled state by activating the CAN bus driver (such as setting the bus enable signal to "ON").

[0069] The initialization process of this step can lay a foundation for subsequent data reception and transmission, ensuring that the system is in a controllable initial state.

[0070] S202. Receive the first single-frame data.

[0071] S203. Obtain the software cache status of the software cache and the hardware cache status of the hardware cache.

[0072] S204. If the software cache status is the idle state and the hardware cache status is the transmission completed state, store the first single-frame data in the software cache.

[0073] In some embodiments of the present application, the principles of steps S202 - S204 in the second embodiment are basically the same as those of steps S101 - S102 in the first embodiment, so they will not be elaborated here.

[0074] S205. If the software cache status is in the occupied state, obtain the historical reception duration of the hardware cache. If the historical reception duration is less than or equal to a preset duration threshold, return to execute S203. If the historical reception duration is greater than the preset duration threshold, execute S206.

[0075] Among them, the historical reception duration refers to the duration from the time when the hardware cache received the previous frame of data (i.e., the second single-frame data) to the current moment. For example, if the hardware cache received the previous frame of wind speed command data 9 ms ago, the historical reception duration is 9 ms. The duration threshold is used to determine whether the hardware cache is suitable for receiving new data and is usually set based on the real-time requirements of the system. For example, in this embodiment, the duration threshold is 10 ms, indicating the minimum time interval for the hardware cache to receive new single-frame data.

[0076] If the historical reception duration is 8 ms, which is less than or equal to 10 ms, the controller returns to execute S203 to re-obtain the software cache and hardware cache status. This can ensure that the hardware cache sends new data only after a sufficient interval, avoiding data conflicts with the data sent by other devices. If the historical reception duration is 15 ms, which is greater than 10 ms, the controller executes S206, indicating that the hardware cache has waited for a sufficient time interval and can send new data. At this time, set the software cache status to idle and the hardware cache status to not sent yet to prepare for sending the first single-frame data later.

[0077] In Figure 3 the embodiment, corresponding to entering step Sh through steps Sa and Sb, if the historical reception duration is less than or equal to the preset duration threshold, return to steps Sa and Sb again; if the historical reception duration is greater than the preset duration threshold, enter step Si.

[0078] S206. Send the first single-frame data in the software cache to the hardware cache, set the software cache status to the idle state, and set the hardware cache status to the not sent yet state.

[0079] After sending the first single-frame data in the software cache to the hardware cache here, since the software cache data is removed and no longer occupies the buffer, its status is set to idle here; and the hardware cache status is set to not sent yet because the hardware cache data needs to wait for the CAN bus to send it successfully after being loaded. This step maintains the transmission order of single-frame communication by synchronously updating the cache status.

[0080] S207. Send the first single-frame data through the hardware cache via the CAN bus. If the hardware cache status is updated to the sent state, it is determined that the first single-frame data has been sent successfully.

[0081] In the above embodiments, the software cache is initialized to be idle, the hardware cache is initialized to be sent, and the CAN bus is initialized to be enabled before receiving the first single-frame data, laying a stable foundation for subsequent transmission. In the scenario of continuous data transmission, through preset status, duration threshold control, and status synchronization update, this method effectively solves the problem of low-priority frame delay or loss caused by frequent inter-frame competition. Especially when the software cache is occupied, the system reasonably schedules data according to the reception duration of the hardware cache, reducing the bus competition risk. Through status confirmation and sequential transmission, the integrity of the data frame is further ensured.

[0082] Please refer to Figure 4 , Figure 4 , which is a schematic flowchart of the CAN communication method provided in the third embodiment of this application. Although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order from that shown in the drawings. Specifically, the specific process of the CAN communication method provided in this third embodiment is as follows:

[0083] S301, before receiving the single-frame data of the first frame, set the software cache status to the idle state, set the hardware cache status to the sent state, set the restart CAN flag to the non-restart flag, and set the CAN bus status to the enabled state.

[0084] S302, receive the first single-frame data.

[0085] In some embodiments of this application, the principles of steps S301 - S302 in the above second embodiment are basically the same as those of steps S201 - S202 in the second embodiment, so they will not be elaborated here.

[0086] S303, obtain the software cache status of the software cache and the hardware cache status of the hardware cache, and obtain the CAN bus status and the restart CAN flag. If the software cache status is the idle state and the hardware cache status is the sent state, then execute S304. If the software cache status is the idle state or the occupied state and the hardware cache status is the unsent state, then execute S306.

[0087] In this embodiment, retransmission judgment and restart judgment are also combined. Therefore, in addition to obtaining the software cache status of the software cache and the hardware cache status of the hardware cache, the CAN bus status and the restart CAN flag are also obtained for retransmission judgment and restart judgment.

[0088] If the software cache status is the idle state and the hardware cache status is the transmission completed state, it indicates that the software and hardware cache resources meet the conditions for sending the first single-frame data in a single-frame transmission. At this time, S304 is executed. If the software cache status is the idle state or the occupied state and the hardware cache status is the not-yet-transmitted-completed state, it means that the hardware cache has not completed the transmission of the single-frame data stored therein and a new frame of data cannot be sent. Therefore, only S306 can be performed.

[0089] In this step, if S304 is subsequently executed here, it corresponds to Figure 3 entering step Sh through steps Sa and Sb in Figure 3 If S304 is subsequently executed here, it corresponds to

[0090] S304, store the first single-frame data in the software cache and back up the first single-frame data in the software cache.

[0091] Backing up the first single-frame data in the software cache here is because the CAN bus may fail to transmit the first single-frame data due to reasons such as excessive CAN bus load and unreadiness of the receiving node. Therefore, it is backed up in the software cache in advance for direct invocation when the first single-frame data is to be sent again later.

[0092] S305, send the first single-frame data through the hardware cache via the CAN bus. If the hardware cache status is the not-yet-transmitted-completed state, then S306 is executed. If the hardware cache status is the transmission completed state, then S11 is executed.

[0093] In this step, if S306 is subsequently executed here, it corresponds to Figure 3 entering step Sd through step Si in Figure 3 If S311 is subsequently executed here, it corresponds to

[0094] ending the process after step Si and completing the transmission of the first single-frame data.

[0095] Among them, the transmission duration is used to indicate the duration of the current hardware cache transmitting the first single-frame data via the CAN bus, and the retransmission count is used for the total number of times the hardware cache retransmits the first single-frame data via the CAN bus.

[0096] If the transmission duration is greater than a preset transmission duration threshold (e.g., 3 s), and the number of retransmission attempts is less than or equal to a preset number of retransmission attempts threshold (e.g., 3 times), it indicates that the current transmission of the first single-frame data fails and the restart count standard for restarting the CAN bus has not been reached yet. Therefore, execute S307 and subsequent steps. If the transmission duration is greater than a preset transmission duration threshold (e.g., 3 s), and the number of retransmission attempts is greater than a preset number of retransmission attempts threshold (e.g., 3 times), it indicates that the current transmission of the first single-frame data fails and the restart count standard for restarting the CAN bus has been reached. Therefore, execute S309.

[0097] In this step, if S307 is to be executed subsequently here, it corresponds to Figure 3 the subsequent path in step Sd where the judgment result of "number of retransmission attempts D > 3" is No. If S309 is to be executed subsequently here, it corresponds to Figure 3 the subsequent path in step Sd where the judgment result of "number of retransmission attempts D > 3" is Yes.

[0098] S307, reset the transmission duration to zero and increment the number of retransmission attempts by 1.

[0099] It can be understood that here, resetting the transmission duration to zero is for re-timing during the next retransmission.

[0100] S308, store the backed-up first single-frame data in the software cache, backup the first single-frame data in the software cache, and return to execute S305.

[0101] When retransmitting here, the instruction that was not successfully transmitted needs to be restored using the backed-up first single-frame data. At the same time, backup again to support subsequent retries, and then return to execute S305 to attempt to send the first single-frame data through the hardware cache via the restarted CAN bus again.

[0102] S309, set the CAN bus state of the CAN bus to the closed state, set the restart CAN flag of the CAN bus to the to-be-restarted flag, and reset the number of retransmission attempts to zero.

[0103] Since multiple retransmission failures may indicate physical faults, configuration errors, or continuous high-load competition on the bus, it is necessary to terminate the abnormal communication by closing the bus and prepare for restart. Therefore, here, the CAN bus state is set to the closed state, the restart CAN flag is set to the to-be-restarted flag, and the number of retransmission attempts is reset to zero.

[0104] S310, restart the CAN bus. After the CAN bus state is updated to the start state, return to execute S303. At this time, if the CAN bus state is the open state and the restart CAN flag is the to-be-restarted flag, then return to execute S308.

[0105] After restarting the CAN bus, the CAN bus resumes normal communication function. Therefore, it returns to execute S303 for the next iteration and starts to resend the first single-frame data again.

[0106] After restarting here, it is necessary to use the backed-up first single-frame data to restore the instructions that were not successfully sent. At the same time, back up again to support subsequent retries. Then return to execute S305 and try to send the first single-frame data through the hardware cache via the restarted CAN bus again.

[0107] This step corresponds to Figure 3 In the restart step of Sg first in [], then return to Sa, Sb, and enter Se and Sf for the resending operation after restart.

[0108] S311, determine that the first single-frame data has been sent.

[0109] In the above embodiments, by backing up data and monitoring the sending duration and the number of resends, the situation of transmission failure is dynamically processed. In this embodiment, the backup mechanism (S304, S308) ensures that the data can be resent, reducing the risk of frame loss; through resend management (S306 - S308) to handle temporary contention or errors, reducing transmission interruptions; through resending after bus restart (S309 - S310) to solve physical faults or continuous contention problems and restore the communication function. This multi-level fault-tolerant design significantly improves the stability of CAN communication in high-load, multi-node environments, provides a reliable guarantee for real-time control scenarios, effectively solves the problem of data loss caused by bus anomalies, and enhances the robustness of the system and the integrity of instruction transmission.

[0110] In some embodiments of the present application, after obtaining the software cache status of the software cache and the hardware cache status of the hardware cache, the CAN communication method further performs the following steps: If the software cache status is the occupied state and the hardware cache status is the sent complete state, then send the single-frame data stored in the software cache through the hardware cache via the CAN bus.

[0111] At this time, the single-frame data stored in the software cache may be one of the following two situations:

[0112] This single-frame data is the second single-frame data: that is, the previous frame data of the first single-frame data (for example, in the scenario of continuous data sending, the second single-frame data has not been sent completely). In this case, it is necessary to give priority to sending the second single-frame data in the software cache to ensure the sending order and integrity of the data frame, and then process the first single-frame data.

[0113] This single-frame data is the first single-frame data: In the loop processing or specific scenarios, the data stored in the software cache may be the first single-frame data itself (for example, in the resend mechanism, the backed-up first single-frame data is reloaded into the software cache). At this time, directly send this first single-frame data through the hardware cache.

[0114] In the above embodiments, corresponding to Figure 3 first enter Sa, and then enter the subsequent path where the judgment result of "hardware cache state C = 1" is yes through Sc.

[0115] Based on the third embodiment, the above embodiments further optimize the frame sending logic of CAN communication, ensuring the orderliness and reliability of data transmission.

[0116] To facilitate better implementation of the CAN communication method of the present application, the present application also provides a CAN communication device based on the above CAN communication method. The meanings of the terms are the same as those in the above CAN communication method, and the specific implementation details can be referred to the description in the method embodiments.

[0117] Please refer to Figure 5 , Figure 5 which is a schematic structural diagram of the CAN communication device provided by the embodiments of the present application, and specifically may include:

[0118] A status acquisition module 501, configured to acquire the software cache status of the software cache and the hardware cache status of the hardware cache when receiving the first single-frame data; wherein, the first single-frame data is a frame of data to be sent currently.

[0119] A data sending module 502, configured to store the first single-frame data in the software cache and send the first single-frame data through the hardware cache via the CAN bus if the software cache status is the idle state and the hardware cache status is the sending completion state; wherein, the sending completion state is used to indicate that the hardware cache has completed the sending of the second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

[0120] For the above CAN communication device, the status acquisition module 501 is configured to acquire the software cache status of the software cache and the hardware cache status of the hardware cache when receiving the first single-frame data. The data sending module 502 is configured to store and send the first single-frame data only when the software cache status is the idle state and the hardware cache status is the sending completion state, realizing the orderliness and reliability of data transmission. In the continuous data sending scenario of this embodiment, by strictly checking the cache status, it is ensured that new data frames are processed only when the cache resources are available, and through the single-frame sending mechanism of confirming that the hardware cache has completed the sending of the second single-frame data before sending a new frame, the problem of low-priority frame delay or interruption caused by frequent frame competition can be effectively avoided, and the risk of partial data frame loss caused by competition failure is reduced.

[0121] In some embodiments of the present application, after storing the first single-frame data in the software cache, the method further includes: if the software cache status is an occupied state, sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to an idle state, and setting the hardware cache status to an untransmitted completed state; after sending the first single-frame data through the hardware cache via the CAN bus, the method further includes: if the hardware cache status is updated from the untransmitted completed state to the transmitted completed state, determining that the first single-frame data has been sent successfully.

[0122] In some embodiments of the present application, before sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to an idle state, and setting the hardware cache status to an untransmitted completed state, the method further includes: obtaining the historical reception duration of the hardware cache; wherein, the historical reception duration is used to indicate the duration from the time when the hardware cache received the second single-frame data to the current moment; if the historical reception duration is less than or equal to a preset duration threshold, returning to execute the step of obtaining the software cache status of the software cache and the hardware cache status of the hardware cache; if the historical reception duration is greater than the preset duration threshold, executing the step of sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to an idle state, and setting the hardware cache status to an untransmitted completed state.

[0123] In some embodiments of the present application, after storing the first single-frame data in the software cache, the method further includes: backing up the first single-frame data in the software cache; after sending the first single-frame data through the hardware cache via the CAN bus, the method further includes: if the hardware cache status is an untransmitted completed state, obtaining the transmission duration and the number of retransmissions of the hardware cache; wherein, the transmission duration is used to indicate the duration for the current hardware cache to send the first single-frame data via the CAN bus, and the number of retransmissions is used for the total number of times the hardware cache re-sends the first single-frame data via the CAN bus; if the transmission duration is greater than a preset transmission duration threshold, and the number of retransmissions is less than or equal to a preset number of transmission thresholds, resetting the transmission duration to zero, and incrementing the number of retransmissions by 1; storing the backed-up first single-frame data in the software cache, backing up the first single-frame data in the software cache, and returning to execute the step of sending the first single-frame data through the hardware cache via the CAN bus.

[0124] In some embodiments of the present application, after obtaining the transmission duration and the number of retransmissions of the hardware buffer, the method further includes: if the transmission duration is greater than a preset transmission duration threshold and the number of retransmissions is greater than a preset number of retransmissions threshold, setting the CAN bus state of the CAN bus to the closed state, setting the restart CAN flag of the CAN bus to the to-be-restarted flag, and resetting the number of retransmissions to zero; restarting the CAN bus, and after the CAN bus state is updated to the started state, returning to execute the step of obtaining the software buffer state of the software buffer and the hardware buffer state of the hardware buffer; if the CAN bus state is the open state and the restart CAN flag is the to-be-restarted flag, storing the backed-up first single-frame data in the software buffer, backing up the first single-frame data in the software buffer, and returning to execute the step of sending the first single-frame data through the hardware buffer via the CAN bus.

[0125] In some embodiments of the present application, after obtaining the software buffer state of the software buffer and the hardware buffer state of the hardware buffer, the method further includes: if the software buffer state is the idle state or the occupied state and the hardware buffer state is the not-sent-completed state, executing the step of obtaining the transmission duration and the number of retransmissions of the hardware buffer.

[0126] In some embodiments of the present application, after obtaining the software buffer state of the software buffer and the hardware buffer state of the hardware buffer, the method further includes: if the software buffer state is the occupied state and the hardware buffer state is the sent-completed state, sending the single-frame data stored in the software buffer through the hardware buffer via the CAN bus.

[0127] In some embodiments of the present application, before receiving the single-frame data of the first frame, the method includes: setting the software buffer state to the idle state, setting the hardware buffer state to the sent-completed state, setting the restart CAN flag to the not-to-be-restarted flag, and setting the CAN bus state to the open state.

[0128] In addition, the present application further provides a terminal device, as Figure 6 shown, which shows a schematic structural diagram of the terminal device involved in the present application. Specifically:

[0129] The terminal device may include a processor 601 with one or more processing cores, a memory 602 with one or more computer-readable storage media, a power supply 603, an input unit 604, and other components. Those skilled in the art can understand that Figure 6 the structural diagram of the terminal device shown in does not constitute a limitation on the terminal device, and it may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements.

[0130] Wherein:

[0131] The processor 601 is the control center of the terminal device, connecting various parts of the entire terminal device through various interfaces and circuits. By running or executing software programs and / or modules stored in the memory 602, and by invoking the data stored in the memory 602, it executes various functions of the terminal device and processes data, thereby monitoring the terminal device as a whole. Optionally, the processor 601 may include one or more processing cores; preferably, the processor 601 may integrate an application processor and a modem processor. Among them, the application processor mainly processes the operating system, user interface, application programs, etc., and the modem processor mainly processes wireless communications. It can be understood that the above-mentioned modem processor may not be integrated into the processor 601 either.

[0132] The memory 602 can be used to store software programs and modules. The processor 601 executes various functional applications and data processing by running the software programs and modules stored in the memory 602. The memory 602 mainly includes a program storage area and a data storage area. Among them, the program storage area can store the operating system, application programs required for at least one function, etc.; the data storage area can store data created according to the use of the terminal device, etc. In addition, the memory 602 may include high-speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid-state storage devices. Correspondingly, the memory 602 may also include a memory controller to provide the processor 601 with access to the memory 602.

[0133] The terminal device further includes a power supply 603 for powering each component. Preferably, the power supply 603 can be logically connected to the processor 601 through a power management system, so as to realize functions such as management of charging, discharging, and power consumption management through the power management system. The power supply 603 may also include any components such as one or more DC or AC power supplies, a recharge system, a power device debugging circuit, a power converter or inverter, and a power status indicator.

[0134] The terminal device may further include an input unit 604, which can be used to receive input digital or character information, and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.

[0135] Although not shown, the terminal device may further include a display unit and the like, which will not be elaborated here. Specifically, in this embodiment, the processor 601 in the terminal device will, according to the following instructions, load the executable files corresponding to the processes of one or more application programs into the memory 602, and the processor 601 will run the application programs stored in the memory 602, so as to implement the steps in any of the CAN communication methods provided in the embodiments of the present application: when receiving the first single-frame data, obtain the software cache status of the software cache and the hardware cache status of the hardware cache; wherein, the first single-frame data is a frame of data to be sent currently; if the software cache status is the idle state and the hardware cache status is the sent completion state, then store the first single-frame data into the software cache, and send the first single-frame data through the hardware cache via the CAN bus; wherein, the sent completion state is used to indicate that the hardware cache has completed the sending of the second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

[0136] In the above embodiment, by obtaining the software cache and hardware cache status when receiving the first single-frame data, and storing and sending the first single-frame data only when the software cache status is the idle state and the hardware cache status is the sent completion state, the orderliness and reliability of data transmission are realized. In the scenario of continuous data sending in this embodiment, by strictly checking the cache status, it is ensured that only when the cache resources are available, new data frames are processed, and through the single-frame sending mechanism of confirming that the hardware cache has completed the sending of the second single-frame data before sending a new frame, the problem of low-priority frame delay or interruption caused by frequent frame competition can be effectively avoided, and the risk of partial data frame loss caused by competition failure is reduced.

[0137] For the specific implementation of each of the above operations, reference may be made to the previous embodiments, which will not be elaborated here.

[0138] Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructions, or by instructions controlling relevant hardware. The instructions can be stored in a computer-readable storage medium and loaded and executed by a processor.

[0139] Therefore, the present application provides a computer-readable storage medium, on which a computer program is stored. The computer program can be loaded by a processor to execute the steps in any of the CAN communication methods provided by the present application.

[0140] For the specific implementation of each of the above operations, reference may be made to the previous embodiments, which will not be elaborated here.

[0141] Among them, the computer-readable storage medium may include: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disk, etc.

[0142] Since the instructions stored in the computer-readable storage medium can execute the steps in any of the CAN communication methods provided in this application, the beneficial effects achievable by any of the CAN communication methods provided in this application can be realized. For details, refer to the previous embodiments and will not be elaborated here.

[0143] The above has introduced in detail a CAN communication method, device, terminal device, and computer-readable storage medium provided in this application. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.

Claims

1. A CAN communication method, characterized in that, The method includes: When receiving the first single-frame data, obtaining the software cache status of the software cache and the hardware cache status of the hardware cache; wherein, the first single-frame data is a frame of data to be sent currently. If the software cache status is the idle state and the hardware cache status is the sent completed state, storing the first single-frame data into the software cache, and sending the first single-frame data through the hardware cache via the CAN bus; wherein, the sent completed state is used to indicate that the hardware cache has completed the sending of the second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

2. The CAN communication method according to claim 1, wherein After storing the first single-frame data into the software cache, the method further includes: If the software cache status is the occupied state, sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to the idle state, and setting the hardware cache status to the not sent completed state. After sending the first single-frame data through the hardware cache via the CAN bus, the method further includes: If the hardware cache status is updated from the not sent completed state to the sent completed state, determining that the first single-frame data has been sent completed.

3. The CAN communication method according to claim 2, characterized in that, Before sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to the idle state, and setting the hardware cache status to the not sent completed state, the method further includes: Obtaining the historical receiving duration of the hardware cache; wherein, the historical receiving duration is used to indicate the duration from the time when the hardware cache received the second single-frame data to the current moment. If the historical receiving duration is less than or equal to a preset duration threshold, returning to execute the step of obtaining the software cache status of the software cache and the hardware cache status of the hardware cache. If the historical receiving duration is greater than the preset duration threshold, executing the step of sending the first single-frame data in the software cache to the hardware cache, setting the software cache status to the idle state, and setting the hardware cache status to the not sent completed state.

4. The CAN communication method according to claim 1, characterized in that, After storing the first single-frame data into the software cache, the method further includes: backing up the first single-frame data in the software cache. After sending the first single-frame data through the hardware cache via the CAN bus, the method further includes: If the hardware cache status is the not sent completed state, obtaining the sending duration and the retransmission times of the hardware cache; wherein, the sending duration is used to indicate the duration for the hardware cache to send the first single-frame data through the CAN bus currently, and the retransmission times is used for the total number of times for the hardware cache to retransmit the first single-frame data through the CAN bus. If the sending duration is greater than a preset sending duration threshold and the retransmission times is less than or equal to a preset sending times threshold, resetting the sending duration to zero, and incrementing the retransmission times by 1. Store the backed-up first single-frame data in the software cache, back up the first single-frame data in the software cache, and return to execute the step of sending the first single-frame data via the CAN bus through the hardware cache.

5. The CAN communication method according to claim 4, characterized in that, After obtaining the transmission duration and the number of retransmissions of the hardware cache, the method further includes: If the transmission duration is greater than a preset transmission duration threshold and the number of retransmissions is greater than a preset number of retransmission threshold, set the CAN bus state of the CAN bus to the closed state, set the restart CAN flag of the CAN bus to the to-be-restarted flag, and reset the number of retransmissions to zero; Restart the CAN bus. After the CAN bus state is updated to the started state, return to execute the step of obtaining the software cache state of the software cache and the hardware cache state of the hardware cache; If the CAN bus state is the open state and the restart CAN flag is the to-be-restarted flag, store the backed-up first single-frame data in the software cache, back up the first single-frame data in the software cache, and return to execute the step of sending the first single-frame data via the CAN bus through the hardware cache.

6. The CAN communication method according to claim 4, characterized in that, After obtaining the software cache state of the software cache and the hardware cache state of the hardware cache, the method further includes: If the software cache state is the idle state or the occupied state and the hardware cache state is the not-sent-completed state, execute the step of obtaining the transmission duration and the number of retransmissions of the hardware cache.

7. The CAN communication method according to claim 1, wherein After obtaining the software cache state of the software cache and the hardware cache state of the hardware cache, the method further includes: If the software cache state is the occupied state and the hardware cache state is the sent-completed state, send the single-frame data stored in the software cache via the CAN bus through the hardware cache.

8. The CAN communication method according to any one of claims 1, wherein Before receiving the single-frame data of the first frame, the method includes: Set the software cache state to the idle state, set the hardware cache state to the sent-completed state, set the restart CAN flag to the not-to-be-restarted flag, and set the CAN bus state to the open state.

9. A CAN communication device, characterized in that, The CAN communication device includes: A state acquisition module, configured to, when receiving the first single-frame data, acquire the software cache state of the software cache and the hardware cache state of the hardware cache; wherein the first single-frame data is a frame of data to be currently sent; A data sending module, configured to, if the software cache state is the idle state and the hardware cache state is the sent-completed state, store the first single-frame data in the software cache, and send the first single-frame data via the CAN bus through the hardware cache; wherein the sent-completed state is used to indicate that the hardware cache has completed the sending of the second single-frame data, and the second single-frame data is the previous frame of data to be sent of the first single-frame data.

10. A terminal device, characterized in that, It includes a memory and a processor. When the computer program stored in the memory is executed by the processor, the processor is caused to execute the steps of the method according to any one of claims 1 to 8.