Serial port communication method for rapidly controlling lamp beads and application system
By adopting command packets and answer packets in serial communication between the host and slave, combining response timeout timing, header inspection and CRC verification, the problems of low control efficiency and large delay in traditional LED bead control methods are solved, and fast, flexible and precise control of a large number of LED beads is achieved.
Patent Information
- Application Number
- CN202510328600.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-19
- Publication Date
- 2025-06-17
AI Technical Summary
The traditional LED lamp bead control method cannot achieve flexible real-time control and precise brightness adjustment. When controlling a large number of LED lamp beads, due to the bandwidth limitation of serial communication and the redundancy of the ASCII code protocol, the transmission efficiency of control instructions is low, making it difficult to meet the real-time control needs in rapidly changing scenarios.
The command packet is built through the master and sent to the slave, and the reply timeout timing is activated at the same time. The triple protection mechanism of the acknowledgement timeout, header checking and CRC verification ensures the reliability of communication. After the slave receives the command packet, it performs header inspection and CRC verification, parses the command code and performs corresponding lamp bead control operations to construct the feedback execution result of the response packet.
It improves the communication efficiency between the master and slave, reduces the delay when controlling the lamp beads, ensures the reliability and stability of communication, and achieves fast, flexible and precise control of a large number of LED lamp beads.
Smart Images

