CN111858447A - Method for receiving data from serial port and processing data frame - Google Patents

Method for receiving data from serial port and processing data frame Download PDF

Info

Publication number
CN111858447A
CN111858447A CN202010669896.8A CN202010669896A CN111858447A CN 111858447 A CN111858447 A CN 111858447A CN 202010669896 A CN202010669896 A CN 202010669896A CN 111858447 A CN111858447 A CN 111858447A
Authority
CN
China
Prior art keywords
data
serial port
reading
queue
receiving
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010669896.8A
Other languages
Chinese (zh)
Inventor
王文科
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen Jixian Technology Co ltd
Original Assignee
Shenzhen Jixian Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shenzhen Jixian Technology Co ltd filed Critical Shenzhen Jixian Technology Co ltd
Priority to CN202010669896.8A priority Critical patent/CN111858447A/en
Publication of CN111858447A publication Critical patent/CN111858447A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/38Information transfer, e.g. on bus
    • G06F13/42Bus transfer protocol, e.g. handshake; Synchronisation
    • G06F13/4282Bus transfer protocol, e.g. handshake; Synchronisation on a serial bus, e.g. I2C bus, SPI bus
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L47/00Traffic control in data switching networks
    • H04L47/50Queue scheduling
    • H04L47/62Queue scheduling characterised by scheduling criteria
    • H04L47/622Queue service order

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Communication Control (AREA)

Abstract

The invention discloses a method for receiving data from a serial port and processing data frames, which comprises the following steps of S1: initializing a serial port, and creating a serial port receiving task and a serial port reading task by a receiving end; s2: after receiving data from the serial port, executing serial port interrupt processing, and when the serial port interrupt processing is carried out, placing the data received by the serial port in a message node by a receiving end and sending the data of the message node to a received message queue; s3: executing a serial port receiving task, namely continuously acquiring data of message nodes from a received message queue, then acquiring the data received by the serial port from the message nodes, and adding the data to a circular queue; s4: executing a serial port reading task, namely reading data received by the serial port from the annular queue; and framing the data read from the circular queue. The received serial port data are issued to the message queue, so that the time consumption of interrupt processing is reduced, and meanwhile, the data received by the serial port are separated from the data processing, so that the condition that the data are lost due to the overflow of the serial port is avoided.

Description

