A CAN communication acceleration method based on shared memory and host computer

By adopting the CAN communication method with shared memory and multi-thread assistance in industrial control systems, the problem that traditional CAN bus communication cannot meet real-time and reliability is solved, and data transmission efficiency is improved and system stability is enhanced.

CN120045486BActive Publication Date: 2025-08-29临海市新睿电子科技股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510137540.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-07
Publication Date
2025-08-29
Estimated Expiration
2045-02-07

AI Technical Summary

Technical Problem

Traditional CAN bus communication methods cannot effectively respond to the real-time and reliability requirements of data transmission in industrial control systems, especially in high-demand production line control and equipment monitoring scenarios.

Method used

The CAN communication method based on shared memory is adopted, and by creating user-state and kernel-state threads, using shared memory to transmit data frames, accelerate the data transmission process, reduce context switching and data copying, improve memory and CPU usage efficiency, and reduce energy consumption and hardware requirements.

Benefits of technology

It improves data transmission efficiency, reduces system jitter and network failure risks, enhances system stability and anti-interference capabilities, and optimizes communication efficiency between the upper computer and the target device.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045486B_ABST
    Figure CN120045486B_ABST
Patent Text Reader

Abstract

The present application relates to the field of Internet of Things communication technology, and provides a CAN communication acceleration method based on shared memory and a host computer. The method is applied to the host computer, including receiving control data for a target device; a first user-mode thread writes multiple data frames to a first shared memory pointed to by a send space address by calling a system write function request, and switches to a first kernel-mode thread; the first kernel-mode thread writes multiple data frames into the first shared memory in sequence, and sends a data send request to a second kernel-mode thread; the second kernel-mode thread responds to the data send request and scans the first shared memory to determine whether there is a data frame written, and when it is determined that there is a data frame written, reads the data frame from the first shared memory and writes it into a CAN send register area, so as to send the data frame to the target device via the CAN bus, thereby improving the transmission rate of the data frame within the host computer system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of Internet of Things communication technology, and in particular to a CAN communication acceleration method based on shared memory, a host computer, and a computer-readable storage medium. Background Art

[0002] In industrial automation systems, industrial controllers are often used for tasks such as production line control, equipment monitoring, and precision operation control to achieve control of related production operation equipment. In applications, industrial controllers and production operation equipment communicate based on the CAN bus. However, under the scheduling mechanism of traditional operating systems, the CAN bus-based communication method cannot effectively cope with scenarios with high requirements for the real-time and reliability of data transmission. Summary of the Invention

[0003] In order to improve the data transmission rate and meet the real-time control requirements, the embodiment of the present application provides a CAN communication acceleration method and a host computer based on shared memory.

[0004] On the one hand, the method provided in the implementation of the present application is applied to an industrial control system, which includes a host computer and a target device. The method is implemented based on the host computer and includes the following steps: receiving control data for the target device; creating a first user-mode thread in response to the control data; initializing a first shared memory based on the first user-mode thread, determining a send space address based on the first shared memory, and encapsulating the control data into multiple data frames; wherein the send space address is a virtual address and establishes a mapping relationship with the first shared memory; the first user-mode thread calls a system write function to request that the multiple data frames be written to the first shared memory pointed to by the send space address, and switches to a first kernel-mode thread; the first kernel-mode thread writes the multiple data frames in sequence to the first shared memory pointed to by the send space address, and sends a data send request to a second kernel-mode thread; the second kernel-mode thread responds to the data send request and scans the first shared memory pointed to by the send space address to determine whether there is a data frame written. When it is determined that there is a data frame written, the data frame is read from the first shared memory and written into the CAN send register area to send the data frame to the target device via the CAN bus.

[0005] In the above technical solution, based on the frame-by-frame transmission characteristics of the CAN bus data transmission process, shared memory and the assistance of multiple threads in the data frame transmission process are used to accelerate the transmission of data frames in user space and kernel space, thereby improving data transmission efficiency. At the same time, by creating a mapping relationship between the sending space address and shared memory, data frames can be directly read and written to physical memory during transmission, eliminating unnecessary context switching and data copying, improving memory and CPU utilization efficiency, and reducing energy consumption and hardware requirements for devices. In addition, the use of shared memory can reduce competition between multiple threads, improve system stability, and reduce the risk of potential network failures caused by the network stack.

