Method and system for serial port equipment transmission
By configuring child processes and shared memory for serial port devices and binding high priority on multi-core CPUs, the packet loss problem in multi-channel serial port device transmission is solved, improving data transmission efficiency and simplifying the deployment process.
Patent Information
- Application Number
- CN202511317249.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-16
- Publication Date
- 2025-12-30
AI Technical Summary
Existing technologies are prone to data packet loss in multi-channel serial port transmission, especially when there are too many serial port devices. The buffer size limit can cause data to be lost due to failure to read in time, or the use of filtered serial port drivers increases the deployment difficulty.
Each serial port device is configured with a corresponding subprocess and shared memory, and bound to a CPU physical core. The subprocess is set to high priority, and the main process controls the parallel reception of data and storage in shared memory. By utilizing the parallelism and high priority characteristics of multi-core CPUs, data transmission efficiency is improved.
It effectively solves the packet loss problem caused by untimely reception of multiple serial ports, improves data transmission efficiency, reduces the overhead of inter-process switching, and simplifies the deployment process.
Smart Images

Figure CN121233362A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of embedded technology, and in particular to methods and systems for serial port device transmission. Background Technology
[0002] There are currently two methods for handling active transmission from multiple serial port devices.
[0003] One approach is to use multithreading, creating a separate thread for each serial port device to receive serial data. The main thread focuses solely on core business logic. The advantage of this method is that data received by different threads resides within the same process, making data access easy. The disadvantage is that data loss can easily occur when there are many serial port devices, typically four or more. This is because the receive buffer size in the serial port driver layer is limited, generally only 4KB. If the system cannot read data from the buffer in time, subsequent data will overwrite the unread data, resulting in packet loss.
[0004] Another approach is to use a filtered serial port driver. Data from the serial device first goes to the filtered serial port driver, which allocates a sufficiently large buffer to cache all serial port data, preventing packet loss due to slow read speeds from the upper-layer application. The advantage of this method is that it completely solves the problem of packet loss across multiple serial ports. However, the disadvantages are also obvious: it requires the development of an additional filtered serial port driver. Driver signing and installation increase workload and deployment difficulty, and it can also affect other serial devices connected to the computer. Summary of the Invention
[0005] This invention provides a method and system for serial port device transmission, which can solve the packet loss problem caused by untimely reception of multiple serial ports in the prior art. Improving these problems will enhance data transmission efficiency and avoid data packet loss.
[0006] In one aspect of the present invention, a method for transmission via a serial port device is provided. The method includes: when a serial port device is connected, configuring a corresponding subprocess and shared memory for the serial port device, binding the subprocess to a CPU physical core, configuring the priority of the subprocess in the corresponding CPU physical core to a preset high priority; and having a main process control all the subprocesses to receive serial port device data transmitted by the corresponding serial port device in parallel, and storing the serial port device data in the corresponding shared memory.
[0007] In another aspect of the present invention, a system for serial port device transmission is provided. The system includes: a subprocess configured to access a corresponding serial port device and bound to a corresponding CPU physical core, wherein the subprocess is configured to have a preset high priority within the corresponding CPU physical core; shared memory configured to correspond to a serial port device and connected to the subprocess corresponding to the serial port device; and a main process configured to control all the subprocesses to receive serial port device data transmitted by the corresponding serial port device in parallel, and to store the serial port device data in the corresponding shared memory.
[0008] According to this invention, corresponding sub-processes and shared memory are configured for each of the at least one connected serial port device. The sub-processes are bound to CPU physical cores, and their priority within the bound CPU physical core is configured as a preset high priority. This ensures that the sub-processes are executed only on the bound CPU core, reducing the overhead of inter-process switching. The main process checks whether the preset labels of the shared memory corresponding to all serial port devices are correct. If correct, after all sub-processes have started, the sub-processes receive serial port device data transmitted by the corresponding serial port devices in parallel and store the serial port device data in the corresponding shared memory. In this way, the parallelism of multi-core CPUs and the high priority of sub-processes can be fully utilized to improve data transmission efficiency and solve the packet loss problem caused by untimely reception of multiple serial ports. Attached Figure Description
[0009] Figure 1 This is a flowchart of a method for serial port device transmission according to an embodiment of the present invention; Figure 2 This is a flowchart illustrating the specific steps of a method for serial port device transmission according to an embodiment of the present invention. Figure 3 This is a schematic diagram of a system for serial port device transmission according to an embodiment of the present invention. Detailed Implementation
[0010] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.
[0011] Since the buffer size of serial port devices at the operating system layer is immutable and transparent to application layer programs, existing technologies use multi-threading for active transmission of multiple serial port devices. However, when there are too many serial port devices, data loss can easily occur if data is not read from the buffer in a timely manner. Using a filtered serial port driver for active transmission of multiple serial port devices increases the deployment difficulty.
[0012] To address at least the aforementioned technical problems, this disclosure provides a method for serial port device transmission. For each of the at least one connected serial port device, a corresponding sub-process and shared memory are configured. The sub-processes are bound to CPU physical cores, and their priority within the bound CPU physical cores is configured to a preset high priority. A main process controls all the sub-processes to receive serial port device data transmitted by the corresponding serial port device in parallel, and stores the serial port device data in the corresponding shared memory. In this way, the parallelism of multi-core CPUs and the high priority of sub-processes are fully utilized to improve data transmission efficiency and solve the packet loss problem caused by untimely reception of multiple serial port signals.
[0013] In the following, the technical solutions according to this disclosure will be described with reference to specific embodiments and in conjunction with the accompanying drawings.
[0014] Figure 1 This is a flowchart illustrating a method 100 for serial port device transmission according to an embodiment of the present disclosure. (Refer to...) Figure 1 The method 100 includes the following steps 102 to 104.
[0015] In step 102, a corresponding subprocess and shared memory are configured for each of the at least one connected serial port device. The subprocess is bound to a CPU physical core, and the priority of the subprocess in the corresponding CPU physical core is configured to a preset high priority.
[0016] In this way, the priority of each child process is increased, giving it a high priority within the bound CPU physical core. This fully utilizes the parallel characteristics of multi-core CPUs to improve system efficiency. Furthermore, the child process is only executed on the bound CPU core, reducing the overhead of process switching.
[0017] In some embodiments, the device name of the serial port device is used to name the corresponding shared memory. In some embodiments, a preset tag is stored in the first preset length bytes of the shared memory.
[0018] In step 104, the main process controls all the child processes to receive the serial port device data transmitted by the corresponding serial port device in parallel, and stores the serial port device data in the corresponding shared memory.
[0019] In some embodiments, the main process checks whether the preset labels of the shared memory corresponding to all the serial port devices are correct. If correct, after all the child processes have started, the main process controls all the child processes to receive the serial port device data transmitted by the corresponding serial port device in parallel. In this way, it can be ensured that each serial port device has a corresponding shared memory, and the problem of packet loss during data transmission from multiple serial port devices can be avoided when the size of the memory area where all shared memory is located cannot be modified.
[0020] In some embodiments, after all child processes have started, the preset tags in the shared memory corresponding to all serial port devices are cleared. In this way, by adding tags to the shared memory corresponding to the serial port devices, and then clearing the tags in the shared memory corresponding to the serial port devices after the main process checks that the tags are correct, it is possible to ensure that the data transmitted by the serial port devices has storage space while preventing tags from occupying shared memory during data transmission.
[0021] In some embodiments, the main process maintains a data processing dictionary. This dictionary stores information about each serial port device and its corresponding processed data length. If the data length in the shared memory corresponding to a serial port device exceeds the processed data length in the data processing dictionary, the main process processes the data in the shared memory and updates the processed data length in the data processing dictionary. In this way, the main process can discover unprocessed data in the shared memory using the data processing dictionary, improving the processing efficiency of serial port device data.
[0022] The method described above for serial port device transmission dynamically binds the CPU core and serial port using the API provided by Windows, without involving modifications to the bootloader. It also solves the difficulties of synchronizing the main process with multiple serial port child processes and mutual exclusion among multiple serial port child processes.
[0023] For example, the main APIs used in Windows systems include: SetProcessAffinityMask, which sets the affinity of a process for a specific CPU core when selecting a CPU core for a child process; SetPriorityClass, which sets the priority of a child process to ABOVE_NORMAL_PRIORITY_CLASS; CreateFileMapping, which creates named shared memory; and CreateProcess, which creates a child process.
[0024] The following description, through examples, illustrates application scenarios of the method and system for serial port device transmission according to embodiments of the present invention.
[0025] Figure 2 This is a flowchart illustrating the specific steps of a method for serial port device transmission according to an embodiment of the present invention, which includes steps 202 to 210.
[0026] In step 202, a pair of child processes and shared memory are created for each serial port device. The name of the shared memory is the name of the serial port device, and a size of 4M is allocated. After the child process enters, it sets the first 4 bytes of the shared memory to a tag of 0x55AAAA55.
[0027] Specifically, the shared memory is a 4MB system memory area named after the corresponding serial port device name. After a child process enters, the first 4 bytes of the corresponding shared memory are set to a tag of 0x55AAAA55 to facilitate synchronization of all child processes before the main process starts to ensure they are running successfully. After checking, the tag is set to 0. The first (4MB-4B) bytes of the shared memory are the data area, and the last 4 bytes store the actual length of data received in the data area.
[0028] In step 204, a physical core of the CPU is selected for each child process.
[0029] In step 206, the kernel priority of each child process is set to above_normal.
[0030] In step 208, the main process checks whether the first 4 bytes of the shared memory tag of all serial port devices are correct. After all child processes have started, the shared memory tag is cleared.
[0031] In step 210, the main process executes its business logic.
[0032] In this embodiment, the main process maintains a dictionary that stores the corresponding serial port device and the length of the data that has been processed. When the length of the data in the shared memory is greater than the length of the data that has been processed, it means that there is new data to be processed. After the processing is completed, the length of the data that has been processed is updated.
[0033] The method for serial port device transmission in this invention can be applied to all multi-serial port communication scenarios based on Windows systems, such as aging tests, stress tests, and system-level (SLT) tests.
[0034] Figure 3 This is a schematic diagram illustrating a system 300 for serial port device transmission according to an embodiment of the present invention. (Refer to...) Figure 3 The system includes child process 302, shared memory 304, and main process 306.
[0035] At least one child process 302 is configured to connect to at least one corresponding serial port device, be bound to a corresponding CPU physical core, and have its priority in the bound CPU physical core set to a preset high priority. In some embodiments, the preset high priority is above_normal.
[0036] Shared memory 304 is configured to be coupled to a subprocess corresponding to the serial port device.
[0037] The main process 306 is configured to control all the child processes to receive serial port device data transmitted by the corresponding serial port device in parallel, and to store the serial port device data in the corresponding shared memory.
[0038] In some embodiments, a preset tag is stored in the first preset length bytes of the shared memory, and the main process is configured to check whether the preset tag of the shared memory corresponding to all the serial port devices is correct. If it is correct, after all the child processes have started, the main process controls all the child processes to receive the serial port device data transmitted by the corresponding serial port device in parallel.
[0039] In some embodiments, the main process is further configured to clear the preset tags in the shared memory corresponding to all the serial port devices after all the child processes have started. Furthermore, the main process is further configured to maintain a data processing dictionary, which stores each serial port device and its corresponding processed data length. If the data length in the shared memory corresponding to a serial port device is greater than the processed data length in the data processing dictionary corresponding to that serial port device, then the data in the shared memory is processed, and the processed data length in the data processing dictionary corresponding to that serial port device is updated.
[0040] In summary, the method and system for serial port device transmission provided by this invention configure corresponding child processes and shared memory for the serial port device when it is connected, bind the child processes to CPU physical cores, and configure the priority of the child processes in the corresponding CPU physical cores to a preset high priority. This ensures that the child processes are only executed on the bound CPU cores, reducing the overhead of process switching. The main process checks whether the preset labels of the shared memory corresponding to all serial port devices are correct. If correct, after all child processes have started, it controls all child processes to receive the serial port device data transmitted by the corresponding serial port device in parallel and stores the serial port device data in the corresponding shared memory. In this way, the parallelism of multi-core CPUs and the high priority of child processes can be fully utilized to improve data transmission efficiency and solve the packet loss problem caused by untimely reception of multiple serial ports.
[0041] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for serial device transmission, characterized in that, Comprising: configuring corresponding sub-processes and shared memories for the accessed at least one serial port device respectively, binding CPU physical cores for the sub-processes, and configuring the priority of the sub-processes in the bound CPU physical cores as a preset high priority; and controlling all the sub-processes by a master process to receive serial port device data transmitted by the corresponding serial port devices in parallel, and storing the serial port device data into the corresponding shared memories.
2. The method of claim 1, wherein, Configuring corresponding sub-processes and shared memories for the accessed at least one serial port device respectively comprises: naming the corresponding shared memories using the device name of the serial port device.
3. The method of claim 1, wherein, Configuring corresponding sub-processes and shared memories for the accessed at least one serial port device respectively comprises: storing a preset label at the front preset length bytes of the shared memory.
4. The method of claim 3, wherein, Controlling all the sub-processes by a master process to receive serial port device data transmitted by the corresponding serial port devices in parallel comprises: checking by the master process whether the preset labels of the shared memories corresponding to all the serial port devices are correct; and if correct, controlling all the sub-processes to receive serial port device data transmitted by the corresponding serial port devices in parallel after all the sub-processes are started.
5. The method of claim 4, wherein, Further comprising: emptying the preset labels in the shared memories corresponding to all the serial port devices after all the sub-processes are started.
6. The method of claim 1, wherein, Further comprising: maintaining by the master process a data processing dictionary for storing each serial port device and the corresponding processed data length; and if the data length of the shared memory corresponding to a serial port device is greater than the processed data length of the data processing dictionary corresponding to the serial port device, processing by the master process the data in the shared memory and updating the processed data length of the data processing dictionary corresponding to the serial port device. Comprising:
7. A system for serial device transmission, characterized by, at least one sub-process configured to access corresponding at least one serial port device respectively, bound to a corresponding CPU physical core, and configured to have the priority of the sub-process in the bound CPU physical core as a preset high priority; a shared memory configured to be coupled to the sub-process corresponding to the serial port device; and a master process configured to control all the sub-processes to receive serial port device data transmitted by the corresponding serial port devices in parallel, and store the serial port device data into the corresponding shared memories. The preset label is stored at the front preset length bytes of the shared memory, and the master process is configured to: check whether the preset labels of the shared memories corresponding to all the serial port devices are correct; and 8. The system of claim 7, wherein, if correct, control all the sub-processes to receive serial port device data transmitted by the corresponding serial port devices in parallel after all the sub-processes are started. The master process is configured to: empty the preset labels in the shared memories corresponding to all the serial port devices after all the sub-processes are started.
9. The system of claim 8, wherein, The master process is further configured to: maintain a data processing dictionary for storing each serial port device and the corresponding processed data length; and 10. The system of claim 7, wherein, If the data length of the shared memory corresponding to the serial port device is greater than the processed data length corresponding to the serial port device in the data processing dictionary, the data in the shared memory is processed, and the processed data length corresponding to the serial port device in the data processing dictionary is updated.