Figure CN120165816A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of lighting control, and particularly relates to a serial communication method and application system for quickly controlling lamp beads. Background Art
[0002] With the rapid development of lighting technology, LED lamp beads have become the dominant light source in the lighting field, with advantages such as high energy efficiency, long service life, and small size. However, in actual applications, traditional LED lamp bead control methods mainly rely on simple on-off control or preset lighting modes, and cannot achieve flexible real-time control and precise brightness adjustment, which limits the application effect of LED lamp beads in intelligent lighting systems.
[0003] In related technologies, control instructions can be sent to the LED driver module through the serial port to achieve control of the brightness and color of LED lamp beads. This method uses ASCII code as the communication protocol, encodes the control instructions and transmits them through the serial port. After receiving the instructions, the LED driver module decodes and executes the corresponding control operations, thus achieving real-time control of LED lamp beads.
[0004] However, when a large number of LED lamp beads need to be controlled simultaneously, due to the bandwidth limitation of serial communication and the redundancy of the ASCII code protocol, the transmission efficiency of control instructions is low, it is difficult to meet the real-time control requirements in rapidly changing scenarios, and the response speed of the system is significantly lagged. Summary of the Invention
[0005] This application provides a serial communication method and application system for quickly controlling lamp beads, which is used to improve the communication efficiency between the host and the slave and reduce the delay when controlling lamp beads.
[0006] In the first aspect, this application provides a serial communication method for quickly controlling lamp beads. The host constructs a command data packet; the host sends the command data packet to the slave through the serial port and starts the response timeout timing. After receiving the command data packet, the slave sends a response data packet to the host through the serial port. If the response timeout timing is greater than the preset threshold when the host receives the response data packet, the host resends the command data packet. If the response timeout timing is not greater than the preset threshold when the host receives the response data packet, the host determines whether the packet header of the response data packet is correct. If the packet header of the response data packet is correct, the host calculates the CRC of the response data packet and performs verification. If the verification passes, the host parses the command confirmation code ACK in the response data packet and determines the execution result of the slave according to the command confirmation code ACK. If the command confirmation code ACK indicates success, the host sends the next command data packet. If the command confirmation code ACK indicates failure, the host resends the command data packet; If the check fails, the host discards the response data packet; If the packet header of the response data packet is incorrect, the host discards the response data packet.
[0007] By adopting the above technical solution, the host constructs a command data packet and sends it to the slave, and at the same time starts the response timeout timing to establish a communication connection. Combining the triple protection mechanisms of response timeout timing, packet header check and CRC check, it can timely detect abnormal situations such as data loss and data error in the communication process. When a response timeout or data anomaly occurs, the host will automatically resend the command data packet to ensure the reliability of communication. Through the setting of the command confirmation code ACK, the slave can feedback the execution result of the command to the host, enabling the host to accurately judge whether the slave has correctly executed the control instruction, reducing data errors and losses in the communication process, and improving the stability and reliability of communication. Using the command data packet method to transmit control instructions, the control information is structured and encapsulated, which is convenient for data parsing and processing, reduces the complexity of data processing, and improves the response speed of the system.
[0008] Combined with some embodiments of the first aspect, in some embodiments, the host constructs a command data packet, specifically including: The host receives the selected color lamp performance style of the user; The host determines the corresponding performance file according to the color lamp performance style; The host reads the color palette in the performance file and parses the number of colors and color values of the color palette; The host calculates the number of bits required for each color encoding according to the number of colors; The host obtains the command data packet after encoding according to the number of bits.
[0009] By adopting the above technical solution, through the adaptive calculation of color encoding on the host side, the number of bits required for each color is dynamically determined according to the number of colors in the color palette, so that the encoding length of color information can be automatically adjusted according to actual needs. This adaptive encoding method avoids the problems of resource waste or insufficient expression ability that may be caused by using fixed-bit encoding. When the number of colors in the color palette is small, fewer bits are used to represent color information, reducing the data transmission volume; when the number of colors increases, the number of bits is correspondingly increased to meet the expression requirements. This dynamic encoding method based on the actual number of colors compresses the data volume while ensuring the complete expression of color information, reduces the communication burden, and improves the data transmission efficiency. By optimizing the encoding method, the data volume of each communication is reduced, thereby reducing the communication delay and enhancing the real-time performance of the lamp bead control.
[0010] In some embodiments in combination with some embodiments of the first aspect, before sending a response data packet to the host through the serial port, the method further includes: The slave checks whether the packet header of the command data packet is correct; If the packet header is correct, the slave calculates and verifies the CRC of the command data packet; If the verification passes, the slave parses the command code CMD of the command data packet and performs the corresponding LED control operation; The slave constructs a response data packet according to the execution result of the command code CMD; If the verification fails, the slave discards the command data packet; If the packet header is incorrect, the slave discards the command data packet.
[0011] By adopting the above technical solution, an integrity verification mechanism for received data is implemented at the slave end. Through double verification of packet header check and CRC verification, data errors occurring in the communication process can be effectively identified. When data anomalies are found, the slave will directly discard the error data packet to avoid executing incorrect control instructions. This mechanism of data verification before performing control operations ensures that only complete and correct control instructions will be executed, effectively preventing incorrect actions of the LED beads caused by data errors. At the same time, the slave will construct a response data packet according to the execution result of the command and return it to the host, enabling the host to timely understand the execution situation of the command. This mechanism based on data verification and execution result feedback improves the accuracy and reliability of LED bead control and reduces control anomalies caused by communication errors.
[0012] In some embodiments in combination with some embodiments of the first aspect, when the slave parses the command code CMD of the command data packet and performs the corresponding LED control operation, it specifically includes: The slave parses the command data packet to obtain the command code CMD and determines the CMD value; If the CMD value is 0, the slave parses the palette data corresponding to the palette in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the palette data to the response data packet when the slave constructs the response data packet; If the CMD value is 1, the slave reads the breathing and color-changing information in the command data packet and updates the LED control strategy according to the breathing and color-changing information; The slave sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the LED control strategy to the response data packet when the slave constructs the response data packet; If the CMD value is 2, the slave parses the LED bead color and duration in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the LED bead color and duration to the response data packet when the slave constructs the response data packet.
[0013] By adopting the above technical solutions, multi-mode control of the lamp beads is achieved by defining different command codes CMD, including functions such as palette data synchronization, breathing effect control, and direct lighting control. The slave device executes the corresponding control logic according to different CMD values and returns specific execution parameters in the response data packet, enabling the master device to accurately grasp the working state of the slave device. This control method based on command codes enables the system to flexibly achieve different lighting effects. By encapsulating both control parameters and execution results in the data packet for transmission, the integrity and traceability of the control process are ensured. This structured control mechanism not only improves the maintainability of the system but also enhances the scalability of the system, facilitating the subsequent addition of new control functions. At the same time, by implementing parameter parsing and execution logic at the slave device end, the processing burden on the master device is reduced, and the operating efficiency of the entire system is improved.
[0014] Combined with some embodiments of the first aspect, in some embodiments, after the command acknowledgment code ACK indicates success, the method further includes: When the master device determines that the ACK value is equal to 0 according to the command acknowledgment code ACK, the master device confirms the palette data and synchronizes it to the slave device; The master device reads the next performance file script; When the master device determines that the CMD value of the next performance file script is equal to 1, the master device generates a command data packet according to the lamp bead control strategy and executes the step of the master device sending the next command data packet; When the master device determines that the CMD value of the next performance file script is equal to 2, the master device generates a command data packet according to the lamp bead color and duration and executes the step of the master device sending the next command data packet; When the master device determines that the CMD value of the next performance file script is not 1 or 2, the master device executes the step of the master device reading the next performance file script.
[0015] By adopting the above technical solutions, by the master device confirming and synchronizing the palette data to the slave device, combined with the master device adopting corresponding data packet generation and sending strategies according to different CMD values, the system can accurately identify and process different types of lamp bead control instructions. When the CMD value is 1, the master device can generate a corresponding command data packet according to the lamp bead control strategy, enabling the slave device to control the lamp beads according to the preset breathing and color-changing information; when the CMD value is 2, the master device can generate a command data packet according to the specific lamp bead color and duration parameters to ensure that the lamp beads can be displayed according to the expected effect. For other CMD value cases, the system will continue to read the next performance file script, avoiding the processing of invalid instructions. This classification processing mechanism based on CMD values improves the accuracy and efficiency of instruction processing, reduces the error rate during instruction parsing, and at the same time ensures the continuity and smoothness of lamp bead control, making the entire lamp bead control process more reliable and stable.
[0016] In some embodiments in combination with some embodiments of the first aspect, after the host generates a command data packet according to the lamp bead control strategy and performs the step of the host sending the next command data packet, the method further includes: The host receives and parses the ACK value in the response data packet sent by the slave; The host determines whether the ACK value is equal to 0; If the ACK value is equal to 0, the host reads the next performance file script; If the ACK value is not equal to 0, the host resends the command.
[0017] By adopting the above technical solution, the host establishes a reliable data transmission verification mechanism by receiving and parsing the ACK value in the response data packet returned by the slave and taking corresponding processing measures according to the ACK value. When the ACK value is equal to 0, it indicates that the instruction is executed successfully, and the system can continue to process the next performance file script, ensuring the continuity of the lamp bead control process; when the ACK value is not equal to 0, it indicates that the instruction execution fails, and the system will resend the command to ensure that each control instruction can be correctly executed. This feedback mechanism based on the ACK value can timely detect and handle abnormal situations during the instruction execution process, avoid lamp bead control abnormalities caused by communication failures or instruction execution failures, improve the reliability and stability of the entire system, and make the lamp bead control effect more accurate and coherent.
[0018] In some embodiments in combination with some embodiments of the first aspect, after the host generates a command data packet according to the lamp bead color and duration and performs the step of the host sending the next command data packet, the method further includes: The host sets a timer and reads the timing duration of the timer; When the host determines that the timing duration is greater than the preset duration, the host reads the next performance file script.
[0019] By adopting the above technical solution, by setting a timer and monitoring the timing duration, the system realizes precise control of the lamp bead display duration. When the timing duration exceeds the preset duration, the system will automatically read the next performance file script. This mechanism ensures that each lamp bead display effect can be switched strictly according to the preset time. The introduction of the timer enables the system to precisely manage the lamp bead control in the time dimension, avoid time deviation of the display effect, and ensure the timing accuracy between various effects during the entire lamp bead performance process. This control mechanism based on the timer improves the time accuracy of the lamp bead performance, makes the entire performance process more smooth and natural, and enhances the viewing experience.
[0020] In a second aspect, an embodiment of the present application provides an application system, which includes: one or more processors and a memory; the memory is coupled to the one or more processors, and the memory is used to store computer program code, the computer program code includes computer instructions, and the one or more processors call the computer instructions to cause the application system to execute the method described in the first aspect and any possible implementation manner in the first aspect.
[0021] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, including instructions, when the above instructions run on the application system, causing the above application system to execute the method described in the first aspect and any possible implementation manner in the first aspect.
[0022] In a fourth aspect, an embodiment of the present application provides a computer program product, characterized in that when the computer program product runs on the application system, causing the application system to execute the method described in any possible implementation manner in the first aspect.
[0023] One or more technical solutions provided in the embodiments of the present application have at least the following technical effects or advantages: 1. The present application provides a serial communication method for quickly controlling light beads. By the host constructing a command data packet and sending it to the slave, and at the same time starting the response timeout timing to establish a communication connection, combined with the triple protection mechanisms of response timeout timing, packet header check, and CRC check, abnormal situations such as data loss and data errors during the communication process can be detected in a timely manner. When a response timeout or data anomaly occurs, the host will automatically retransmit the command data packet to ensure the reliability of the communication. Through the setting of the command confirmation code ACK, the slave can feedback the execution result of the command to the host, enabling the host to accurately judge whether the slave has correctly executed the control instruction, reducing data errors and losses during the communication process, and improving the stability and reliability of the communication. Using the command data packet method to transmit control instructions, the control information is structured and encapsulated, facilitating data parsing and processing, reducing the complexity of data processing, and improving the response speed of the system.
[0024] 2. This application provides a serial communication method for quickly controlling LED beads. By defining different command codes CMD, multi-mode control of LED beads is achieved, including functions such as palette data synchronization, breathing effect control, and direct lighting control. The slave device executes corresponding control logic according to different CMD values and returns specific execution parameters in the response data packet, enabling the master device to accurately grasp the working state of the slave device. This control method based on command codes enables the system to flexibly achieve different lighting effects. By encapsulating control parameters and execution results in data packets for transmission, the integrity and traceability of the control process are ensured. This structured control mechanism not only improves the maintainability of the system but also enhances the scalability of the system, facilitating the addition of new control functions in the future. At the same time, by implementing parameter parsing and execution logic at the slave device end, the processing burden on the master device is reduced, and the operating efficiency of the entire system is improved.
[0025] 3. This application provides a serial communication method for quickly controlling LED beads. By the master device confirming and synchronizing palette data to the slave device and combining the master device's corresponding data packet generation and sending strategies according to different CMD values, the system can accurately identify and process different types of LED bead control instructions. When the CMD value is 1, the master device can generate corresponding command data packets according to the LED bead control strategy, enabling the slave device to control the LED beads according to the preset breathing and color-changing information. When the CMD value is 2, the master device can generate command data packets according to specific LED bead color and duration parameters to ensure that the LED beads can be displayed according to the expected effect. For other CMD value cases, the system will continue to read the next performance file script, avoiding the processing of invalid instructions. This classification processing mechanism based on CMD values improves the accuracy and efficiency of instruction processing, reduces the error rate during instruction parsing, and at the same time ensures the continuity and smoothness of LED bead control, making the entire LED bead control process more reliable and stable. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 is a schematic diagram of the command interaction process between the master device and the slave device in an application system provided by this application.
[0027] Figure 2 is a schematic flowchart of a serial communication method for quickly controlling LED beads in an embodiment of this application.
[0028] Figure 3 is another schematic flowchart of a serial communication method for quickly controlling LED beads in an embodiment of this application.
[0029] Figure 4 is a schematic diagram of the physical device structure of an application system for quickly controlling LED beads provided by an embodiment of this application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0030] The terms used in the following embodiments of the present application are only for the purpose of describing specific embodiments and are not intended to limit the present application. As used in the specification and appended claims of the present application, the singular forms "a", "an", "the", "above", "said", "this" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used in the present application refers to any and all possible combinations including one or more of the listed items.
[0031] Hereinafter, the terms "first" and "second" are only used for descriptive purposes and should not be construed as implying or suggesting relative importance or implicitly indicating the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include one or more of such features. In the description of the embodiments of the present application, unless otherwise specified, the meaning of "a plurality" is two or more.
[0032] First, in combination with Figure 1 , the command interaction process between the host and the slave in the application system of the present application will be described. Please refer to Figure 1 , which is a schematic diagram of the command interaction process between the host and the slave in an application system provided by the present application. Among them, the host first sets the palette process, and then constructs a command packet of header (2 bytes) + packet length (2 bytes) + CMD(0) + palette data + CRC (2 bytes) and sends it to the slave. After receiving the command packet, the slave checks the header, packet length, and CRC check. If the check passes, the slave constructs a response packet of header (2 bytes) + packet length (2 bytes) + CMD(0) + ACK (1 byte) + CRC (2 bytes) and sends it to the host. Then the host sends the breathing color change or control LED color command process, and constructs a command packet of header (2 bytes) + packet length (2 bytes) + CMD(1 / 2) + relevant data + CRC (2 bytes) and sends it to the slave. The slave repeats the steps of checking the header, packet length, and CRC check. If the check passes, it constructs a response packet of header (2 bytes) + packet length (2 bytes) + CMD(1 / 2) + ACK (1 byte) + CRC (2 bytes) and sends it to the host.
[0033] Next, a serial communication method for quickly controlling LED beads in an embodiment of the present application will be described by using an example and in combination with Figure 1 : Please refer to Figure 1 , which is a flowchart of a serial communication method for quickly controlling LED beads in an embodiment of the present application.
[0034] S101. The host constructs a command data packet; The host constructs a command data packet. Specifically, the host receives the selected color light performance style of the user; The host determines the corresponding performance file according to the colored light performance style; The host reads the color palette in the performance file and parses the number of colors and color values in the color palette; The host calculates the number of bits required for encoding each color according to the number of colors; After encoding according to the number of bits, the host obtains a command data packet.
[0035] In this step, the host first receives the colored light performance style selected by the user, and then determines the corresponding performance file according to the selected performance style. The performance file contains color palette information. The host reads and parses the number of colors and specific color values in the color palette. According to the number of colors, the host calculates the number of bits required for encoding each color, and encodes the color values according to the calculated number of bits, and finally obtains a command data packet. In the serial communication protocol generated in this application, color encoding adopts an adaptive method, and the number of bits occupied by each color is determined according to the total number of colors (N) in the color palette. The calculation formula is rounding up of log2 N. For example, when the total number of colors N = 15, each specified color will be encoded using 4 bits (rounding up of log216). This method effectively saves the data transmission volume while meeting the color representation requirements.
[0036] In specific implementation, the host can provide a user interface for the user to select different colored light performance styles. For example, a drop-down menu or an icon list can be provided for the user to click and select. The way to determine the performance file can be through a pre-set mapping relationship, which maps the performance style to the file name one by one. The format of the color palette can be a fixed data structure, including a color number field and a color value array, which is convenient for the host to parse. The encoding method of the color value can be selected according to specific requirements, such as RGB encoding, HSV encoding, etc.
[0037] In some scenarios, different colored lights may support different numbers of colors. Therefore, when the host constructs a command data packet, it can also first send a query instruction to the colored light to obtain the maximum number of colors supported by the colored light, and then calculate the number of bits and perform encoding according to this number, which can improve the compatibility and flexibility of the system. When the host needs to control multiple colored lights, the address information of the colored lights can also be added to the command data packet to achieve separate control of different colored lights.
[0038] S102. The host sends the command data packet to the slave through the serial port and starts the response timeout timing; In this step, the host sends the constructed command data packet to the slave through the serial port, and at the same time starts a response timeout timer. This timer is used to limit the time for the host to wait for the slave's response, and avoid the host being blocked in the waiting response state for a long time.
[0039] The communication between the master and the slave can adopt the standard RS232 serial communication protocol or other serial-based communication protocols. Parameters such as the baud rate, data bits, stop bits, and parity bits of the communication can be configured according to specific requirements. The response timeout timer can be implemented using the built-in timer module of the master. The setting of the timeout time can be determined according to factors such as the communication rate and the packet size, and generally can be set to several times the packet transmission time.
[0040] To improve the reliability of communication, when the master sends a command data packet, it can also adopt mechanisms such as data verification and retransmission. For example, the master can add a verification code to the data packet. After the slave receives the data packet, it performs verification. If the verification fails, it sends a retransmission request to the master. After receiving the retransmission request, the master resends the command data packet until the slave receives it successfully. In addition, the master can also send heartbeat packets to the slave regularly to detect the connectivity of the communication link. If no response from the slave is received within a certain period of time, it can be considered that the communication has an abnormality, and corresponding abnormality handling measures are taken.
[0041] S103. After the slave receives the command data packet, it sends a response data packet to the master through the serial port; In this step, after the slave receives the command data packet sent by the master, it needs to send a response data packet to the master to inform the master that the command has been received. The response data packet can contain simple confirmation information or the status information of the slave, etc.
[0042] The way the slave receives the data packet is similar to the way the master sends the data packet, and it also communicates through the serial port. The slave needs to continuously monitor the serial port and perform parsing and processing when receiving data. The format of the response data packet can be similar to the format of the command data packet, including fields such as a packet header, a data body, and a verification code.
[0043] To improve the communication efficiency, after the slave receives the command data packet, it can immediately perform parsing and processing instead of waiting until the response data packet is sent. This can reduce the response time of the slave and improve the real-time performance of the system. In addition, the slave can also adopt a double-buffer mechanism. One buffer is used to receive the command data packet from the master, and the other buffer is used to send the response data packet. The two buffers are used alternately, which can further improve the communication efficiency. In the case of multiple slaves, when the slave sends a response data packet, it also needs to consider avoiding conflicts caused by multiple slaves sending simultaneously, and mechanisms such as time-division multiplexing and token passing can be used to coordinate the sending timing of multiple slaves.
[0044] S104. If the response timeout timer is greater than the preset threshold when the master receives the response data packet, the master resends the command data packet; In this step, after the host sends the command data packet, it will wait for the slave's response data packet. If the response data packet is not received within the preset timeout period, or the value of the timeout timer is greater than the preset threshold when the response data packet is received, it is considered that the communication fails this time, and the host needs to resend the command data packet.
[0045] The setting of the timeout threshold needs to be determined according to the specific communication environment and system requirements, and a suitable value can be obtained through testing and statistics. The operation of the host to resend the command data packet can be exactly the same as the first sending, or some optimization measures can be adopted, such as increasing the retry count limit, adjusting the timeout time, modifying the data packet format, etc.
[0046] Frequent communication timeouts and retransmissions will affect the performance and stability of the system. Therefore, the host can also take some measures to reduce the probability of timeouts, such as dynamically adjusting the timeout threshold according to the communication quality, adopting an alternative communication method (such as switching to another serial port or using wireless communication, etc.) when the communication is abnormal, or using a reliable communication protocol (such as TCP) at the application layer to replace the original serial port-based communication method. In addition, the host can also record and analyze the communication timeouts and retransmissions through means such as logs and monitoring, so as to discover and locate problems in time.
[0047] S105. If the response timeout timer is not greater than the preset threshold when the host receives the response data packet, the host judges whether the packet header of the response data packet is correct; In this step, if the host receives the slave's response data packet within the preset timeout period and the value of the timeout timer is not greater than the preset threshold, it is considered that the communication is successful this time. Next, the host needs to judge whether the received response data packet is legal, and first check whether the packet header of the data packet is correct.
[0048] The packet header of the response data packet can contain some fixed fields, such as the start flag, packet type, packet length, etc. The host can judge whether the packet header is correct according to the values of these fields. For example, the start flag can be a fixed character or string, the packet type can be represented by an enumeration value, and the packet length can be calculated according to the actual data length.
[0049] In order to improve the efficiency and accuracy of data packet parsing, the host can use state machine and other technologies to implement packet header judgment and verification. For example, a state machine can be defined, including states such as waiting for the start flag, receiving the packet type, and receiving the packet length. The state machine conversion is driven according to the received byte stream until the entire packet header is parsed. During the parsing process, if data that does not conform to the format is encountered, the parsing can be terminated in time and the data packet can be discarded to avoid affecting subsequent data. In addition, in order to enhance the security and integrity of the response data packet, fields such as checksum, sequence number, and timestamp can be added to the packet header. The host also needs to check these fields when verifying the packet header.
[0050] S106, the host calculates and verifies the CRC of the response data packet; If the header of the response data packet is correct, the host calculates the CRC of the response data packet and verifies it.
[0051] In this step, after the host confirms that the header of the response data packet is correct, it is also necessary to verify the data body of the data packet to ensure that the data has not been damaged or tampered with during transmission. A common data verification method is CRC (cyclic redundancy check). The host can calculate the CRC value based on the data body of the data packet and compare it with the CRC value in the data packet. If the two are equal, the data packet verification is considered to have passed, otherwise the verification is considered to have failed.
[0052] The principle of CRC check is that the sender calculates a check code of fixed length based on the data content and appends it to the end of the data packet. After receiving the data packet, the receiver uses the same algorithm to recalculate the check code and compares it with the received check code. There are many variants of CRC algorithm, such as CRC-8, CRC-16, CRC-32, etc. Different variants use different generating polynomials and initial values, and the calculated check code length is also different.
[0053] In addition to CRC check, other data check methods can also be used, such as parity check, sum check, Hamming code, etc. These methods have their own advantages and disadvantages, and can be selected according to specific application scenarios and requirements. For example, the calculation of parity check and sum check is simple, but the error detection capability is weak; the error detection and correction capability of Hamming code is strong, but the calculation complexity is high. In addition, multiple check methods can be used in combination to improve the reliability of data transmission. When the check fails, the host can send a retransmission request to the slave, requiring the slave to resend the response data packet, or it can directly discard the data packet, depending on the application requirements.
[0054] S107, if the verification is passed, the host parses the command confirmation code ACK in the response data packet, and determines the execution result of the slave according to the command confirmation code ACK; In this step, if the CRC check of the response packet by the host passes, it indicates that the data body of the packet is complete and correct. Next, the host needs to parse the command confirmation code ACK from the data body, which is used to represent the execution result of the command by the slave. The host determines whether the slave has successfully executed the command and the specific execution result based on the value of ACK.
[0055] The command confirmation code ACK can be represented by an enumeration type or an integer value, and different values correspond to different execution results. For example, it can be defined that 0 represents success, 1 represents failure, 2 represents parameter error, etc. The length of the ACK field can be set as needed, and usually one byte or two bytes are sufficient. When parsing ACK, the host needs to follow the pre-agreed format and can use bit operations, shifts, etc. to extract the value of ACK.
[0056] To enhance the flexibility and scalability of the system, the ACK field can also carry some additional information, such as error codes, status codes, etc. In this way, the host can not only know the execution result of the command but also obtain more detailed information for corresponding processing. For example, if ACK indicates execution failure, the host can judge the reason for failure according to the carried error code, whether it is a parameter error or a device failure, etc., and then take different measures. To support this expansion, the length of the ACK field can be set longer or a variable-length coding method can be used. In addition, when parsing ACK, the host also needs to perform boundary checks and exception handling to avoid parsing errors caused by incomplete or incorrect data in the packet.
[0057] S108. If the command confirmation code ACK indicates success, the host sends the next command packet; In this step, if the command confirmation code ACK obtained by the host parsing the response packet indicates that the slave has successfully executed the command, the host can continue to send the next command. In this case, the host does not need to retransmit the current command and can directly send the subsequent command packets to maintain the continuity and efficiency of communication.
[0058] The process of the host sending the next command packet is similar to the process of sending commands before. It is necessary to construct the command packet, send it to the slave through the serial port, and start the response timeout timer, etc. To improve communication efficiency, the host can prepare the next command immediately after sending a command without waiting for the response from the slave. Of course, this requires the host to have enough buffer space to store multiple commands and needs to set the timeout time reasonably to avoid packet loss or duplication.
[0059] In some cases, the host may need to send a series of related commands, which have a sequential order and dependency relationship. To ensure the correct execution of the commands, before sending the next command, the host can first determine whether the current command has been successfully executed. Only after the current command is successfully executed will the host send the next command. Otherwise, strategies such as retransmission or skipping can be adopted. This approach can improve the reliability of command execution and avoid situations where command execution is incorrect or incomplete. In addition, according to the application requirements, after sending all the commands, the host can send a special command to notify the slave that all commands have been sent, enabling the slave to perform corresponding processing and responses.
[0060] S109. If the command acknowledgment code ACK indicates failure, the host re-sends the command data packet. In this step, if the command acknowledgment code ACK obtained by the host from parsing the response data packet indicates that the slave has failed to execute the command, the host needs to re-send the command data packet. In this case, the host cannot continue to send subsequent commands but should repeat sending the current command until the slave successfully executes it.
[0061] The process of the host re-sending the command data packet is basically the same as the first sending, including steps such as constructing the command data packet, sending it through the serial port, and starting the response timeout timer. To avoid the impact of repeated command sending on system performance, the host can set an upper limit on the number of retransmissions, for example, retransmitting at most 3 times. If a failure response is still received after exceeding the number of retransmissions, the host can consider that the slave has malfunctioned and take corresponding exception handling measures, such as alarming or restarting the slave.
[0062] To find out the reason for the command execution failure, the host can check and correct the content of the command data packet before re-sending the command. For example, check whether the command parameters are correct and whether the data format meets the requirements. It can also judge the specific reason for the failure based on the error code or status information returned by the slave, such as parameter out of range, device busy, etc. The host can also adopt some fault tolerance mechanisms, such as adding a random delay when re-sending the command to avoid conflicts caused by multiple hosts re-sending simultaneously. It can also send a status query command after the response timeout to understand the current status of the slave and then decide whether to re-send the command. Additionally, if a certain command fails to execute multiple times, the host can try to send a simple test command to test whether the slave is working properly. If the test command also fails, there may be a problem with the communication link or the slave itself, and further troubleshooting and handling are required.
[0063] S110. If the verification fails, the host discards the response data packet. In this step, if the host finds that the CRC check fails for the response packet, it indicates that an error occurred during the transmission of the packet, the data is incomplete or tampered with. In this case, the host cannot obtain valid information from the response packet, so it needs to directly discard the packet without further processing.
[0064] The operation of the host discarding the response packet is relatively simple. It only needs to clear the receive buffer and release the relevant memory space. To avoid losing subsequent packets, the host can start receiving the next packet immediately after discarding the current packet. Or it can wait for a period of time after discarding the packet before starting to receive, in case the packet error is caused by a short-term interference.
[0065] To reduce the situation of packet check failure, the host and the slave can take some measures to improve the communication quality, such as using shielded cables, increasing grounding protection, reducing the communication rate, etc. Optimization can also be carried out at the software level, such as adding a packet retransmission mechanism, using a more reliable communication protocol, etc. In addition, the host can also record the situation of packet check failure through means such as logs and monitoring for problem analysis and location. If the check failure occurs frequently, it may be necessary to check and maintain the communication link or device. After discarding the packet, the host also needs to consider whether to resend the command and how to ensure the continuity and consistency of the command. One way is to resend the last successfully executed command to restore the state; another way is to send a status query command first during the next communication to understand the current state of the slave, and then send a new command.
[0066] S111. The host discards the response packet.
[0067] If it is determined in step S105 that the packet header of the response packet is incorrect, the host discards the response packet.
[0068] In this step, if the host finds that the packet header of the packet is incorrect after receiving the response packet, it indicates that the packet is not a legitimate response packet, which may be caused by packet loss, duplication, misalignment, etc. In this case, the host also needs to discard the packet and cannot process it as valid response data.
[0069] The operation of the host discarding the response packet is similar to the handling when the verification fails, and it is necessary to clear the receive buffer and release the relevant resources. However, since the packet header error may be caused by an incomplete or out-of-sync data packet, after the host discards the current data packet, it may be necessary to take some additional measures to restore the data transmission state. For example, the host can send a special synchronization command to the slave to request the slave to re-establish the communication connection; or the host can re-send the last successfully received command to ensure the consistency of the state.
[0070] To avoid the situation of incomplete or out-of-sync response packets, the communication protocol between the host and the slave needs to be designed to be robust and flexible enough. For example, fields such as sequence numbers and timestamps can be added to the data packet to detect the order and timeliness of the data packet; or a fixed-length data frame format can be used to avoid the situation of uncertain data packet lengths. In addition, an error recovery mechanism can also be added to the communication protocol, such as timeout retransmission, flow control, error correction, etc., to improve the reliability of data transmission. After discarding the response packet, the host also needs to decide the next operation according to the specifications of the communication protocol, whether to re-send the command, perform state synchronization, or directly abort the communication, etc. This needs to be fully considered during protocol design and strictly tested and verified during implementation to ensure the correctness and stability of the communication process.
[0071] It should be noted that the host command format obtained in this application is: Name Packet Header Packet Length Command Code DATA CRC Length (byte) 2 2 1 Variable Length 2 Slave response format: Name Packet Header Packet Length Command Code ACK CRC Length (byte) 2 2 1 1 2 In the above embodiment, by the host constructing a command data packet and sending it to the slave, and at the same time starting the response timeout timing to establish a communication connection, combined with the triple protection mechanisms of response timeout timing, packet header check, and CRC check, abnormal situations such as data loss and data errors in the communication process can be detected in a timely manner. When a response timeout or data anomaly occurs, the host will automatically re-send the command data packet to ensure the reliability of communication. Through the setting of the command confirmation code ACK, the slave can feedback the execution result of the command to the host, enabling the host to accurately judge whether the slave has correctly executed the control instruction, reducing data errors and losses in the communication process, and improving the stability and reliability of communication. Using the command data packet method to transmit control instructions, the control information is structured and encapsulated, which is convenient for data parsing and processing, reduces the complexity of data processing, and improves the response speed of the system.
[0072] Furthermore, before sending the response packet to the host through the serial port in the above embodiment steps, the following operations can also be performed. The following combines Figure 2, another serial communication method for quickly controlling LED beads in the embodiments of the present application will be described: Please refer to Figure 2 , which is another process schematic diagram of a serial communication method for quickly controlling LED beads in the embodiments of the present application.
[0073] S201. The slave checks whether the packet header of the command data packet is correct; In this step, after receiving the command data packet sent by the master, the slave first needs to check whether the packet header of the data packet is correct. The packet header is the starting part of the data packet, which contains some key information, such as the data packet type, length, version number, etc. By parsing the packet header, the slave can quickly determine whether the data packet is the expected command data packet and whether the format of the data packet is correct.
[0074] The method for the slave to check the packet header can be designed according to the specific communication protocol. A common way is to add a fixed signature or magic word in the packet header. After receiving the data packet, the slave first checks whether the signature matches. For example, the first few bytes of the packet header can be set to fixed ASCII codes or hexadecimal values, such as CMD, 0x55AA, etc. Another way is to add a check field in the packet header, such as CRC, parity bit, etc. The slave calculates the check value of the received data packet and compares it with the check field in the packet header to determine whether the data packet is complete and correct.
[0075] In practical applications, due to the complexity and variability of the communication environment, various abnormal situations may occur in the packet header of the data packet, such as data corruption, incompleteness, out-of-sync, etc. To improve the reliability and stability of communication, the slave can adopt various strategies to handle abnormal situations when checking the packet header. For example, the slave can set a timeout mechanism when receiving the data packet. If the complete packet header is not received within the specified time, the data packet is considered invalid and directly discarded. In addition, the slave can also perform preprocessing such as filtering and denoising on the received data through software or hardware methods to reduce the impact of data corruption on the packet header check. During the check process, if a packet header error is found, the slave can immediately discard the data packet and no longer continue to receive and process the subsequent data to avoid the impact of invalid data on the system.
[0076] S202. The slave calculates and checks the CRC of the command data packet; If the packet header is correct, the slave calculates and checks the CRC of the command data packet.
[0077] In this step, after the slave checks that the packet header of the command data packet is correct, it is also necessary to verify the content of the data packet to ensure the integrity and accuracy of the data. CRC (Cyclic Redundancy Check) is a commonly used data verification method. It performs polynomial division on the data to obtain a remainder of a fixed length, which is used as the data verification code. When the sender transmits data, it calculates the CRC verification code of the data and appends it to the end of the data packet. After receiving the data, the receiver recalculates the CRC verification code and compares it with the received CRC verification code. If the two are the same, it means the data transmission is correct; otherwise, it means the data is incorrect.
[0078] The specific implementation method for the slave to calculate the CRC verification code can refer to the standards of CRC algorithms, such as CRC-8, CRC-16, CRC-32, etc. Different CRC algorithms use different generating polynomials and initial values, and the lengths of the calculated verification codes are also different. The slave can select an appropriate CRC algorithm according to factors such as the length of the data packet, data type, and bit error rate requirements. When calculating the CRC verification code, the slave needs to first extract the data part in the data packet and then perform CRC calculation on the data to obtain the verification code. Then, compare the calculated verification code with the CRC field at the end of the data packet. If the two are the same, it means the data packet passes the verification; otherwise, it means the data packet is incorrect.
[0079] Although CRC verification is a relatively reliable data verification method, in some cases, verification failures may still occur. For example, when the data packet is severely interfered with or damaged during transmission, even if the CRC verification code is correct, the data itself may already be corrupted. To address this situation, the slave can request the host to resend the data packet again after the CRC verification fails until the verification passes. In addition, the slave can also verify the correctness of the data through other methods, such as comparing the length, format, range, etc. of the data packet to promptly detect and handle abnormal situations. When the CRC verification fails, the slave needs to promptly discard the data packet to avoid the impact of incorrect data on the operation of the system. At the same time, the slave can also record and notify the situation of CRC verification failure through logs, alarms, etc. for fault troubleshooting and maintenance.
[0080] S203. If the verification passes, the slave parses the command code CMD of the command data packet and performs the corresponding LED bead control operation; If the verification passes, the slave parses the command code CMD of the command data packet and performs the corresponding LED bead control operation. Specifically: The slave parses the command data packet to obtain the command code CMD and determines the CMD value; If the CMD value is 0, the slave parses the palette data corresponding to the palette in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the palette data to the response data packet when the slave constructs the response data packet; If the CMD value is 1, the slave reads the breathing and color-changing information in the command data packet and updates the LED control strategy according to the breathing and color-changing information; The slave sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the LED control strategy to the response data packet when the slave constructs the response data packet; If the CMD value is 2, the slave parses the LED color and duration in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value, the LED color and the duration to the response data packet when the slave constructs the response data packet.
[0081] In this step, if the CRC check of the command data packet by the slave passes, it indicates that the content of the data packet is complete and correct. Next, the slave needs to parse the command code CMD from the data packet and perform corresponding LED control operations according to the value of CMD. CMD is a predefined field used to indicate the specific operations that the slave needs to perform, such as setting the LED color, adjusting the brightness, starting the animation effect, etc. By parsing the CMD field, the slave can quickly identify the control instructions sent by the host and take corresponding actions.
[0082] The method for the slave to parse the CMD field can be designed according to the communication protocol and the data packet format. A common way is to place the CMD field at a fixed position in the data packet, such as the first byte after the packet header. After receiving the data packet, the slave can directly extract the value of this byte as the CMD code. The CMD code can be represented using an enumeration type or an integer value, and different values correspond to different operations. For example, 0 can be defined to represent setting the LED color, 1 to represent adjusting the brightness, 2 to represent starting the animation effect, etc. The slave calls the corresponding processing function or driver according to the parsed CMD code to perform specific LED control operations.
[0083] In practical applications, during the process of the slave device performing the LED control operation, various abnormal situations may occur, such as parameters exceeding the range, device failures, resource conflicts, etc. To ensure the accuracy and stability of the control, the slave device can verify and filter the parameters before performing the operation, such as checking whether the LED number is legal, whether the color value is within the specified range, etc. For some complex operations, such as starting an animation effect, the slave device can also first determine whether there are other operations being executed currently to avoid conflicts or interference. After performing the operation, the slave device needs to check the result of the operation and return the corresponding status code, such as success, failure, timeout, etc., so that the master device can timely understand the effect of the control. In addition, the slave device can also record relevant log information during the operation, such as the operation type, parameter values, execution time, etc., for problem troubleshooting and performance optimization.
[0084] S204. If the verification fails, the slave device discards the command data packet; In this step, if the CRC verification of the command data packet by the slave device fails, it indicates that an error has occurred during the transmission of the data packet, and the integrity and accuracy of the data cannot be guaranteed. In this case, the slave device cannot continue to parse and execute the command in the data packet, but needs to directly discard the data packet to prevent the wrong data from affecting the system.
[0085] The operation of the slave device discarding the command data packet is relatively simple. Usually, it only needs to clear the receive buffer and release the relevant memory resources. To avoid losing subsequent data packets, the slave device can start receiving the next data packet immediately after discarding the current data packet. Of course, if data packets with verification failures are received continuously for multiple times, the slave device can take some additional measures, such as pausing data reception, resetting the communication interface, sending an error notification to the master device, etc., to quickly restore the normal communication state.
[0086] S205. The slave device constructs a response data packet according to the execution result of the command code CMD; In this step, after the slave device finishes performing the operation corresponding to the command code CMD, it needs to feedback the execution result to the master device so that the master device can understand the execution situation of the command. The slave device sends the execution result to the master device by constructing a response data packet. The response data packet usually contains some key information, such as the command execution status, error code, data length, etc. The master device can judge whether the command is executed successfully based on this information and take corresponding subsequent operations.
[0087] The format and content of the response data packet constructed by the slave device can be designed according to the communication protocol and command type. A common method is to include a fixed response code field in the response data packet to indicate the status of command execution, such as success, failure, timeout, etc. The response code can be represented using an enumeration type or an integer value, and different values correspond to different statuses. For example, it can be defined that 0 represents success, 1 represents failure, 2 represents timeout, etc. In addition to the response code field, the response data packet can also contain other information, such as the result data of command execution, error information, timestamp, etc. These information can help the host better understand the detailed situation of command execution.
[0088] S206. The slave device discards the command data packet.
[0089] If the judgment result in step S201 is that the packet header is incorrect, the slave device discards the command data packet.
[0090] In this step, if the slave device finds that the packet header of the data packet is incorrect after receiving the command data packet, it means that the data packet is not a legitimate command data packet, which may be caused by data packet loss, duplication, misalignment, etc. In this case, the slave device cannot continue to parse and process the data packet, but needs to directly discard the data packet to prevent invalid data from affecting the normal operation of the system.
[0091] In the above embodiments, an integrity verification mechanism for received data is implemented at the slave device end. Through double verification of packet header check and CRC check, data errors occurring in the communication process can be effectively identified. When data anomalies are found, the slave device directly discards the error data packet to avoid executing incorrect control instructions. This mechanism of performing data verification before executing control operations ensures that only complete and correct control instructions will be executed, effectively preventing misoperation of the lamp beads caused by data errors. At the same time, the slave device constructs a response data packet based on the execution result of the command and returns it to the host, enabling the host to timely understand the execution situation of the command. This mechanism based on data verification and execution result feedback improves the accuracy and reliability of lamp bead control and reduces control anomalies caused by communication errors.
[0092] The system in the embodiments of the present invention application will be described from the perspective of hardware processing. Please refer to Figure 4 , which is a schematic structural diagram of an entity device of an application system provided by an embodiment of the present application.
[0093] It should be noted that Figure 4 The structure of the system shown is only an example and should not bring any limitations to the functions and usage scopes of the embodiments of the present invention.
[0094] Such as Figure 4As shown, the application system includes a Central Processing Unit (CPU) 301, which can perform various appropriate actions and processes according to the program stored in the Read-Only Memory (ROM) 302 or the program loaded from the storage section 308 into the Random Access Memory (RAM) 303, such as executing the method in the above embodiment. In the RAM 303, various programs and data required for system operation are also stored. The CPU 301, ROM 302, and RAM 303 are connected to each other via a bus 304. An Input / Output (I / O) interface 305 is also connected to the bus 304.
[0095] The following components are connected to the I / O interface 305: an input section 306 including a camera, an infrared sensor, etc.; an output section 307 including a Liquid Crystal Display (LCD), a speaker, etc.; a storage section 308 including a hard disk, etc.; and a communication section 309 including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication section 309 performs communication processing via a network such as the Internet. A drive 310 is also connected to the I / O interface 305 as required. A removable medium 311, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 310 as required so that a computer program read from it can be installed into the storage section 308 as required.
[0096] Specifically, according to an embodiment of the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present invention includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network via the communication section 309, and / or installed from the removable medium 311. When the computer program is executed by the Central Processing Unit (CPU) 301, various functions defined in the present invention are executed.
[0097] It should be noted that the computer-readable medium shown in the embodiments of the present invention can be a computer-readable signal medium, a computer-readable storage medium, or any combination of the two. A computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples of a computer-readable storage medium can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium can be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In the present invention, a computer-readable signal medium can include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable computer program. Such a propagated data signal can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above.
[0098] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of the present invention. Among them, each block in the flowchart or block diagram can represent a module, a program segment, or a part of code, and the above module, program segment, or part of code contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than marked in the accompanying drawings. For example, two consecutive blocks shown may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram or flowchart, and the combination of blocks in the block diagram or flowchart, can be implemented by a dedicated hardware-based system that performs the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.
[0099] As another aspect, the present invention also provides a computer-readable storage medium, which may be included in the system described in the above embodiments; or may exist alone without being assembled into the system. The above storage medium carries one or more computer programs, and when the above one or more computer programs are executed by a processor of a system, the system implements the method provided in the above embodiments.
[0100] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, and are not intended to limit them; although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the various embodiments of the present application.
[0101] As used in the above embodiments, depending on the context, the term "when..." can be interpreted to mean "if...", or "after...", or "in response to determining...", or "in response to detecting...". Similarly, depending on the context, the phrase "when determining..." or "if detecting (the stated condition or event)" can be interpreted to mean "if determining...", or "in response to determining...", or "when detecting (the stated condition or event)", or "in response to detecting (the stated condition or event)".
[0102] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present application are generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center by wire (such as coaxial cable, optical fiber, digital subscriber line) or wirelessly (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access, or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (such as a floppy disk, hard disk, magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid-state drive), etc.
[0103] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by hardware instructed by a computer program. This program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the above method embodiments. The foregoing storage medium includes: various media that can store program codes such as ROM or random access memory RAM, magnetic disks, or optical discs.
Claims
1. A serial communication method for quickly controlling lamp beads, applied to an application system, the application system includes a host and a slave, characterized in that: include: The host constructs a command data packet; The host sends the command data packet to the slave through the serial port and starts a response timeout; After receiving the command data packet, the slave sends a response data packet to the host through the serial port; If the response timeout is greater than a preset threshold when the host receives the response data packet, the host resends the command data packet; If the response timeout is not greater than the preset threshold when the host receives the response data packet, the host determines whether the header of the response data packet is correct; If the header of the response data packet is correct, the host calculates and verifies the CRC of the response data packet; If the verification is passed, the host parses the command confirmation code ACK in the response data packet, and determines the execution result of the slave according to the command confirmation code ACK; If the command confirmation code ACK indicates success, the host sends the next command data packet; If the command confirmation code ACK indicates failure, the host resends the command data packet; If the verification fails, the host discards the response data packet; If the header of the response data packet is incorrect, the host discards the response data packet.
2. The method according to claim 1, characterized in that The host constructs a command data packet, which specifically includes: The host receives a color light performance style selected by a user; The host determines a corresponding performance file according to the lantern performance style; The host reads the color palette in the performance file and parses the color quantity and color value of the color palette; The host calculates the number of bits required for encoding each color according to the number of colors; The host obtains a command data packet after encoding according to the number of bits.
3. The method according to claim 1, characterized in that Before sending the response data packet to the host through the serial port, the method further includes: The slave checks whether the header of the command data packet is correct; If the packet header is correct, the slave calculates and verifies the CRC of the command data packet; If the verification is passed, the slave machine parses the command code CMD of the command data packet and performs the corresponding lamp bead control operation; The slave constructs a response data packet according to the execution result of the command code CMD; If the verification fails, the slave discards the command data packet; If the packet header is erroneous, the slave device discards the command data packet.
4. The method according to claim 3, characterized in that The slave machine parses the command code CMD of the command data packet and performs the corresponding lamp bead control operation, specifically including: The slave parses the command data packet, obtains the command code CMD, and determines the CMD value; If the CMD value is 0, the slave parses the palette data corresponding to the palette in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the palette data to the response data packet when the slave constructs the response data packet; If the CMD value is 1, the slave reads the breathing and color-changing information in the command data packet, and updates the lamp bead control strategy according to the breathing and color-changing information; The slave sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value and the lamp bead control strategy to the response data packet when the slave constructs the response data packet; If the CMD value is 2, the slave parses the lamp bead color and duration in the command data packet, sets the ACK value to 0 or 1 according to the setting result, and adds the ACK value, the lamp bead color and the duration to the response data packet when the slave constructs the response data packet.
5. The method according to claim 1, 3 or 4, characterized in that: After the command confirmation code ACK indicates success, the method further includes: When the host determines that the ACK value is equal to 0 according to the command confirmation code ACK, the host confirms the palette data and synchronizes it to the slave; The host reads the next performance file script; When the host determines that the CMD value of the next performance file script is equal to 1, the host generates a command data packet according to the lamp bead control strategy, and executes the step of the host sending the next command data packet; When the host determines that the CMD value of the next performance file script is equal to 2, the host generates a command data packet according to the color and duration of the lamp beads, and executes the step of the host sending the next command data packet; When the host determines that the CMD value of the next performance file script is not 1 or 2, the host executes the step of the host reading the next performance file script.
6. The method according to claim 5, characterized in that After the host generates a command data packet according to the lamp bead control strategy and executes the step of the host sending the next command data packet, the method further includes: The host receives and analyzes the ACK value in the response data packet sent by the slave; The host determines whether the ACK value is equal to 0; If the ACK value is equal to 0, the host reads the next performance file script; If the ACK value is not equal to 0, the host resends the command.
7. The method according to claim 5, characterized in that After the host generates a command data packet according to the color and duration of the lamp beads and executes the step of the host sending the next command data packet, the method further includes: The host sets a timer and reads the timing duration of the timer; When the host determines that the timing duration is greater than the preset duration, the host reads the next performance file script.
8. A serial communication system for quickly controlling lamp beads, characterized in that: The system comprises: One or more processors and a memory; the memory is coupled to the one or more processors, the memory is used to store computer program code, the computer program code includes computer instructions, and the one or more processors call the computer instructions to cause the system to execute the method as described in any one of claims 1-7.
9. A computer-readable storage medium comprising instructions, characterized in that: When the instructions are executed on a system, the system is caused to execute the method according to any one of claims 1 to 7.
10. A computer program product, characterized in that When the computer program product is run on a system, the system is caused to execute the method according to any one of claims 1 to 7.
Citation Information
Cited By
Communication method, device and equipment of detonation plate slave and storage medium
CN120710822A
Communication method, air conditioning unit and communication method thereof
CN120915623A
Transmission method of Esp32 serial port communication system
CN121255680A