[0006] In one implementation, the number of times the first user-mode thread calls the system write function is less than the number of the data frames.

[0007] Based on the above technical solution, the switching of the first user-mode thread between the user space and the kernel space can be reduced, the number of system calls can be reduced, thereby reducing system jitter and improving the system's anti-interference ability.

[0008] In one embodiment, the data sending request carries the total number of data frames; when the second kernel-mode thread completes a data frame sending, it updates the sending index, and when it determines that the amount of sent data indicated by the sending index is consistent with the total number of data frames, it completes the processing of the data sending request.

[0009] In one embodiment, the method further includes: after the second kernel-mode thread completes processing of the data sending request, determining whether there are other data sending requests that have not been processed, and automatically exiting if no other data sending requests have been processed.

[0010] In one embodiment, the method further includes: after processing the data send request, creating a second user-mode thread for receiving response data returned by the target device in response to the control data; the second user-mode thread initializing a second shared memory, determining a receive space address based on the second shared memory, calling a system read function to request reading data in the second shared memory pointed to by the receive space address, and switching to a third kernel-mode thread; wherein the receive space address is a virtual address and has a mapping relationship with the second shared memory; the third kernel-mode thread sending the receive space address to a fourth kernel-mode thread and monitoring a receive queue; when the receive queue writes a target message, updating a read index, synchronizing the read index and the number of target messages to the user space, and switching back to the second user-mode thread; the second user-mode thread reading the response data from the second shared memory pointed to by the receive space address based on the read index and the number of target messages, and when determining that the response data has not been sent, switching to the third kernel-mode thread and again performing the step of monitoring the receive queue; wherein the target message is added to the receive queue after the fourth kernel-mode thread writes the response data to the second shared memory pointed to by the receive space address.

[0011] Based on the above technical solution, it is possible to accelerate the transmission of response data within the host computer system, thereby further improving the communication efficiency between the host computer and the target device.

[0012] In one embodiment, the method for the fourth kernel mode thread to write the response data to the second shared memory pointed to by the receive space address includes: when an interrupt signal of the CAN register is monitored, reading the response data in the CAN receive register area, and writing the response data to the second shared memory pointed to by the receive space address.

[0013] Based on the above technical solution, the fourth kernel-mode thread triggers the reading of data in the shared memory by obtaining the interrupt signal of the CAN register, which can ensure that new data can be read every time, avoid invalid operations, and save system resources.

[0014] In one embodiment, the method further includes creating a fifth kernel-state thread, wherein the fifth kernel-state thread determines the real-time data transmission rate based on the read operation of the second kernel-state thread on the first shared memory and the write operation of the fourth kernel-state thread on the second shared memory, and determines the baud rate adjustment method of the CAN register based on the relationship between the real-time data transmission rate and the baud rate threshold.

[0015] In one implementation, the fifth kernel-mode thread calculates the real-time data transmission rate Rt based on the following formula: Among them, F is the message frequency; Ps is the packet size; Po is the protocol overhead; Br is the bus utilization; Mr is the error rate.

[0016] On the other hand, an embodiment of the present application further provides a host computer, which can communicate with the target device based on the above method.

[0017] In addition, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which implements the above method when executed by a processor. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The drawings that constitute a part of this application are used to provide further understanding of this application. The illustrative embodiments of this application and their descriptions are used to explain this application and do not constitute improper limitations on this application.

[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0020] Figure 1 A schematic diagram of the structure of an industrial control system provided by an embodiment of the present application is shown.

[0021] Figure 2 A flow chart of a CAN communication acceleration method based on shared memory provided in an embodiment of the present application is shown.

[0022] Figure 3 A flow chart of a CAN communication acceleration method based on shared memory provided by another embodiment of the present application is shown. DETAILED DESCRIPTION

[0023] The following will be combined with the accompanying drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments of this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0024] In the description of the embodiments of the present application, unless otherwise specified, "multiple" means two or more, and "first", "second" and various numerical numbers are only distinctions for the convenience of description and are not used to limit the scope of the embodiments of the present application.

[0025] The features, structures, or characteristics of this application may be combined in any suitable manner in one or more embodiments. In the various embodiments of this application, the order of the sequence numbers of the processes does not necessarily indicate the order of execution. The order of execution of the processes should be determined by their functions and inherent logic, and should not constitute any limitation on the implementation of the embodiments of this application.