Method for receiving data from serial port and processing data frame
Technical Field
The invention relates to the technical field of data transmission, in particular to a method for receiving data from a serial port and processing a data frame.
Background
In the architecture of the internet of things, a common architecture is a mode of a communication module and a single chip microcomputer, and data communication is carried out between the communication module and the single chip microcomputer through a serial port. When both sides receive data from the serial port, the direct receiving mode is generally adopted at present, but when the communication data volume is large or more frequent, effective data can be lost firstly, and complete and effective data frames can not be received secondly.
Accordingly, the prior art is deficient and needs improvement.
Disclosure of Invention
The technical problem to be solved by the invention is as follows: the method for receiving data from the serial port and processing the data frame is provided, and can improve the capability of receiving the complete data frame and reduce the probability of losing effective data when the serial port is in communication, particularly under the conditions of large data volume and frequent communication.
The technical scheme of the invention is as follows: a method for receiving data from a serial port and processing data frames is provided, which comprises the following steps: the following steps.
S1: and initializing the serial port, and establishing a serial port receiving task and a serial port reading task by a receiving end.
S2: and after receiving the data from the serial port, the receiving terminal executes the serial port interrupt processing, and when the serial port interrupt processing is performed, the receiving terminal places the data received by the serial port in the message node and sends the data of the message node to the received message queue. The message node is a storage unit, the data of the message node is the information of the storage unit, the receiving end places the data received by the serial port in the message node and sends the data of the message node to the message receiving queue, namely temporarily stores and sequences the data received by the outlet.
S3: the receiving end executes the serial port receiving task, namely continuously acquires the data of the message node from the received message queue, then acquires the data received by the serial port from the message node, and adds the data to the circular queue. And the receiving end takes out the data received by the corresponding serial port from the message node according to the data of the message node in the message queue and puts the data into the annular queue for sequencing.
S4: the receiving end executes a serial port reading task, namely, data received by the serial port is read from the annular queue; framing the data read from the circular queue; that is, the data received by the serial port is taken out from the ring queue, and the data received by the serial port is framed.
The data received by the serial port is read in the following mode: periodically reading, and taking out the ring queue every 5ms-500 ms; or, the mode of reading the data received by the serial port is as follows: in the process of executing the serial port receiving task, after data is successfully added into the circular queue, the serial port waiting for the semaphore is informed to read the task by releasing the semaphore, and the data is fetched from the circular queue. The format of the frame includes: frame header, length, data, checksum.
The message nodes and the annular queue are equivalent to a reservoir and can temporarily store data received by the serial port, so that the data volume exchanged through the serial port is very large, the capacity of receiving complete data frames can be improved under the condition of frequent communication, and the probability of losing effective data can be reduced. In the serial port interrupt processing, the receiving end sends the received serial port data to the message queue, so that the time consumption of interrupt processing is reduced, and meanwhile, the serial port received data is separated from the data processing, and the condition that the data is lost due to serial port overflow is avoided. By means of the ring queue, the size of the queue can be set, and a receiving end can receive data sent by a sending end as much as possible. In the serial port reading task executed by the receiving end, the serial port can be periodically read, and the serial port can be read through the semaphore, so that the timeliness of serial port receiving can be guaranteed.
One method for framing is as follows: directly reading related fields from the circular queue to perform framing; the method specifically comprises the following steps.
S11: reading the frame header from the circular queue.
S12: and judging whether the frame header is correct or not, if so, entering the step S13, and if not, returning to the step S11.
S13: the length is read from the circular queue.
S14: and judging whether the length is correct or not, if so, entering the step S15, and if not, returning to the step S11.
S15: and reading data and checksum from the circular queue.
S16: and judging whether the checksum is correct or not, if so, entering the step S17, and if not, returning to the step S11.
S17: the complete frame is received.
Under the condition that the sending end frequently sends data or the data amount is large, the receiving end can receive effective data as much as possible, the capability of acquiring complete data frames can be improved, and the probability of losing the effective data can be greatly reduced.
Another method for framing is as follows: acquiring data from the annular queue, putting the data into a local array, and framing the data from the local array; the method specifically comprises the following steps.
S21: a large segment of data is read from the circular queue and placed in the local array.
S22: reading the data placed in the local array (the data is the data read from the circular queue in step S21), and determining whether valid data exists in the local array; if valid data is read, the process proceeds to step S23, and if valid data is not read, the process returns to step S21. The valid data at least comprises: header and length data in frame format.
S23: the frame header is read from the local array.
S24: and judging whether the frame header is correct or not, if so, entering the step S25, and if not, returning to the step S23.
S25: the length is read from the local array.
S26: and judging whether the length is correct or not, if so, entering the step S27, and if not, removing the frame header of the current time from the local array and returning to the step S23.
S27: judging whether the length of the valid data left in the local array is smaller than the length (i.e. the length of the frame) read from the local array in step S25; if not, the process proceeds to step S28, and if not, the process returns to step S21.
S28: the data (which is the data referred to in the frame format), the checksum, is read from the local array.
S29: judging whether the checksum is correct or not; if the result is correct, the process goes to step S210, and if the result is incorrect, the frame header of this time is removed from the local array, and the process returns to step S23.
S210: the complete frame is received and the data for the frame at this time is removed from the local array.
Under the condition that the sending end frequently sends data or the data amount is large, the receiving end can receive effective data as much as possible, the capability of acquiring complete data frames can be improved, and the probability of losing the effective data can be greatly reduced.
The serial port is a data communication channel between the single chip microcomputer and the communication module, the receiving end is the single chip microcomputer or the communication module, and the corresponding sending end is the communication module or the single chip microcomputer.
By adopting the scheme, the invention provides the method for receiving the data from the serial port and processing the data frame. By means of the ring queue, the size of the queue can be set, and a receiving end can receive data sent by a sending end as much as possible. In the serial port reading task executed by the receiving end, the serial port can be periodically read, and the serial port can be read through the semaphore, so that the timeliness of serial port receiving can be guaranteed.
Drawings
FIG. 1 is a flow chart of the present invention;
FIG. 2 is a flow chart of the receiving end processing data according to the present invention;
FIG. 3 is a flow chart of framing in accordance with the present invention;
FIG. 4 is a framing diagram of another framing of the present invention.
Detailed Description
The invention is described in detail below with reference to the figures and the specific embodiments.
Referring to fig. 1 and fig. 2, the present invention provides a method for receiving data from a serial port and processing a data frame, including: the following steps.
S1: and initializing the serial port, and establishing a serial port receiving task and a serial port reading task by a receiving end.
S2: and after receiving data from the serial port, the receiving terminal executes serial port interrupt processing, and when the serial port interrupt processing is performed, the receiving terminal places the data received by the serial port in the message node and sends the information of the message node to the message receiving queue.
S3: the receiving end executes the serial port receiving task, namely continuously acquires the information of the message node from the message receiving queue, acquires the data received by the serial port from the message node, and adds the data to the circular queue.
S4: the receiving end executes a serial port reading task, namely, data received by the serial port is read from the annular queue; and framing the data read from the circular queue.
The data received by the serial port is read in the following mode: periodically reading, and taking out the ring queue every 5ms-500 ms; or, the mode of reading the data received by the serial port is as follows: in the process of executing the serial port receiving task, after data is successfully added into the circular queue, the serial port waiting for the semaphore is informed to read the task by releasing the semaphore, and the data is fetched from the circular queue. The format of the frame includes: frame header, length, data, checksum.
The message nodes and the annular queue are equivalent to a reservoir and can temporarily store data received by the serial port, so that the data volume exchanged through the serial port is very large, the capacity of receiving complete data frames can be improved under the condition of frequent communication, and the probability of losing effective data can be reduced. In the serial port interrupt processing, the receiving end sends the received serial port data to the message queue, so that the time consumption of interrupt processing is reduced, and meanwhile, the serial port received data is separated from the data processing, and the condition that the data is lost due to serial port overflow is avoided. By means of the ring queue, the size of the queue can be set, and a receiving end can receive data sent by a sending end as much as possible. In the serial port reading task executed by the receiving end, the serial port can be periodically read, and the serial port can be read through the semaphore, so that the timeliness of serial port receiving can be guaranteed.
Referring to fig. 3, one method for framing is: directly reading related fields from the circular queue to perform framing; the method specifically comprises the following steps.
S11: reading the frame header from the circular queue.
S12: and judging whether the frame header is correct or not, if so, entering the step S13, and if not, returning to the step S11.
S13: the length is read from the circular queue.
S14: and judging whether the length is correct or not, if so, entering the step S15, and if not, returning to the step S11.
S15: and reading data and checksum from the circular queue.
S16: and judging whether the checksum is correct or not, if so, entering the step S17, and if not, returning to the step S11.
S17: the complete frame is received.
Under the condition that the sending end frequently sends data or the data amount is large, the receiving end can receive effective data as much as possible, the capability of acquiring complete data frames can be improved, and the probability of losing the effective data can be greatly reduced.
Referring to fig. 4, another method for framing includes: acquiring data from the annular queue, putting the data into a local array, and framing the data from the local array; the method specifically comprises the following steps.
S21: a large segment of data is read from the circular queue and placed in the local array.
S22: reading the data placed in the local array (the data is the data read from the circular queue in step S21), and determining whether valid data exists in the local array; if valid data is read, the process proceeds to step S23, and if valid data is not read, the process returns to step S21.
S23: the frame header is read from the local array.
S24: and judging whether the frame header is correct or not, if so, entering the step S25, and if not, returning to the step S23.
S25: the length is read from the local array.
S26: and judging whether the length is correct or not, if so, entering the step S27, and if not, removing the frame header of the current time from the local array and returning to the step S23.
S27: judging whether the length of the valid data left in the local array is smaller than the length (i.e. the length of the frame) read from the local array in step S25; if not, the process proceeds to step S28, and if not, the process returns to step S21.
S28: the data (which is the data referred to in the frame format), the checksum, is read from the local array.
S29: judging whether the checksum is correct or not; if the result is correct, the process goes to step S210, and if the result is incorrect, the frame header of this time is removed from the local array, and the process returns to step S23.
S210: the complete frame is received and the data for the frame at this time is removed from the local array.
Under the condition that the sending end frequently sends data or the data amount is large, the receiving end can receive effective data as much as possible, the capability of acquiring complete data frames can be improved, and the probability of losing the effective data can be greatly reduced.
The serial port is a data communication channel between the single chip microcomputer and the communication module, the receiving end is the single chip microcomputer or the communication module, and the corresponding sending end is the communication module or the single chip microcomputer.
In summary, the present invention provides a method for receiving data from a serial port and processing data frames, wherein in the serial port interrupt processing, a receiving end sends the received serial port data to a message queue, so as to reduce the time consumption of interrupt processing, and simultaneously separate the data received by the serial port from the data processing, thereby avoiding the situation that the data is lost due to the overflow of the serial port. By means of the ring queue, the size of the queue can be set, and a receiving end can receive data sent by a sending end as much as possible. In the serial port reading task executed by the receiving end, the serial port can be periodically read, and the serial port can be read through the semaphore, so that the timeliness of serial port receiving can be guaranteed.
The present invention is not limited to the above preferred embodiments, and any modifications, equivalent substitutions and improvements made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (4)

