CAN communication acceleration method based on shared memory and upper computer

By adopting a CAN communication acceleration method based on shared memory in industrial automation systems, using multi-threading and shared memory for data transmission, the data transmission process is accelerated, and the problem that traditional CAN bus communication is difficult to meet the real-time and reliability requirements is solved, and more efficient, stable and reliable communication is achieved.

CN120045486AActive Publication Date: 2025-05-27临海市新睿电子科技股份有限公司

Patent Information

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

AI Technical Summary

Technical Problem

Traditional CAN bus communication is difficult to meet the real-time and reliability requirements of data transmission in industrial automation systems.

Method used

The CAN communication acceleration method based on shared memory is adopted, and the system efficiency is improved by creating user-state and kernel-state threads, using shared memory to transmit data frames, and by reducing the number of system calls and context switching.

Benefits of technology

It improves data transmission efficiency, meets real-time control needs, reduces energy consumption and hardware requirements, and improves system stability and anti-interference capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045486A_ABST
    Figure CN120045486A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of Internet of Things communication, and provides a CAN communication acceleration method based on a shared memory and an upper computer. The method is applied to an upper computer and comprises the following steps: receiving control data of target equipment; the first user mode thread writes the plurality of data frames into a first shared memory pointed by the sending space address by calling a system write function request, and switches to a first kernel mode thread; the first kernel mode thread sequentially writes the plurality of data frames into a first shared memory and sends a data sending request to a second kernel mode thread; and the second kernel mode thread responds to the data sending request, scans the first shared memory to determine whether a data frame is written or not, reads the data frame from the first shared memory when determining that the data frame is written, and writes the data frame into a CAN sending register area so as to send the data frame to target equipment through a CAN bus, and the transmission rate of the data frame in the upper computer system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] In an industrial automation system, an industrial controller is often used for tasks such as production line control, equipment monitoring, and precise operation control to achieve control over related production operation equipment. In applications, communication between the industrial controller and the production operation equipment is based on the CAN bus. However, under the scheduling mechanism of traditional operating systems, the communication method based on the CAN bus cannot effectively meet the demand scenarios with high requirements for the real-time performance 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, an embodiment of this application provides a CAN communication acceleration method and a host computer based on shared memory.

[0004] On the one hand, the method provided by the embodiment of this application is applied to an industrial control system, and the industrial control system includes a host computer and a target device. The method is implemented based on the host computer and includes the steps of: 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-mode thread, determining a sending space address based on the first shared memory, and encapsulating the control data into multiple data frames; where the sending space address is a virtual address and has a mapping relationship with the first shared memory; the first user-mode thread requests to write the multiple data frames into the first shared memory pointed to by the sending space address by calling a system write function and switches to a first kernel-mode thread; the first kernel-mode thread sequentially writes the multiple data frames into the first shared memory pointed to by the sending space address and sends a data sending request to a second kernel-mode thread; the second kernel-mode thread, in response to the data sending request, 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 there is a data frame written, the data frame is read out from the first shared memory and written into the CAN send register area to send the data frame to the target device through the CAN bus.

[0005] In the above technical solution, according to the feature of sending data frame by frame during the CAN bus data transmission process, by leveraging shared memory and the assistance of multi-threading during the data frame transmission process, the transmission of data frames in the user space and the kernel space is accelerated, thereby improving the data transmission efficiency. At the same time, by creating a mapping relationship between the sending space address and the shared memory, the data frame can directly read and write physical memory during the transmission process, eliminating unnecessary context switching and data copying, improving the usage efficiency of memory and CPU, reducing energy consumption and the hardware requirements for the device. In addition, using shared memory can reduce the competition among multi-threads, improve the stability of the system, and reduce the potential network failure risk 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 data frames.

[0007] Based on the above technical solution, it is possible to reduce the switching between the first user-mode thread in the user space and the kernel space, reduce the number of system calls, thereby reducing system jitter and improving the anti-interference ability of the system.

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

[0009] In one implementation, the method further includes: after the second kernel-mode thread completes the processing of the data transmission request, it determines whether there are other unprocessed data transmission requests currently. If not, it automatically exits.