[0026] Some optional features in the embodiments of the present application can be implemented independently in some scenarios without relying on other features to solve corresponding technical problems and achieve corresponding effects. They can also be combined with other features in some scenarios according to needs.

[0027] In this application, unless otherwise specified, the same or similar parts between the various embodiments can refer to each other. In the various embodiments of this application, unless otherwise specified and there is no logical conflict, the terms and / or descriptions between different embodiments are consistent and can be referenced to each other. The technical features in different embodiments can be combined to form new embodiments based on their inherent logical relationships. The implementation methods of this application do not constitute a limitation on the scope of protection of this application.

[0028] The following describes the embodiments of the present application in detail with reference to the accompanying drawings.

[0029] Please refer to Figure 1 The industrial control system includes a host computer 11 and multiple target devices 12. The target devices 12 communicate with the host computer 11 based on the CAN bus.

[0030] The host computer 11 runs a Linux system and has an application control program installed. The application control program can be used to monitor and control each target device 12. In one application example, the host computer 11 can be an industrial controller, and the target device 12 is a servo motor. It is understood that the method provided in the embodiments of the present application can also be applied to other IoT application scenarios based on CAN bus communication.

[0031] In one example, data interaction between the host computer 11 and the target device 12 includes, but is not limited to, the host computer 11 sending control data to the target device 12 via the CAN bus to control the operating status of the target device 12, such as powering on, powering off, pausing, adjusting the operating mode, collecting specified data, etc. Upon receiving the control data, the target device 12 performs the corresponding operation and returns response data, including, but not limited to, the current operating status, operating parameters, and monitoring data. This allows the host computer 11 to obtain the latest status of the target device or acquire monitoring data based on the response data, thereby performing subsequent related operations.

[0032] It can be seen that the communication process between the host computer 11 and the target device 12 generally includes two stages. The first stage is that the host computer 11 sends data to the target device 12 and the host computer 11 receives data reported by the target device. The method provided in the embodiment of the present application optimizes the process of sending and receiving data by the host computer 11, thereby achieving communication acceleration.

[0033] Please refer to Figure 2 The embodiment of the present application provides a CAN communication acceleration method based on shared memory, which is executed by a host computer and specifically includes the following steps.

[0034] S201: Receive control data for a target device.

[0035] In practice, control data can be automatically sent by the device control program or by the user. The control data is used to instruct the target device to complete the corresponding operation and feedback the response data.

[0036] S202: Create a first user-mode thread in response to the control data.

[0037] The host computer creates a first user-mode thread dedicated to sending control data.

[0038] S203: Initialize a first shared memory based on the first user state thread, and determine a sending space address based on the first shared memory.

[0039] In implementation, the size of the first shared memory can be determined first, and a storage space of a corresponding size can be allocated in the physical memory as the first shared memory. A virtual address can be obtained in the user-mode virtual space as the send space address, where the size of the space indicated by the send space address is the same as the size of the first shared memory. A mapping relationship between the send space address and the first shared memory can be established, allowing the system to access the first shared memory through the send space address.

[0040] In one example, based on system requirements, the size of the first shared memory can be set to 16 KB, of which the first 8 KB is a write buffer area. The first shared memory content is initialized.

[0041] S204: Encapsulate the control data into multiple data frames.

[0042] In implementation, the first user-mode thread may convert the control data into corresponding frame data based on the data format of the CAN communication protocol to obtain multiple data frames.

[0043] S205 , the first user-state thread calls a system write function to request writing all of the multiple data frames into the first shared memory pointed to by the sending space address, and switches to the first kernel-state thread.

[0044] In implementation, when the first user-mode thread completes a data frame conversion during the process of converting the control data, it may call a system write function based on the sending space address to request that the data frame be written into the first shared memory.

[0045] In another implementation, in order to reduce the number of system calls, reduce system jitter, and improve anti-interference performance, the number of times the first user-state thread calls the system write function is less than the number of data frames. In other words, the first user-state thread can write data frames to the first shared memory in batches, and the number of data frames sent in each batch can be multiple, thereby reducing the number of times the system write function is called.

[0046] Optionally, the first user state thread calls the system write function once, that is, after completing the format conversion of the control data, the system write function is called once to request to write all data frames into the first shared memory.

