CN117743222A - Serial port communication method, system, equipment and storage medium - Google Patents
Serial port communication method, system, equipment and storage medium Download PDFInfo
- Publication number
- CN117743222A CN117743222A CN202311734593.XA CN202311734593A CN117743222A CN 117743222 A CN117743222 A CN 117743222A CN 202311734593 A CN202311734593 A CN 202311734593A CN 117743222 A CN117743222 A CN 117743222A
- Authority
- CN
- China
- Prior art keywords
- baud rate
- serial port
- address
- adaptive signal
- local
- 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
Links
- 238000004891 communication Methods 0.000 title claims abstract description 52
- 238000000034 method Methods 0.000 title claims abstract description 31
- 230000003044 adaptive effect Effects 0.000 claims abstract description 36
- 238000004590 computer program Methods 0.000 claims description 9
- 230000003993 interaction Effects 0.000 claims description 6
- 238000004364 calculation method Methods 0.000 claims description 4
- 230000004044 response Effects 0.000 claims description 3
- 230000006870 function Effects 0.000 description 6
- 238000010586 diagram Methods 0.000 description 5
- 230000014509 gene expression Effects 0.000 description 2
- 230000004048 modification Effects 0.000 description 2
- 238000012986 modification Methods 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004422 calculation algorithm Methods 0.000 description 1
- 238000013461 design Methods 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 230000006872 improvement Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 238000012545 processing Methods 0.000 description 1
Landscapes
- Information Transfer Systems (AREA)
Abstract
The invention discloses a serial port communication method, which comprises the following steps: receiving an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device; judging whether the address in the self-adaptive signal is a local address or not; responding to the local address, and adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the self-adaptive signal; and communicating with the main equipment according to the adjusted baud rate parameter of the local serial port. The invention also discloses a system, computer equipment and a readable storage medium. The scheme provided by the invention can realize simple and reliable analog serial port communication in the singlechip, and overcomes the limitation of the traditional serial port communication method. Meanwhile, the function of a master mode and a plurality of slave modes expands the application range of communication, so that the host can perform efficient serial port communication with a plurality of slaves.
Description
Technical Field
The present invention relates to the field of communications, and in particular, to a serial port communication method, system, device, and storage medium.
Background
In embedded systems, serial communication between a single chip microcomputer and other devices is a common and important requirement. Conventional serial communication methods generally rely on hardware serial modules, which not only require manual configuration of baud rate parameters, but also have limited hardware serial resources. Under the condition that the interrupt processing speed of the singlechip is limited, high-speed serial communication becomes difficult, and the baud rate is complex to configure, so that the flexibility and convenience of serial communication are limited.
Disclosure of Invention
In view of this, in order to overcome at least one aspect of the above-mentioned problems, an embodiment of the present invention provides a serial port communication method, including the following steps:
receiving an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device;
judging whether the address in the self-adaptive signal is a local address or not;
responding to the local address, and adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the self-adaptive signal;
and communicating with the main equipment according to the adjusted baud rate parameter of the local serial port.
In some embodiments, further comprising:
the address of each slave device is obtained by the master device, and a plurality of adaptive signals are generated according to the address of each slave device and the baud rate identifier.
In some embodiments, the baud rate identifier in each of the adaptive signals is different.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
according to the adjusted baud rate, a timer and an interrupt service routine are configured to realize data interaction with the opposite serial device, and a ring buffer is configured to realize data receiving and transmitting.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
and determining the baud rate parameter according to the time interval of the falling edge in the calculation of the baud rate identifier.
In some embodiments, further comprising:
the master device communicates by connecting the receiving pins and the transmitting pins of the plurality of slaves.
In some embodiments, further comprising:
and connecting a transmitting pin of the equipment machine with a cathode of a diode, and connecting a receiving pin of the equipment machine with an anode of the diode.
Based on the same inventive concept, according to another aspect of the present invention, an embodiment of the present invention further provides a serial port communication system, including:
a receiving module configured to receive an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device;
a judging module configured to judge whether an address in the adaptive signal is a local address;
an adjustment module configured to adjust a baud rate parameter of a local serial port according to a baud rate identifier in the adaptive signal in response to being a local address;
and the communication module is configured to communicate with the main equipment according to the adjusted baud rate parameter of the local serial port.
Based on the same inventive concept, according to another aspect of the present invention, an embodiment of the present invention further provides a computer apparatus, including:
at least one processor; and
a memory storing a computer program executable on the processor, the processor executing steps of any of the serial communication methods described above.
Based on the same inventive concept, according to another aspect of the present invention, there is also provided a computer-readable storage medium storing a computer program which, when executed by a processor, performs the steps of any of the serial communication methods described above.
The invention has one of the following beneficial technical effects: the scheme provided by the invention can realize simple and reliable analog serial port communication in the singlechip, and overcomes the limitation of the traditional serial port communication method. Meanwhile, the function of a master mode and a plurality of slave modes expands the application range of communication, so that the host can perform efficient serial port communication with a plurality of slaves.
Drawings
In order to more clearly illustrate the embodiments of the invention or the technical solutions in the prior art, the drawings that are necessary for the description of the embodiments or the prior art will be briefly described, it being obvious that the drawings in the following description are only some embodiments of the invention and that other embodiments may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a schematic flow chart of a serial port communication method according to an embodiment of the present invention;
fig. 2 is a schematic diagram of hardware connection of a serial port communication system according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of connection between a master and a slave according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of a serial port communication system according to an embodiment of the present invention;
FIG. 5 is a schematic diagram of a computer device according to an embodiment of the present invention;
fig. 6 is a schematic structural diagram of a computer-readable storage medium according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the following embodiments of the present invention will be described in further detail with reference to the accompanying drawings.
It should be noted that, in the embodiments of the present invention, all the expressions "first" and "second" are used to distinguish two entities with the same name but different entities or different parameters, and it is noted that the "first" and "second" are only used for convenience of expression, and should not be construed as limiting the embodiments of the present invention, and the following embodiments are not described one by one.
According to an aspect of the present invention, an embodiment of the present invention proposes a serial port communication method, as shown in fig. 1, which may include the steps of:
s1, receiving an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device;
s2, judging whether the address in the self-adaptive signal is a local address or not;
s3, responding to the local address, and adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the self-adaptive signal;
and S4, communicating with the main equipment according to the adjusted baud rate parameter of the local serial port.
The scheme provided by the invention can realize simple and reliable analog serial port communication in the singlechip, and overcomes the limitation of the traditional serial port communication method. Meanwhile, the function of a master mode and a plurality of slave modes expands the application range of communication, so that the host can perform efficient serial port communication with a plurality of slaves.
In some embodiments, further comprising:
the address of each slave device is obtained by the master device, and a plurality of adaptive signals are generated according to the address of each slave device and the baud rate identifier.
In some embodiments, the baud rate identifier in each of the adaptive signals is different.
Specifically, firstly, initializing operation is performed, and hardware resources such as pins and timers on the singlechip are set for simulating serial port communication. And then, through the automatic baud rate identification step, the singlechip receives the 0xAA byte data sent by the opposite serial port device and calculates the time interval of the falling edge in the 0xAA byte data so as to automatically identify the baud rate of the opposite serial port device. According to the identified baud rate of the opposite side equipment, the singlechip automatically adjusts the baud rate parameter of the local serial port so as to adapt to the baud rate of the opposite side.
Wherein each slave configures a unique device address and when the master transmits a frame, the frame header includes a frame header (baud rate identifier) 0xAA and a slave address. The slave machine judges to adapt to the local baud rate according to the received frame header, if the local baud rate requirement is met, the slave machine which recognizes that the identifier and the address are met responds when judging whether the address is a local address. The master uses different communication rates to actively transmit according to the addresses of the slaves.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
according to the adjusted baud rate, a timer and an interrupt service routine are configured to realize data interaction with the opposite serial device, and a ring buffer is configured to realize data receiving and transmitting.
Specifically, as shown in fig. 2, in the communication interaction stage, a timer, an interrupt service routine and a ring buffer are configured according to the adjusted local baud rate, so that data interaction with the opposite serial device is realized. And through the annular buffer, the data is received and sent, and the reliability of communication is ensured.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
and determining the baud rate parameter according to the time interval of the falling edge in the calculation of the baud rate identifier.
In some embodiments, further comprising:
the master device communicates by connecting the receiving pins and the transmitting pins of the plurality of slaves.
In some embodiments, further comprising:
and connecting a transmitting pin of the equipment machine with a cathode of a diode, and connecting a receiving pin of the equipment machine with an anode of the diode.
Specifically, as shown in fig. 3, a function of a master multi-slave mode is added in the present invention, so that a host can communicate with a plurality of slaves at the same time. In this mode, the master communicates through RX (receive) and TX (transmit) pins connecting the multiple slaves. Each slave has a unique address. When the host transmits a frame, the frame header includes 0xAA bytes and a slave address. The slave determines whether the received frame header is an own address or not according to the received frame header, and only the slave which recognizes the frame header responds. As shown in fig. 2, the TX pin of the slave is connected to the diode cathode, and the diode anode is connected to the RX pin of the master. This configuration can eliminate sink current between slaves. Besides, the RX of the master and the slave should be configured in a pull-up input mode to eliminate signal interference on the RX pin when the transmitting end is not powered on.
In order to save the communication rate of each slave, the frame sent by the host also comprises the address of the slave and the corresponding communication rate information. The master uses different communication rates to actively transmit according to the addresses of the slaves.
The invention can realize simple and reliable analog serial port communication in the singlechip and overcomes the limitation of the traditional serial port communication method. Meanwhile, the function of a master mode and a plurality of slave modes expands the application range of communication, so that the host can perform efficient serial port communication with a plurality of slaves.
Based on the same inventive concept, according to another aspect of the present invention, there is further provided a serial port communication system 400, as shown in fig. 4, including:
a receiving module 401, configured to receive, by using a slave device, an adaptive signal sent by a master device, where the adaptive signal includes an address of the slave device and a baud rate identifier;
a determining module 402 configured to determine whether an address in the adaptive signal is a local address;
an adjustment module 403 configured to adjust a baud rate parameter of a local serial port according to a baud rate identifier in the adaptive signal in response to being a local address;
and the communication module 404 is configured to communicate with the master device according to the adjusted baud rate parameter of the local serial port.
In some embodiments, further comprising:
the address of each slave device is obtained by the master device, and a plurality of adaptive signals are generated according to the address of each slave device and the baud rate identifier.
In some embodiments, the baud rate identifier in each of the adaptive signals is different.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
according to the adjusted baud rate, a timer and an interrupt service routine are configured to realize data interaction with the opposite serial device, and a ring buffer is configured to realize data receiving and transmitting.
In some embodiments, adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the adaptive signal further comprises:
and determining the baud rate parameter according to the time interval of the falling edge in the calculation of the baud rate identifier.
In some embodiments, further comprising:
the master device communicates by connecting the receiving pins and the transmitting pins of the plurality of slaves.
In some embodiments, further comprising:
and connecting a transmitting pin of the equipment machine with a cathode of a diode, and connecting a receiving pin of the equipment machine with an anode of the diode.
The scheme provided by the invention can realize simple and reliable analog serial port communication in the singlechip, and overcomes the limitation of the traditional serial port communication method. Meanwhile, the function of a master mode and a plurality of slave modes expands the application range of communication, so that the host can perform efficient serial port communication with a plurality of slaves.
Based on the same inventive concept, according to another aspect of the present invention, as shown in fig. 5, an embodiment of the present invention further provides a computer apparatus 501, including:
at least one processor 520; and
the memory 510, the memory 510 stores a computer program 511 executable on a processor, and the processor 520 executes the steps of any of the serial communication methods described above.
According to another aspect of the present invention, as shown in fig. 6, based on the same inventive concept, an embodiment of the present invention further provides a computer-readable storage medium 601, where the computer-readable storage medium 601 stores a computer program 610, and the computer program 610 performs the steps of any of the serial communication methods described above when executed by a processor.
Finally, it should be noted that, as will be appreciated by those skilled in the art, all or part of the procedures in implementing the methods of the embodiments described above may be implemented by a computer program for instructing relevant hardware, and the program may be stored in a computer readable storage medium, and the program may include the procedures of the embodiments of the methods described above when executed.
Further, it should be appreciated that the computer-readable storage medium (e.g., memory) herein can be either volatile memory or nonvolatile memory, or can include both volatile and nonvolatile memory.
Those of skill would further appreciate that the various illustrative logical blocks, modules, circuits, and algorithm steps described in connection with the disclosure herein may be implemented as electronic hardware, computer software, or combinations of both. To clearly illustrate this interchangeability of hardware and software, various illustrative components, blocks, modules, circuits, and steps have been described above generally in terms of their functionality. Whether such functionality is implemented as software or hardware depends upon the particular application and design constraints imposed on the overall system. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present disclosure.
The foregoing is an exemplary embodiment of the present disclosure, but it should be noted that various changes and modifications could be made herein without departing from the scope of the disclosure as defined by the appended claims. The functions, steps and/or actions of the method claims in accordance with the disclosed embodiments described herein need not be performed in any particular order. Furthermore, although elements of the disclosed embodiments may be described or claimed in the singular, the plural is contemplated unless limitation to the singular is explicitly stated.
It should be understood that as used herein, the singular forms "a", "an", and "the" are intended to include the plural forms as well, unless the context clearly supports the exception. It should also be understood that "and/or" as used herein is meant to include any and all possible combinations of one or more of the associated listed items.
The foregoing embodiment of the present invention has been disclosed with reference to the number of embodiments for the purpose of description only, and does not represent the advantages or disadvantages of the embodiments.
It will be understood by those skilled in the art that all or part of the steps for implementing the above embodiments may be implemented by hardware, or may be implemented by a program for instructing relevant hardware, and the program may be stored in a computer readable storage medium, where the storage medium may be a read-only memory, a magnetic disk or an optical disk, etc.
Those of ordinary skill in the art will appreciate that: the above discussion of any embodiment is merely exemplary and is not intended to imply that the scope of the disclosure of embodiments of the invention, including the claims, is limited to such examples; combinations of features of the above embodiments or in different embodiments are also possible within the idea of an embodiment of the invention, and many other variations of the different aspects of the embodiments of the invention as described above exist, which are not provided in detail for the sake of brevity. Therefore, any omission, modification, equivalent replacement, improvement, etc. of the embodiments should be included in the protection scope of the embodiments of the present invention.
Claims (10)
1. The serial port communication method is characterized by comprising the following steps of:
receiving an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device;
judging whether the address in the self-adaptive signal is a local address or not;
responding to the local address, and adjusting the baud rate parameter of the local serial port according to the baud rate identifier in the self-adaptive signal;
and communicating with the main equipment according to the adjusted baud rate parameter of the local serial port.
2. The method as recited in claim 1, further comprising:
the address of each slave device is obtained by the master device, and a plurality of adaptive signals are generated according to the address of each slave device and the baud rate identifier.
3. The method of claim 2, wherein the baud rate identifier in each of the adaptive signals is different.
4. The method of claim 1, wherein adjusting the baud rate parameter of the local serial port based on the baud rate identifier in the adaptive signal further comprises:
according to the adjusted baud rate, a timer and an interrupt service routine are configured to realize data interaction with the opposite serial device, and a ring buffer is configured to realize data receiving and transmitting.
5. The method of claim 1, wherein adjusting the baud rate parameter of the local serial port based on the baud rate identifier in the adaptive signal further comprises:
and determining the baud rate parameter according to the time interval of the falling edge in the calculation of the baud rate identifier.
6. The method as recited in claim 1, further comprising:
the master device communicates by connecting the receiving pins and the transmitting pins of the plurality of slaves.
7. The method as recited in claim 1, further comprising:
and connecting a transmitting pin of the equipment machine with a cathode of a diode, and connecting a receiving pin of the equipment machine with an anode of the diode.
8. A serial communication system, comprising:
a receiving module configured to receive an adaptive signal sent by a master device by using a slave device, wherein the adaptive signal comprises an address and a baud rate identifier of the slave device;
a judging module configured to judge whether an address in the adaptive signal is a local address;
an adjustment module configured to adjust a baud rate parameter of a local serial port according to a baud rate identifier in the adaptive signal in response to being a local address;
and the communication module is configured to communicate with the main equipment according to the adjusted baud rate parameter of the local serial port.
9. A computer device, comprising:
at least one processor; and
a memory storing a computer program executable on the processor, wherein the processor performs the steps of the method of any one of claims 1-7 when the program is executed.
10. A computer readable storage medium storing a computer program, characterized in that the computer program when executed by a processor performs the steps of the method according to any one of claims 1-7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202311734593.XA CN117743222A (en) | 2023-12-15 | 2023-12-15 | Serial port communication method, system, equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202311734593.XA CN117743222A (en) | 2023-12-15 | 2023-12-15 | Serial port communication method, system, equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN117743222A true CN117743222A (en) | 2024-03-22 |
Family
ID=90278589
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202311734593.XA Pending CN117743222A (en) | 2023-12-15 | 2023-12-15 | Serial port communication method, system, equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN117743222A (en) |
-
2023
- 2023-12-15 CN CN202311734593.XA patent/CN117743222A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7286673B2 (en) | Embedded internet for hearing aids | |
US20200119990A1 (en) | Automatic configuration of a network switch in a multi-chassis link aggregation group | |
US10268624B2 (en) | Subscriber station for a bus system and method for improving the error tolerance of a subscriber station of a bus system | |
US9811487B2 (en) | Back channel support for systems with split lane swap | |
CN101626334B (en) | Method and device for controlling communication bus | |
CN111277395B (en) | Method and device for determining path loss reference signal | |
US20210328674A1 (en) | Optical communications apparatus, optical line termination, and optical communication processing method | |
US10778481B1 (en) | Adaptable can transceiver and system | |
TWI535251B (en) | Method and system for low-latency networking | |
US10826847B2 (en) | Port auto-negotiation method and device | |
EP2985955B1 (en) | Controller area network (can) device and method for emulating classic can error management | |
CN109245950B (en) | Method for determining link failure position and related device | |
KR20170040326A (en) | Communication control device for a subscriber station of a bus system, programming tool and method for programming subscriber stations in a bus system which has subscriber stations communicating according to different protocols | |
US10055322B2 (en) | Interpreting signals received from redundant buses | |
EP3914032B1 (en) | Data communication methods. | |
CN117743222A (en) | Serial port communication method, system, equipment and storage medium | |
US10432424B2 (en) | Ring network system and network node | |
CN213342249U (en) | LIN network simulation system | |
US7046722B2 (en) | Transceiver with means for error management | |
US20200336439A1 (en) | Control of ethernet link-partner gpio using oam | |
CN110391823B (en) | Communication circuit, control method thereof, and device having communication function | |
CN114443551B (en) | Method, system, equipment and medium for realizing I2C communication by single line | |
EP4068721B1 (en) | Controller area network device | |
US20240097947A1 (en) | Data interface equalization adjustment method and apparatus, device, and storage medium | |
CN115065437B (en) | Tunable optical module and method and system for automatically configuring wavelength of tunable optical module |
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 |