[0010] In one implementation, the method further includes: after the processing of the data sending request is completed, creating a second user-mode thread for receiving response data returned by the target device for the control data; the second user-mode thread initializes a second shared memory, determines a receiving space address based on the second shared memory, requests to read data in the second shared memory pointed to by the receiving space address by calling a system read function, and switches to a third kernel-mode thread; wherein, the receiving space address is a virtual address and has a mapping relationship with the second shared memory; the third kernel-mode thread sends the receiving space address to a fourth kernel-mode thread, and monitors a receiving queue, when a target message is written to the receiving queue, updates a read index, synchronizes the read index and the target message quantity to the user space, and switches back to the second user-mode thread; the second user-mode thread reads the response data from the second shared memory pointed to by the receiving space address based on the read index and the target message quantity, and when it is determined that the response data has not been completely sent, switches to the third kernel-mode thread, and again executes the step of monitoring the receiving queue; wherein, the target message is added to the receiving queue after the fourth kernel-mode thread writes the response data to the second shared memory pointed to by the receiving space address.

[0011] Based on the above technical solution, it is possible to achieve the acceleration of 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 implementation, the method for the fourth kernel-mode 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, reading the response data in the CAN receive register area, and writing the response data to the second shared memory pointed to by the receiving 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 each time, avoid invalid operations, and save system resources.

[0014] In one implementation, the method further includes creating a fifth kernel-mode thread, and the fifth kernel-mode thread determines a real-time data transmission rate according to the read operation of the first shared memory by the second kernel-mode thread and the write operation of the second shared memory by the fourth kernel-mode thread, and determines a baud rate adjustment method of the CAN register based on the relationship between the real-time data transmission rate and a 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: Wherein, F is the message frequency; Ps is the data packet size; Po is the protocol overhead; Br is the bus utilization rate; 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 a 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, and when the computer program is executed by a processor, the above method is implemented. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The schematic embodiments and descriptions thereof of the present application are used to explain the present application and do not constitute an improper limitation to the present application.

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

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

[0021] Figure 2 The flowchart of a CAN communication acceleration method based on shared memory provided by the embodiment of the present application is shown.

[0022] Figure 3 The flowchart of a CAN communication acceleration method based on shared memory provided by another embodiment of the present application is shown. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0023] The following will clearly and completely describe the technical solutions in the embodiments of the present application with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments of the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present application.

[0024] In the description of the embodiments of the present application, unless otherwise stated, "a plurality of" means two or more, and the "first", "second" and various numerical numbers are only for the convenience of description and do not limit the scope of the embodiments of the present application.

[0025] The features, structures, or characteristics in this application can be combined in one or more embodiments in any suitable manner. In various embodiments of this application, the magnitude of the serial numbers of the processes does not imply the order of execution. The order of execution of each process should be determined by its function and internal logic, and should not constitute any limitation to the implementation process of the embodiments of this application.

[0026] Some optional features in the embodiments of this application can, in certain scenarios, be implemented independently without relying on other features to solve corresponding technical problems and achieve corresponding effects. In some scenarios, they can also be combined with other features according to requirements.

[0027] In this application, unless otherwise specified, the same or similar parts between various embodiments can be referred to each other. In various embodiments of this application, if there is no special specification and logical conflict, the terms and / or descriptions between different embodiments are consistent and can be mutually referred to. The technical features in different embodiments can be combined to form new embodiments according to their internal logical relationships. The implementation manners of this application do not constitute a limitation to the protection scope of this application.

[0028] The embodiments of this application will be described in detail below with reference to the drawings.

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

[0030] Among them, the host computer 11 runs a Linux system and installs an application control program, which can be used to monitor and control each target device 12, etc. In an application example, the host computer 11 can be an industrial controller, and the target device 12 is a servo motor. It can be understood that the method provided by the embodiments of this application can also be applied to other Internet of Things application scenarios based on CAN bus communication.

[0031] In an example, the 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 based on the CAN bus to control the operating state of the target device 12, such as power on, power off, pause, adjust the operating mode, collect specified data, etc. When the target device 12 receives the control data, it executes the corresponding operation and returns response data, including but not limited to the current operating state, operating parameters, and monitoring data, etc., so that the host computer 11 can know the latest state of the target device or obtain the monitoring data according to the response data, and then perform 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 the data reported by the target device. The method provided by the embodiment of the present application optimizes the processes of the host computer 11 sending and receiving data respectively, so as to achieve communication acceleration.

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

[0034] S201, receive the control data for the target device.

[0035] In implementation, the control data can be automatically sent by the device control program or sent 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, in response to the control data, create a first user-mode thread.