[0047] It is worth noting that after successfully calling the system write function, the first user-state thread automatically switches to the kernel state and becomes the first kernel-state thread. Therefore, after reducing the number of calls to the system write function through the above embodiment, the number of switches between the user state and the kernel state of the first user-state thread can also be reduced, thereby reducing system jitter and ensuring stable system operation.

[0048] S206: The first kernel state thread writes the multiple data frames sequentially into the first shared memory pointed to by the sending space address, and sends a data sending request to the second kernel state thread.

[0049] In implementation, the first kernel-mode thread can directly send each data frame to the sending space address in sequence, and the system writes each data frame into the corresponding first shared memory based on the mapping relationship between the sending space address and the first shared memory.

[0050] The data sending request is used to notify the second kernel state thread to send the data frame in the first shared memory pointed to by the sending space address.

[0051] The second kernel state thread is created by the system based on the data frame sending requirement.

[0052] S207 , in response to the data sending request, the second kernel state thread scans the first shared memory pointed to by the sending space address to determine whether there is a data frame written thereto.

[0053] In implementation, after receiving the data sending request, the second kernel state thread extracts the sending space address from the data sending request, and accesses the corresponding memory space based on the sending space address to determine the data frame writing status in the memory space.

[0054] When it is determined that a data frame is written, step S208 is executed; otherwise, the first shared memory area pointed to by the sending space address lock is continuously scanned.

[0055] S208, reading the data frame from the first shared memory and writing it into the CAN transmit register area.

[0056] In implementation, when determining that a data frame is written into the first shared memory, the second kernel state thread reads the data frame and writes the data frame into the CAN send register area, so as to send the data frame to the target device through the CAN bus.

[0057] To ensure stable transmission of data frames, the second kernel-mode thread first determines the current CAN register's transmit status before reading the data frame from the first shared memory, and determines whether the previous data frame has been sent. If so, it reads the data frame from the first shared memory. Specifically, the second kernel-mode thread can determine whether the previous frame has been sent by combining the transmit busy flag and transmit buffer ready flag of the CAN register. When the flag information indicates that the current state is not busy and the transmit buffer is ready, it is determined that the previous frame has been sent and the CAN register is currently idle. If the previous frame has not been sent, it waits until the previous frame is sent. This prevents packet loss and ensures communication quality.

[0058] In one embodiment of the present application, in order to avoid repeated sending of data frames, the data sending request carries the total number of data frames. Each time the second kernel-state thread completes the sending of a data frame, it updates the sending index according to the number of sent data frames, and completes the processing of the data sending request when it is determined that the amount of sent data indicated by the sending index is consistent with the total number of data frames. At the same time, it updates the write index based on the total number of data frames to ensure that these sent packets will not be read repeatedly during the next reading.

[0059] In one implementation, after completing processing a data transmission request, the second kernel-state thread determines whether other unprocessed data transmission requests exist. If not, the thread automatically exits, thereby promptly releasing system resources. Specifically, once created, the second kernel-state thread can be used to process multiple data transmission requests sent by the first kernel-state thread, thereby reducing the number of threads in the system and simplifying multi-thread management, thereby optimizing system resource allocation and maintaining system stability.

[0060] Based on the method provided in the above embodiment, by optimizing the scheduling mechanism within the host computer system, the user-state related processing and the pure kernel-state processing process are effectively distinguished and handed over to different threads for processing, so that the process of writing and reading data frames from the first shared memory can be carried out independently, thereby using the parallel processing of two threads to improve the transmission rate of data frames in the system, thereby achieving communication acceleration.

[0061] Furthermore, by establishing a mapping between the send space address and the first shared memory, data frames can be read and written directly based on real physical memory. Compared to SocketCAN-based communication solutions, this reduces the number of data copies in system memory and eliminates unnecessary context switches. This not only further improves data transmission efficiency, but also reduces energy consumption and hardware requirements by improving memory and CPU utilization efficiency. In addition, the use of the first shared memory can reduce competition between multiple processes, improve system stability, and reduce the risk of potential network failures caused by the network stack.

[0062] The host computer 11 is based on Figure 2 After the method shown completes the sending of control data, it enters the state of waiting to receive response data. Please refer to Figure 3 The shared memory-based CAN communication acceleration method provided in the embodiment of the present application also includes the following steps.

