Mastering multi-serial port concurrent cooperative communication method, electronic equipment and storage medium
By constructing a buffered circular queue and combining it with priority sequences and interrupt mechanisms, the problem of data loss in traditional serial communication is solved, achieving efficient data transmission and system stability for multi-serial port concurrent communication.
Patent Information
- Application Number
- CN202411943129.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-11-18
- Estimated Expiration
- 2044-12-27
AI Technical Summary
In traditional serial communication, when multiple serial ports communicate concurrently, the data generation rate varies greatly. The conventional sequential processing mode cannot keep up with the pace, resulting in buffer congestion, high risk of data loss, and impact on production safety and system stability.
A buffered circular queue is constructed, and the queue length is dynamically adjusted according to the serial communication speed and data processing speed. Combined with a preset priority sequence and interrupt mechanism, high-priority data is processed first to avoid buffer overflow.
It improves the reliability of data transmission, ensures timely processing of high-priority data, avoids data loss, and enhances system stability and production safety.
Smart Images

Figure CN119807110B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data communication, in particular to a method for master control of concurrent and collaborative communication of multiple serial ports, an electronic device and a storage medium. BACKGROUND
[0002] In modern society, the fields of industrial automation, intelligent Internet of Things, etc. are booming, and it is very common for devices to integrate multiple serial ports to connect different peripherals and work collaboratively. In industrial production lines, the master control device needs to communicate with a large number of sensors and actuators via serial ports to monitor and control production; in intelligent buildings, the central control needs to interface with various environmental monitoring devices to maintain a good state inside the building.
[0003] However, the traditional serial port communication scheme has serious defects when multiple serial ports communicate concurrently. The devices connected by different serial ports have different functions, and the data generation rates differ greatly, with some devices instantaneously generating a large amount of data. The conventional sequential processing mode cannot keep up with the pace, and the serial port buffer is quickly congested. Once overflow occurs, a large amount of data is at risk of being lost, resulting in the loss of critical information and seriously affecting production safety and system stability. SUMMARY
[0004] The present application aims to at least solve one of the technical problems existing in the prior art. To this end, the present application provides a method for master control of concurrent and collaborative communication of multiple serial ports, which can avoid data loss during communication of serial ports and improve data transmission reliability.
[0005] In a first aspect, the present application provides a method for master control of concurrent and collaborative communication of multiple serial ports, comprising:
[0006] obtaining a serial port communication speed and a data processing speed;
[0007] constructing a circular buffer queue according to the serial port communication speed and the data processing speed;
[0008] obtaining at least one data information transmitted from a serial port group; wherein the data information comprises a corresponding serial port identifier;
[0009] generating an interrupt instruction according to the data information, and sequentially storing the data information in corresponding positions in the circular buffer queue according to the serial port identifier of each data information, a preset priority sequence and a write pointer of the circular buffer queue; wherein the priority sequence represents the processing priority of each serial port in the serial port group, and the interrupt instruction is used to interrupt the currently executing task;
[0010] taking out the data information from the corresponding positions in the circular buffer queue according to a read pointer of the circular buffer queue, and performing data processing on the data information.
[0011] According to the master multi-port concurrent cooperative communication method of the first aspect of the present application, at least the following beneficial effects are achieved: first, by detecting and analyzing the hardware parameters of each serial port and the data transmission situation during actual operation, the average serial port communication speed of the serial port group is accurately obtained, and the processor performance of the master control device, the load of other tasks currently running and other situations are evaluated to comprehensively calculate the data processing speed. According to the obtained serial port communication speed and data processing speed, the length, capacity and other parameters of the cache circular queue are determined according to certain rules and algorithms to create a suitable cache circular queue structure. Each serial port is in a state of listening to the transmission of external data at all times, and once data is received, the data from which serial port it comes from is recorded, and the data information is completely captured. When the serial port receives data information, the hardware immediately triggers an interrupt mechanism to generate an interrupt instruction, which is passed to the master processor to pause the non-critical tasks being executed, and to preferentially respond to the reception and caching of data information. Then, according to the serial port identifier carried by the data information, the pre-set priority sequence is searched to determine the priority of the serial port data, and the data information of high priority is preferentially stored in the corresponding position of the cache circular queue according to the write pointer indication. Subsequently, the system extracts the data information in turn according to the read pointer of the cache circular queue, and performs corresponding data processing on the data information. In the present application, the cache circular queue acts as a data "transit station" and can temporarily store the received data flexibly according to the different communication speeds of each serial port and the data processing speed of the system, avoiding the overflow of the buffer and the loss of data caused by the instantaneous influx of a large amount of data from some serial ports, and improving the reliability of data transmission. At the same time, the pre-set priority sequence can distinguish the importance and urgency of data from different serial ports, ensure that high-priority data is stored in the cache circular queue first, and can be processed preferentially subsequently, avoid high-priority data being "flooded" by low-priority data due to regular sequential processing, and ensure the timeliness of important data.
[0012] According to some embodiments of the first aspect of the present application, the cache circular queue is constructed according to the serial port communication speed and the data processing speed, comprising:
[0013] An initial circular queue is constructed according to the serial port communication speed and the data processing speed.
[0014] A pre-set redundancy ratio is obtained.
[0015] The initial circular queue is adjusted according to the redundancy ratio to obtain a cache circular queue.
[0016] According to some embodiments of the first aspect of the present application, after the step of adjusting the initial circular queue according to the redundancy ratio to obtain a cache circular queue, the method further comprises:
[0017] acquire a first total data amount of the data information received from each serial port and a second total data amount of the data information taken out from the buffer circular queue in a preset first time length;
[0018] obtain a third total data amount according to the first total data amount corresponding to each serial port;
[0019] update the serial port communication speed according to the first time length and the third total data amount;
[0020] update the data processing speed according to the first time length and the second total data amount;
[0021] adjust the buffer circular queue according to the redundancy ratio, the updated serial port communication speed and the data processing speed.
[0022] According to some embodiments of the first aspect of the application, the adjusting the buffer circular queue according to the redundancy ratio, the updated serial port communication speed and the data processing speed comprises:
[0023] obtaining an initial queue length according to the buffer circular queue;
[0024] calculating a target queue length according to the redundancy ratio, the updated serial port communication speed and the data processing speed;
[0025] maintaining the original buffer circular queue when an absolute difference between the initial queue length and the target queue length is less than or equal to a preset adjustment threshold;
[0026] updating the buffer circular queue according to the target queue length when the absolute difference between the initial queue length and the target queue length is greater than the adjustment threshold.
[0027] According to some embodiments of the first aspect of the application, the sequentially storing the data information in the corresponding positions of the buffer circular queue according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the buffer circular queue comprises:
[0028] obtaining a first serial port identifier and a second serial port identifier corresponding to the current data information and the new data information respectively when the data information is being stored in the buffer circular queue and the new data information is received from the serial port group;
[0029] obtaining a first priority corresponding to the first serial port identifier and a second priority corresponding to the second serial port identifier according to the first serial port identifier, the second serial port identifier and the priority sequence;
[0030] When the first priority is greater than or equal to the second priority, according to the write pointer of the cache circular queue, the current data information is continuously stored into the cache circular queue;
[0031] When the first priority is less than the second priority, according to the write pointer of the cache circular queue, the new data information is stored into the cache circular queue until the new data information is completely stored, and then the last data information is stored into the cache circular queue.
[0032] According to some embodiments of the first aspect of the present application, after the step of generating an interrupt instruction according to the data information, and storing the data information into the corresponding position of the cache circular queue according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the cache circular queue, the method further comprises:
[0033] Interval a preset second duration, and count a fourth total amount of data received from each serial port;
[0034] Calculate the sum of the fourth total amount of data corresponding to all serial ports to obtain a fifth total amount of data;
[0035] When the ratio of the fourth total amount of data of one of the serial ports to the fifth total amount of data is less than a preset first threshold value, the priority of the serial port is downgraded, and the priority sequence is adjusted;
[0036] When the ratio of the fourth total amount of data of one of the serial ports to the fifth total amount of data is greater than a preset second threshold value, the priority of the serial port is upgraded, and the priority sequence is adjusted.
[0037] According to some embodiments of the first aspect of the present application, the step of storing the data information into the corresponding position of the cache circular queue according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the cache circular queue comprises:
[0038] Obtaining a preset verification algorithm;
[0039] Generating a first verification value related to the verification algorithm according to the verification algorithm;
[0040] Storing the data information and the corresponding first verification value into the corresponding position of the cache circular queue according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the cache circular queue;
[0041] Correspondingly, the data information is taken out from the corresponding position of the cache circular queue according to the read pointer of the cache circular queue, and the data information is processed, comprising:
[0042] The data information and the corresponding second check value are taken out from the corresponding position of the cache circular queue according to the read pointer of the cache circular queue.
[0043] When the first check value is consistent with the second check value, the data information is processed.
[0044] According to some embodiments of the first aspect of the application, after the step of taking out the data information and the corresponding second check value from the corresponding position of the cache circular queue according to the read pointer of the cache circular queue, further comprising:
[0045] When the first check value is inconsistent with the second check value, a retransmission instruction is sent to the corresponding serial port according to the serial port identifier of the data information, and the cumulative failure number of the corresponding serial port is recorded.
[0046] When the cumulative failure number of the first serial port in the serial port group exceeds a preset number threshold, a standby serial port in the serial port group is started to replace the first serial port, or the data information sent by the first serial port is stopped.
[0047] Secondly, the application also provides an electronic device, comprising:
[0048] At least one memory;
[0049] At least one processor;
[0050] At least one program;
[0051] The program is stored in the memory, and the processor executes at least one program to implement the master multi-serial port concurrent cooperative communication method as described in any one of the embodiments of the first aspect.
[0052] Thirdly, the application also provides a computer readable storage medium, which stores computer executable signals, and the computer executable signals are used to execute the master multi-serial port concurrent cooperative communication method as described in any one of the embodiments of the first aspect.
[0053] Additional aspects and advantages of the application will be in part apparent and in part pointed out hereinafter. BRIEF DESCRIPTION OF DRAWINGS
[0054] Additional aspects and advantages of this application will become apparent and readily understood in conjunction with the following description of the embodiments, in which:
[0055] Figure 1 A flowchart of the master control multi-serial port concurrent collaborative communication method provided in this application;
[0056] Figure 2 For this application Figure 1 Flowchart for step S120;
[0057] Figure 3 For this application Figure 2 Flowchart following step S230;
[0058] Figure 4 For this application Figure 3 Flowchart for step S350;
[0059] Figure 5 For this application Figure 1 Flowchart for step S140;
[0060] Figure 6 For this application Figure 1 Flowchart following step S140;
[0061] Figure 7 For this application Figure 1 Flowcharts for steps S140 and S150;
[0062] Figure 8 For this application Figure 7 Flowchart for step S730. Detailed Implementation
[0063] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting this application.
[0064] In the description of this application, it should be understood that the orientation descriptions, such as up, down, front, back, left, right, etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application.
[0065] In the description of the present application, if the first, second are described for the purpose of distinguishing technical features, it cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated or the sequence of the technical features indicated.
[0066] In the description of the present application, unless otherwise explicitly limited, the words such as setting, installing, connecting and the like should be broadly understood, and the person skilled in the art can reasonably determine the specific meaning of the above words in the present application in combination with the specific content of the technical scheme.
[0067] In modern society, the fields of industrial automation, intelligent Internet of Things, etc. are booming, and it is very common for devices to integrate multiple serial ports to connect different peripherals and work together. On the industrial production line, the host device needs to communicate with a large number of sensors and actuators through serial ports to monitor and control production; in the intelligent building, the central control needs to interface with various environmental monitoring devices to maintain a good state inside the building.
[0068] However, the traditional serial communication scheme has serious defects when multiple serial ports are concurrently communicating. The devices connected by different serial ports have different functions, and the data generation rates differ greatly. Some devices can suddenly flood with a large amount of data, and the conventional sequential processing mode cannot keep up with the pace. The serial port buffer is quickly congested, and once it is overflowed, a large amount of data is at risk of being lost, which causes the loss of critical information and seriously affects production safety and system stability.
[0069] Based on this, the present application provides a method for concurrently and cooperatively communicating multiple serial ports, an electronic device and a storage medium to solve the technical problems mentioned above. The technical solutions provided by the present application are described in detail one by one as follows.
[0070] In a first aspect, with reference to Figure 1 The present application provides a method for concurrently and cooperatively communicating multiple serial ports, which can include but is not limited to the following steps:
[0071] Step S110: Obtain the serial communication speed and the data processing speed;
[0072] Step S120: According to the serial communication speed and the data processing speed, a circular buffer queue is constructed;
[0073] Step S130: Obtain at least one data information transmitted from the serial port group; wherein the data information includes a corresponding serial port identifier;
[0074] Step S140: According to the data information, an interrupt instruction is generated, and according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the circular buffer queue, the data information is sequentially stored in the corresponding position of the circular buffer queue; wherein the priority sequence is used to represent the processing priority of each serial port in the serial port group, and the interrupt instruction is used to interrupt the currently executing task.
[0075] Step S150: According to the read pointer of the cache circular queue, the data information is taken out from the corresponding position of the cache circular queue, and the data information is processed.
[0076] In steps S110 to S150, first, by detecting and analyzing the serial port hardware parameters and the actual running data transmission situation, the average serial port communication speed of the serial port group is accurately obtained, and the processor performance of the host device, the current running other task load and the like are evaluated, and the data processing speed is comprehensively calculated. According to the obtained serial port communication speed and data processing speed, the length, capacity and the like of the cache circular queue are determined according to certain rules and algorithms, and a suitable cache circular queue structure is created. Each serial port is in a state of listening to external data transmission at any time, and once data is received, the data from which serial port it comes from is recorded, and the data information is completely captured. When the serial port receives data information, the hardware immediately triggers an interrupt mechanism to generate an interrupt instruction, which is transmitted to the host processor to suspend the non-critical task being executed, and the data information receiving and caching operation is preferentially responded, and then according to the serial port identifier carried by the data information, the preset priority sequence is searched, the priority of the serial port data is judged, and the high-priority data information is stored in the cache circular queue according to the write pointer. Subsequently, the system will extract the data information according to the read pointer of the cache circular queue, and process the data information. In this application, the cache circular queue constructed acts as a data "transit station", which can temporarily store the received data according to the different communication speeds of each serial port and the data processing speed of the system, avoid the situation that the buffer is directly overflowed and the data is lost due to the instantaneous influx of a large amount of data from part of the serial port, and improve the data transmission reliability. At the same time, the preset priority sequence can distinguish the importance and urgency of different serial port data, can ensure that high-priority data is stored in the cache circular queue in priority, and can also be taken out and processed in priority subsequently, avoid that high-priority data is "flooded" by low-priority data due to long-time waiting caused by regular processing in sequence, and ensure the timeliness of important data.
[0077] It should be noted that in step S140, when the serial port receives data, the hardware immediately triggers the interrupt mechanism to generate an interrupt instruction, which is passed to the host processor to suspend the non-critical task being executed, and to respond to the data reception and caching operation in priority. Then according to the serial port identification carried by the data, the preset priority sequence is searched to judge the priority of the serial port data. If there is new data arriving from a high-priority serial port, even if the current write pointer is processing the data storage operation of a low-priority serial port, the low-priority operation will be suspended to ensure that the high-priority data is stored in the corresponding position of the cache circular queue according to the write pointer indication, and before storing the data, a check value can be added to the data to ensure the integrity of the data. If the queue is almost full, part of the low-priority data will be cleared according to the priority strategy to free up space to ensure that new data can be successfully stored.
[0078] Referring to Figure 2 It can be understood that in step S120, the following steps can be included but are not limited to:
[0079] Step S210: constructing an initial circular queue according to the serial port communication speed and the data processing speed;
[0080] Step S220: obtaining a preset redundancy ratio;
[0081] Step S230: adjusting the initial circular queue according to the redundancy ratio to obtain a cache circular queue.
[0082] In steps S210 to S230, the rate of data generation and transmission is not always stable. In some specific moments or scenarios, a large amount of data may suddenly flow into the serial port, far exceeding the average data flow. The system will set a redundancy ratio in advance, which is often obtained through a large number of preliminary tests, in-depth analysis of the past running data of the equipment, and combined with industry experience. For example, in industrial production scenarios with large data flow fluctuations and frequent electromagnetic interference, to prevent sudden conditions from causing a large influx of data, the redundancy ratio may be set to 20% to 30%. In a relatively stable office automation environment, the redundancy ratio may only need to be maintained at around 10%. After obtaining the redundancy ratio, queue adjustment work is carried out, and the additional queue capacity needed is accurately calculated through multiplication, that is, "additional capacity = initial circular queue length x redundancy ratio". According to the result, the storage space of the initial circular queue is flexibly expanded in memory, and the head and tail pointers are accurately moved to the position of the new capacity, and the capacity identifier, index range and other key attributes of the queue are updated simultaneously, thereby successfully creating a cache circular queue. Setting a redundancy ratio can reserve a certain amount of cache space to deal with such data flow peaks, ensuring that all data can be properly cached during data flow peaks, avoiding data loss. At the same time, the additional space provided by the redundancy ratio can serve as a buffer mechanism to accommodate possible duplicate data, error data or additional data that needs to be retransmitted when these problems occur, ensuring the stable operation of the system and reducing the risk of system crashes or errors due to data abnormalities.
[0083] Referring to Figure 3 It can be understood that after step S230, the following steps can also be included, but are not limited to:
[0084] Step S310: After a preset first time interval, obtain a first total amount of data received from each serial port and a second total amount of data taken out from the buffer circular queue;
[0085] Step S320: Obtain a third total amount of data according to the first total amount of data corresponding to each serial port;
[0086] Step S330: Update the serial communication speed according to the first time interval and the third total amount of data;
[0087] Step S340: Update the data processing speed according to the first time interval and the second total amount of data;
[0088] Step S350: Adjust the cache circular queue according to the redundancy ratio, the updated serial communication speed and the data processing speed.
[0089] In step S310, every preset first time length, start statistics from each serial port received data information of the first data total amount. This statistical process can be realized by adding a counter in each serial port data receiving program, whenever there is a new data block is received, the corresponding counter will increase the size of the data block. At the same time, in the data taking out end of the buffer circular queue, there is also a counter to record the second data total amount of data information taken out from the buffer circular queue in the same time length, this counter will also be added according to the size of each data block taken out.
[0090] In step S320 to step S330, the first data total amount corresponding to each serial port is summed up to obtain the third data total amount. Then divide the third data total amount by the first time length, the updated serial port communication speed can be obtained. This updated speed is calculated based on the actual received data amount in the recent period of time, which can more accurately reflect the current actual data transmission of the serial port.
[0091] In step S340, similarly, the second data total amount is divided by the first time length to obtain the updated data processing speed. Through the above steps, the current data processing capacity of the system can be reflected in real time. Considering that the system load and the efficiency of the processing algorithm may change over time, such updating can make the system have a more accurate grasp of its own data processing speed.
[0092] In step S350, according to the updated serial port communication speed and data processing speed, and the pre-set redundancy ratio, the size and related parameters of the buffer circular queue are re-adjusted according to the method of constructing the buffer circular queue mentioned above (i.e. first calculate the initial circular queue length, then adjust according to the redundancy ratio). For example, if the serial port communication speed increases significantly, and the data processing speed does not follow up accordingly, the capacity of the buffer circular queue may need to be appropriately expanded to avoid data accumulation and loss. In actual application, the devices connected by the serial port may change the data generation rate due to various reasons (such as device upgrade, change of working mode, change of external environment, etc.). At the same time, the processing capacity of the system itself may also change due to other tasks running, hardware state and other factors. By regularly updating the serial port communication speed and data processing speed, and adjusting the buffer circular queue accordingly, the system can always maintain an ideal working state. When the data flow increases, appropriately expanding the buffer can prevent data loss; when the data flow decreases or the data processing capacity improves, reasonably reducing the buffer can release system resources for other tasks. This dynamic updating mechanism enables the system to automatically adapt to changes in data flow and processing capacity, effectively avoiding problems caused by mismatch between data flow and processing capacity, optimizing the allocation of system resources, and improving the overall performance and stability of the system.
[0093] With reference to Figure 4 It can be understood that in step S350, the following steps can be included but are not limited to the following steps:
[0094] Step S410: obtaining an initial queue length according to the cache circular queue;
[0095] Step S420: calculating a target queue length according to the redundancy ratio, the updated serial port communication speed and the data processing speed;
[0096] Step S430: when the absolute difference between the initial queue length and the target queue length is less than or equal to a preset adjustment threshold, maintaining the original cache circular queue;
[0097] Step S440: when the absolute difference between the initial queue length and the target queue length is greater than the adjustment threshold, updating the cache circular queue according to the target queue length.
[0098] In steps S410 to S420, first, the initial queue length is determined from the configuration parameters of the cache circular queue or by calculating the size of the current storage area, and the initial queue length is the amount of data that the cache circular queue can accommodate in the current state. According to the updated serial port communication speed and the data processing speed, the target queue length is calculated by combining the preset redundancy ratio, and the target queue length represents the ideal length of the cache circular queue according to the latest system state and redundancy requirements.
[0099] In steps S430 and S440, the absolute difference between the initial queue length and the target queue length is calculated, and this difference is compared with a preset adjustment threshold. The adjustment threshold is a pre-set value used to determine if the difference is large enough to warrant adjustment of the cache circular queue. This value can be determined based on factors such as the system's sensitivity to cache changes and the cost of adjusting the cache. If the absolute difference is less than or equal to the adjustment threshold, it means the current length of the cache circular queue is not significantly different from the ideal length, and the original cache circular queue state is maintained without any modification. If the absolute difference is greater than the adjustment threshold, the cache circular queue needs to be updated according to the target queue length. This may involve reallocating memory space, expanding or shrinking the storage area of the cache circular queue to the size corresponding to the target queue length. By setting an adjustment threshold, frequent adjustments to the cache circular queue due to minor speed changes or calculation errors are avoided. This method reduces the waste of system resources, as each adjustment of the cache circular queue may involve memory reallocation, data migration, and other operations, which consume system resources and time. For example, when data traffic and processing speed are relatively stable, the system will not frequently change the size of the cache queue due to minor fluctuations, thus maintaining efficient system operation. At the same time, this dynamic adjustment mechanism allows the system to better adapt to long-term system changes and external environmental interference. By regularly updating and conditionally adjusting the cache circular queue, the system can maintain relatively stable performance amidst these changes, effectively coping with various complex situations and improving the overall adaptability and stability of the system.
[0100] Reference Figure 5 It is understood that step S140 may include, but is not limited to, the following steps:
[0101] Step S510: When new data information is received from the serial port group while data information is being stored in the buffer circular queue, the corresponding first serial port identifier and second serial port identifier are obtained according to the current data information and the new data information, respectively.
[0102] Step S520: Based on the first serial port identifier, the second serial port identifier, and the priority sequence, obtain the first priority of the first serial port identifier and the second priority of the second serial port identifier;
[0103] Step S530: When the first priority is greater than or equal to the second priority, continue to store the current data information into the cache circular queue according to the write pointer of the cache circular queue;
[0104] Step S540: When the first priority is less than the second priority, the new data information is stored in the circular buffer queue according to the write pointer of the circular buffer queue until the new data information is completely stored, and then the previous data information is stored in the circular buffer queue.
[0105] In steps S510-S520, when the system is storing a piece of data information in the circular buffer queue, if the serial port group receives new data information, the system immediately extracts the serial port identifier of the data being stored as the first serial port identifier, and the serial port identifier of the newly received data as the second serial port identifier. The system looks up the first priority corresponding to the first serial port identifier and the second priority corresponding to the second serial port identifier according to the preset priority sequence. This priority sequence is set in advance during the system initialization stage according to the importance of the devices connected by each serial port, the urgency of the data, and other factors. For example, in an industrial control system, the serial port connected to a safety monitoring device (such as an emergency stop button, a fire alarm, etc.) may be assigned a higher priority, while the serial port connected to a normal state monitoring device (such as a device temperature sensor, within a non-critical threshold range) has a lower priority.
[0106] In steps S530-S540, if the first priority is greater than or equal to the second priority, it means that the data being stored is at least as important as the newly received data information. At this time, the system will continue to store the data being stored in the circular buffer queue according to the current position of the write pointer of the circular buffer queue. When the first priority is less than the second priority, it means that the newly received data information is more important. At this time, the system will pause the storage operation of the current data information, reposition the write pointer of the circular buffer queue, and store the new data information in the circular buffer queue first. The system will store the new data in the queue according to the position of the write pointer and the size of the new data, and the write pointer will be updated accordingly until the new data information is completely stored. After the storage of the new data is completed, the system will store the previously interrupted data information according to the position of the write pointer. At this time, the write pointer will point to the next position after the new data storage is completed, and the storage operation of the previous incomplete data will be continued to ensure that all data information can be correctly stored in the circular buffer queue according to the priority and order.
[0107] Different serial port connected devices have different roles and importance in the whole system, through this priority judgment mechanism, it can ensure that high priority data information can be quickly stored in the cache circular queue and then be processed preferentially. In the above embodiment, in an actual application scenario, such as in a security monitoring system of an intelligent building, if the fire alarm sends an alarm signal (high priority) through the serial port, while ordinary environmental monitoring data (low priority) is being stored in the cache circular queue, the system will process the fire alarm signal preferentially, so as to start the fire fighting measures in time and reduce the loss.
[0108] With reference to Figure 6 It can be understood that after step S140, the following steps can also be included, but not limited to:
[0109] Step S610: interval preset second duration, statistics from each serial port receives data information the fourth data total amount;
[0110] Step S620: calculate the sum of the fourth data total amount corresponding to all serial ports, get the fifth data total amount;
[0111] Step S630: when the ratio of the fourth data total amount of one of the serial ports to the fifth data total amount is less than the preset first threshold value, the priority of the serial port is degraded, and the priority sequence is adjusted;
[0112] Step S640: when the ratio of the fourth data total amount of one of the serial ports to the fifth data total amount is greater than the preset second threshold value, the priority of the serial port is upgraded, and the priority sequence is adjusted.
[0113] In steps S610 to S620, the system is preset with a second duration, which is usually determined based on the approximate period of system data flow change, the demand frequency of business data dynamic adjustment and other factors. After completing the statistics of the fourth data total amount of each serial port, the sum of the fourth data total amount corresponding to all serial ports is calculated to obtain the fifth data total amount. Then, for each serial port, the ratio of its fourth data total amount to the fifth data total amount is calculated, and the ratio is compared with the preset first threshold value and second threshold value.
[0114] In steps S630 to S640, when the ratio of the fourth total data amount to the fifth total data amount of a certain serial port is less than the preset first threshold value, it means that the data receiving amount of this serial port in the recent period of time is relatively small compared to the data receiving amount of the overall system, and the connected device may have low activity or the data importance has decreased, etc. At this time, the system will downgrade the priority of this serial port, for example, assuming that the priority is divided into 5 levels, level 1 being the highest and level 5 being the lowest, and the original priority of this serial port is level 3, then it will be reduced to level 4. And the priority sequence is adjusted accordingly, and the priority order of other serial ports is rearranged to reflect the change in the position of the serial port after the priority is lowered. Conversely, when the ratio of the fourth total data amount to the fifth total data amount of one of the serial ports is greater than the preset second threshold value, it indicates that the data receiving amount of this serial port accounts for a large proportion in the system, and the connected device may be in a critical operation stage or the data urgency and importance have increased. At this time, the system will upgrade the priority of this serial port, for example, from the original level 3 to level 2, and then the priority sequence is also adjusted to ensure that the priority order of each serial port accurately reflects the current actual situation, facilitating subsequent data storage in the circular queue based on the priority.
[0115] In actual multi-serial port application scenarios, the working state of the devices connected by each serial port and the importance of the data generated are not constant. Through this mechanism of periodic statistics and dynamic adjustment of priority according to data amount proportion, the system can automatically adapt to the dynamic changes of business, ensure that related data is processed in time, better meet the needs of different stages of actual business processes, and avoid wasting resources or failing to process critical data in time due to fixed priority sequence allocation of cache resources and processing order, thereby improving the performance of the entire system.
[0116] Reference Figure 7 It can be understood that in step S140, the following steps can be included but are not limited to:
[0117] Step S710: obtaining a preset verification algorithm;
[0118] Step S720: generating a first verification value about the verification algorithm according to the verification algorithm;
[0119] According to the serial port identifier of each data information, the preset priority sequence, and the write pointer of the cache circular queue, the data information and the corresponding first verification value are sequentially stored in the corresponding positions of the cache circular queue.
[0120] Correspondingly, in step S150, the following steps can be included but are not limited to:
[0121] Step S730: According to the read pointer of the cache circular queue, the data information and the corresponding second check value are taken out from the corresponding position of the cache circular queue.
[0122] Step S740: When the first check value is consistent with the second check value, the data information is processed.
[0123] In steps S710 to S720, a preset check algorithm is first obtained, which can be a cyclic redundancy check, a parity check, etc., which is not limited in the present application. According to the obtained check algorithm, the data information to be stored in the cache circular queue is calculated to generate a first check value. After the first check value is generated, the system stores the data information and the corresponding first check value in the corresponding position of the cache circular queue according to the serial port identifier of each data information, the preset priority sequence and the write pointer of the cache circular queue.
[0124] In steps S730 to S740, when data needs to be processed from the cache circular queue, the data information and the corresponding second check value are taken out from the corresponding position according to the read pointer of the cache circular queue. The read pointer reads the data and the check value from the cache space one by one according to the pre-set order, ensuring that the reading order of the data is consistent with the storage order. After the data is taken out, the first check value generated when the data is stored is compared with the second check value taken out now. If they are consistent, it means that the data has not been changed during the storage in the cache circular queue, and at this time the system will process the data information.
[0125] By generating and comparing the check values when the data is stored and taken out, it can effectively detect whether the data has been changed during the storage in the cache circular queue. Whether the change is caused by hardware failure or software exception, as long as the check values are inconsistent, it can be discovered in time. It is ensured that only accurate data can enter the subsequent data processing link, avoiding system error decision or error operation caused by error data, and improving the quality of data processing.
[0126] Reference Figure 8 It can be understood that after step S730, the following steps can also be included, but are not limited to:
[0127] Step S810: When the first check value is inconsistent with the second check value, a retransmission instruction is sent to the corresponding serial port according to the serial port identifier of the data information, and the cumulative failure number of the corresponding serial port is recorded.
[0128] Step S820: When the cumulative failure number of the first serial port in the serial port group exceeds the preset number threshold, the standby serial port in the serial port group is started to replace the first serial port, or the data information sent by the first serial port is stopped.
[0129] In step S810 to step S820, when the first check value is found to be inconsistent with the second check value, the system will first extract the serial port identifier corresponding to the data information. According to this serial port identifier, the system sends a retransmission instruction to the corresponding serial port through the pre-set communication protocol and serial port communication interface. The format and content of this retransmission instruction are formulated according to the rules agreed between the system and the serial port device. For example, in some simple serial port communication protocols, the retransmission instruction may be a specific byte sequence to inform the serial port device to resend the previous data. At the same time, the system will record the failure of this check for the corresponding serial port, and add 1 to the cumulative failure number. This cumulative failure number is usually stored in a special counter or a field in a system log file, which is used to track the data transmission quality of each serial port.
[0130] The system will continue to monitor the cumulative failure number of each serial port. When the cumulative failure number of a certain serial port (referred to as the first serial port) exceeds the pre-set number threshold, the system will check whether there is a backup serial port. If there is a backup serial port, the system will start a series of switching processes. If there is no backup serial port, when the cumulative failure number exceeds the number threshold, the system will stop accepting the data information sent by the first serial port. This is a protective measure to prevent the system from continuing to receive data that may contain errors, thereby affecting the operation of the entire system. The system may record the reason for stopping receiving and send an alarm to the relevant user or management module, prompting the need to repair or check the serial port. By sending a retransmission instruction to the faulty serial port, the system has the opportunity to obtain correct data, thereby correcting errors and omissions in the data transmission process, to ensure the accuracy of the data entering the data processing link, and to avoid system errors or operations caused by incorrect data.
[0131] In a second aspect, the present application also provides an electronic device, comprising: at least one memory, at least one processor and at least one program, the program being stored in the memory, and the processor executing one or more programs to implement the above-mentioned master multi-serial port concurrent cooperative communication method.
[0132] In the electronic device, the method for controlling the concurrent cooperative communication of multiple serial ports is executed. First, the average serial communication speed of the serial port group is accurately obtained by detecting and analyzing the serial port hardware parameters and the data transmission conditions during actual operation. Meanwhile, the processor performance of the host device, the current load of other tasks, and other conditions are evaluated to comprehensively calculate the data processing speed. According to the obtained serial communication speed and data processing speed, the length and capacity of the buffer circular queue are determined according to certain rules and algorithms to create a suitable buffer circular queue structure. Each serial port is in a state of listening to the data transmission from the outside at all times. Once data is received, the data from which serial port it comes from is recorded, and the data information is completely captured. When the serial port receives data information, the hardware immediately triggers an interrupt mechanism to generate an interrupt instruction, which is passed to the host processor to pause the non-critical tasks being executed, and the data information reception and caching operation is prioritized. Then, according to the serial port identifier carried by the data information, the pre-set priority sequence is searched to determine the priority of the serial port data, and the high-priority data information is stored in the corresponding position of the buffer circular queue according to the write pointer indication. Subsequently, the system extracts the data information in sequence according to the read pointer of the buffer circular queue and performs corresponding data processing on the data information. In this application, the buffer circular queue acts as a data "transit station" and can temporarily store the received data according to the different communication speeds of each serial port and the data processing speed of the system, avoiding the overflow of the buffer and the loss of data caused by the instantaneous influx of a large amount of data from some serial ports, and improving the reliability of data transmission. At the same time, the pre-set priority sequence can distinguish the importance and urgency of different serial port data, ensure that high-priority data is stored in the buffer circular queue first, and can be processed first subsequently, avoid the high-priority data being "flooded" by low-priority data due to long waiting time caused by regular sequential processing, and ensure the timeliness of important data.
[0133] The memory can include a program storage area and a data storage area, wherein the program storage area can store an operating system and application programs required by at least one function; the data storage area can store related data of the method for controlling the concurrent cooperative communication of multiple serial ports, etc. In addition, the memory can include a high-speed random access memory and can also include a non-transitory memory, such as at least one disk storage device, a flash memory device, or other non-transitory solid-state memory device. In some embodiments, the memory can optionally include a memory remotely located relative to the processor, which can be connected to the processing module through a network. Examples of the above network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0134] One or more signals are stored in the memory, which, when executed by one or more processors, execute the method for controlling the concurrent cooperative communication of multiple serial ports in any of the above embodiments.
[0135] In a third aspect, the embodiments of the present application provide a computer readable storage medium, which stores a computer program. The computer program is executed by one or more processors, and can make the one or more processors execute the master multi-serial port concurrent cooperative communication method in the above method embodiments.
[0136] The device embodiments described above are merely illustrative, wherein the units illustrated as separate components can or can not be physically separate, and the components illustrated as units can or can not be physical units, i.e., can be located in one place, or can be distributed on multiple network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the embodiments.
[0137] From the above description of the embodiments, those skilled in the art can understand that all or some steps in the above disclosed method and system can be implemented as software, firmware, hardware and appropriate combinations thereof. Some or all physical components can be implemented as software executed by a processor, such as a central processing unit, a digital signal processor or a microprocessor, or as hardware, or as an integrated circuit, such as an application specific integrated circuit. Such software can be distributed on a computer readable medium, which can include computer storage media (or non-transitory media) and communication media (or transitory media). As known to those skilled in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storage of information such as computer readable signals, data structures, program modules or other data. Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tapes, magnetic disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and can be accessed by a computer. In addition, as known to those skilled in the art, communication media generally includes computer readable signals, data structures, program modules or other data in modulated data signals such as carrier waves or other transport mechanisms, and can include any information delivery medium.
[0138] It should be understood that, in the application, "at least one" refers to one or more, and "multiple" refers to two or more. "And / or" is used to describe the association relationship of the associated objects, which means that there can be three relationships, for example, "A and / or B" can represent three cases of only A, only B, and A and B existing at the same time, wherein A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after it. "At least one of the following" or similar expressions means any combination of these items, including any combination of single or multiple items. For example, at least one of a, b or c can represent a, b, c, "a and b", "a and c", "b and c", or "a and b and c", wherein a, b, and c can be single or multiple.
[0139] In several embodiments provided in the application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only illustrative, for example, the division of the above units is only a logical function division, and actual implementation can have another division manner, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed units can be indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0140] The units described above as separate components can or can not be physically separated, and the components shown as units can or can not be physical units, that is, they can be located in one place, or can be distributed on multiple network units. According to actual needs, part or all of the units can be selected to achieve the purpose of the embodiment scheme.
[0141] In addition, the functional units in each embodiment of the application can be integrated in one processing unit, or each unit can be physically present separately, or two or more units can be integrated in one unit. The integrated unit can be realized in the form of hardware or in the form of a software functional unit.
[0142] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a computer-readable storage medium. Based on such understanding, the technical solutions of the present application essentially or say the part that contributes to the prior art or the whole or part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes multiple instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various program storage media.
[0143] The embodiments of the present application are described in detail above in combination with the drawings, but the present application is not limited to the above-described embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the purpose of the present application.
Claims
1. A method for concurrent collaborative communication across multiple serial ports of a master controller, characterized in that, include: Obtain the serial communication speed and data processing speed; Construct a buffer circular queue based on the serial communication speed and the data processing speed; Acquire at least one piece of data information transmitted from the serial port group; wherein the data information includes the corresponding serial port identifier; Based on the data information, an interrupt instruction is generated, and the data information is stored sequentially in the corresponding position in the buffer circular queue according to the serial port identifier, the preset priority sequence, and the write pointer of the buffer circular queue for each data information; wherein, the priority sequence is used to characterize the processing priority of each serial port in the serial port group, and the interrupt instruction is used to interrupt the currently executing task; According to the read pointer of the cache circular queue, the data information is retrieved from the corresponding position in the cache circular queue and the data information is processed. The method further includes, after the steps of generating an interrupt instruction based on the data information and storing the data information sequentially at the corresponding positions in the buffer circular queue according to the serial port identifier, preset priority sequence, and write pointer of each data information, the method further includes: At a preset second time interval, the fourth total amount of data received from each serial port is counted. Calculate the sum of the fourth total data corresponding to all serial ports to obtain the fifth total data; When the ratio of the total fourth data volume to the total fifth data volume of one of the serial ports is less than a preset first threshold, the priority of that serial port is downgraded and the priority sequence is adjusted. When the ratio of the fourth total data volume to the fifth total data volume of one of the serial ports is greater than a preset second threshold, the priority of that serial port is upgraded and the priority sequence is adjusted.
2. The master control multi-serial port concurrent collaborative communication method according to claim 1, characterized in that, The step of constructing a buffer circular queue based on the serial communication speed and the data processing speed includes: Based on the serial communication speed and the data processing speed, construct an initial circular queue; Obtain the preset redundancy ratio; Based on the redundancy ratio, the initial circular queue is adjusted to obtain a cached circular queue.
3. The master control multi-serial port concurrent cooperative communication method according to claim 2, characterized in that, After the step of adjusting the initial circular queue according to the redundancy ratio to obtain the cached circular queue, the method further includes: At a preset first time interval, obtain a first total amount of data received from each serial port and a second total amount of data retrieved from the buffer circular queue; The third total data volume is obtained based on the first total data volume corresponding to each serial port; The serial communication speed is updated based on the first duration and the third total data volume. The data processing speed is updated based on the first duration and the second total data volume; The buffer circular queue is adjusted based on the redundancy ratio, the updated serial communication speed, and the data processing speed.
4. The master control multi-serial port concurrent cooperative communication method according to claim 3, characterized in that, The step of adjusting the buffer circular queue based on the redundancy ratio, the updated serial communication speed, and the data processing speed includes: The initial queue length is obtained based on the cache circular queue; The target queue length is calculated based on the redundancy ratio, the updated serial communication speed, and the data processing speed. When the absolute difference between the initial queue length and the target queue length is less than or equal to a preset adjustment threshold, the original cache circular queue is maintained. When the absolute difference between the initial queue length and the target queue length is greater than the adjustment threshold, the cache circular queue is updated according to the target queue length.
5. The master control multi-serial port concurrent cooperative communication method according to claim 1, characterized in that, The step of storing the data information sequentially in the corresponding positions of the buffer circular queue according to the serial port identifier, preset priority sequence, and write pointer of each data information includes: When new data information is received from the serial port group while the data information is being stored in the cache circular queue, the corresponding first serial port identifier and second serial port identifier are obtained according to the current data information and the new data information, respectively. Based on the first serial port identifier, the second serial port identifier, and the priority sequence, a first priority regarding the first serial port identifier and a second priority regarding the second serial port identifier are obtained; When the first priority is greater than or equal to the second priority, the current data information is stored in the cache circular queue according to the write pointer of the cache circular queue. When the first priority is less than the second priority, the new data information is stored in the cache circular queue according to the write pointer of the cache circular queue, until the new data information is completely stored, and then the previous data information is stored in the cache circular queue.
6. The master control multi-serial port concurrent cooperative communication method according to claim 1, characterized in that, The step of storing the data information sequentially in the corresponding positions of the buffer circular queue according to the serial port identifier, preset priority sequence, and write pointer of each data information includes: Obtain the preset verification algorithm; Based on the verification algorithm, generate a first verification value related to the verification algorithm; The method involves storing the data information and the corresponding first check value in the corresponding positions of the cache circular queue according to the serial port identifier, preset priority sequence, and write pointer of each data information. Correspondingly, retrieving the data information from the corresponding position in the cache circular queue according to the read pointer of the cache circular queue, and processing the data information, includes: According to the read pointer of the cache circular queue, the data information and the corresponding second check value are retrieved from the corresponding position in the cache circular queue; When the first verification value matches the second verification value, the data information is processed.
7. The master control multi-serial port concurrent cooperative communication method according to claim 6, characterized in that, After the step of retrieving the data information and the corresponding second check value from the corresponding position in the cache circular queue according to the read pointer of the cache circular queue, the method further includes: When the first check value is inconsistent with the second check value, a retransmission command is sent to the corresponding serial port according to the serial port identifier of the data information, and the cumulative number of failures of the corresponding serial port is recorded. When the cumulative number of failures corresponding to the first serial port in the serial port group exceeds a preset threshold, the backup serial port in the serial port group is activated to replace the first serial port, or the reception of data information sent by the first serial port is stopped.
8. An electronic device, characterized in that, include: At least one memory; At least one processor; At least one program; The program is stored in the memory, and the processor executes at least one of the programs to implement the master control multi-serial port concurrent cooperative communication method as described in any one of claims 1 to 7.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable signals for performing the master-controlled multi-serial port concurrent cooperative communication method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Management method for high-concurrency multi-serial-port communication
CN117453606A
Optimization system for doubled i / O buffer length
JP1997062483A