[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-mode thread, and determine the sending space address based on the first shared memory.

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

[0040] In an example, according to the system requirements, the size of the first shared memory can be set to 16KB, and the first 8KB is the write buffer area. Initialize the content of the first shared memory.

[0041] S204, encapsulate the control data into multiple data frames.

[0042] In implementation, the first user-mode thread can 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-mode thread requests to write all the multiple data frames into the first shared memory pointed to by the sending space address by calling the system write function, and switches to the first kernel-mode thread.

[0044] In implementation, when the first user-mode thread is converting control data and finishes converting one data frame, it can call the system write function based on the sending space address to request writing the data frame into the first shared memory.

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

[0046] Optionally, the number of times the first user-mode thread calls the system write function is 1 time, that is, after completing the format conversion of the control data, by calling the system write function once, it requests to write all data frames into the first shared memory.

[0047] It should be noted that after the first user-mode thread successfully calls the system write function, it automatically switches to the kernel mode and becomes the first kernel-mode thread. Therefore, after reducing the number of calls to the system write function through the above embodiments, it can also reduce the number of times the first user-mode thread switches between the user mode and the kernel mode, thereby reducing system jitter and ensuring stable system operation.

[0048] S206. The first kernel-mode thread sequentially writes multiple data frames into the first shared memory pointed to by the sending space address, and sends a data sending request to the second kernel-mode 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 realizes writing 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-mode thread to send the data frames in the first shared memory pointed to by the sending space address.

[0051] The second kernel-mode 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-mode thread scans the first shared memory pointed to by the sending space address to determine whether there are data frames written.

[0053] In implementation, after receiving the data sending request, the second kernel-mode 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 writing situation of the data frames in the memory space.

[0054] When it is determined that there is a data frame to be written, step S208 is executed; otherwise, continue to scan the first shared memory area pointed to by the transmission space address lock.

[0055] S208, read the data frame from the first shared memory and write it into the CAN transmission register area.

[0056] In implementation, when the second kernel-mode thread determines that there is a data frame written into the first shared memory, it reads the data frame and writes it into the CAN transmission register area to send the data frame to the target device via the CAN bus.

[0057] To ensure the stable transmission of the data frame, the second kernel-mode thread will first judge the transmission status of the current CAN register before reading the data frame from the first shared memory, and judge whether the previous data frame has been sent. If it has been completed, read the data frame from the first shared memory. Specifically, the second kernel-mode thread can judge whether the previous frame has been sent by combining the transmission busy flag bit and the transmission buffer ready flag bit of the CAN register. When the flag bit information indicates that it is currently not busy and the transmission buffer is ready, it is determined that the previous frame has been sent and the CAN register is currently in an idle state. If the previous frame has not been sent, wait for the previous frame to be sent. In this way, packet loss can be prevented and communication quality can be guaranteed.

[0058] In an implementation of the present application, to avoid repeated transmission of data frames, the data transmission request carries the total number of data frames. Each time the second kernel-mode thread completes the transmission of a data frame, it updates the transmission index according to the number of sent data frames, and when it judges that the sent data volume indicated by the transmission index is consistent with the total number of data frames, it completes the processing of the data transmission request, and at the same time updates the write index based on the total number of data frames to ensure that these already sent packets will not be read repeatedly during the next read.

[0059] In an implementation, after the second kernel-mode thread completes the processing of the data transmission request, it determines whether there are other unprocessed data transmission requests currently. If not, it automatically exits, thereby releasing system resources in a timely manner. Specifically, after the second kernel-mode thread is created, it can be used to process the data transmission requests sent by multiple first kernel-mode threads, so as to reduce the number of threads in the system, reduce the difficulty of multi-thread management, thereby optimizing the system resource allocation and maintaining system stability.

[0060] Based on the method provided in the above embodiments, by optimizing the scheduling mechanism in the host computer system, the processing related to the user state and the processing process of the pure kernel state are effectively distinguished and processed by different threads, so that the process of writing the data frame into the first shared memory and reading it from the first shared memory can be carried out independently, thereby leveraging the parallel processing of the two threads to improve the transmission rate of the data frame in the system, and thus achieving communication acceleration.

[0061] Furthermore, by establishing a mapping between the sending space address and the first shared memory, the data frame can be directly read and written based on the real physical memory. Compared with the communication scheme based on SocketCAN, it reduces the number of data copies in the system memory and eliminates unnecessary context switches. It can not only further improve the data transmission efficiency, but also reduce the energy consumption and hardware requirements by improving the usage efficiency of the memory and CPU. In addition, using the first shared memory can reduce the competition among multiple processes, improve the stability of the system, and reduce the potential network failure risk caused by the network stack.

[0062] The host computer 11 enters the state of waiting to receive response data after completing the sending of the control data based on the Figure 2 method shown. Please refer to Figure 3 . The CAN communication acceleration method based on shared memory provided by the embodiments of this application further includes the following steps.

[0063] S301, After the data sending request is processed, create a second user-mode thread.

[0064] Among them, the second user-mode thread is used to receive the response data returned by the target device for the control data.

[0065] S302, The second user-mode thread initializes the second shared memory and determines the receiving space address based on the second shared memory.

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

[0067] S303, Request to read the data in the second shared memory pointed to by the receiving space address by calling the system read function, and switch to the third kernel-mode thread.

[0068] The second user-mode 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 mode and switches to the third kernel-mode thread.

[0069] S304, The third kernel-mode thread sends the receiving space address to the fourth kernel-mode thread and listens to the receiving queue.

[0070] S305, When the target message is written to the receiving queue, update the read index, synchronize the read index and the number of target messages to the user space, and switch back to the second user-mode thread.

[0071] Among them, the target message is added to the receiving queue after the fourth kernel-mode 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, it reads the response data in the CAN receive register area and writes it to the second shared memory pointed to by the receive space address. When the writing is completed, it writes a target message to the receive queue.

[0073] Among them, the target message is used to indicate that there is new data written to the second shared memory pointed to by the receive space address and the number of data written. In one example, a target message indicates that one 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. During the process of the target device sending response data to the host computer, the CAN register sequentially writes response frames to the CAN receive register area, and triggers an interrupt each time a response frame is written. This interrupt signal is received by the fourth kernel-mode thread to trigger subsequent read and write operations and update the receive queue.

[0075] In addition, to avoid multi-threaded race conditions and data inconsistency problems, before the third kernel-mode thread and the fourth kernel-mode thread perform read and write operations on the receive queue, they need to obtain a mutex lock, lock the receive queue, and unlock it after completing the corresponding read and write operations. Therefore, when the fourth kernel-mode thread completes writing a data frame, it may not be able to obtain the operation permission of the receive queue in time, so it needs to wait. During the waiting process, a write operation of a data frame may be triggered. In this way, when the fourth kernel-mode thread obtains the operation permission of the receive queue, there may be a situation where multiple target messages are written at one time.

[0076] To ensure that the third kernel-mode thread can accurately read the response frame, after the third kernel-mode thread monitors that there is a target message written to the receive queue and successfully locks the receive queue, it can update the read index by calculating the number of target messages in 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 number of target messages and the read index are stored in a predefined cache structure, so that the number of target messages and the read index can be copied to the user space.

[0077] At the same time, to prevent the system from reordering the first index and the read index, the third kernel-mode thread can call the wmb() function after updating the first index and the read index to set a write memory barrier, so as to ensure that the relevant indexes are 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-mode thread actively updates the index.

[0078] After the third kernel-mode thread returns the read index and the number of target messages to be received to user space, it returns to user space to switch to the second user-mode thread.

[0079] S306, the second user-mode thread reads response data from the second shared memory corresponding to the receive space address.

[0080] The second user-mode thread parses the content in the predefined cache structure to obtain the read index and the number of target messages, and reads the corresponding response frames from the second shared memory based on the read index and the target message data.

[0081] After the second user-mode thread obtains the corresponding response frames, it needs to process them, including parsing the response frames to obtain the corresponding response data, verifying the parsed response data to determine the integrity and correctness of the data, and analyzing all the obtained response data to determine whether the response data has been completely sent. If it is determined that the response data has not been completely sent, it enters the kernel space again, switches to the third kernel-mode thread, and returns to execute the step of listening to the receive queue to continue obtaining the remaining response data. If it is determined that the response data has been completely sent, it performs business processing on the response data according to the application program and returns the business processing result.

[0082] In an example, the method for determining whether the response data has been completely sent includes determining whether the response data has been completely sent according to the data requirements provided by the application program, where 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 solutions provided in the above embodiments, it is possible to optimize the communication for the target device to send data to the host computer. On the one hand, the fourth kernel-mode thread can monitor the interrupt information of the CAN register to determine whether there is data to be written to the second shared memory, and the third kernel-mode thread can monitor the receive queue to determine whether data needs to be read from the second shared memory, so that the process of response data from the CAN receive register area to the second shared memory and from the second shared memory to user space can be processed in parallel, thereby improving the transmission time of response data within the system. Similarly, directly forwarding data based on the second shared memory can reduce the number of data copies and eliminate unnecessary upper and lower layer switches, which can improve resource utilization.

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

[0085] It should be noted that in the above example, it is described based on a complete request-response. In the actual application process, 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 the method shown in Figure 2 and Figure 3 can still be used. The difference lies in the different triggering conditions for creating the first user-mode thread and the second user-mode thread. That is, in the scenario of unilateral data transmission, the creation of the corresponding user-mode thread can be triggered based on the set timing task.

[0086] In the actual application process, in order to facilitate users to update the application control program according to business requirements, in some application scenarios, the host computer can allow users to install and run self-developed application control programs or perform secondary development on the installed application control programs. In some embodiments of the present application, in addition to being able to control 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-mode thread, and when it monitors the creation of the first user-mode thread or the second user-mode thread, it starts the fifth kernel-mode 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 according to 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, and if it is less than the rate threshold, the baud rate parameter is decreased.

[0088] In one implementation, the fifth kernel-mode thread can calculate the real-time data transmission rate Rt during the operation of the application control program based on the following formula: where F is the message frequency; Ps is the packet size; Po is the protocol overhead; Br is the bus utilization rate; Mr is the error rate, that is, a coefficient determined based on the error rate and retransmission mechanism in actual communication.

[0089] 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 value of the historical real-time transmission rate. Among them, the rate threshold Rs is determined based on the historical real-time transmission rate. In one example, the maximum value of the real-time data transmission rate during operation can be set.

[0090] Based on this, for the application scenario where users can customize the development of the application control program, when setting the baud rate of the CAN register, users do not need to analyze the data transmission requirements, and can directly select the default value provided by the system or set any value, which reduces the development difficulty of users. Finally, the fifth kernel-mode thread dynamically adjusts the target baud rate according to the actual data transmission requirements, which can not only meet the data transmission requirements between the application control program and the target device, but also actively lower the baud rate when the user sets the baud rate too large, thereby reducing the signal attenuation generated during data transmission and improving the anti-interference ability. At the same time, setting the lower limit value of the CAN baud rate to the average value of the historical real-time transmission rate can avoid excessive downward adjustment and affect the service, and avoid frequent adjustment of the baud rate parameter. At the same time, dynamically setting Rs to the maximum value of the historical real-time transmission rate can avoid frequent adjustment of the baud rate parameter.

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

[0092] Another embodiment of the present application relates to a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the embodiments of the above method are implemented.

[0093] Those skilled in the art can understand that all or part of the steps in implementing the above method embodiments can be completed by instructing relevant hardware through a program. The program is stored in a storage medium, including several instructions for causing a device (which can be a single-chip microcomputer, a chip, etc.) or a processor to execute all or part of the steps of the methods of the various embodiments of the present invention. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.

[0094] The above are all the preferred embodiments of the present application. The protection scope of the present application is not limited by this. Therefore, all equivalent changes made according to the structure, shape, and principle of the present application should be covered within the protection scope 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, the industrial control system includes a host computer and a target device, and the method is implemented based on the host computer, including the steps of: receiving control data for the target device; In response to the control data, creating a first user state thread; Initialize a first shared memory based on the first user state thread, determine a sending space address based on the first shared memory, and encapsulate the control data into a plurality of 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 writes the multiple data frames into the first shared memory pointed to by the sending space address by calling a system write function request, 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 to 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 is determined 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, and when the receiving queue writes the target message, updates the read index, 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 receiving 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 receiving queue again; wherein, the target message is added to the receiving queue after the fourth kernel-state thread writes the response data to the second shared memory pointed to by the receiving 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 according to 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 of 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 data 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 described in 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 transmission and display system and method based on shared memory

    CN118245247A

  • Data processing method and device, equipment and storage medium

    CN118964063A

  • Parallel processing of thread groups

    US20230086989A1

  • Freedom from Interference for Aggregated Communication Channel Handling Using Event-Based Processor Extensions

    US20230117223A1

Cited By

  • Method and device for realizing bidirectional transceiving of FPGA and CPU data frames based on shared memory mechanism

    CN121166397A