[0063] S301: After the data sending request is processed, a second user state thread is created.

[0064] The second user state thread is used to receive response data returned by the target device in response to the control data.

[0065] S302: The second user state thread initializes the second shared memory and determines a receiving space address based on the second shared memory.

[0066] The method of initializing the second shared memory and determining the receiving space address is the same as step S203, except that the second shared memory and the first shared memory correspond to different physical memory areas.

[0067] S303: Calling a system read function to request reading the data in the second shared memory pointed to by the receiving space address, and switching to a third kernel state thread.

[0068] The second user state thread calls the system read function to request to read the data in the second shared memory pointed to by the receiving space address, and at the same time enters the kernel state and switches to the third kernel state thread.

[0069] S304: The third kernel state thread sends the receiving space address to the fourth kernel state thread and monitors the receiving queue.

[0070] S305: When the target message is written into the receiving queue, the read index is updated, the read index and the number of target messages are synchronized to the user space, and the second user state thread is switched back.

[0071] Among them, the target message is added to the receiving queue after the fourth kernel state thread writes the response data to the receiving space address.

[0072] In one implementation, the fourth kernel-mode thread monitors the CAN register in real time. When an interrupt signal of the CAN register is detected, the response data in the CAN receive register area is read and written to the second shared memory pointed to by the receive space address. When the writing is completed, the target message is written to the receive queue.

[0073] The target message is used to indicate that new data has been written into the second shared memory pointed to by the receiving space address and the amount of new data written. In one example, a target message indicates that a response frame has been written.

[0074] It can be understood that the response data is sent based on the CAN communication protocol. Therefore, one response data corresponds to multiple data frames, that is, multiple response frames. In the process of the target device sending response data to the host computer, the CAN register writes the response frame to the CAN receive register area in turn, and triggers an interrupt when each response frame is written. The interrupt signal is received by the fourth kernel state thread to trigger subsequent read and write operations and update the receive queue.

[0075] Furthermore, to avoid multi-threaded race conditions and data inconsistencies, the third and fourth kernel-state threads must acquire a mutex lock before performing read and write operations on the receive queue, locking the receive queue and unlocking it after completing the corresponding read and write operations. Consequently, the fourth kernel-state thread may not be able to obtain permission to operate the receive queue in a timely manner after completing a data frame write, and must therefore wait. During this wait, a data frame write operation may be triggered. Consequently, once the fourth kernel-state thread obtains permission to operate the receive queue, it may be able to write multiple target messages at once.

[0076] In order to ensure that the third kernel-state thread can accurately read the response frame, the third kernel-state thread can update the read index by calculating the number of target messages in the receive queue after monitoring the target message being written to the receive queue and successfully locking the receive queue. The read index is used to determine the position of the current response frame to be read. By setting the return information, the obtained target message number and read index are stored in the predefined cache structure, so that the target message number and read index can be copied to the user space.

[0077] At the same time, in order to prevent the system from reordering the first index and the read index, the third kernel-state thread can set a write memory barrier by calling the wmb() function after updating the first index and the read index, so as to ensure that the relevant index is not modified, thereby ensuring the correctness of the data reading order, and at the same time ensuring that the data has been successfully copied to the receive buffer before the third kernel-state thread actively updates the index.

[0078] After returning the read index and the number of waiting target messages to the user space, the third kernel state thread returns to the user space to switch to the second user state thread.

[0079] S306: The second user state thread reads response data from the second shared memory corresponding to the receiving space address.

[0080] The second user state thread obtains a read index and the number of target messages by parsing the content in the predefined cache structure, and reads a corresponding response frame from the second shared memory based on the read index and the target message data.

[0081] After receiving the corresponding response frame, the second user-mode thread processes it, parsing the response frame to obtain the corresponding response data. The parsed response data is then verified for integrity and correctness. All received response data is then analyzed to determine whether it has been sent. If it is determined that the response data has not been sent, the second user-mode thread reenters kernel space, switches to the third kernel-mode thread, and returns to the step of monitoring the receive queue to continue receiving the remaining response data. If the response data has been sent, the second user-mode thread performs business processing on the response data according to the application and returns the business processing results.

[0082] In one example, a method for determining whether response data has been sent includes determining whether response data has been sent based on data requirements provided by an application, wherein the data requirements include but are not limited to the type of business data, data length, data content, etc. required to be included in the response data.

