Data acquisition method and system based on half-duplex bus, master node and storage medium
Patent Information
- Application Number
- CN202611152736.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-31
- Publication Date
- 2026-09-22
AI Technical Summary
[0003]RS485半双工总线的物理特性决定了同一时刻总线仅支持单方向数据传输,即主站发送指令期间从站无法回复,从站回复数据期间主站无法发送新指令
[0012]这样单个从节点超时仅标记为待重试设备就立即跳转采集下一个从节点。当主节点完成对其他所有正常从节点的采集之后,并在窗口尾部对待重试设备集中进行采集,如此从根本上避免了单从节点超时拖垮全局采集节奏的情形,也使得每个采集批次的实际轮询周期保持稳定可控,从而保证从节点有限的本地缓存不会因轮询延迟而发生新数据覆盖旧数据的现象,确保了所采集数据在时间维度上的连续性与完整性。
Smart Images

Figure CN122802311A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of communication technology, and more specifically, to a data acquisition method, system, master node, and storage medium based on a half-duplex bus. Background Technology
[0002] Due to its advantages such as strong anti-interference capability, long transmission distance, and low cost, the RS485 half-duplex bus is widely used in data acquisition systems in industrial fields. A typical system architecture consists of a master node connected to multiple slave nodes via an RS485 bus, with the master station reading data from each slave station sequentially according to a certain strategy.
[0003] The physical characteristics of the RS485 half-duplex bus dictate that the bus can only support unidirectional data transmission at any given time. This means that the slave cannot reply while the master is sending a command, and the master cannot send new commands while the slave is replying. This limitation has a limited impact when only a single slave exists on the bus; however, when multiple slaves are online simultaneously, the efficiency of bus access scheduling becomes a critical bottleneck for system performance.
[0004] In response, most existing technologies use a multi-slave polling method to collect data. However, if a single slave fails and cannot respond, the master station needs to wait for multiple timeouts before jumping to poll the next slave. This will cause the data collection of all devices on the entire bus to be interrupted for several seconds or even tens of seconds.
[0005] Furthermore, when the slave node is an industrial sensor, the local cache capacity of the industrial sensor is usually extremely limited (it can only temporarily store data from 1 to 2 batches of data). When multiple slave nodes time out consecutively, the polling cycle will be greatly extended. This will cause newly collected data from normal slave stations that have not been polled to overwrite the historical data that has not yet been collected, resulting in the irreversible loss of the data to be polled and directly damaging the continuity and integrity of the data. Summary of the Invention
[0006] The purpose of this invention is to provide a data acquisition method, system, master node, and storage medium based on a half-duplex bus, so as to improve the problems existing in the prior art.
[0007] The embodiments of the present invention can be implemented as follows: In a first aspect, the present invention provides a data acquisition method based on a half-duplex bus, applied to a master node, wherein the master node is communicatively connected to N slave nodes via the half-duplex bus; the method includes: Regarding the first Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of batches collected. In the During the first round of data collection, data was sent to the first... The slave node sends a data collection request, the data collection request being used to instruct the slave node to upload the first... Data collected in each batch; When the first one is received within the preset timeout period When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to acquire the first... One node; When the first [timeout period] is not received within the preset timeout period When the response data is returned from the node, mark the first... After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. There are 10 slave nodes, among which... ; After polling N slave nodes within the polling time window, if there are any devices to be retried, then each device to be retried will be re-collected within the remaining time of the polling time window.
[0008] Secondly, the present invention provides a data acquisition system based on a half-duplex bus, comprising a master node and N slave nodes communicatively connected to the master node via the half-duplex bus; the master node is used for acquiring data from a first slave node... Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of data collection batches; within one round of data collection: The master node is used to send to the... Each slave node sends a data collection request; No. When the slave node receives the data collection request, it is used to transfer the data to the slave node. The data collected in each batch is uploaded to the master node as response data; The master node is also configured to, upon receiving the first [received message] within a preset timeout period... When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to acquire the first... One node; The master node is also configured to, when not receiving the first [unspecified] message within the preset timeout period, [further action is required]. When the response data is returned from the node, mark the first... After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. Each node ; After the master node has polled N slave nodes within the polling time window, if there are any devices to be retried, it will re-collect data for each device to be retried within the remaining time of the polling time window.
[0009] Thirdly, the present invention provides a master node for implementing the method described in the first aspect above.
[0010] Fourthly, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in the first aspect above.
[0011] Compared with the prior art, the embodiments of the present invention provide a data acquisition method, system, master node, and storage medium based on a half-duplex bus, specifically addressing the... Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of collection batches; while in the first batch... During the first round of data collection, it is necessary to send data to the second round. The slave node sends a data collection request, which instructs the slave node to upload the first... The data collected in the first batch; when the data is received within the preset timeout period... When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to the acquisition of the next node. The slave node; when it does not receive the first [unclear] within the preset timeout period When the response data is returned from the node, mark the first one. After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. There are 10 slave nodes, among which... Finally, after polling N slave nodes within the polling time window, if there are devices waiting to be retried, then each device waiting to be retried will be re-collected within the remaining time of the polling time window.
[0012] In this way, if a single slave node times out, it is simply marked as a device to be retried, and the process immediately jumps to collecting data from the next slave node. After the master node has completed collecting data from all other normal slave nodes, it collects data from the devices to be retried at the end of the window. This fundamentally avoids the situation where a single slave node timeout disrupts the overall collection rhythm, and also keeps the actual polling cycle of each collection batch stable and controllable. This ensures that the limited local cache of the slave nodes will not cause new data to overwrite old data due to polling delays, thus ensuring the continuity and integrity of the collected data in the time dimension. Attached Figure Description
[0013] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0014] Figure 1 This is a schematic diagram of data acquisition in existing technologies.
[0015] Figure 2 This is one of the flowcharts illustrating a data acquisition method based on a half-duplex bus provided in an embodiment of the present invention.
[0016] Figure 3 This is a schematic diagram of the structure of a data acquisition system provided in an embodiment of the present invention.
[0017] Figure 4 This is the third flowchart illustrating a data acquisition method based on a half-duplex bus, as provided in an embodiment of the present invention.
[0018] Figure 5 This is the fourth flowchart illustrating a data acquisition method based on a half-duplex bus, provided as an embodiment of the present invention.
[0019] Figure 6 This is the fifth flowchart illustrating a data acquisition method based on a half-duplex bus, as provided in an embodiment of the present invention. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0021] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.
[0022] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.
[0023] Furthermore, the terms "first" and "second" are used only to distinguish descriptions and should not be interpreted as indicating or implying relative importance.
[0024] It should be noted that, where there is no conflict, the features in the embodiments of the present invention can be combined with each other.
[0025] Industrial data acquisition systems mostly use half-duplex buses. Typical application scenarios include: (1) Vibration monitoring: The acquisition station obtains waveform data from multiple acceleration sensors; (2) Temperature monitoring: The controller reads temperature values from multiple temperature transmitters; (3) Power management: The concentrator reads power consumption data from multiple smart meters; (4) Environmental monitoring: The gateway collects concentration data from multiple gas sensors.
[0026] Please combine Figure 1 The current mainstream data acquisition method is "single-slave serial blocking polling," which works as follows: The master station maintains a global slave index, pointing to the currently communicating slave number. A timer triggers polling at fixed intervals, sending a data request only to the slave corresponding to the current index each time it is triggered. The master station blocks and waits for the slave's response. Only after receiving a response or after multiple timeouts (e.g., 3 retries) does the index increment to point to the next slave at the next timer trigger. After all slaves have been traversed, the global data batch number is incremented by 1, and the next round begins. The master station receiver uses a fixed-size static buffer to assemble frames: after detecting the frame header, bytes are added according to the payload length until the buffer is full.
[0027] However, the above methods have the following four technical drawbacks in actual industrial deployment: (1) Single slave station failure can easily lead to global data acquisition blockage: Field equipment failure is a common occurrence in industrial environments. For example, loose sensor wiring can cause disconnections, instruments can crash due to harsh environments, and the bus can be accidentally severed during construction. Existing solutions use a queuing polling mechanism. When a slave station does not respond, the master station waits continuously and only jumps to the next slave station after multiple timeouts. During the waiting period, all other normal devices on the bus cannot be accessed. A single slave station failure can cause the data acquisition of all devices on the entire bus to be interrupted for several seconds or even tens of seconds. More seriously, the local data buffer capacity of industrial sensors is usually extremely limited (it can only temporarily store data from 1 to 2 batches of data). When multiple slave stations time out consecutively, causing the polling cycle to be greatly extended, the newly acquired data from the subsequent unpolled normal slave stations will overwrite the historical data that has not yet been acquired, causing irreversible loss of the data to be polled. This directly damages the continuity and integrity of the data and affects the upper-level trend analysis and fault diagnosis.
[0028] (2) Poor data acquisition timeliness and uncontrollable delay: Industrial sites have high requirements for data timeliness. For example, vibration monitoring needs to capture abnormal trends, energy consumption management needs real-time electricity consumption data, and environmental monitoring needs minute-level concentration change curves. However, the existing polling scheme is a queuing serial mode, with N slave stations waiting in sequence. The data timeliness of the slave stations at the back of the queue is significantly worse than that of the slave stations at the front. More importantly, while the slave stations are collecting and processing data locally (e.g., sensors performing AD conversion, electricity meters accumulating a cycle of electricity), the master station cannot sense when the slave stations are ready. It can only poll at a fixed rhythm. Polling when the slave station is not ready is an invalid operation, and after the slave station is ready, it may have to wait for a long time before being polled. Ultimately, this leads to a large amount of uncontrollable data delay, the acquisition cycle cannot be guaranteed, and it affects upper-level analysis and decision-making.
[0029] (3) Bus interference leads to poor data continuity: The RS485 bus is exposed to electromagnetic interference such as motor start-stop, inverter harmonics, and lightning surges in industrial environments for a long time, which may cause abnormalities such as garbled characters, loss, and misalignment of data frames. However, the existing frame processing scheme is only based on simple splicing of a fixed-size static buffer, which is not robust enough. Once a frame of data is abnormal, all subsequent frames cannot be parsed correctly and must wait for a specific condition to trigger a reset before it can be restored.
[0030] (4) Low bus bandwidth utilization and limited access capacity: The bandwidth resources of the half-duplex bus are inherently limited, and in the existing scheme, the master station spends most of its time waiting, such as waiting for slave station responses, waiting for data processing to complete, and waiting for timeout counts. The actual data transmission time accounts for a very low proportion of the total time, and the bus utilization is only about 1 / N (N is the number of slave stations). This directly limits the number of devices that can be connected to a single bus: the more slave stations there are, the longer the acquisition cycle of a single device, and the worse the data real-time performance. Users can only accept low-frequency sampling, or add additional buses and acquisition stations, resulting in a significant increase in hardware costs and construction complexity.
[0031] Based on the discovery of the aforementioned technical problems, the inventors, through creative labor, proposed the following technical solutions to solve or improve these problems. It should be noted that the deficiencies in the solutions of the prior art are all results derived by the inventors after practical experience and careful research. Therefore, the discovery process of the aforementioned problems and the solutions proposed in the embodiments of this application below should be considered contributions made by the inventors to this application during the inventive process, and should not be construed as technical content known to those skilled in the art.
[0032] Please refer to Figure 2 , Figure 2 This is a flowchart illustrating a data acquisition method based on a half-duplex bus provided in an embodiment of the present invention. The execution subject of this method can be a master node, which communicates with N slave nodes through a half-duplex bus.
[0033] The master node can be, but is not limited to, a dedicated data acquisition station, an RTU (Remote Terminal Unit), or a PLC (Programmable Logic Controller); the slave node can be, but is not limited to, sensors, instruments, actuators, etc.; and the half-duplex bus can be, but is not limited to, an RS485 bus, a CAN (Controller Area Network) bus, or an M-Bus (Meter-Bus) bus.
[0034] like Figure 2 The method may include the following steps S1 to S5.
[0035] S1, Regarding the first Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of batches collected.
[0036] In this embodiment, The values can be 1, 2, 3, up to , For a pre-defined total number of collection batches, for example =16 or 20. The polling time window is a fixed-size time window. , Slightly less than the interval required for a node to complete two adjacent data acquisitions. This allows sufficient time for the slave nodes to collect the next batch of data, ensuring that the master node can read the data from each slave node within the next polling window. Data collected in batches.
[0037] For example, , It should be noted that this example is merely illustrative and is not intended to be limiting.
[0038] And in the During the first round of data collection The process of data collection from a node may include the following steps S2~S4, wherein, .
[0039] S2, in the During the first round of data collection, data was sent to the first... The slave node sends a data collection request, which instructs the slave node to upload the first... Data collected in batches.
[0040] In this embodiment, the data acquisition request carries the global batch number (the global batch number is...). ), so the first When the slave node receives the data collection request, it will... The data collected in each batch is uploaded to the master node as response data.
[0041] S3, when the first message is received within the preset timeout period. When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to the acquisition of the next node. Each node.
[0042] S4. If the first message is not received within the preset timeout period... When the response data is returned from the node, mark the first one. After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. Each node.
[0043] In this embodiment, when the master node receives the response data returned by the slave station within a preset timeout period, it immediately performs serial port reception processing and frame parsing processing on the response data. After completing the frame buffering processing, it immediately jumps to the (n+1)th slave node to continue acquisition, that is, assigns n+1 to n and returns to step S2, until the acquisition of the Nth slave node is completed or the polling time window ends. The core significance of this rapid switch to the next slave node when any slave node times out is to protect the integrity of the data: the local buffer capacity of slave nodes such as industrial sensors is usually only enough to temporarily store data for 1 to 2 acquisition cycles. If the polling is blocked and the reading interval exceeds the buffer cycle, the new data will overwrite the unacquired data, causing irreversible permanent loss.
[0044] If the master node does not receive the response data from the nth slave node within the preset timeout period, it will only mark the nth slave node as a device to be retried, and then immediately jump to collect data from the (n+1)th slave node, that is, assign n+1 to n and return to execute step S2, until the collection of data from the Nth slave node is completed or the polling time window ends.
[0045] Among them, the preset timeout duration It can be flexibly configured based on baud rate and data reporting load length, but it needs to ensure... Slightly longer than the theoretical transmission time of a single uploaded data frame at the current baud rate, but much shorter than the polling time window length. The average availability time is obtained by dividing by the number of valid slave nodes N. This ensures that the timeout of a single slave node does not affect the polling opportunities of other slave nodes.
[0046] S5. After polling N slave nodes within the polling time window, if there are devices to be retried, then re-collect data for each device to be retried within the remaining time of the polling time window.
[0047] In this embodiment, in the first During the round-robin data acquisition process, after polling N slave nodes within the polling time window, if any marked devices are to be retried, then for the remaining time of the polling time window, data will be re-acquired from each device, i.e., a data packet carrying the global batch number will be sent to the device again. The system will process data collection requests, and when the polling time window ends, it will stop retrying and start the next round of data collection.
[0048] Optionally, each device to be retried is allowed 2 or 3 retries. If the timeout still occurs, the device to be retried is marked as a suspected faulty device. When a slave node is marked as a suspected faulty device in multiple consecutive data collection batches, the slave node is directly marked as an offline device and excluded from the list of valid slave stations to be collected. Alternatively, an external command can be used to perform a software reset on the slave node.
[0049] The data acquisition method based on a half-duplex bus provided in this invention immediately jumps to the next slave node for acquisition when a single slave node times out, only marking it as a device to be retried. After the master node completes the acquisition of all other normal slave nodes, it collects data from the devices to be retried at the end of the window. This fundamentally avoids the situation where a single slave node failure disrupts the overall acquisition rhythm, and also keeps the actual polling cycle of each acquisition batch stable and controllable. This ensures that the limited local cache of the slave node will not cause new data to overwrite old data due to polling delay, thus ensuring the continuity and integrity of the acquired data in the time dimension. At the same time, it also reduces the idle time of the half-duplex bus and improves the utilization rate of the half-duplex bus. Compared with the prior art, it supports the access of more slave nodes.
[0050] In one of the optional implementation methods, before the first round of data collection, the master node can open a period of [duration to be specified]. Delay timer, The value can range from 1000 to 30000 ms. During this time, the master node can sequentially send software reset commands to N slave nodes. Upon receiving the software reset command, the slave nodes will perform software initialization (including self-calibration, warm-up, etc.) and immediately begin data acquisition, then wait for the master node's data acquisition request. In this way, the N slave nodes will not prepare the data for the first acquisition batch simultaneously, but will prepare it sequentially, waiting for the master node to acquire it.
[0051] When the delay timer expires, the master node will initiate M rounds of data collection. The polling time window can be implemented using a periodic timer, the period of which is... .
[0052] Optionally, the delay timer and periodic timer can be operating system timers, event multiplexers, software timers, or hardware timers, etc., and there is no limitation here.
[0053] Optionally, at the beginning of each round of data collection, it is necessary to traverse all configured slave nodes, filter out the "registered and normal" slave nodes, and obtain a valid list. The valid list includes the N slave nodes to be collected in this round. Ideally, the number of slave nodes in the valid list determined at the beginning of each round of data collection is basically N. However, due to factors such as slave node hardware and software failures and communication failures, the number of slave nodes in the valid list determined at the beginning of a certain round of data collection may be less than N.
[0054] Optional, please see Figure 3 The master node may include a processor, a serial port unit, and a storage unit. Both the serial port unit and the storage unit are connected to the processor via an internal bus. The serial port unit includes a serial port buffer, and the storage unit includes a parsing buffer. The processor runs a scheduling thread, an interrupt handling module, a parsing thread, and a processing thread.
[0055] The scheduling thread is used to poll and collect data from N slave nodes within the polling time window. The serial port buffer can be a FIFO (First In First Out) queue, and the parsing buffer can be a contiguous linear buffer area.
[0056] Combination Figure 3 The following is a detailed description of the implementation process of the data acquisition method based on a half-duplex bus provided by the present invention, as shown in the schematic diagram.
[0057] Optional, in Figure 2 Based on this, please refer to Figure 4 In step S2 above, "to the first" "Sending data collection requests from slave nodes" means: S21, the scheduling thread directs to the... Each slave node sends a data collection request.
[0058] In this embodiment, the scheduling thread will be based on the current global batch number. and the The node ID of the slave node is used to generate a data acquisition request, which is then sent to the first slave node via a half-duplex bus after passing through the serial port unit. Each node.
[0059] Optional, please continue to see Figure 4 In step S2 above, for the first The processing of response data returned from the node may include the following sub-steps S31 to S36.
[0060] S31. When a data stream arrives at the serial port unit on the half-duplex bus, the serial port unit buffers the data stream into the serial port buffer.
[0061] In this embodiment, the first The response data returned by the slave node will reach the serial port unit of the master node via a half-duplex bus. The data stream is in byte stream form and will be directly buffered in the serial port buffer first.
[0062] S32. When the serial port unit has finished buffering the data stream and the time since the last serial port reception exceeds the preset timeout period, the serial port unit clears the serial port buffer.
[0063] When the data stream buffer is complete, the serial port unit calculates the time difference between the current serial port reception time and the previous serial port reception time. If this time difference exceeds the preset timeout duration, it indicates that the... If a slave node responds to a timeout, the serial port unit will directly clear the serial port buffer; otherwise, if the time difference does not exceed the preset timeout duration, step S33 will be executed.
[0064] S33. When the data stream is buffered and the time since the last serial port reception does not exceed the preset timeout period, the serial port unit generates a serial port interrupt signal and transmits it to the processor.
[0065] S34. In response to the serial port interrupt signal, the processor calls the interrupt handling module to read all the response data in the serial port buffer into the parsing buffer.
[0066] In this embodiment, the serial port interrupt signal is used to notify the processor that the serial port buffer is not empty, that is, to notify the processor to read from the serial port buffer. Therefore, combined with Figure 3 When the processor receives a serial port interrupt signal, it will directly call the interrupt handling module to read all the response data in the serial port buffer into the parsing buffer.
[0067] S35. The parsing thread performs frame extraction processing on all data in the parsing buffer, so as to extract the first frame. After the first response data frame returned from the node, the second... The first response data frame returned from the node is sent to the processing thread, and a jump notification is sent to the scheduling thread, so that the scheduling thread jumps to collect the first data frame. Each node.
[0068] When the parsing buffer is not empty, the parsing thread will continuously execute the frame extraction task. The parsing thread can extract a maximum of K frames (e.g., 3 or 5 frames) at a time. Therefore, the size of the parsing buffer can be set to K × maximum load length.
[0069] When the parsing thread extracts the first... When a response data frame is returned from a node, it is sent to the processing thread, and a jump notification is sent to the scheduling thread. Upon receiving this jump notification, the scheduling thread will automatically jump to collect the next response data frame. The first node, i.e., the one from the second node. Each slave node sends a data collection request.
[0070] S36. The processing thread stores and processes the received response data frames.
[0071] In this embodiment, the processing thread stores and processes the response data frames sent by the parsing thread.
[0072] Optional, please continue to see Figure 4 The implementation process of step S4 above may include: S41. If the scheduling thread does not receive a jump notification from the parsing thread within the preset timeout period, it directly sends a jump notification to the first... Each slave node sends a data collection request.
[0073] That is, the scheduling thread directs to the first... If the scheduling thread does not receive a jump notification from the parsing thread within the timeout period after the first slave node sends a data collection request, it means that the first slave node... If a slave node times out or fails to respond, the scheduling thread will automatically jump to the next data collection node. The first node, i.e., the one from the second node. Each slave node sends a data collection request.
[0074] In an optional implementation, the response data frame from the slave node may include the following three parts in its frame format: 1. Frame header: Occupies a configurable fixed number of bytes (e.g., 4 bytes). The frame header may include, but is not limited to, frame header flags (e.g., 0xFF, marking the start of a frame), payload length (indicating the size of the payload), and node ID. 2. Load loader: This refers to the data that the slave nodes need to upload; 3. Frame tail portion: Occupies a fixed 1 byte or configurable multiple bytes. The frame tail portion includes a frame tail marker (e.g., 0xBB), which marks the end of a data frame.
[0075] Therefore, in Figure 4 Based on this, please refer to Figure 5The implementation process of step S35 above, "the parsing thread performs frame extraction processing on all data in the parsing buffer", may include the following sub-steps S351 to S358.
[0076] S351. The parsing thread searches the parsing buffer for frame header markers to determine the frame header portion.
[0077] Taking a frame header consisting of 4 bytes as an example, the parsing thread will start searching from the starting address (i.e., the first address) of the parsing buffer. When the frame header marker is found, it can be determined that the 4 bytes starting from the frame header marker are the frame header.
[0078] S352. The parsing thread obtains the payload length recorded in the frame header and determines the data to be verified following the frame header based on the payload length.
[0079] Taking an 8-byte payload recorded in the frame header and a 1-byte frame trailer as an example, the parsing thread can determine that the 9 bytes following the frame header are the data to be verified. Then, the parsing thread will perform a frame integrity check, that is, check whether the 9 bytes of data to be verified include a frame trailer marker. If so, proceed to step S353 and then step S358; otherwise, proceed to step S354.
[0080] S353. If the data to be verified includes a frame end marker, the parsing thread reads the frame header and the data to be verified from the parsing buffer to obtain a response data frame.
[0081] In this embodiment, the parsing thread sends each extracted response data frame to the processing thread. Specifically, if the node ID of the extracted response data frame belongs to the... If the number of slave nodes is 1, then the response data frame is the 1st slave node. A response data frame returned by a node.
[0082] S354. If the data to be verified does not include a frame end marker, the parsing thread searches for a new frame header marker within the data to be verified.
[0083] In this embodiment, if a new frame header marker exists in the data to be verified, it indicates a frame sticking phenomenon (i.e., an incomplete data frame is stuck with the next data frame). At this time, the parsing thread jumps to execute step S355 and then returns to execute step S352. If no new frame header marker exists in the data to be verified, it means that the current frame is an erroneous frame and needs to be discarded. The parsing thread then jumps to execute steps S356 and S357 in sequence.
[0084] S355. If a new frame header marker exists, the parsing thread discards the incomplete data frames before the new frame header marker and obtains the new frame header portion corresponding to the new frame header marker.
[0085] S356. If no new frame header marker exists, the parsing thread clears the frame header portion and the data to be verified in the parsing buffer.
[0086] S357. The parsing thread checks whether the parsing buffer is empty.
[0087] If the parsing buffer is not empty, the parsing thread returns to step S351; if the parsing buffer is empty, the parsing thread jumps to step S358.
[0088] S358, Parse thread stops frame extraction.
[0089] like Figure 5 This invention differs from the simple splicing method in existing technologies. It uses a linear buffer region as the parsing buffer and employs a complete parsing process including frame header search, frame length verification, frame integrity check, frame tail verification, and frame extraction. Even if frame overlap or erroneous frames occur, they can be accurately cleaned up without interfering with the correct parsing and extraction of subsequent data frames. Furthermore, the frame format parameters of the response data frames in this invention are configurable, enabling adaptation to different communication protocols.
[0090] Among the optional implementation methods, such as Figure 3 The processing thread may include a first sub-thread and a second sub-thread, and the storage unit may further include a frame buffer space and two buffers jointly maintained by the two sub-threads. Figure 3 (This example uses areas A and B as examples only).
[0091] To prevent data races caused by two child threads accessing area A or area B simultaneously, both buffers support a mutex lock mechanism, and each buffer has a corresponding ready flag. A ready flag of 1 indicates that the buffer is not empty, and a ready flag of 0 indicates that the buffer is empty.
[0092] To ensure that the two child threads access the two buffers in an orderly manner to store and process the data frame, the first child thread and the second child thread maintain write index and read index respectively. Both the write index and the read index can switch between the two buffers, and the write index and the read index keep pointing to different buffers to achieve interleaved write and read.
[0093] Therefore, please see Figure 6 In step S36 above, the process of "the processing thread stores the received response data frame" can include the following sub-steps S361 to S36.
[0094] S361. When the first child thread receives a response data frame, it acquires the mutex lock of the buffer pointed to by the write index. S362. If the ready flag of the buffer pointed to by the write index is 0, the first child thread caches the received response data frame in the buffer pointed to by the write index, resets the ready flag of the buffer pointed to by the write index to 1, switches the write index to another buffer, sends a read notification to the second child thread, and then releases the acquired mutex lock.
[0095] S363. If the ready flag of the buffer pointed to by the write index is 1, the first child thread discards the received response data frame, records the number of overflow frames, and then directly releases the acquired mutex lock.
[0096] In this embodiment, the parsing thread sends the extracted response data frame to the first child thread.
[0097] For example, in combination Figure 3 When the first child thread receives the response data frame, assuming the write index points to area A, the first child thread needs to acquire the mutex lock of area A first (so that it can write to area A), and then check whether the ready flag of area A (let's call it flag A) is 0; If flag A=0, it means that area A is empty at this time and supports writing. Then, the response data frame can be written to area A, then flag A is set to 1, the write index is switched to area B, and after sending a read notification to the second child thread, the mutex lock of area A is released. If flag A ≠ 0 (i.e., flag A = 1), it means that area A is not empty at this time, and the double buffer overflows. At this time, the response data frame can only be discarded, and then the number of overflow frames is recorded (i.e., the number of overflow frames + 1), and the mutex lock of area A is released.
[0098] Recording the number of overflow frames helps in subsequent load assessment of the double buffer to adjust its size. It should be noted that the above example is merely illustrative and not intended to be limiting.
[0099] S364. When the second child thread receives a read notification, it acquires the mutex lock of the buffer pointed to by the read index.
[0100] S365. If the ready flag of the buffer pointed to by the read index is 1, the second child thread will transfer the response data frame in the buffer pointed to by the read index to the frame buffer space, reset the ready flag of the buffer pointed to by the read index to 0, switch the read index to another buffer, and release the acquired mutex lock.
[0101] S366. If the ready flag of the buffer pointed to by the read index is 0, then the second child thread directly releases the acquired mutex lock.
[0102] For example, combined Figure 3 When the second child thread receives the read notification, assuming the read index points to area B, the second child thread needs to first acquire the mutex lock of area B (so that it can read in area B), and then check whether the ready flag of area B (let's call it flBg B) is 1; If flBg B=1, it means that area B is not empty at this time and supports reading. Then, the response data frame cached in area B can be transferred to the frame buffer space, flBg B is set to 0, the write index is switched to area A, and finally the mutex lock of area B is released. If flBg B≠1 (i.e. flBg B=0), it means that area B is empty at this time (i.e., the first child thread has not yet written to area B). You only need to release the mutex lock of area B and continue to wait for the read notification.
[0103] Recording the number of overflow frames helps in subsequent load assessment of the double buffer to adjust its size. It should be noted that the above example is merely illustrative and not intended to be limiting.
[0104] For the response data frames extracted by the parsing thread, this invention achieves complete parallelism in data reception and processing through a notification and wake-up mechanism of two sub-threads and a mutual exclusion protection mechanism of double buffers. This eliminates data contention and loss, and even if multiple response data frames arrive at the processing thread sequentially, storage processing can be achieved quickly, avoiding processing blockage. The mutual exclusion protection mechanism protects access to the two buffers, as well as index switching and ready flag setting operations.
[0105] In an optional implementation, the processor may also run a sorting thread. For response data frames within the frame buffer space, the sorting thread can process them outside the lock. That is, the sub-step of step S36 above further includes: S367. After the sorting thread parses and verifies each response data frame in the frame buffer space, it moves it to the designated buffer space for persistent storage.
[0106] The cache space can be either the internal storage space of the master node or the external storage space of the connection.
[0107] In an optional implementation, during a round of data acquisition by the master node, the master node may also send other instructions (such as configuration query instructions or status confirmation instructions) to a slave node. In order to ensure the immediate response of other instructions and the efficiency of data acquisition processing, the frame header may also include a function code field. The function code field can be a acquisition type value (e.g., 0x01) or an immediate response value (e.g., 0x02). The acquisition type value represents the response data frame of data acquisition, and the immediate response value represents the response data frame of other instructions.
[0108] Therefore, after step S353 above, the following steps (1) and (2) may also be included: Step (1): If the function code field in the frame header of the response data frame is a preset acquisition type value, the parsing thread will send the response data frame to the processing thread for asynchronous processing. Step (2): If the function code field in the frame header of the response data frame is the preset instant response value, the parsing thread will cache the response data frame in the global response buffer for other threads that are synchronously waiting for the instant response to read.
[0109] Specifically, for the response data frame extracted in step S353, the parsing thread will examine the function code field in the frame header: If the function code field is a collection type value (e.g., 0x01), it means that the response data frame is a response from a slave node to a data collection request. The parsing thread can send the response data frame to the first child thread of the processing thread for asynchronous processing. If the function code field is an immediate response value (e.g., 0x02), it means that the response data frame is a response from a slave node to other instructions issued by the master node. The parsing thread caches the response data frame in the global response buffer of the storage unit, so that other threads waiting for immediate responses can read it themselves.
[0110] This invention implements the splitting of response data frames through the function code field, ensuring that the response data frames of the slave node corresponding to the data acquisition request can be diverted to the frame buffer space, and also ensuring that the response data frames of the slave node to other instructions issued by other threads of the master node can be diverted to the global response buffer for other threads to obtain synchronously.
[0111] Based on the same inventive concept as the data acquisition system based on a half-duplex bus described above, this embodiment of the invention also provides a data acquisition system based on a half-duplex bus, which includes a master node and N slave nodes communicatively connected to the master node via a half-duplex bus; the master node is used for targeting the... Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of data collection batches; within one round of data collection: The master node is used to send to the first Each slave node sends a data collection request; No. When the slave node receives a data collection request, it is used to transfer the data to the slave node. The data collected in each batch is uploaded to the master node as response data; The master node is also used to receive the first [unclear] within a preset timeout period. When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to the acquisition of the next node. One node; The master node is also used to handle situations where the first message is not received within a preset timeout period. When the response data is returned from the node, mark the first one. After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. Each node ; After the master node has polled N slave nodes within the polling time window, if there are devices waiting to be retried, it will re-collect data for each device waiting to be retried within the remaining time of the polling time window.
[0112] In the data acquisition system, please refer to the above introduction for the detailed process of the master node performing a round of data acquisition, which will not be repeated here.
[0113] This invention also provides a master node for implementing the data acquisition method based on a half-duplex bus described above.
[0114] This invention also provides a computer-readable storage medium storing a computer program. When executed by a processor, this computer program implements the data acquisition method based on a half-duplex bus disclosed in the above embodiments. The computer-readable storage medium can be, but is not limited to, a USB flash drive, a portable hard drive, RAM (Random Access Memory), ROM (Read Only Memory), FLASH (Flash Memory), PROM (Programmable Read-Only Memory), EPROM (Erasable Programmable Read-Only Memory), EEPROM (Electric Erasable Programmable Read-Only Memory), and various other media capable of storing program code.
[0115] In summary, this invention provides a data acquisition method, system, master node, and storage medium based on a half-duplex bus. When a single slave node times out, it immediately jumps to acquiring the next slave node after marking it as a device to be retried. After the master node has completed acquiring data from all other normal slave nodes, it collects data from the devices to be retried at the end of the window. This fundamentally avoids the situation where a single slave node failure disrupts the overall acquisition rhythm, and also ensures that the actual polling cycle of each acquisition batch remains stable and controllable. This guarantees that the limited local cache of the slave nodes will not overwrite old data due to polling delays, ensuring the continuity and integrity of the acquired data in the time dimension. It also reduces the idle time of the half-duplex bus, improves the utilization rate of the half-duplex bus, and supports the access of more slave nodes compared to existing technologies.
[0116] The above are merely specific embodiments of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A data acquisition method based on a half-duplex bus, characterized in that, Applied to a master node, the master node is communicatively connected to N slave nodes via the half-duplex bus; the method includes: Regarding the first Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of batches collected. In the During the first round of data collection, data was sent to the first... The slave node sends a data collection request, the data collection request being used to instruct the slave node to upload the first... Data collected in each batch; When the first one is received within the preset timeout period When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to acquire the next data. One node; When the first [unclear] is not received within the preset timeout period When the response data is returned from the node, mark the first... After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. There are 10 slave nodes, among which... ; After polling N slave nodes within the polling time window, if there are any devices to be retried, then each device to be retried will be re-collected within the remaining time of the polling time window.
2. The method according to claim 1, characterized in that, The master node includes a processor, a serial port unit, and a storage unit. The serial port unit and the storage unit are both connected to the processor via an internal bus. The serial port unit includes a serial port buffer, and the storage unit includes a parsing buffer. The processor runs a scheduling thread, an interrupt handling module, a parsing thread, and a processing thread. The scheduling thread is used to poll and collect data from N slave nodes within a polling time window. When the first one is received within the preset timeout period When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to acquire the next data. The steps for each slave node include: When a data stream arrives at the serial port unit on the half-duplex bus, the serial port unit buffers the data stream into the serial port buffer. The serial port unit clears the serial port buffer when the data stream has been buffered and the time since the last serial port reception exceeds the preset timeout period. When the data stream is buffered and the time since the last serial port reception does not exceed the preset timeout period, the serial port unit generates a serial port interrupt signal and transmits it to the processor. In response to the serial port interrupt signal, the processor calls the interrupt handling module to read all the response data in the serial port buffer into the parsing buffer. The parsing thread performs frame extraction processing on all data in the parsing buffer, so as to extract the frame of the first... After the first response data frame returned from the node, the first... The first response data frame returned from the node is sent to the processing thread, and a jump notification is sent to the scheduling thread, so that the scheduling thread jumps to collect the first response data frame. One node; The processing thread stores and processes the received response data frames.
3. The method according to claim 2, characterized in that, To the The steps for a slave node to send a data collection request include: The scheduling thread sends to the first Each slave node sends a data collection request; When the first [unclear] is not received within the preset timeout period When the response data is returned from the node, mark the first... After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. The essence of a slave node includes: If the scheduling thread does not receive a jump notification from the parsing thread within the preset timeout period, it directly sends a jump notification to the first... Each slave node sends a data collection request.
4. The method according to claim 2, characterized in that, The step of the parsing thread performing frame extraction processing on all data in the parsing buffer includes: The parsing thread searches the parsing buffer for frame header markers to determine the frame header portion; The parsing thread obtains the payload length recorded in the frame header and determines the data to be verified following the frame header based on the payload length. If the data to be verified includes a frame tail marker at the end, the parsing thread reads the frame header and the data to be verified from the parsing buffer to obtain a response data frame, and then returns to perform the step of searching for the frame header marker in the parsing buffer until the parsing buffer is empty; the frame header of the response data frame includes a node ID; If the data to be verified does not include a frame end marker, the parsing thread searches for a new frame header marker within the data to be verified. If the new frame header marker exists, the parsing thread discards the incomplete data frames before the new frame header marker, obtains the new frame header portion corresponding to the new frame header marker, and returns to execute the step of obtaining the payload length recorded in the frame header portion. If the new frame header marker does not exist, the parsing thread, after clearing the frame header portion and the data to be verified from the parsing buffer, returns to the step of searching for the frame header marker in the parsing buffer until the parsing buffer is empty.
5. The method according to claim 2, characterized in that, The processing thread includes a first sub-thread and a second sub-thread. The storage unit also includes a frame buffer space and two buffers jointly maintained by the two sub-threads. Both buffers support a mutex lock mechanism and have corresponding ready flags. The first sub-thread and the second sub-thread maintain a write index and a read index, respectively, and the write index and the read index switch between the two buffers. The step of the processing thread storing the received response data frame includes: When the first child thread receives the response data frame, it acquires the mutex lock of the buffer pointed to by the write index. If the ready flag of the buffer pointed to by the write index is 0, the first sub-thread will cache the received response data frame in the buffer pointed to by the write index, reset the ready flag of the buffer pointed to by the write index to 1, switch the write index to another buffer, send a read notification to the second sub-thread, and release the acquired mutex lock. If the ready flag of the buffer pointed to by the write index is 1, the first child thread discards the received response data frame, records the number of overflow frames, and then directly releases the acquired mutex lock. When the second child thread receives the read notification, it acquires the mutex lock of the buffer pointed to by the read index; If the ready flag of the buffer pointed to by the read index is 1, the second sub-thread will transfer the response data frame in the buffer pointed to by the read index to the frame buffer space, reset the ready flag of the buffer pointed to by the read index to 0, switch the read index to another buffer, and release the acquired mutex lock. If the ready flag of the buffer pointed to by the read index is 0, then the second child thread directly releases the acquired mutex lock.
6. The method according to claim 5, characterized in that, The processor also runs a sorting thread, and after the processing thread performs storage processing on the received response data frames, the processor further includes: The sorting thread parses and verifies each response data frame in the frame buffer space, and then transfers it to the designated buffer space for persistent storage.
7. The method according to claim 4, characterized in that, After the parsing thread reads the frame header portion and the data to be verified from the parsing buffer to obtain a response data frame, the method further includes: If the function code field in the header of the response data frame is a preset acquisition type value, the parsing thread will send the response data frame to the processing thread for asynchronous processing. If the function code field in the header of the response data frame is a preset instant response value, the parsing thread will cache the response data frame in the global response buffer for other threads that are synchronously waiting for an instant response to read.
8. A data acquisition system based on a half-duplex bus, characterized in that, It includes a master node and N slave nodes that are connected to the master node via the half-duplex bus; the master node is used for targeting the first... Each data collection batch collects data from N slave nodes within a polling time window. , This refers to the total number of data collection batches; within one round of data collection: The master node is used to send to the... Each slave node sends a data collection request; No. When a slave node receives the data collection request, it is used to transfer the data to the slave node. The data collected in each batch is uploaded to the master node as response data; The master node is also configured to, upon receiving the first [unclear] within a preset timeout period... When the response data is returned from the node, the response data undergoes serial port reception processing and frame parsing processing, followed by frame buffering. Simultaneously, the process jumps to acquire the next data. One node; The master node is also configured to, when not receiving the first [unspecified] message within the preset timeout period, [further action is required]. When the response data is returned from the node, mark the first... After the slave node is identified as a device to be retried, the process jumps to the data acquisition stage. Each node ; After the master node has polled N slave nodes within the polling time window, if there are any devices to be retried, it will re-collect data for each device to be retried within the remaining time of the polling time window.
9. A master node, characterized in that, The master node is used to implement the method described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method of any one of claims 1-7.