1. A method for receiving data from a serial port and processing data frames is characterized by comprising the following steps: the method comprises the following steps:
s1: initializing a serial port, and creating a serial port receiving task and a serial port reading task by a receiving end;
s2: after receiving data from the serial port, a receiving terminal executes serial port interrupt processing, and when the serial port interrupt processing is performed, the receiving terminal places the data received by the serial port in a message node and sends the data of the message node to a received message queue;
S3: the receiving end executes a serial port receiving task, namely continuously acquiring data of message nodes from a received message queue, then acquiring the data received by the serial port from the message nodes, and adding the data to the circular queue;
s4: the receiving end executes a serial port reading task, namely, data received by the serial port is read from the annular queue; framing the data read from the circular queue;
the data received by the serial port is read in the following mode: periodically reading, and taking out the ring queue every 5ms-500 ms; or, the mode of reading the data received by the serial port is as follows: in the process of executing the serial port receiving task, after data is successfully added into the circular queue, the serial port waiting for the semaphore is informed to read the task by releasing the semaphore, and the data is fetched from the circular queue.
2. The method of claim 1, wherein the frame format comprises: frame header, length, data and checksum, wherein the framing method comprises the following steps: directly reading related fields from the circular queue to perform framing; the method specifically comprises the following steps:
s11: reading a frame header from the circular queue;
s12: judging whether the frame header is correct, if so, entering the step S13, and if not, returning to the step S11;
S13: reading the length from the circular queue;
s14: judging whether the length is correct, if so, entering the step S15, and if not, returning to the step S11;
s15: reading data and a checksum from the circular queue;
s16: judging whether the checksum is correct or not, if so, entering the step S17, and if not, returning to the step S11;
s17: the complete frame is received.
3. The method of claim 1, wherein the frame format comprises: frame header, length, data and checksum, wherein the framing method comprises the following steps: acquiring data from the annular queue, putting the data into a local array, and framing the data from the local array; the method specifically comprises the following steps:
s21: reading a large segment of data from the circular queue and placing the large segment of data in a local array;
s22: reading data placed in the local array, and judging whether valid data exists in the local array; if valid data is read, the process goes to step S23, and if valid data is not read, the process returns to step S21;
s23: reading a frame header from a local array;
s24: judging whether the frame header is correct, if so, entering the step S25, and if not, returning to the step S23;
S25: reading the length from the local array;
s26: judging whether the length is correct, if so, entering the step S27, and if not, removing the frame header of the current time from the local array and returning to the step S23;
s27: judging whether the length of the remaining valid data in the local array is smaller than the length read from the local array in the step S25; if not, go to step S28, if yes, go back to step S21;
s28: reading data and a checksum from the local array;
s29: judging whether the checksum is correct or not; if the result is correct, the step S210 is carried out, if the result is incorrect, the frame header of the current time is removed from the local array, and the step S23 is returned;
s210: the complete frame is received and the data for the frame at this time is removed from the local array.
4. The method as claimed in claim 1, wherein the serial port is a data communication channel between the single chip and the communication module, and the receiving end is the single chip or the communication module.
CN202010669896.8A 2020-07-13 2020-07-13 Method for receiving data from serial port and processing data frame Pending CN111858447A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010669896.8A CN111858447A (en) 2020-07-13 2020-07-13 Method for receiving data from serial port and processing data frame

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010669896.8A CN111858447A (en) 2020-07-13 2020-07-13 Method for receiving data from serial port and processing data frame