[0083] Based on the technical solution provided by the above embodiment, it is possible to optimize the communication of sending data from the target device to the host computer. On the one hand, the fourth kernel thread can monitor the interrupt information of the CAN register to determine whether there is data that needs to be written to the second shared memory, and the third kernel thread can monitor the receive queue to determine whether data needs to be read from the second shared memory. This allows the response data to be processed in parallel from the CAN receive register area to the second shared memory, and from the second shared memory to the user space, thereby improving the transmission time of the response data within the system. Similarly, forwarding data directly based on the second shared memory can reduce the number of data copies and eliminate unnecessary upper and lower switching, which can improve resource utilization.

[0084] Furthermore, the update and use of the index are controlled by the same thread, which can ensure that the second user-mode thread reads the response frames from the second shared memory in an accurate order and quantity, avoiding confusion or loss of data access order, thereby ensuring the correctness and consistency of the data.

[0085] It is worth noting that the above examples are based on a complete request response. In actual applications, there is also a scenario of unilateral data transmission between the host computer and the target device, that is, the host computer only receives or sends data, and it can also be used Figure 2 and Figure 3 The difference lies in the triggering conditions for creating the first user state thread and the second user state thread. That is, in the scenario of unilateral data transmission, the creation of the corresponding user state thread can be triggered based on the set timed task.

[0086] In actual application, in order to facilitate users to update the application control program according to business needs, in some application scenarios, the host computer may allow users to install and run self-developed application control programs or perform secondary development of installed application control programs. In some embodiments of the present application, in addition to controlling the data communication between the application control program and the target device based on the above method, it can also automatically adjust the CAN communication baud rate.

[0087] Specifically, the system creates a fifth kernel-state thread, and when it detects that the system creates the first user-state thread or the second user-state thread, it starts the fifth kernel-state thread to monitor the real-time data transmission rate between the application control program and the target device, and determines the method of adjusting the CAN register baud rate based on the relationship between the real-time data transmission rate and the rate threshold, that is, when the real-time transmission rate exceeds the rate threshold, the CAN register baud rate is increased; if it is less than the rate threshold, the baud rate parameter is reduced.

[0088] In one implementation, the fifth kernel-mode thread may calculate the real-time data transmission rate Rt during the execution of the application control program based on the following formula:

[0089]

[0090] Among them, F is the message frequency; Ps is the packet size; Po is the protocol overhead; Br is the bus utilization; Mr is the error rate, which is a coefficient determined based on the error rate and retransmission mechanism in actual communication.

[0091] Then, by comparing the real-time data transmission rate Rt with the rate threshold Rs, when Rt is greater than or equal to Rs, the CAN baud rate is adjusted to the maximum value; when Rt is less than Rs, the CAN baud rate is adjusted to the average of the historical real-time transmission rates. The rate threshold Rs is determined based on the historical real-time transmission rates. In one example, the maximum real-time data transmission rate during operation can be set.

[0092] Based on this, for application scenarios where users can customize the development of application control programs, when setting the baud rate of the CAN register, users do not need to analyze data transmission requirements. They can directly select the system-provided default value or set an arbitrary value, reducing the user's development difficulty. Ultimately, the fifth kernel-mode thread dynamically adjusts the target baud rate based on actual data transmission requirements. This not only meets the data transmission requirements between the application control program and the target device, but also actively lowers the baud rate when the user sets the baud rate too high, thereby reducing signal attenuation during data transmission and improving anti-interference capabilities. At the same time, setting the lower limit of the CAN baud rate to the average of historical real-time transmission rates can avoid excessive reductions that affect business and avoid frequent adjustments to the baud rate parameters. At the same time, dynamically setting Rs to the maximum historical real-time transmission rate can avoid frequent adjustments to the baud rate parameters.

[0093] Furthermore, the fifth kernel-state thread can directly monitor the data operations on the first shared memory and the second shared memory to obtain accurate and real data transmission requirements, providing a reliable data basis for setting the target baud rate; at the same time, the fifth kernel-state thread can directly set the baud rate of the CAN register without entering the user state to perform related operations, which can reduce the switching overhead between the system kernel state and the user state and save system operating resources.

[0094] Another embodiment of the present application relates to a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the above-mentioned method.

