CN113934521B - Serial data framing method, driving device and readable storage medium for UNIX system - Google Patents
Serial data framing method, driving device and readable storage medium for UNIX system Download PDFInfo
- Publication number
- CN113934521B CN113934521B CN202111089746.0A CN202111089746A CN113934521B CN 113934521 B CN113934521 B CN 113934521B CN 202111089746 A CN202111089746 A CN 202111089746A CN 113934521 B CN113934521 B CN 113934521B
- Authority
- CN
- China
- Prior art keywords
- serial port
- framing
- current
- byte
- data
- 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.)
- Active
Links
- 238000009432 framing Methods 0.000 title claims abstract description 120
- 238000000034 method Methods 0.000 title claims abstract description 55
- 230000006870 function Effects 0.000 description 17
- 238000004891 communication Methods 0.000 description 7
- 230000000903 blocking effect Effects 0.000 description 3
- 230000001133 acceleration Effects 0.000 description 2
- 238000010586 diagram Methods 0.000 description 2
- 238000005538 encapsulation Methods 0.000 description 2
- 230000005484 gravity Effects 0.000 description 2
- 230000005540 biological transmission Effects 0.000 description 1
- 230000006855 networking Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 239000000523 sample Substances 0.000 description 1
- 238000009987 spinning Methods 0.000 description 1
- 230000001960 triggered effect Effects 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4812—Task transfer initiation or dispatching by interrupt, e.g. masked
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F13/00—Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
- G06F13/10—Program control for peripheral devices
- G06F13/102—Program control for peripheral devices where the programme performs an interfacing function, e.g. device driver
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F13/00—Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
- G06F13/38—Information transfer, e.g. on bus
- G06F13/42—Bus transfer protocol, e.g. handshake; Synchronisation
- G06F13/4282—Bus transfer protocol, e.g. handshake; Synchronisation on a serial bus, e.g. I2C bus, SPI bus
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2213/00—Indexing scheme relating to interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
- G06F2213/0002—Serial port, e.g. RS232C
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Software Systems (AREA)
- Communication Control (AREA)
- Debugging And Monitoring (AREA)
Abstract
The embodiment of the invention provides a serial data framing method, a driving device and a readable storage medium of a UNIX system, wherein the serial data framing method of the UNIX system comprises the following steps: by adding the data framing function in the driver, the arrival interval time between byte data randomly sent by a high-real-time accurate resolution serial port of the driver is utilized, two bytes lower than the preset interval time are distributed to the same data frame, and bytes higher than the preset interval time are distributed to different data frames, so that the real-time of serial port data framing is improved to the level of a real-time operating system in a UNIX class non-real-time high-level operating system, and compared with the real-time of serial port data framing in an application program layer of the UNIX class system, the real-time of serial port data framing is improved to the level of millisecond from the level of hundred milliseconds, and meanwhile, when industrial control class products are designed, the UNIX class system can be selected to be finished, and the high-time timeliness required by industrial control can be met, and meanwhile, the high-level function of the operating system can be used to finish complex demand design.
Description
Technical Field
The present invention relates to the field of hardware communications, and in particular, to a serial port data framing method, a driving device and a readable storage medium for a UNIX system.
Background
With the development of industrial internet and the increasing complexity of industrial application, the requirement of networking for traditional serial terminal equipment is increasing. Many traditional devices transmit data through serial ports, and some devices such as sensor terminal devices are not suitable for supporting complex network interfaces due to own requirements, so that the data of the devices have great limitation in the use range and can only be locally linked with a remote server. The serial data is converted into network data, so that the serial data becomes a very general functional requirement. In the process from serial port data to network data, bytes received by serial ports need to be packaged into frames, but in the current UNIX (You Nake S) system, a common serial port data framing mode is realized by using equipment files for operating serial ports in an application program layer, and although the method is simpler, the serial port data framing performance is low and the speed is low, so that the industrial control high-performance framing requirement cannot be met.
Disclosure of Invention
The invention mainly aims to provide a serial data framing method of a UNIX system, which aims to solve the problems that serial data framing performance is low and speed is low in an application program layer and industrial control high-performance framing requirements cannot be met.
In order to achieve the above object, the present invention provides a serial data framing method of a UNIX system, wherein the serial data framing method of the UNIX system includes the following steps:
when a serial port triggers an interrupt program, acquiring a current serial port number of the serial port of the trigger interrupt program, and taking the serial port of the trigger interrupt program as a current serial port;
Locking a spin lock of the current serial port;
receiving a current byte sent by a current serial port, and storing the current byte as data into a buffer area;
Judging whether the current serial port is a debugging serial port or not through the current serial port number;
if the current byte is not the debug serial port, judging whether the received current byte is the first byte of a data frame or not;
if the received current byte is the first byte of a data frame, starting a framing timer of the current serial port, releasing a spin lock of the current serial port, and synchronously detecting whether a serial port triggering interrupt program exists or not;
If the framing timer reaches the preset time, triggering and executing a timing callback program, acquiring a serial number of a triggering timing callback serial port, and taking the triggering timing callback serial port as a current serial port;
Locking a spin lock of the current serial port;
pushing the data cached in the cache area to an application program layer as a data frame and releasing the spin lock.
Further, before the step of acquiring the current serial number of the serial port of the interrupt triggering program when the serial port triggers the interrupt, the method includes:
Configuring a framing timer for a non-debugging serial port of a loading driver based on user operation, wherein the framing timer parameters comprise: preset framing time and/or preset framing length upper limit.
Further, the storing the current byte into the buffer area includes:
And after storing the bytes into the buffer, recording the length of the bytes cached in the current buffer.
Further, after the step of determining whether the current serial port is the debug serial port according to the current serial port number, the method further includes:
If the current serial port is the debugging serial port, the data cached in the cache area is pushed to the application program layer and the spin lock is released.
Further, the determining whether the received current byte is the first byte of a data frame includes:
judging whether the length of the current buffer area stored byte of the record is 0;
if the length is 0, the received current byte is the first byte of a data frame;
If the length is not 0, the received current byte is not the first byte of a data frame.
Further, after the step of determining whether the received current byte is the first byte of a data frame, the method further includes:
if the received byte is not the first byte of a data frame, resetting the framing timer of the current serial port and releasing the spin lock.
Further, before the step of determining whether the received current byte is the first byte of a data frame, the step of determining includes:
Judging whether the byte length stored in the recorded buffer area is equal to the preset framing length upper limit;
And if the frame length is equal to the preset upper limit of the frame length, pushing the data cached in the cache area to an application program layer as a data frame.
Further, after the step of pushing the data cached in the cache area to the application layer as a data frame, the method includes:
Resetting the byte length of the current buffer to 0 and stopping the framing timer.
In addition, the present application also provides a driving device including: the system comprises a memory, a processor and a UNIX system serial data framing program which is stored in the memory and can run on the processor, wherein the UNIX system serial data framing program realizes the steps of the UNIX system serial data framing method when being executed by the processor.
In addition, the application also provides a computer readable storage medium, the readable storage medium stores a serial port data framing program of the UNIX system, and the serial port data framing program of the UNIX system realizes the steps of the serial port data framing method of the UNIX system when being executed by a processor.
According to the serial port data framing method of the UNIX system, the data framing function is added in the driver, the arrival interval time between byte data randomly sent by a serial port is resolved accurately in high instantaneity of the driver, two bytes lower than the preset interval time are distributed to the same data frame, and bytes higher than the preset interval time are distributed to different data frames, so that timeliness of serial port data framing is improved to the level of the real-time operating system in the UNIX non-real-time advanced operating system, and compared with timeliness of serial port data framing in an application program layer of the UNIX system, timeliness of serial port data framing is improved by 2 orders of magnitude, and is improved to a millisecond level from hundred milliseconds. Meanwhile, when industrial control products are designed, the UNIX system can be selected to be completed, and advanced functions of an operating system can be used to complete complex demand design while high timeliness required by industrial control is met.
Drawings
FIG. 1 is a schematic diagram of a driving device of a hardware running environment according to an embodiment of the present invention;
FIG. 2 is a flow chart of a method for framing serial data of a UNIX-like system according to an embodiment of the present invention;
FIG. 3 is a detailed flowchart of an alternative embodiment of the method for framing serial data of UNIX system according to the present invention after step S40;
Fig. 4 is a detailed flowchart of another alternative embodiment after step S30 in the serial data framing method of the UNIX system according to the present invention.
The achievement of the objects, functional features and advantages of the present invention will be further described with reference to the accompanying drawings, in conjunction with the embodiments.
Detailed Description
It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention.
The main solutions of the embodiments of the present invention are: the serial data is framed based on a serial driver with high real-time performance running in a kernel mode in a UNIX-based system. The driver divides frames by judging the interval time between bytes, divides the bytes with interval time lower than a set threshold value into the same group of data frames, and pushes the packaged data frames to the application program.
Because the serial data framing method commonly used in the prior art carries out framing on an application program layer and is realized by operating equipment files of a serial port, although the method is simpler, the real-time performance of the application program is poorer, so that the serial data framing performance is low and the speed is low by using the application program, and the industrial control high-performance framing requirement cannot be met.
The invention provides a solution, which uses a serial port driver with high real-time performance to complete framing work in a UNIX system, improves the real-time performance of serial port data framing to the level of a real-time operation system, and improves the real-time performance of serial port data framing by 2 orders of magnitude from hundred milliseconds to millisecond compared with the application program layer of the UNIX system.
Referring to fig. 1, fig. 1 is a schematic diagram of an apparatus structure of a hardware running environment according to an embodiment of the present invention.
The device of the embodiment of the invention can be a PC, or can be an electronic terminal device or device with a driving function such as a smart phone, a tablet personal computer, a portable computer and the like.
As shown in fig. 1, the apparatus may include: a processor 1001, such as a CPU, a network interface 1004, a user interface 1003, a memory 1005, a communication bus 1002. Wherein the communication bus 1002 is used to enable connected communication between these components. The user interface 1003 may include a Display, an input unit such as a Keyboard (Keyboard), and the optional user interface 1003 may further include a standard wired interface, a wireless interface. The network interface 1004 may optionally include a standard wired interface, a wireless interface (e.g., WI-FI interface). The memory 1005 may be a high-speed RAM memory or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also optionally be a storage device separate from the processor 1001 described above.
Optionally, the apparatus may further include a camera, an RF (Radio Frequency) circuit, a sensor, an audio circuit, a WiFi module, and the like. Among other sensors, such as light sensors, motion sensors, and other sensors. Specifically, the light sensor may include an ambient light sensor that may adjust the brightness of the display screen according to the brightness of ambient light, and a proximity sensor that may turn off the display screen and/or the backlight when the mobile terminal moves to the ear. As one of the motion sensors, the gravity acceleration sensor can detect the acceleration in all directions (generally three axes), and can detect the gravity and the direction when the mobile terminal is stationary, and the mobile terminal can be used for recognizing the gesture of the mobile terminal (such as horizontal and vertical screen switching, related games, magnetometer gesture calibration), vibration recognition related functions (such as pedometer and knocking), and the like; of course, the mobile terminal may also be configured with other sensors such as a gyroscope, a barometer, a hygrometer, a thermometer, an infrared sensor, and the like, which are not described herein.
It will be appreciated by those skilled in the art that the device structure shown in fig. 1 is not limiting of the device and may include more or fewer components than shown, or may be combined with certain components, or a different arrangement of components.
As shown in fig. 1, an operating system, a network communication module, a user interface module, and a UNIX-type system serial data framing program may be included in a memory 1005 as a computer storage medium.
In the terminal shown in fig. 1, the network interface 1004 is mainly used for connecting to a background server and performing data communication with the background server; the user interface 1003 is mainly used for connecting a client (user side) and performing data communication with the client; and the processor 1001 may be configured to invoke the UNIX-like system serial data framing program stored in the memory 1005, and perform the following operations:
when a serial port triggers an interrupt program, acquiring a current serial port number of the serial port of the trigger interrupt program, and taking the serial port of the trigger interrupt program as a current serial port;
Locking a spin lock of the current serial port;
receiving a current byte sent by a current serial port, and storing the current byte as data into a buffer area;
Judging whether the current serial port is a debugging serial port or not through the current serial port number;
if the current byte is not the debug serial port, judging whether the received current byte is the first byte of a data frame or not;
if the received current byte is the first byte of a data frame, starting a framing timer of the current serial port, releasing a spin lock of the current serial port, and synchronously detecting whether a serial port triggering interrupt program exists or not;
If the framing timer reaches the preset time, triggering and executing a timing callback program, acquiring a serial number of a triggering timing callback serial port, and taking the triggering timing callback serial port as a current serial port;
Locking a spin lock of the current serial port;
pushing the data cached in the cache area to an application program layer as a data frame and releasing the spin lock.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
the step of acquiring the current serial number of the serial port of the triggering interrupt program when the serial port triggers the interrupt program comprises the following steps:
Configuring a framing timer for a non-debugging serial port of a loading driver based on user operation, wherein the framing timer parameters comprise: preset framing time and/or preset framing length upper limit.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
The storing the current byte into the buffer area includes:
And after storing the bytes into the buffer, recording the length of the bytes cached in the current buffer.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
After the step of judging whether the current serial port is the debug serial port according to the current serial port number, the method further comprises:
If the current serial port is the debugging serial port, the data cached in the cache area is pushed to the application program layer and the spin lock is released.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
the determining whether the received current byte is the first byte of a data frame includes:
judging whether the length of the current buffer area stored byte of the record is 0;
if the length is 0, the received current byte is the first byte of a data frame;
If the length is not 0, the received current byte is not the first byte of a data frame.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
After the step of determining whether the received current byte is the first byte of a data frame, the method further includes:
if the received byte is not the first byte of a data frame, resetting the framing timer of the current serial port and releasing the spin lock.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
The step of determining whether the received current byte is the first byte of a data frame includes:
Judging whether the byte length stored in the recorded buffer area is equal to the preset framing length upper limit;
And if the frame length is equal to the preset upper limit of the frame length, pushing the data cached in the cache area to an application program layer as a data frame.
Further, the processor 1001 may call the UNIX-class system serial data framing program stored in the memory 1005, and further perform the following operations:
After the step of pushing the data cached in the cache area to the application program layer as a data frame, the method comprises the following steps:
Resetting the byte length stored in the current buffer to 0 and stopping the framing timer.
Referring to fig. 2, a first embodiment of the present invention provides a serial data framing method of a UNIX system, where the serial data framing method of the UNIX system includes:
Step S10, when any serial port triggers an interrupt program, acquiring a current serial port number of the serial port of the trigger interrupt program, taking the serial port of the trigger interrupt program as a current serial port, and locking a spin lock of the current serial port;
It can be understood that the implementation subject of the serial data encapsulation into frames in this embodiment is a serial driver in UNIX systems, which are all non-real-time operating systems, i.e. the real-time characteristics of such operating systems are inferior to those of real-time operating systems. In the UNIX system, the application program of the user runs in the user mode, and enters the kernel mode when the application program calls the system program, the real-time performance of the kernel mode is higher than that of the user mode, and the serial driver is usually the system program running in the kernel mode.
For more clear explanation of the technical scheme of the application, in the embodiment, a NXP-i.MX6ULL hardware platform and a Linux system (the Linux system belongs to a UNIX system) are taken as examples to be explained. Compared with the current serial port driver, the embodiment adds a timer configuration interface in the probe function of the driver mount of the kernel serial port driver, which is used for configuring related parameters of serial port framing.
When the serial port of the hardware device generates data (the most basic unit of the data generated by the serial port is a byte, and each byte may occur randomly), for the receiving end of the serial port data, it is not known when and how much data will be generated to trigger the interrupt program, the driver will acquire the serial port number of the serial port of the trigger interrupt program, and the serial port is used as the current serial port. And simultaneously, in order to ensure that the interrupt program uses the shared resource of the current serial port, the lock is locked in the interrupt program, the lock represents the use authority of the shared resource, and other processes which want to acquire the use of the shared resource by the lock enter a spinning state.
Step S20, receiving a current byte sent by a current serial port, and storing the byte into a buffer area;
and storing the bytes generated by the receiving trigger serial port in a buffer memory area of the serial port driver.
Step S30, judging whether the current serial port is a debugging serial port or not through the current serial port number, and judging whether the received current byte is the first byte of a data frame or not if the current serial port is not the debugging serial port;
In order to realize that a driver has a framing function, when a serial port driver is loaded, initializing a timer of a serial port of a non-system debugging serial port through an added timer configuration interface comprises the following steps: a timing callback program (which is used for pushing framed data to an application program) to be executed after the binding timing time arrives; timing time of the timer; adding the serial number of the non-system debug serial port to a data field; the configured timer is added to the timer queue of the system. In addition, a parameter configuration interface of a serial port driving layer is added in the kernel serial port driving program: imx _ioctl. The timing time of the framing timer and the length of the data frame can be configured through the interface. The non-debug serial port for loading the driver is thus configured with framing timers based on user operations, the framing timer parameters including: preset framing time and/or preset framing length upper limit.
The driver program can judge whether the serial port corresponding to the serial port number is a debugging serial port according to the obtained serial port number (the serial port for debugging can be marked in a preset mode on the serial port name, and is not limited in the preset mode), if the serial port is judged not to be the debugging serial port through the serial port name, whether the byte generated by the serial port received by the driver program is the first byte in a new frame is further judged, and if the buffer area of the driver program is any byte in a buffer before the byte is received, the first byte of the new frame is judged.
In addition, if the current serial port is a debugging serial port, the data of the buffer area is pushed to an application program layer and the spin lock is released. That is, when the obtained serial number is the debug serial port, a timer will not be configured for the serial port, and the data driver received from the serial port will be directly pushed to the application layer, and the steps described below will be stopped at the same time. In order to prevent the original functional logic of the debugging serial port of the Linux system from being affected after the driver adds the framing function of the driver layer.
Step S40, if the received current byte is the first byte of a data frame, starting a framing timer of the current serial port, releasing a spin lock of the current serial port, and synchronously detecting whether a serial port triggering interrupt program exists or not;
in this embodiment, a framing timer start function is inserted into the serial interrupt receiving interface imx _ rxint of the kernel serial driver.
When the byte received by the driver is judged to be the first byte of a new frame, a framing timer is started, and the next byte is waited to be received, namely whether a serial port triggering interrupt program exists or not is detected. And after the framing timing is started, releasing the lock of the interrupt program, namely releasing the serial port shared resource used by the interrupt program.
Step S50, if the framing timer reaches the preset time, triggering and executing a timing callback program, obtaining a serial number of a triggering timing callback serial port, taking the triggering timing callback serial port as a current serial port, and locking a spin lock of the current serial port;
When the framing timer of the non-system debugging serial port reaches the preset time, namely when the interval time of two bytes exceeds the preset time, triggering a timing callback program, firstly acquiring the serial number of the triggering timing callback serial port, taking the serial port corresponding to the serial number as the current serial port, and locking the spin lock of the current serial port.
Step S60, pushing the data cached in the cache area to an application program layer as a data frame and releasing the spin lock.
The data which is already buffered in the current buffer area is sent to the application program layer as a data frame, and the data received from the application program layer at this time is already in the unit of frame. In addition, in order to realize the function of driving to frame to the application program layer in a high-efficient and quick way, a kernel tty layer (TELETYPES, originally: an electronic typewriter) of the driver is divided into three layers of a character device layer, a tty layer and a serial port hardware layer, and the tty layer implements decoupling with a bottom layer (i.e. the serial port hardware layer) and an upper layer (i.e. the character device layer) through abstraction of an intermediate process, so that the timeout time of a tty read interface of the driver is reduced, specifically: in the drive file n_tty.c of the tty layer of the Linux system, an n_tty_read interface function is found, and the timeout time under the condition of blocking the received data is modified from 100 milliseconds to 1 millisecond. Meanwhile, a driving program with an efficient framing function is synchronously added, parameters such as serial port baud rate and the like are opened and configured in an application program, and a receiving mode is blocking, wherein the specific mode is as follows: when a certain serial port is used, a corresponding serial port numbered equipment file is opened in a blocking terminal mode, after conventional parameters such as the baud rate, check bit, data bit, stop bit and the like of the serial port are configured, parameters of framing time and framing length upper limit are configured through an ioctl interface, and then serial port data are waited to be received, and when the data are received, a correct data frame is received.
In this embodiment, the parameter configuration interface of the serial port driving layer is added in the kernel serial port driving program, the timer configuration interface is added, and the framing timer starting function is inserted. Compared with the framing of the application program, the framing function of the driver program is higher in instantaneity, and can accurately distinguish the arrival interval time between byte data randomly transmitted by the serial port, so that two bytes lower than the preset interval time are allocated to the same data frame, and bytes higher than the preset interval time are allocated to different data frames. And meanwhile, the application program is correspondingly modified and configured to cooperate with receiving the data frames sent by the driver program with the framing added function. The method has the advantages that in the UNIX-type non-real-time advanced operating system, the real-time performance of serial data framing is improved to the level of the real-time operating system, and compared with the real-time performance of serial data framing in the application program layer of the UNIX-type system, the method has the advantages that the real-time performance of serial data framing is improved by 2 orders of magnitude, and the hundred millisecond level is improved to the millisecond level. Meanwhile, when industrial control products are designed, the UNIX system can be selected to be completed, and advanced functions of an operating system can be used to complete complex demand design while high timeliness required by industrial control is met.
Optionally, referring to fig. 3, in a second embodiment of the present invention, after the step of storing bytes in the buffer, the serial port data framing method of the UNIX system further includes:
Step S20, receiving bytes sent by a current serial port, and storing the bytes into a buffer area;
Receiving bytes generated by the trigger serial port, storing the bytes into the buffer area, and recording the length of the bytes stored in the current buffer area. The byte length may indicate the number of bytes already stored in the current buffer and may be used in the next step of determining the number of bytes.
Step S30, judging whether the current serial port is a debugging serial port or not through the current serial port number, and judging whether the received current byte is the first byte of a data frame or not if the current serial port is not the debugging serial port;
if the current serial port is not the debugging serial port, judging whether the length of the recorded current buffer area storage bytes is 0; if the length is 0, the received byte is the first byte of a frame; if the length is not 0, the received byte is not the first byte of a frame.
If the current serial port is the debugging serial port, pushing the data of the buffer area to an application layer and releasing the spin lock. And initializing a framing timer and a buffer area, and ending the current program process. The function logic of the original debugging serial port of the Linux system is not affected after the driving program is added with the driving layer framing function.
In step S41, if the received byte is not the first byte of a data frame, the framing timer of the current serial port is reset and the spin lock is released.
If the received byte is not the first byte of the new frame, in this case, it is assumed that in the first embodiment, the first byte of a frame already causes the framing timer to start, in this embodiment, the received byte is the second byte generated by the serial port after the first byte is generated, and the second byte is received by the driver after the framing timer starts and before the framing timer reaches the preset time, the driver starts to execute step S10, step S20, step S30 and step S40 again, and it is determined that the first byte still exists in the buffer area and the framing timer does not reach the preset time, the first byte and the second byte are separated into the same frame, and then the framing timer time is reset to 0 to restart timing and release the spin lock, and start to wait for the next byte to receive. Similarly, if a third byte is received before the framing timing reaches the timing time, the byte and the first and second bits of the buffer are divided into the same frame, the framing timer is reset, the spin lock is released to wait for the next byte to be received, and the cycle is ended until the framing timer reaches a preset value.
Step S50, if the framing timer reaches the preset time, triggering and executing a timing callback program, obtaining a serial number of a triggering timing callback serial port, taking the triggering timing callback serial port as a current serial port, and locking a spin lock of the current serial port;
When the framing timer of the non-system debugging serial port reaches the preset time, namely when the interval time of two bytes exceeds the preset time, triggering a timing callback program, firstly acquiring the serial number of the triggering timing callback serial port, taking the serial port corresponding to the serial number as the current serial port, and locking the spin lock of the current serial port.
Step S60, pushing the data cached in the cache area to an application program layer as a data frame and releasing the spin lock;
Step S61, resetting the byte length stored in the current buffer to 0 and stopping the framing timer;
When the driver pushes the data stored in the buffer to the application layer, it represents that the transmission of the one frame is completed, and therefore the byte length stored in the buffer is reset to 0, and the framing timer is stopped. The framing timer and the initialization buffer are initialized for the next data frame of the split-charging.
In this embodiment, after the first byte sent by the serial port has been triggered to start the framing timer, the timer starts to count, and before the timing time arrives, the second byte is received and is buffered as a frame, and the bytes below the timing time interval are divided into the same frame in a sub-cycle until the timing time of the timer arrives, and no new byte is received. And pushing the cached bytes to an application program layer as a data frame, and resetting a timer and a buffer area after pushing is completed. The high real-time performance of the driver program is used for accurately distinguishing the time interval between byte data in the serial random data stream and completing the encapsulation of the data frame.
Optionally, referring to fig. 4, in a third embodiment of the present invention, after the step of storing bytes in the buffer, the serial port data framing method of the UNIX system further includes:
step S31, judging whether the current serial port is a debugging serial port or not through the serial port number;
if the current serial port is the debugging serial port, pushing the data of the buffer area to an application layer and releasing the spin lock. And initializing a framing timer and a buffer area, and ending the current program process.
If the current serial port is not the debug serial port, step S32 is executed.
Step S32, judging whether the byte length stored in the recorded buffer area is equal to the preset framing length upper limit;
And if the frame length is equal to the preset upper limit of the frame length, pushing the data cached in the cache area to an application program layer as a data frame. Before judging whether the received byte is the first byte of a frame, judging whether the byte length recorded in the buffer area is equal to the preset upper limit of the frame length, and preventing the excessive bytes contained in the frame.
If the current byte is not equal to the preset frame length upper limit, the step of judging whether the received current byte is the first byte of a data frame in step S30 is executed.
Step S61, if the byte length is equal to the preset frame length upper limit, pushing the data buffered in the buffer as a data frame to the application layer, resetting the byte length stored in the current buffer to 0, and stopping the frame timer.
In this embodiment, a step of determining whether the byte length recorded in the buffer is greater than a preset frame length upper limit is added before determining whether the received byte is the first byte of a frame, so as to prevent an excessive number of bytes contained in a data frame.
In addition, the present application also provides a driving device including: the system comprises a memory, a processor and a UNIX system serial data framing program which is stored in the memory and can run on the processor, wherein the UNIX system serial data framing program realizes the steps of the UNIX system serial data framing method when being executed by the processor.
In addition, the application also provides a computer readable storage medium, the readable storage medium stores a serial port data framing program of the UNIX system, and the serial port data framing program of the UNIX system realizes the steps of the serial port data framing method of the UNIX system when being executed by a processor.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or system. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or system that comprises the element.
The foregoing embodiment numbers of the present invention are merely for the purpose of description, and do not represent the advantages or disadvantages of the embodiments.
From the above description of the embodiments, it will be clear to those skilled in the art that the above-described embodiment method may be implemented by means of software plus a necessary general hardware platform, but of course may also be implemented by means of hardware, but in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art in the form of a software product stored in a storage medium (e.g. ROM/RAM, magnetic disk, optical disk) as described above, comprising instructions for causing a terminal device (which may be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to perform the method according to the embodiments of the present invention.
The foregoing description is only of the preferred embodiments of the present invention, and is not intended to limit the scope of the invention, but rather is intended to cover any equivalents of the structures or equivalent processes disclosed herein or in the alternative, which may be employed directly or indirectly in other related arts.
Claims (10)
1. The serial data framing method of the UNIX system is characterized by comprising the following steps of:
when a serial port triggers an interrupt program, acquiring a current serial port number of the serial port of the trigger interrupt program, and taking the serial port of the trigger interrupt program as a current serial port;
Locking a spin lock of the current serial port;
receiving a current byte sent by a current serial port, and storing the current byte as data into a buffer area;
Judging whether the current serial port is a debugging serial port or not through the current serial port number;
if the current byte is not the debug serial port, judging whether the received current byte is the first byte of a data frame or not;
if the received current byte is the first byte of a data frame, starting a framing timer of the current serial port, releasing a spin lock of the current serial port, and synchronously detecting whether a serial port triggering interrupt program exists or not;
If the framing timer reaches the preset time, triggering and executing a timing callback program, acquiring a serial number of a triggering timing callback serial port, and taking the triggering timing callback serial port as a current serial port;
Locking a spin lock of the current serial port;
pushing the data cached in the cache area to an application program layer as a data frame and releasing the spin lock.
2. The method for framing serial data of UNIX-like system as claimed in claim 1, wherein said step of obtaining a current serial number of a serial port of a trigger interrupt program when the serial port triggers the interrupt program is preceded by:
Configuring a framing timer for a non-debugging serial port of a loading driver based on user operation, wherein the framing timer parameters comprise: preset framing time and/or preset framing length upper limit.
3. The method for framing serial data of UNIX-like system as recited in claim 2, wherein said storing said current byte in a buffer includes:
And after storing the bytes into the buffer, recording the length of the bytes cached in the current buffer.
4. The method for framing serial data of UNIX-like system as recited in claim 3, further comprising, after said step of determining whether the current serial port is a debug serial port by using the current serial port number:
If the current serial port is the debugging serial port, the data cached in the cache area is pushed to the application program layer and the spin lock is released.
5. The method of framing serial data of a UNIX-like system as recited in claim 4, wherein said determining whether said current byte received is a first byte of a data frame comprises:
judging whether the length of the current buffer area stored byte of the record is 0;
if the length is 0, the received current byte is the first byte of a data frame;
If the length is not 0, the received current byte is not the first byte of a data frame.
6. The method for framing serial data of UNIX-like system as recited in claim 5, further comprising, after said step of determining whether said current byte received is the first byte of a data frame:
if the received byte is not the first byte of a data frame, resetting the framing timer of the current serial port and releasing the spin lock.
7. The method for framing serial data of UNIX-like system as recited in claim 6, wherein said step of determining if said current byte received is the first byte of a data frame is preceded by the steps of:
Judging whether the byte length stored in the recorded buffer area is equal to the preset framing length upper limit;
And if the frame length is equal to the preset upper limit of the frame length, pushing the data cached in the cache area to an application program layer as a data frame.
8. The method for framing serial data of UNIX-like system as recited in claim 7, wherein after said step of pushing the data buffered in the buffer as a data frame to the application layer, the method comprises:
Resetting the byte length stored in the current buffer to 0 and stopping the framing timer.
9. A driving device, characterized in that the driving device comprises: the method for framing serial data of a UNIX system according to any one of claims 1 to 8, comprising a memory, a processor and a serial data framing program of a UNIX system stored on the memory and executable on the processor, wherein the serial data framing program of the UNIX system is executed by the processor.
10. A computer readable storage medium, wherein a UNIX-like system serial data framing program is stored on the readable storage medium, and the UNIX-like system serial data framing program, when executed by a processor, implements the steps of the UNIX-like system serial data framing method as claimed in any one of claims 1 to 8.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111089746.0A CN113934521B (en) | 2021-09-16 | 2021-09-16 | Serial data framing method, driving device and readable storage medium for UNIX system |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111089746.0A CN113934521B (en) | 2021-09-16 | 2021-09-16 | Serial data framing method, driving device and readable storage medium for UNIX system |
Publications (2)
Publication Number | Publication Date |
---|---|
CN113934521A CN113934521A (en) | 2022-01-14 |
CN113934521B true CN113934521B (en) | 2024-06-04 |
Family
ID=79276080
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202111089746.0A Active CN113934521B (en) | 2021-09-16 | 2021-09-16 | Serial data framing method, driving device and readable storage medium for UNIX system |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN113934521B (en) |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101493808A (en) * | 2009-02-26 | 2009-07-29 | 华为技术有限公司 | Serial using method and multi-core processor |
CN102629240A (en) * | 2012-02-13 | 2012-08-08 | 上海创远仪器技术股份有限公司 | Method and device for serial communication |
CN107423239A (en) * | 2017-05-05 | 2017-12-01 | 山东大学 | Low-power scm complete data frame method of reseptance under Halt patterns |
CN110351281A (en) * | 2019-07-15 | 2019-10-18 | 珠海格力电器股份有限公司 | Universal data frame analysis method, device and equipment |
CN111651256A (en) * | 2020-05-31 | 2020-09-11 | 西安爱生技术集团公司 | Serial communication data synchronization method based on FreeRTOS |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US7065598B2 (en) * | 2002-12-20 | 2006-06-20 | Intel Corporation | Method, system, and article of manufacture for adjusting interrupt levels |
-
2021
- 2021-09-16 CN CN202111089746.0A patent/CN113934521B/en active Active
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101493808A (en) * | 2009-02-26 | 2009-07-29 | 华为技术有限公司 | Serial using method and multi-core processor |
CN102629240A (en) * | 2012-02-13 | 2012-08-08 | 上海创远仪器技术股份有限公司 | Method and device for serial communication |
CN107423239A (en) * | 2017-05-05 | 2017-12-01 | 山东大学 | Low-power scm complete data frame method of reseptance under Halt patterns |
CN110351281A (en) * | 2019-07-15 | 2019-10-18 | 珠海格力电器股份有限公司 | Universal data frame analysis method, device and equipment |
CN111651256A (en) * | 2020-05-31 | 2020-09-11 | 西安爱生技术集团公司 | Serial communication data synchronization method based on FreeRTOS |
Also Published As
Publication number | Publication date |
---|---|
CN113934521A (en) | 2022-01-14 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN107276789B (en) | Log uploading method and device and computer readable storage medium | |
CN107590057B (en) | Screen freezing monitoring and solving method, mobile terminal and computer readable storage medium | |
US8296445B1 (en) | Software testing harness | |
US8336029B1 (en) | Debugger connection | |
CN107678876B (en) | Screen freezing monitoring and solving method, mobile terminal and computer readable storage medium | |
CN107861817B (en) | Thread blocking-based memory optimization method, mobile terminal and readable storage medium | |
US20160315999A1 (en) | Device and method for associating applications | |
CN107704133B (en) | Screen freezing monitoring and solving method, mobile terminal and computer readable storage medium | |
US8959394B2 (en) | Electronic device, system, and method for testing exception handling mechanism | |
CN107704363B (en) | Screen freezing monitoring method, mobile terminal and computer readable storage medium | |
CN107885635B (en) | Black screen detection method, mobile terminal and computer readable storage medium | |
US20160274892A1 (en) | Data update method, user terminal, and data updated system | |
CN109002547B (en) | Log file storage method, mobile terminal and computer readable storage medium | |
US9582584B2 (en) | Method, apparatus and system for filtering data of web page | |
CN107908491B (en) | Card screen detection and solution method, mobile terminal and computer readable storage medium | |
CN107765922B (en) | Card screen detection and solution method, mobile terminal and computer readable storage medium | |
CN107766222B (en) | Black screen detection method, mobile terminal and computer readable storage medium | |
EP3584700A1 (en) | Fingerprint recognition method and related product | |
CN107818036B (en) | Black screen detection method, mobile terminal and computer readable storage medium | |
CN113934521B (en) | Serial data framing method, driving device and readable storage medium for UNIX system | |
CN107861827B (en) | Card screen detection method, mobile terminal and computer readable storage medium | |
CN107908492B (en) | Black screen detection method, mobile terminal and computer readable storage medium | |
CN107861826B (en) | Screen freezing monitoring and solving method, mobile terminal and computer readable storage medium | |
CN107872367B (en) | Black screen detection method, mobile terminal and computer readable storage medium | |
CN107832191B (en) | Black screen detection method, mobile terminal and computer readable storage medium |
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 | ||
GR01 | Patent grant | ||
GR01 | Patent grant |