Publications (1)

Publication Number Publication Date
CN111858447A true CN111858447A (en) 2020-10-30

Family

ID=72984771

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010669896.8A Pending CN111858447A (en) 2020-07-13 2020-07-13 Method for receiving data from serial port and processing data frame

Country Status (1)

Country Link
CN (1) CN111858447A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113824731A (en) * 2021-09-30 2021-12-21 深圳市卓翼科技股份有限公司 Serial port protocol frame analysis method and device and computer readable storage medium
CN114650217A (en) * 2022-05-19 2022-06-21 长沙竹叶电子科技有限公司 Serial port received data processing method, device, equipment and medium

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103095703A (en) * 2013-01-11 2013-05-08 瑞斯康达科技发展股份有限公司 Method and equipment and system for achieving data interaction between network and serial port
CN107222379A (en) * 2017-06-07 2017-09-29 南京南瑞继保电气有限公司 A kind of method and apparatus of serial communication
CN107302531A (en) * 2017-06-19 2017-10-27 青岛华潞佳电子科技有限公司 A kind of serial port command analytic method based on state machine and circle queue
US20180159963A1 (en) * 2015-07-22 2018-06-07 Huawei Technologies Co., Ltd. Computer device and method for reading or writing data by computer device
CN110019498A (en) * 2017-08-14 2019-07-16 北京京东尚科信息技术有限公司 Log synchronous method and device, storage medium, electronic equipment
CN110399229A (en) * 2018-04-25 2019-11-01 清华大学 Communication means, device, system, medium and terminal between process
CN110515881A (en) * 2019-08-23 2019-11-29 西安爱生技术集团公司 A kind of synchronization system and synchronization high performance method of serial communication frame
CN110704356A (en) * 2019-09-25 2020-01-17 天津津航计算技术研究所 Universal serial port data analyzing method

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103095703A (en) * 2013-01-11 2013-05-08 瑞斯康达科技发展股份有限公司 Method and equipment and system for achieving data interaction between network and serial port
US20180159963A1 (en) * 2015-07-22 2018-06-07 Huawei Technologies Co., Ltd. Computer device and method for reading or writing data by computer device
CN107222379A (en) * 2017-06-07 2017-09-29 南京南瑞继保电气有限公司 A kind of method and apparatus of serial communication
CN107302531A (en) * 2017-06-19 2017-10-27 青岛华潞佳电子科技有限公司 A kind of serial port command analytic method based on state machine and circle queue
CN110019498A (en) * 2017-08-14 2019-07-16 北京京东尚科信息技术有限公司 Log synchronous method and device, storage medium, electronic equipment
CN110399229A (en) * 2018-04-25 2019-11-01 清华大学 Communication means, device, system, medium and terminal between process
CN110515881A (en) * 2019-08-23 2019-11-29 西安爱生技术集团公司 A kind of synchronization system and synchronization high performance method of serial communication frame
CN110704356A (en) * 2019-09-25 2020-01-17 天津津航计算技术研究所 Universal serial port data analyzing method

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113824731A (en) * 2021-09-30 2021-12-21 深圳市卓翼科技股份有限公司 Serial port protocol frame analysis method and device and computer readable storage medium
CN114650217A (en) * 2022-05-19 2022-06-21 长沙竹叶电子科技有限公司 Serial port received data processing method, device, equipment and medium