[0095] Those skilled in the art will appreciate that all or part of the steps in the above-described embodiments can be accomplished by instructing related hardware through a program stored in a storage medium, which includes a number of instructions for causing a device (which may be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0096] The above are all preferred embodiments of the present application, and are not intended to limit the scope of protection of the present application. Therefore, any equivalent changes made based on the structure, shape, and principle of the present application should be included in the scope of protection of the present application.

Claims

1. A CAN communication acceleration method based on shared memory, characterized in that: The method is applied to an industrial control system, which includes a host computer and a target device. The method is implemented based on the host computer and includes the following steps: receiving control data for the target device; In response to the control data, creating a first user mode thread; Initializing a first shared memory based on the first user state thread, determining a sending space address based on the first shared memory, and encapsulating the control data into multiple data frames; wherein the sending space address is a virtual address and establishes a mapping relationship with the first shared memory; The first user state thread calls a system write function to request writing the multiple data frames into the first shared memory pointed to by the sending space address, and switches to the first kernel state thread; The first kernel state thread writes the multiple data frames into the first shared memory pointed to by the sending space address in sequence, and sends a data sending request to the second kernel state thread; In response to the data sending request, the second kernel mode thread scans the first shared memory pointed to by the sending space address to determine whether there is a data frame written. When it is determined that a data frame is written, the data frame is read from the first shared memory and written into the CAN sending register area to send the data frame to the target device via the CAN bus.

2. The method according to claim 1, characterized in that The number of times the first user-mode thread calls the system write function is less than the number of the data frames.

3. The method according to claim 1, characterized in that The data sending request carries the total number of data frames; when the second kernel state thread completes a data frame sending, it updates the sending index, and when it determines that the amount of sent data indicated by the sending index is consistent with the total number of data frames, it completes the processing of the data sending request.

4. The method according to claim 3, characterized in that The method further comprises: After completing the processing of the data sending request, the second kernel state thread determines whether there are other data sending requests that have not been processed yet, and automatically exits if no other data sending requests exist.

5. The method according to claim 1, characterized in that The method further comprises: After the data sending request is processed, a second user state thread is created to receive response data returned by the target device in response to the control data; The second user-state thread initializes the second shared memory, determines the receiving space address based on the second shared memory, requests to read the data in the second shared memory pointed to by the receiving space address by calling the system read function, and switches to the third kernel-state thread; wherein, the receiving space address is a virtual address and establishes a mapping relationship with the second shared memory; the third kernel-state thread sends the receiving space address to the fourth kernel-state thread, monitors the receiving queue, updates the read index when the receiving queue writes the target message, synchronizes the read index and the number of target messages to the user space, and switches back to the second user-state thread; The second user-state thread reads the response data from the second shared memory pointed to by the receive space address based on the read index and the number of target messages, and when it is determined that the response data has not been sent, switches to the third kernel-state thread and executes the step of monitoring the receive queue again; wherein, the target message is added to the receive queue after the fourth kernel-state thread writes the response data to the second shared memory pointed to by the receive space address.

6. The method according to claim 5, characterized in that The method for the fourth kernel state thread to write the response data to the second shared memory pointed to by the receiving space address includes: When an interrupt signal of the CAN register is detected, the response data in the CAN receiving register area is read, and the response data is written into the second shared memory pointed to by the receiving space address.

7. The method according to claim 1 or 5, characterized in that The method also includes creating a fifth kernel-state thread, wherein the fifth kernel-state thread determines a real-time data transmission rate based on a read operation of the second kernel-state thread on the first shared memory and a write operation of the fourth kernel-state thread on the second shared memory, and determines a baud rate adjustment method for the CAN register based on a relationship between the real-time data transmission rate and a baud rate threshold.

8. The method according to claim 7, characterized in that The fifth kernel state thread calculates the real-time data transmission rate Rt based on the following formula: Among them, F is the message frequency; Ps is the packet size; Po is the protocol overhead; Br is the bus utilization; Mr is the error rate.

9. A host computer, characterized in that: The host computer realizes communication with the target device based on the method according to any one of claims 1 to 8.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

Citation Information

Patent Citations

  • Method and device for communication between kernel mode and user mode and terminal

    CN108062253A

  • Data processing method and device, equipment and storage medium

    CN118964063A