CN114915365A - Time synchronization method and medium - Google Patents
Time synchronization method and medium Download PDFInfo
- Publication number
- CN114915365A CN114915365A CN202210634507.7A CN202210634507A CN114915365A CN 114915365 A CN114915365 A CN 114915365A CN 202210634507 A CN202210634507 A CN 202210634507A CN 114915365 A CN114915365 A CN 114915365A
- Authority
- CN
- China
- Prior art keywords
- index
- data packet
- time
- data
- time synchronization
- 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.)
- Withdrawn
Links
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04J—MULTIPLEX COMMUNICATION
- H04J3/00—Time-division multiplex systems
- H04J3/02—Details
- H04J3/06—Synchronising arrangements
- H04J3/0635—Clock or time synchronisation in a network
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D30/00—Reducing energy consumption in communication networks
- Y02D30/70—Reducing energy consumption in communication networks in wireless communication networks
Landscapes
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Synchronisation In Digital Transmission Systems (AREA)
- Signal Processing For Digital Recording And Reproducing (AREA)
Abstract
The invention provides a time synchronization method and a medium, comprising the following steps: step S1: the MCU collects data and sends the data with a fixed data sending period T mcu Sending a data packet to the PC, and adding 1 to the Index value of the data packet every time the data packet is sent; step S2: the PC receives the data packet and sets a fixed time synchronization period T pc At a time T pc In the method, when the PC receives the first byte of the first data packet, the PC records the current system time T sys And recording the Index value Index of the first data packet 1 According to T sys Byte length and baud rate, calculating the first data packet sending time, i.e. reference time T base (ii) a Step S3: at a T pc In the PC, each time a packet is received, the PC will determine the Index value Index of the packet according to the Index value Index of the packet n And T base And Index 1 Calculating the sending time of the data packet; step S4: through a T pc Thereafter, execution of step S2 is resumed to update the reference time. The invention can ensure the real-time performance of data and improve the sending frequency of the MCU data packet。
Description
Technical Field
The present invention relates to the field of communications technologies, and in particular, to a time synchronization method and medium.
Background
In a system involving information processing of multiple sensors, it is generally necessary to form accurate timestamps of the acquisition times of sensor data, that is, to perform time synchronization, so as to facilitate data analysis and processing of the system.
The prior art has the following technical defects: the prior art generally needs time synchronization through a network, and is not friendly to a sensor without a network module and has high cost.
The invention patent with publication number CN106604383B discloses a time synchronization method, a master time synchronization device and a communication system, wherein the master time synchronization device synchronizes local frame information with frame information of a host base station through an air interface synchronization module, and then converts the synchronized frame information to obtain PTP time and then times; during synchronization, the master time synchronizer acquires the local current PTP time and inserts a time synchronization message and then sends the time synchronization message to the slave base station to be synchronized; and after receiving the time synchronization message from the base station, the slave station completes time synchronization according to the PTP time in the time synchronization message.
Disclosure of Invention
In view of the drawbacks of the prior art, the present invention provides a time synchronization method and medium.
According to the time synchronization method and the medium provided by the invention, the scheme is as follows:
in a first aspect, a time synchronization method is provided, and the method includes:
step S1: the MCU collects data and sends the data with a fixed data sending period T mcu Sending a data packet to a PC, and adding 1 to the Index value of the data packet every time the data packet is sent;
step S2: the PC receives the data packet and sets a fixed time synchronization period T pc During a time synchronization period, the PC records the current system time T immediately when it receives the first byte of the first data packet sys And recording the first numberIndex value Index of packet 1 Then according to T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base ;
Step S3: during a time synchronization period, every time a PC receives a data packet, the PC will determine the Index value Index of the data packet according to the Index value Index of the data packet n And T base And Index 1 Calculating the sending time of the data packet;
step S4: after a time synchronization period, the execution of step S2 is resumed to update the reference time.
Preferably, the format and length of each packet is fixed, and includes a header, an Index, data, and a trailer.
Preferably, the step S2 is based on T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base The method comprises the following steps:
one byte comprises a start bits, b data bits, c check bits and d stop bits, the baud rate is Bd, and the time required for transmitting one byte is T byte =(a+b+c+d)/Bd;
Then T base =T sys –T byte 。
Preferably, the Index value Index of the data packet is used in step S3 n And T base And Index 1 Calculating the sending time T of the data packet n The method comprises the following steps:
the Index occupies m bytes in each data packet, and the Index value is 256 at most m -1;
If Index n ≥Index 1 Then T is n =T base +(Index n -Index 1 )·T mcu ;
If Index n <Index 1 Then T is n =T base +(256 m +Index n -Index 1 )·T mcu 。
Preferably, the time synchronization period T of the PC pc Data transmission period T greater than MCU mcu 。
Preferably, T pc /T mcu <256 m 。
In a second aspect, a computer-readable storage medium is provided, in which a computer program is stored, which computer program, when being executed by a processor, carries out the steps of the method.
Compared with the prior art, the invention has the following beneficial effects:
1. according to the invention, in a simple, practical and low-cost manner, the specific time when the MCU sends the data packet to the PC is confirmed under the condition of meeting the precision requirement, so that a high-precision timestamp is formed, the real-time property of data is ensured, and the data analysis by the PC is facilitated;
2. the invention only uses the mode of unidirectional transmission from the MCU to the PC, and does not need the PC to send a data reading instruction, thereby improving the sending frequency of the MCU data packet.
Drawings
Other features, objects and advantages of the invention will become more apparent upon reading of the detailed description of non-limiting embodiments with reference to the following drawings:
FIG. 1 is a schematic flow diagram of the present invention;
FIG. 2 is a diagram of a data packet format according to the present invention;
FIG. 3 is a diagram illustrating the length of a data packet according to an embodiment of the present invention;
fig. 4 is a schematic diagram of a packet length according to an embodiment of the present invention.
Detailed Description
The present invention will be described in detail with reference to specific examples. The following examples will assist those skilled in the art in further understanding the invention, but are not intended to limit the invention in any way. It should be noted that it would be obvious to those skilled in the art that various changes and modifications can be made without departing from the spirit of the invention. All falling within the scope of the present invention.
An embodiment of the present invention provides a time synchronization method, which, referring to fig. 1, specifically includes:
step S1: the MCU collects data and sends the data with a fixed data sending period T mcu And sending a data packet to the PC, adding 1 to the Index value of the data packet every time the data packet is sent, wherein the format of the data packet is shown in figure 2. The format and length of each data packet are fixed, and each data packet comprises a frame header, an Index, data and a frame tail. In this embodiment, the Micro Control Unit (MCU) is also called a Single Chip Microcomputer (Single Chip Microcomputer) or a Single Chip Microcomputer.
Step S2: the PC receives the data packet and sets a fixed time synchronization period T pc During a time synchronization period, the PC immediately records the current system time T when receiving the first byte of the first data packet sys And records the Index value Index of the first data packet 1 Then according to T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base 。
In the step S2 according to T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base The method comprises the following steps:
one byte comprises a start bits, b data bits, c check bits and d stop bits, the baud rate is Bd, and the time required for transmitting one byte is T byte =(a+b+c+d)/Bd;
Then T base =T sys –T byte 。
According to the known data length (for example, 1 byte) including 1 bit of the fixed start bit (frame head), 8 bits of the data, no check bit and 1 bit of the stop bit (frame tail), the transmission time of 1 byte data packet can be calculated according to the known baud rate and other information:
1*(1+8+1)/115200=0.000086806s=86.806us
step S3: during a time synchronization period, every time a PC receives a data packet, the PC will determine the Index value Index of the data packet according to the Index value Index of the data packet n And T base And Index 1 And calculating the sending time of the data packet:
specifically, each data packet has an Index of m bytes, and the Index value is 256 at most m -1;
If Index n ≥Index 1 Then T is n =T base +(Index n -Index 1 )·T mcu ;
If Index n <Index 1 Then T is n =T base +(256 m +Index n -Index 1 )·T mcu 。
As shown in FIGS. 3 and 4, when m is 2, the Index value is 65535 at the maximum;
when Index n ≥Index 1 Is T n =T base +(Index n -Index 1 )·T mcu ;
When Index n Index after recycle overflow n <Index 1 At this time, it is T n =T base +(256 m +Index n -Index 1 )·T mcu ;
Wherein the time synchronization period T of the PC pc Data transmission period T greater than MCU mcu ;T pc /T mcu <256 m 。
Step S4: after a time synchronization period, the execution of step S2 is resumed to update the reference time.
Next, the present invention will be described in more detail.
1) The MCU collects data and sends the data with a fixed data sending period T mcu Sending a data packet to the PC for 10ms, and adding 1 to the Index value of the data packet every time the data packet is sent;
2) the PC receives the data packet and sets a fixed time synchronization period T pc =2min;
3) During a time synchronization period, every time a PC receives a data packet, the PC will determine the Index value Index according to the Index value of the data packet n And T base And Index 1 Calculating the sending time T of the data packet n
4) After a time synchronization period of 2min, the PC updates the time reference T once again according to the step 2) base 。
The embodiment of the invention provides a time synchronization method and a medium, which can confirm the specific time when a data packet is sent to a PC by an MCU under the condition of meeting the precision requirement in a simple, practical and low-cost mode, form a high-precision timestamp, ensure the real-time performance of data and facilitate the data analysis of the PC. Only the MCU is used for transmitting data to the PC in a one-way mode, and the PC is not required to send a data reading instruction, so that the sending frequency of the MCU data packet is improved.
Those skilled in the art will appreciate that, in addition to implementing the system and its various devices, modules, units provided by the present invention as pure computer readable program code, the system and its various devices, modules, units provided by the present invention can be fully implemented by logically programming method steps in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers and the like. Therefore, the system and various devices, modules and units thereof provided by the invention can be regarded as a hardware component, and the devices, modules and units included in the system for realizing various functions can also be regarded as structures in the hardware component; means, modules, units for performing the various functions may also be regarded as structures within both software modules and hardware components for performing the method.
The foregoing description has described specific embodiments of the present invention. It is to be understood that the present invention is not limited to the specific embodiments described above, and that various changes or modifications may be made by one skilled in the art within the scope of the appended claims without departing from the spirit of the invention. The embodiments and features of the embodiments of the present application may be combined with each other arbitrarily without conflict.
Claims (7)
1. A method of time synchronization, comprising:
step S1: the MCU collects data and sends the data with a fixed data sending period T mcu Sending a data packet to a PC, and adding 1 to the Index value of the data packet every time the data packet is sent;
step S2: the PC receives the data packet and sets a fixed time synchronization period T pc During a time synchronization period, the PC records the first byte of the first data packet immediately after receiving the first byteCurrent system time T sys And records the Index value Index of the first data packet 1 Then according to T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base ;
Step S3: during a time synchronization period, every time a PC receives a data packet, the PC will determine the Index value Index of the data packet according to the Index value Index of the data packet n And T base And Index 1 Calculating the sending time of the data packet;
step S4: after a time synchronization period, step S2 is executed again to update the reference time.
2. The method for time synchronization of claim 1, wherein each packet has a fixed format and length, and comprises a header, an Index, data, and a trailer.
3. The method for time synchronization according to claim 1, wherein said step S2 is based on T sys Byte length and baud rate, and calculating the sending time of the first data packet, i.e. the reference time T base The method comprises the following steps:
one byte comprises a start bits, b data bits, c check bits and d stop bits, the baud rate is Bd, and the time required for transmitting one byte is T byte =(a+b+c+d)/Bd;
Then T base =T sys –T byte 。
4. The method for time synchronization according to claim 1, wherein the Index value Index of the data packet is used as the basis for the step S3 n And T base And Index 1 Calculating the sending time T of the data packet n The method comprises the following steps:
the Index occupies m bytes in each data packet, and the Index value is 256 at most m -1;
If Index n ≥Index 1 Then T is n =T base +(Index n -Index 1 )·T mcu ;
If Index n <Index 1 Then T is n =T base +(256 m +Index n -Index 1 )·T mcu 。
5. The time synchronization method of claim 4, wherein the PC has a time synchronization period T pc Data transmission period T greater than MCU mcu 。
6. Time synchronization method according to claim 5, characterized in that T pc /T mcu <256 m 。
7. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out the steps of the method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210634507.7A CN114915365A (en) | 2022-06-07 | 2022-06-07 | Time synchronization method and medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210634507.7A CN114915365A (en) | 2022-06-07 | 2022-06-07 | Time synchronization method and medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN114915365A true CN114915365A (en) | 2022-08-16 |
Family
ID=82771205
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210634507.7A Withdrawn CN114915365A (en) | 2022-06-07 | 2022-06-07 | Time synchronization method and medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114915365A (en) |
-
2022
- 2022-06-07 CN CN202210634507.7A patent/CN114915365A/en not_active Withdrawn
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11558136B2 (en) | High speed embedded protocol for distributed control system | |
CN105577308B (en) | The method and apparatus of In-vehicle networking time synchronization is provided using redundancy master clock | |
US10673565B2 (en) | Confirming data accuracy in a distributed control system | |
US20010018720A1 (en) | Method and device for the exchange of data between at least two users connected to a bus system | |
US9830298B2 (en) | Media time based USB frame counter synchronization for Wi-Fi serial bus | |
US20060203748A1 (en) | Method and device for the exchange of data between at least two users connected to a bus system | |
CN106464559B (en) | High-Speed Embedded Protocols for Distributed Control Systems | |
EP3675398A1 (en) | Check code processing method, electronic device, and storage medium | |
WO2015196685A1 (en) | Clock synchronization method and apparatus | |
CN112583477B (en) | A delay measurement method, system and storage medium | |
KR102707737B1 (en) | Controller diagnostic device and method thereof | |
US7428608B2 (en) | Communication system, communication circuit and communication method | |
CN103369662A (en) | Adapter, baseband processing unit and base station system | |
CN112583509A (en) | Method and device for acquiring time stamp of data stream, storage medium and electronic device | |
JP5372699B2 (en) | In-vehicle network device | |
CN100403825C (en) | Method for Transmission Frame Timing and Frame Number of Universal Radio Interface | |
CN114915365A (en) | Time synchronization method and medium | |
US20250048067A1 (en) | In-vehicle communication apparatus and operation method thereof | |
WO2016000324A1 (en) | Method and apparatus for implementing time synchronization | |
CN112291852A (en) | UCI transmission and receiving method, device, system, equipment and medium | |
CN113133097B (en) | Wireless communication method and system | |
CN116193375B (en) | Bluetooth receiving and transmitting time sequence control method and device | |
CN113242167A (en) | Single-bit-synchronization-based semi-asynchronous CAN bus control method and controller | |
JP2013131836A (en) | Serial communication method, communication device, and serial communication system | |
CN114868354B (en) | Time synchronization method and equipment |
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 | ||
WW01 | Invention patent application withdrawn after publication |
Application publication date: 20220816 |
|
WW01 | Invention patent application withdrawn after publication |