Similar Documents

Publication Publication Date Title
CN111858447A (en) Method for receiving data from serial port and processing data frame
CA2328127C (en) System and method for facilitating inter-nodal protocol agreement in a telecommunications system
EP2926265B1 (en) Data processing device, data processing method, and program
CN113342484B (en) Process engine method, system, device and storage medium
CN111782473A (en) Distributed log data processing method, device and system
US20220182305A1 (en) Request Processing System and Method Thereof
US20040248559A1 (en) Process for transmitting status messages to terminals of a satellite data transmission system, in particular in a satellite navigation system
CA3088571A1 (en) Paging method, terminal device and network device
CN112908877A (en) Photovoltaic cell detection method and system, electronic device and storage medium
CN112636808A (en) Telescope remote data transmission and control method based on Beidou short message
CN108737352B (en) Wireless physical layer processing system and method compatible with multi-protocol and nonstandard protocol
CN115562898A (en) Distributed payment system exception handling method and device
CN111882051B (en) Global broadcast data input circuit for neural network processing
CN113886314A (en) Method and system for receiving and analyzing serial port GPS message data
JPH10303768A (en) Information reception device
JPH0965435A (en) Standby control system for digital mobile telephone set
CN109031368B (en) Method for reading GPS receiving output effective data by combining PPS signal
CN113473063A (en) Method and device for enabling smart watch to be compatible with multiple sets of video call SDKs
CN114064221A (en) Interrupt processing method, device, system, equipment and storage medium
CN110266749A (en) Order information method for pushing, device and terminal
CN113286330B (en) Data transmission method and device
CN112910958B (en) Method, device, communication system and storage medium for unifying feature vectors
CN102056214B (en) Method and device for determining network events in communication network
CN112149439B (en) Decoding self-alignment method, device and equipment for SWP physical layer S2
CN108474858A (en) A kind of sharing method and terminal of auxiliary positioning data

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination