Serial port communication method and device
By determining the command storage status and historical success rate in serial communication, and using multi-byte repeated command number transmission, the problems of slow transmission speed and poor anti-interference ability in serial communication are solved, and more efficient data transmission is achieved.
Patent Information
- Application Number
- CN202510648403.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-20
- Publication Date
- 2025-09-02
AI Technical Summary
The existing serial communication has slow transmission speed, poor anti-interference capability, and limited transmission distance. The existing solutions have failed to effectively improve transmission efficiency and anti-interference capability by increasing the protocol complexity and retransmission mechanism.
By determining whether the serial port command has been stored on the slave, dynamically adjust the transmission method based on the historical transmission success rate, multi-byte repeated command number transmission is used to reduce the amount of data and improve the anti-interference ability.
It improves the transmission speed and anti-interference ability of serial communication, reduces the data transmission volume, and enhances the stability and efficiency of transmission.
Smart Images

Figure CN120578618A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data communication technology, and in particular to a serial port communication method and device. Background Art
[0002] Serial communication is a commonly used communication interface. It is widely used in products due to its advantages such as simple implementation, low cost, and good compatibility. However, due to its disadvantages such as slow transmission speed, poor anti-interference ability, limited transmission distance, and lack of flexibility, it encounters various problems in actual product applications.
[0003] At present, most serial communication technologies on the market solve the above problems by increasing the complexity of the protocol and the retransmission mechanism. However, the increase in protocol complexity increases the amount of transmitted data and reduces transmission efficiency. The retransmission mechanism cannot solve the data anti-interference problem under severe interference conditions. Summary of the Invention
[0004] In view of this, the purpose of the present invention is to provide a serial communication method and device, which can reduce the amount of transmitted data by transmitting the command number of the serial port command, thereby improving the transmission speed; improve the anti-interference ability by transmitting multi-byte repeated command numbers; and dynamically adjust the transmission mode according to the success rate of historical transmission to improve the transmission speed and anti-interference ability.
[0005] In a first aspect, an embodiment of the present invention provides a serial port communication method, which is applied to a host, and the method includes: communicating data with a slave through a serial port, determining whether a serial port command has been stored in the slave; if it has been stored in the slave, determining whether to perform anti-interference transmission based on the success rate of historical transmission of the serial port command; if anti-interference transmission is not performed, transmitting the command number of the serial port command to the slave; if anti-interference transmission is performed, transmitting the command numbers of multiple repeated serial port commands to the slave.
[0006] In an optional embodiment of the present application, after the above-mentioned step of transmitting the command number of the serial port command to the slave machine, the method further includes: receiving return information sent by the slave machine; if the return information indicating successful transmission is received within the preset time, recording the number of successful transmissions; if the return information indicating failed transmission is received within the preset time, recording the number of failed transmissions, and re-transmitting the command number of the serial port command to the slave machine; if no return information is received within the preset time, re-transmitting the command number of the serial port command to the slave machine.
[0007] In an optional embodiment of the present application, after the above-mentioned step of determining whether the serial port command has been stored in the slave, the method further includes: if it has not been stored in the slave, transmitting the serial port command to the slave; determining whether the serial port command is stored in the slave; if it is not stored in the slave, sending a first command word to the slave; if it is stored in the slave, sending a second command word to the slave.
[0008] In an optional embodiment of the present application, after the above-mentioned step of sending the first command word to the slave, the method further includes: receiving return information sent by the slave; if no return information is received within a preset time, re-transmitting the serial port command to the slave.
[0009] In an optional embodiment of the present application, after the above-mentioned step of retransmitting the serial port command to the slave, the method further includes: if the retransmission of the serial port command to the slave is successful, recording the number of successful retransmissions; if the retransmission of the serial port command to the slave fails, recording the number of failed retransmissions.
[0010] In a second aspect, an embodiment of the present invention further provides a serial port communication method, which is applied to a slave device, and the method includes: receiving a command number sent by a host and performing a cyclic redundancy check; if the cyclic redundancy check is successful, determining the serial port command corresponding to the command number, and sending a return message indicating a successful transmission to the host; if the cyclic redundancy check fails, sending a return message indicating a failed transmission to the host.
[0011] In an optional embodiment of the present application, the above method includes: receiving a first command word sent by the host and performing a cyclic redundancy check; if the cyclic redundancy check is successful, determining the serial port command corresponding to the first command word, and sending return information indicating successful transmission to the host; if the cyclic redundancy check fails, sending return information indicating failed transmission to the host; receiving a second command word sent by the host and performing a cyclic redundancy check; if the cyclic redundancy check is successful, determining the serial port command corresponding to the second command word, saving the serial port command, and sending return information indicating successful transmission to the host; if the cyclic redundancy check fails, sending return information indicating failed transmission to the host.
[0012] In an optional embodiment of the present application, the above method includes: receiving a fourth command word sent by the host, adjusting the baud rate of communication between the host and the slave; receiving a fifth command word sent by the host, adjusting the range of the cyclic redundancy check and the data field check.
[0013] In a third aspect, an embodiment of the present invention further provides a serial port communication device, which is applied to a host, and the device includes: a serial port command storage judgment module, which is used to communicate data with a slave through a serial port to determine whether the serial port command has been stored in the slave; an anti-interference transmission judgment module, which is used to determine whether to perform anti-interference transmission based on the success rate of the historical transmission of the serial port command if it has been stored in the slave; a command number transmission module, which is used to transmit the command number of the serial port command to the slave if anti-interference transmission is not performed; and an anti-interference transmission module, which is used to transmit the command numbers of multiple repeated serial port commands to the slave if anti-interference transmission is performed.
[0014] In a fourth aspect, an embodiment of the present invention further provides a serial port communication device, which is applied to a slave machine, and the device includes: a command number receiving module, which is used to receive the command number sent by the host and perform a cyclic redundancy check; a first return information sending module, which is used to determine the serial port command corresponding to the command number if the cyclic redundancy check is successful, and send return information indicating successful transmission to the host; a second return information sending module, which is used to send return information indicating transmission failure to the host if the cyclic redundancy check fails.
[0015] The embodiments of the present invention bring the following beneficial effects:
[0016] Embodiments of the present invention provide a serial port communication method and device for communicating data with a slave device via a serial port. The method determines whether a serial port command has been stored in the slave device. If so, the method determines whether to perform anti-interference transmission based on the historical success rate of serial port command transmissions. If anti-interference transmission is not performed, the command number of the serial port command is transmitted to the slave device. If anti-interference transmission is performed, the command numbers of multiple repeated serial port commands are transmitted to the slave device. This method reduces the amount of data transmitted by transmitting the command number of the serial port command, thereby increasing transmission speed. Transmitting multi-byte repeated command numbers improves anti-interference capabilities. The method dynamically adjusts the transmission mode based on the historical success rate of transmissions, thereby improving transmission speed and anti-interference capabilities.
[0017] Other features and advantages of the present disclosure will be set forth in the following description, or some features and advantages may be inferred or unambiguously determined from the description, or may be learned by practicing the above-mentioned technology of the present disclosure.
[0018] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the specific embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0020] Figure 1 A flowchart of a serial communication method applied to a host provided by an embodiment of the present invention;
[0021] Figure 2 A flowchart of another serial port communication method applied to a host provided by an embodiment of the present invention;
[0022] Figure 3A schematic diagram of a serial port communication method applied to a host provided by an embodiment of the present invention;
[0023] Figure 4 A flowchart of a serial communication method applied to a slave device provided by an embodiment of the present invention;
[0024] Figure 5 A schematic diagram of a serial communication method applied to a slave device provided by an embodiment of the present invention;
[0025] Figure 6 A schematic structural diagram of a serial communication device applied to a host provided by an embodiment of the present invention;
[0026] Figure 7 A schematic structural diagram of a serial communication device applied to a slave device provided by an embodiment of the present invention;
[0027] Figure 8 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0028] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of them. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.
[0029] Serial communication, a commonly used communication interface, is widely used in products due to its advantages such as simple implementation, low cost, and good compatibility. However, due to its shortcomings such as slow transmission speed, poor anti-interference ability, limited transmission distance, and lack of flexibility, it encounters various problems in actual product applications. Currently, serial communication solutions on the market generally adopt increased protocol complexity and retransmission mechanisms to solve these problems. However, the increased protocol complexity increases the amount of data transmitted and reduces transmission efficiency. The retransmission mechanism cannot solve the data anti-interference problem under severe interference conditions.
[0030] Based on this, embodiments of the present invention provide a serial port communication method and device that specifically improves the serial port transmission speed and stability of kitchen appliances. These methods can be applied to household appliances requiring serial port transmission, such as range hoods and ovens, and can be applied to both industrial and embedded products, primarily to improve serial port transmission efficiency and stability. This embodiment reduces the amount of data transmitted by transmitting serial port command numbers, thereby increasing transmission speed; improves anti-interference capabilities by transmitting multi-byte repeated command numbers; and dynamically adjusts the transmission mode based on historical transmission success rates, thereby improving transmission speed and anti-interference capabilities.
[0031] To facilitate understanding of this embodiment, a serial port communication method disclosed in an embodiment of the present invention is first introduced in detail.
[0032] Example 1:
[0033] The embodiment of the present invention provides a serial communication method, which is applied to a host. Figure 1 The flowchart of a serial communication method applied to a host is shown, and the serial communication method includes the following steps:
[0034] Step S102 , performing data communication with the slave device via the serial port, and determining whether the serial port command has been stored in the slave device.
[0035] With the development of the times, this embodiment can reduce the amount of data transmitted by exchanging space for time. In this embodiment, the serial communication device end (i.e., the slave in this embodiment) can store the received serial port command (whether to record this serial port command is agreed in the protocol) and feedback the stored command number through the protocol.
[0036] Each time the sending end of the serial communication in this embodiment (i.e., the host in this embodiment) sends a serial command, it can communicate data with the slave through the serial port to determine whether the serial command has been stored in the slave, thereby determining whether the serial command needs to be sent in full or only the command number needs to be sent.
[0037] Step S104: If it has been stored in the slave device, determine whether to perform anti-interference transmission based on the historical success rate of serial port command transmission.
[0038] In this embodiment, after each transmission, it can be determined whether each transmission is successful. Therefore, for the serial port commands stored in the slave, this embodiment can determine whether to perform anti-interference transmission based on the success rate of the historical transmission of the serial port commands.
[0039] For example, if the success rate of historical transmission is greater than or equal to the threshold, anti-interference transmission can be omitted and fast transmission can be achieved by increasing the baud rate and modifying the transmission protocol. If the success rate is less than the threshold, anti-interference transmission can be performed and the anti-interference capability can be improved by reducing the baud rate and adding anti-interference data in the protocol.
[0040] Therefore, this embodiment dynamically adjusts the transmission mode according to the historical transmission success rate to improve the transmission speed and anti-interference capability.
[0041] Step S106: If anti-interference transmission is not performed, the command number of the serial port command is transmitted to the slave device.
[0042] If the serial command is not stored in the slave, the master can determine that the entire serial command needs to be sent. If the serial command is already stored in the slave and anti-interference transmission is not performed, the master can determine that only the command number needs to be sent, and does not need to send the entire serial command, which can reduce the amount of data transmitted and increase the transmission speed.
[0043] Step S108: If anti-interference transmission is performed, the command numbers of multiple repeated serial port commands are transmitted to the slave device.
[0044] If the serial port command has been stored in the slave and is being transmitted with anti-interference, this embodiment can solve the transmission anti-interference problem by transmitting a multi-byte repeated command number (ie, transmitting multiple repeated command numbers to the slave).
[0045] In this embodiment, by changing the software logic, the serial port commands that were not interfered with are already stored in the machine. Now, only the command numbers stored in the machine are transmitted, which reduces the probability of interference. In addition, the original serial port CRC (Cyclic Redundancy Check) verification needs to verify the entire transmitted data. As long as any of the transmitted data is interfered with, all data must be retransmitted. However, in this embodiment, the focus is on the command number. Therefore, in the protocol, multiple bytes can be transmitted to repeat the command number. The CRC algorithm does not need to verify all data. The verification algorithm can be verified by inverting the command. With the improved verification algorithm and two repeated command numbers, the anti-interference ability of the transmission can be greatly improved.
[0046] An embodiment of the present invention provides a serial port communication method for communicating data with a slave device via a serial port. The method determines whether a serial port command has been stored in the slave device. If so, the method determines whether to perform anti-interference transmission based on the historical success rate of serial port command transmissions. If anti-interference transmission is not performed, the command number of the serial port command is transmitted to the slave device. If anti-interference transmission is performed, the command numbers of multiple repeated serial port commands are transmitted to the slave device. This method reduces the amount of data transmitted by transmitting the command numbers of the serial port commands, thereby increasing transmission speed. Transmitting multi-byte repeated command numbers improves anti-interference capabilities. The method dynamically adjusts the transmission mode based on the historical success rate of transmissions, thereby improving transmission speed and anti-interference capabilities.
[0047] Example 2:
[0048] The present invention provides another serial communication method for a host. This method is implemented based on the above embodiment, focusing on the specific steps of the host serial communication method. First, the serial port protocol is defined, which may include the following fields:
[0049] 1. Frame header: length 2, description: fixed 0x55, 0xFF, marking the start of the data frame.
[0050] 2. Device address: length 1, description: master and slave addresses (for example, 0x00 is the master, 0x01-0xff is the slave address).
[0051] 3. Command word: Length 2. Description: The first byte of the operation instruction: 0x10 indicates read, 0x20 indicates write. The second byte: 0x01 indicates the complete command is being transmitted, and the slave does not need to store it locally. 0x02 indicates that the complete command is being transmitted and this is the first transmission, so the slave needs to store it and return the command number. 0x03 indicates that this command has been successfully transmitted once, and the following data segment transmits the command number stored locally by the slave. 0x04 indicates that both parties need to reset the baud rate to improve the transmission's anti-interference ability. 0x05 indicates that the command number is being transmitted, and the data field contains three command numbers. Command word 0x27 indicates the slave's return information.
[0052] 4. Data length: Length 1, Description: Actual bytes of the data field (range from 0x00-0xff).
[0053] 5. Data field: length N, description: payload.
[0054] 6. CRC check: length 2, description: calculate the CRC-16 check value from the frame header to the data field.
[0055] 7. Frame tail: Length 2, Description: Fixed to 0x0D0x0A, marking the end of the data frame.
[0056] In addition, command word 04 indicates that anti-interference transmission is required. The entire transmission protocol remains unchanged. The only changes that need to be made are the CRC checksum and the data field. Command word 05 indicates that anti-interference transmission is required. The entire transmission protocol remains unchanged. The only changes that need to be made are the CRC checksum and the data field.
[0057] The CRC check in this embodiment includes: frame header, device address, command word, and data length (excluding the check of the data field).
[0058] The data field in this embodiment includes: the first byte, indicating the command number; the second byte, indicating the inverse check of the first byte; the third byte, indicating the command number; the fourth byte, indicating the inverse check of the third byte; the fifth byte, indicating the command number; and the sixth byte, indicating the inverse check of the fifth byte.
[0059] Based on the above description, see Figure 2 The flowchart of another serial communication method applied to a host is shown, and the serial communication method includes the following steps:
[0060] Step S202 , performing data communication with the slave device via the serial port, and determining whether the serial port command has been stored in the slave device.
[0061] See also Figure 3 The schematic diagram of a serial port communication method applied to a host is shown in FIG. The host starts serial port initialization, communicates data with the slave through the serial port, and determines whether the serial port command has been stored in the slave.
[0062] Step S204, if not stored in the slave, transmit the serial port command to the slave; determine whether the serial port command is stored in the slave; if not stored in the slave, send a first command word to the slave; if stored in the slave, send a second command word to the slave.
[0063] like Figure 3 As shown, if the serial port command is not stored in the slave, it is necessary to determine whether the serial port command needs to be stored in the slave for this transmission. If the serial port command does not need to be stored in the slave for this transmission, the command word 0x01 (ie, the first command word) can be sent.
[0064] In some embodiments, the device may also receive return information sent by the slave device; if no return information is received within a preset time, the serial port command is retransmitted to the slave device.
[0065] like Figure 3 As shown in FIG. 1 , after sending the command word 0x01, the transmission is completed and the slave device is waiting for feedback information. If the slave device does not receive the return information within a preset time (e.g., 100ms), data retransmission can be initiated.
[0066] In some embodiments, if the serial port command is successfully retransmitted to the slave, the number of successful retransmissions is recorded as one; if the serial port command is unsuccessful retransmission to the slave, the number of failed retransmissions is recorded as one.
[0067] like Figure 3 As shown, if the retransmission is successful, the number of successful retransmissions can be recorded; if the retransmission fails, the number of failed retransmissions can be recorded, which can be used as a basis for judging whether to perform anti-interference transmission later.
[0068] like Figure 3 As shown, if the return information from the slave is received within a preset time (eg, 100ms), it can be considered that one serial port command transmission is completed, and the number of successful transmissions is recorded.
[0069] like Figure 3 As shown, if the serial port command needs to be stored in the slave during this transmission, the command word 0x02 (i.e., the second command word) can be sent, indicating that this command is the first transmission, and the slave needs to save the data field of this command and wait for the slave to feedback the return information.
[0070] Step S206: If it has been stored in the slave, determine whether to perform anti-interference transmission based on the success rate of the historical transmission of the serial command; if anti-interference transmission is not performed, transmit the command number of the serial command to the slave; if anti-interference transmission is performed, transmit the command numbers of multiple repeated serial commands to the slave.
[0071] like Figure 3 As shown, if it has been stored in the slave, it can be determined whether to perform anti-interference transmission based on the historical success rate of serial port command transmission. For example, a threshold is set as the basis for whether to initiate anti-interference transmission.
[0072] like Figure 3 As shown in the figure, if anti-interference transmission is not required, the transmission command number is 0x03, and the data field transmits the command number saved by the slave. If anti-interference transmission is required, the transmission command number is 0x04, and the CRC does not check the data field. The data field uses the verification algorithm of the method.
[0073] In some embodiments, return information sent by the slave device can also be received; if return information indicating successful transmission is received within a preset time, the number of successful transmissions is recorded; if return information indicating failed transmission is received within a preset time, the number of failed transmissions is recorded, and the command number of the serial port command is re-transmitted to the slave device; if no return information is received within the preset time, the command number of the serial port command is re-transmitted to the slave device.
[0074] like Figure 3As shown, the master can receive the return information sent by the slave. If the slave feeds back a return message indicating a successful transmission within a preset time (e.g., 100ms), the number of successful transmissions is recorded as one. If the slave feeds back a return message indicating a failed transmission within a preset time (e.g., 100ms), the number of failed transmissions is recorded as one and a retransmission is performed. If the slave does not feed back a return message within a preset time (e.g., 100ms), a retransmission is performed.
[0075] The above method provided by the embodiment of the present invention specifically describes the specific steps of the serial port communication method of the host. The serial port command that has been successfully transmitted does not need to be retransmitted, and only the command number needs to be transmitted, which reduces the amount of data transmitted and improves the transmission efficiency. By transmitting multi-byte repeated command numbers, retransmission is only required when all the transmitted data bytes are interfered with, which greatly reduces the probability of retransmission. The transmission mode is dynamically adjusted according to the historical transmission success rate, and automatic dynamic adjustment can be performed on the transmission speed and transmission efficiency.
[0076] Example 3:
[0077] The embodiment of the present invention provides another serial communication method, which is applied to a slave. This method is implemented on the basis of the above embodiment, and the specific steps of the serial communication method of the slave are described in detail. Figure 4 The flowchart of a serial communication method applied to a slave device is shown, and the serial communication method includes the following steps:
[0078] Step S402: Receive the command number sent by the host and perform a cyclic redundancy check.
[0079] See also Figure 5 The figure shows a schematic diagram of a serial communication method applied to a slave device. The slave device initializes its serial port, waits for the host to transmit bytes, and parses the corresponding command. The slave device can parse the command bytes according to the transmission protocol.
[0080] like Figure 5 As shown, if the slave receives the command number 0x03 sent by the host, it means that the command number is transmitted, and the corresponding command needs to be retrieved from the local array according to the command number, and a CRC check is required.
[0081] Step S404: If the cyclic redundancy check is successful, determine the serial port command corresponding to the command number, and send a return message indicating successful transmission to the host.
[0082] If the CRC check succeeds, the serial port command corresponding to the command number can be determined, and a return message indicating successful transmission is sent to the host.
[0083] Step S406: If the cyclic redundancy check fails, a return message indicating that the transmission has failed is sent to the host.
[0084] If the CRC check fails, a return message indicating that the transmission failed may be sent to the host.
[0085] In some embodiments, a first command word sent by the host can also be received and a cyclic redundancy check can be performed; if the cyclic redundancy check is successful, the serial port command corresponding to the first command word is determined, and return information indicating successful transmission is sent to the host; if the cyclic redundancy check fails, return information indicating failure of transmission is sent to the host; a second command word sent by the host is received and a cyclic redundancy check can be performed; if the cyclic redundancy check is successful, the serial port command corresponding to the second command word is determined, the serial port command is saved, and return information indicating successful transmission is sent to the host; if the cyclic redundancy check fails, return information indicating failure of transmission is sent to the host.
[0086] like Figure 5 As shown, if the slave receives the first command word 0x01 sent by the host, it indicates normal data transmission, and the transmitted command is a complete command and does not need to be stored.
[0087] like Figure 5 As shown, if the slave receives the second command word 0x02 sent by the host, it means that the transmitted data field needs to be stored, and the command number corresponding to the storage is fed back to the host.
[0088] In some embodiments, a fourth command word sent by the host may be received to adjust the baud rate of communication between the host and the slave; and a fifth command word sent by the host may be received to adjust the range of the cyclic redundancy check and the data field check.
[0089] like Figure 5 As shown, if the slave receives the fourth command word 0x04 sent by the host, it means that the baud rate needs to be reset and the communication is completed at the new baud rate.
[0090] like Figure 5 As shown, if the slave receives the fifth command word 0x05 sent by the host, it means that the command number of the anti-interference part is transmitted, and the CRC check range and the check of the data field need to be processed according to the new logic.
[0091] The above method provided by the embodiment of the present invention specifically describes the specific steps of the serial port communication method of the slave machine, and can record the serial port commands that are successfully transmitted. There is no need to repeatedly transmit the same serial port command later. Only the serial port command number needs to be transmitted, which reduces the amount of data transmitted and improves stability; by inserting multiple command numbers in the same serial port command, retransmission is only required if multiple command numbers are interfered with at the same time, which greatly reduces the probability of retransmission in harsh environments; according to the stability of data transmission, the transmission baud rate is dynamically adjusted to obtain better transmission efficiency.
[0092] Example 4:
[0093] Corresponding to the above method embodiment, the embodiment of the present invention provides a serial communication device, which is applied to a host, see Figure 6 The structure diagram of a serial communication device applied to a host is shown, and the serial communication device includes:
[0094] The serial port command storage determination module 61 is used to perform data communication with the slave through the serial port and determine whether the serial port command has been stored in the slave;
[0095] The anti-interference transmission judgment module 62 is used to determine whether to perform anti-interference transmission based on the success rate of historical transmission of serial port commands if the commands have been stored in the slave device;
[0096] A command number transmission module 63 is used to transmit the command number of the serial port command to the slave device if anti-interference transmission is not performed;
[0097] The anti-interference transmission module 64 is used to transmit the command numbers of multiple repeated serial port commands to the slave device if anti-interference transmission is performed.
[0098] An embodiment of the present invention provides a serial port communication device that communicates data with a slave device via a serial port, determines whether a serial port command has been stored in the slave device, and if so, determines whether to perform anti-interference transmission based on the historical success rate of serial port command transmissions. If anti-interference transmission is not performed, the command number of the serial port command is transmitted to the slave device. If anti-interference transmission is performed, multiple repeated serial port command numbers are transmitted to the slave device. This method reduces the amount of data transmitted by transmitting the serial port command number, thereby increasing transmission speed. Transmitting multi-byte repeated command numbers improves anti-interference capabilities. Dynamically adjust the transmission mode based on the historical success rate of transmissions, thereby improving transmission speed and anti-interference capabilities.
[0099] The above-mentioned device includes: a return information processing module, which is used to receive return information sent by the slave; if the return information indicating successful transmission is received within the preset time, the number of successful transmissions is recorded; if the return information indicating failed transmission is received within the preset time, the number of failed transmissions is recorded, and the command number of the serial port command is re-transmitted to the slave; if no return information is received within the preset time, the command number of the serial port command is re-transmitted to the slave.
[0100] The above-mentioned device includes: a first command word or a second command word sending module, which is used to transmit a serial port command to the slave if it has not been stored in the slave; determine whether the serial port command is stored in the slave; if it is not stored in the slave, send the first command word to the slave; if it is stored in the slave, send the second command word to the slave.
[0101] The first command word or second command word sending module is further configured to receive return information sent by the slave; if no return information is received within a preset time, the serial port command is retransmitted to the slave.
[0102] The first command word or second command word sending module is further configured to record the number of successful retransmissions if the serial port command is successfully retransmitted to the slave; and record the number of failed retransmissions if the serial port command is unsuccessful retransmission to the slave.
[0103] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the serial port communication device applied to the host described above can refer to the corresponding process in the aforementioned embodiment of the serial port communication method applied to the host, and will not be repeated here.
[0104] Embodiment 5:
[0105] Corresponding to the above method embodiment, the embodiment of the present invention provides a serial communication device, which is applied to a slave device, see Figure 7 The structure diagram of a serial communication device applied to a slave device is shown, and the serial communication device includes:
[0106] The command number receiving module 71 is used to receive the command number sent by the host and perform a cyclic redundancy check;
[0107] The first return information sending module 72 is used to determine the serial port command corresponding to the command number if the cyclic redundancy check is successful, and send a return message indicating the successful transmission to the host;
[0108] The second return information sending module 73 is configured to send return information indicating transmission failure to the host if the cyclic redundancy check fails.
[0109] An embodiment of the present invention provides a serial port communication device that can record successfully transmitted serial port commands. There is no need to repeatedly transmit the same serial port command later. Only the serial port command number needs to be transmitted, which reduces the amount of transmitted data and improves stability. By inserting multiple command numbers into the same serial port command, retransmission is only required if multiple command numbers are interfered with at the same time, which greatly reduces the probability of retransmission in harsh environments. According to the stability of data transmission, the transmission baud rate is dynamically adjusted to obtain better transmission efficiency.
[0110] The above-mentioned device also includes: a first command word or second command word processing module, which is used to receive a first command word sent by the host and perform a cyclic redundancy check; if the cyclic redundancy check is successful, determine the serial port command corresponding to the first command word, and send return information indicating successful transmission to the host; if the cyclic redundancy check fails, send return information indicating failure of transmission to the host; receive a second command word sent by the host and perform a cyclic redundancy check; if the cyclic redundancy check is successful, determine the serial port command corresponding to the second command word, save the serial port command, and send return information indicating successful transmission to the host; if the cyclic redundancy check fails, send return information indicating failure of transmission to the host.
[0111] The above device also includes: a fourth command word or a fifth command word processing module, which is used to receive the fourth command word sent by the host and adjust the baud rate of communication between the host and the slave; receive the fifth command word sent by the host and adjust the range of the cyclic redundancy check and the data field check.
[0112] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the serial port communication device applied to the slave described above can refer to the corresponding process in the aforementioned embodiment of the serial port communication method applied to the slave, and will not be repeated here.
[0113] Example 6:
[0114] The embodiment of the present invention further provides an electronic device for executing the above serial communication method; Figure 8 The structure diagram of an electronic device shown in the figure includes a memory 100 and a processor 101, wherein the memory 100 is used to store one or more computer instructions, and the one or more computer instructions are executed by the processor 101 to implement the above-mentioned serial communication method.
[0115] Furthermore, Figure 8 The electronic device shown further includes a bus 102 and a communication interface 103 , and the processor 101 , the communication interface 103 and the memory 100 are connected via the bus 102 .
[0116] The memory 100 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is achieved through at least one communication interface 103 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used. The bus 102 may be an ISA bus, a PCI bus, or an EISA bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 8Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0117] The processor 101 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in the processor 101 or software instructions. The above processor 101 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present invention can be directly embodied as being executed by a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium well-known in the art, such as a random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or register. The storage medium is located in the memory 100, and the processor 101 reads the information in the memory 100 and, in conjunction with its hardware, completes the steps of the method of the aforementioned embodiment.
[0118] An embodiment of the present invention further provides a computer-readable storage medium, which stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement the above-mentioned serial port communication method. The specific implementation can be found in the method embodiment and will not be repeated here.
[0119] The computer program product of the serial communication method and apparatus provided in the embodiments of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the method in the previous method embodiment. The specific implementation can be found in the method embodiment and will not be repeated here.
[0120] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working process of the system and / or device described above can refer to the corresponding process in the aforementioned method embodiment and will not be repeated here.
[0121] In addition, in the description of the embodiments of the present invention, unless otherwise expressly specified or limited, the terms "mounted," "connected," and "connected" should be understood in a broad sense. For example, they may refer to fixed connections, detachable connections, or integral connections; they may refer to mechanical connections or electrical connections; they may refer to direct connections or indirect connections through an intermediate medium; and they may refer to internal communication between two components. Those skilled in the art will understand the specific meanings of the above terms in the present invention based on the specific circumstances.
[0122] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the various embodiments of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, and other media that can store program code.
[0123] In the description of the present invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings and are intended solely to facilitate and simplify the description of the present invention. They are not intended to indicate or imply that the devices or components referred to must have, be constructed, or operate in a specific orientation, and therefore should not be construed as limitations on the present invention. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0124] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the above-described embodiments, those skilled in the art should understand that any person skilled in the art can modify or easily conceive of changes to the technical solutions described in the above-described embodiments within the technical scope disclosed by the present invention, or replace some of the technical features therein with equivalents. Such modifications, changes, or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be based on the scope of protection of the claims.
Claims
1. A serial communication method, characterized in that: Applied to a host, the method includes: Performing data communication with a slave device via a serial port to determine whether a serial port command has been stored in the slave device; If it has been stored in the slave device, determining whether to perform anti-interference transmission based on the success rate of historical transmission of the serial port command; If anti-interference transmission is not performed, transmitting the command number of the serial port command to the slave device; If anti-interference transmission is performed, a plurality of repeated command numbers of the serial port command are transmitted to the slave device.
2. The method according to claim 1, characterized in that After the step of transmitting the command number of the serial port command to the slave device, the method further includes: receiving the return information sent by the slave; If the return information indicating successful transmission is received within the preset time, the number of successful transmissions is recorded; If the return information indicating a transmission failure is received within a preset time, the number of transmission failures is recorded, and the command number of the serial port command is retransmitted to the slave device; If the return information is not received within the preset time, the command number of the serial port command is retransmitted to the slave device.
3. The method according to claim 1, characterized in that After the step of determining whether the serial port command has been stored in the slave device, the method further includes: If the command is not already stored in the slave device, transmitting the serial port command to the slave device; Determining whether the serial port command is stored in the slave; If storage is not performed in the slave, sending a first command word to the slave; If storage is to be performed in the slave, a second command word is sent to the slave.
4. The method according to claim 3, characterized in that After the step of sending the first command word to the slave, the method further includes: receiving the return information sent by the slave; If the return information is not received within the preset time, the serial port command is retransmitted to the slave device.
5. The method according to claim 4, characterized in that After the step of retransmitting the serial port command to the slave device, the method further includes: If the serial port command is successfully retransmitted to the slave, the number of successful retransmissions is recorded. If retransmission of the serial port command to the slave fails, the number of retransmission failures is recorded.
6. A serial communication method, characterized in that: Applied to a slave device, the method includes: Receive the command number sent by the host and perform cyclic redundancy check; If the cyclic redundancy check succeeds, determining the serial port command corresponding to the command number, and sending a return message indicating successful transmission to the host; If the cyclic redundancy check fails, a return message indicating that the transmission failed is sent to the host.
7. The method according to claim 6, characterized in that The method comprises: receiving a first command word sent by the host and performing a cyclic redundancy check; if the cyclic redundancy check succeeds, determining a serial port command corresponding to the first command word, and sending a return message indicating a successful transmission to the host; if the cyclic redundancy check fails, sending a return message indicating a failed transmission to the host; Receive a second command word sent by the host and perform a cyclic redundancy check; if the cyclic redundancy check is successful, determine the serial port command corresponding to the second command word, save the serial port command, and send return information indicating successful transmission to the host; if the cyclic redundancy check fails, send return information indicating failed transmission to the host.
8. The method according to claim 6, characterized in that The method comprises: receiving a fourth command word sent by the host, and adjusting a baud rate at which the host communicates with the slave; Receive the fifth command word sent by the host and adjust the range of the cyclic redundancy check and the data field check.
9. A serial communication device, characterized in that: Applied to a host, the device includes: A serial port command storage determination module is used to perform data communication with the slave via the serial port and determine whether the serial port command has been stored in the slave; an anti-interference transmission judgment module, configured to determine whether to perform anti-interference transmission based on a success rate of historical transmission of the serial port command if the command has been stored in the slave; A command number transmission module, configured to transmit the command number of the serial port command to the slave device if anti-interference transmission is not performed; The anti-interference transmission module is used to transmit a plurality of repeated command numbers of the serial port commands to the slave device if anti-interference transmission is performed.
10. A serial communication device, characterized in that: Applied to a slave device, the device includes: The command number receiving module is used to receive the command number sent by the host and perform cyclic redundancy check; a first return information sending module, configured to, if a cyclic redundancy check is successful, determine the serial port command corresponding to the command number, and send return information indicating successful transmission to the host; The second return information sending module is used to send return information indicating transmission failure to the host if the cyclic redundancy check fails.