Method and device for adaptively selecting communication parameter matching, equipment and storage medium
Through the CRC checksum error count accumulated and combined with the high and low level count collected by the timer, the serial communication parameters adaptation under unknown communication protocols are realized, solving the problem that serial communication cannot proceed normally, and reducing management and maintenance costs .
Patent Information
- Application Number
- CN202411418975.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-11
- Publication Date
- 2025-01-10
AI Technical Summary
The existing serial communication technology cannot adaptively select communication parameters without the communication protocol, resulting in the inability to carry out serial communication normally, increasing the communication management and maintenance costs.
By receiving serial communication data in preset data format, performing CRC verification, and determining the number of errors. If the number of errors exceeds the number of preset consecutive times, the adaptive baud rate processing will be turned on, and the timer is used to collect the high and low level count of serial communication data. Check the table according to the count value to obtain the baud rate, and adjust the serial port parameters according to the baud rate to realize the setting of the adaptive baud rate and check bit.
Under the unknown communication protocol, adaptive matching of serial communication parameters is achieved, ensuring the normal progress of serial communication, reducing communication management and maintenance costs, and improving the accuracy of adaptive baud rate processing.
Smart Images

Figure CN119299053A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of signal processing technology, and in particular to a method, device, equipment and storage medium for adaptively selecting communication parameter matching. Background Art
[0002] At present, the scheme for adaptively adjusting the parameters of the serial port is mainly to adapt the baud rate parameter. Other parameters such as data bits, parity bits, and stop bits cannot be changed. During the communication process, it is also necessary to know the relevant information of the communication protocol between the two communicating parties, which increases the cost of communication management and maintenance. At the same time, the existing scheme mainly realizes the baud rate modification function by sending a fixed feature code or performing special encoding on the data. The scheme using a fixed feature code takes a relatively long communication time and needs to be sent multiple times. Special encoding belongs to the non-return-to-zero encoding method. The non-return-to-zero encoding method has poor interference and it is difficult to distinguish the end of one bit and the beginning of another bit. In addition, this encoding method is more sensitive to slight changes in the signal. If there is interference during the communication process, the interference may cause the minimum pulse width count value to be calculated incorrectly, thereby affecting the selection of the baud rate. At the same time, other parameters of the serial communication of this scheme cannot be modified.
[0003] Therefore, there is an urgent need for a method that can adaptively select communication parameter matching and implement normal serial port communication in the case of an unknown communication protocol. Summary of the invention
[0004] In view of this, the present invention provides a method, device, equipment and storage medium for adaptively selecting communication parameter matching to solve the problem that in the case of an unknown communication protocol, the communication parameter matching cannot be adaptively selected, resulting in the inability to perform serial port communication normally.
[0005] In a first aspect, the present invention provides a method for adaptively selecting communication parameter matching, the method comprising:
[0006] Receive serial communication data in a preset data format;
[0007] Perform CRC check on serial communication data and determine whether the CRC check is correct;
[0008] If the CRC check is correct, the serial communication data will be processed and returned directly;
[0009] If the CRC check is wrong, the number of errors is accumulated, and whether to turn on the adaptive baud rate processing is determined based on the relationship between the accumulated number and the preset number of consecutive times;
[0010] When the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial communication data to change the serial port parameter settings of the serial communication data.
[0011] The method for adaptively selecting communication parameter matching provided by the present invention determines whether to enter adaptive baud rate processing by checking whether serial port communication data is correct and the number of errors accumulated through CRC. When the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial port communication data to change the serial port parameter setting of the serial port communication data, and adaptively selecting communication parameter matching in the case of an unknown communication protocol is achieved, so that the serial port parameters of the serial port communication data can be set, the normal serial port communication is ensured, the management and maintenance costs of the communication are reduced, and the problem that the serial port communication cannot be performed normally due to the inability to adaptively select communication parameter matching in the case of an unknown communication protocol is solved.
[0012] In an optional implementation, receiving serial communication data in a preset data format includes:
[0013] The serial communication data is received bit by bit in the order of the start bit, data bit, check bit and stop bit, and the data bit uses the preset number of bits.
[0014] The method for adaptively selecting communication parameter matching provided by the present invention receives serial port communication data bit by bit in the order of a start bit, a data bit, a check bit and a stop bit, the data bit adopts a preset number of bits, and receives serial port communication data according to a set data format, thereby achieving the unification and standardization of the data format and providing conditions for the subsequent CRC check of the serial port communication data.
[0015] In an optional implementation, performing CRC check on the serial communication data and determining whether the CRC check is correct includes:
[0016] Obtain the CRC check code of the serial communication data at the time of sending, and compare the CRC check code of the serial communication data at the time of sending with the CRC check code of the serial communication data in the preset data format received. If the comparison is consistent, it is determined that the CRC check is correct, otherwise it is determined that the CRC check is wrong.
[0017] The method for adaptively selecting communication parameter matching provided by the present invention can detect whether serial communication data is erroneous during transmission through CRC check, thus providing a prerequisite for whether to perform adaptive baud rate processing.
[0018] In an optional implementation, judging whether to enable adaptive baud rate processing according to the relationship between the accumulated number of times and the preset continuous number of times includes:
[0019] If the accumulated times do not exceed the preset continuous times, the adaptive baud rate processing will not be enabled, the accumulated times will be cleared to zero, the serial communication data will be processed and then returned directly;
[0020] If the accumulated times exceed the preset continuous times, the adaptive baud rate processing is started.
[0021] The method for adaptively selecting communication parameter matching provided by the present invention further determines whether to start adaptive baud rate processing based on the relationship between the cumulative number of errors and the preset number of consecutive errors, thereby improving the accuracy when entering the adaptive baud rate processing and reducing the management and maintenance costs of starting the adaptive baud rate.
[0022] In an optional implementation, when the adaptive baud rate processing is enabled, the adaptive baud rate processing of the serial communication data includes:
[0023] When using a timer to collect serial communication data, the high level and low level of the corresponding pin are counted and the number of high level and low level times is counted to obtain a high level and low level count array;
[0024] Accumulate the count values in the high level and low level count arrays to obtain the count sum, query the preset count and baud rate relationship table based on the count sum, obtain the baud rate corresponding to the serial communication data transmission, and calculate the count value of sending one bit based on the baud rate, that is, the minimum bit width count value;
[0025] Decoding based on the minimum bit width count value to obtain a data timing diagram, and obtaining a stop bit and a check bit according to the data timing diagram;
[0026] Change the serial port parameter settings of the serial communication data using baud rate, stop bit and parity bit.
[0027] The method for adaptively selecting communication parameter matching provided by the present invention adopts a timer to collect the high level and low level of a serial communication data pin, obtains the baud rate of asynchronous serial communication by looking up the table from the high level and low level count values, calculates the count value of sending one bit according to the baud rate, that is, the minimum bit width count value, decodes the minimum bit width count value to obtain a data timing diagram, and finally judges the parameters of the stop bit and the parity bit according to the data format. The adaptive baud rate can be realized without sending a fixed feature code or performing additional encoding on the data. Compared with the prior art, except for abnormal judgment, the serial port parameters of all serial communication data can be adjusted only once to ensure the normal serial communication.
[0028] In an optional implementation, decoding based on the minimum bit width count value to obtain a data timing diagram, and obtaining a stop bit and a check bit according to the data timing diagram includes:
[0029] Decoding the high level and low level count arrays based on the minimum bit width to obtain a data timing diagram, and storing the data timing diagram as a binary array;
[0030] Calculate the total number of bits of serial communication data based on the count sum and baud rate;
[0031] Determine whether the binary array contains a check bit based on the total number of bits and the preset data format, and if so, determine whether the check bit is an odd check bit or an even check bit based on the number of data bits;
[0032] The stop bit is derived based on the total number of bits, the preset data format, and odd or even parity.
[0033] The method for adaptively selecting communication parameter matching provided by the present invention realizes the adaptation of data parity check bit and stop bit parameters through a binary array, reduces the cost of communication management and maintenance, and provides conditions for changing the serial port parameter setting of serial port communication data by using baud rate, stop bit and check bit.
[0034] In an optional implementation, before changing the serial port parameter settings of the serial port communication data using the baud rate, stop bit, and check bit, the method for adaptively selecting communication parameter matching further includes:
[0035] Convert the binary array into a hexadecimal array and perform CRC check on the hexadecimal array. When the CRC check is correct, use the baud rate, stop bit and check bit to change the serial port parameter settings of the serial communication data; when the CRC check is wrong, redetermine the baud rate corresponding to the serial communication data when sending.
[0036] The method for adaptively selecting communication parameter matching provided by the present invention converts a binary array into a hexadecimal array; performs CRC check on the hexadecimal array, and when the CRC check is correct, uses the baud rate, stop bit and check bit to change the serial port parameter setting of the serial port communication data; when the CRC check is wrong, re-determines the baud rate corresponding to the serial port communication data when it is sent, thereby ensuring the accuracy of the serial port communication data after the serial port parameter setting is changed, and providing conditions for the normal serial port communication.
[0037] In a second aspect, the present invention provides a device for adaptively selecting communication parameter matching, the device comprising:
[0038] The lower computer data receiving module is used to receive serial communication data in a preset data format;
[0039] CRC check module, used to perform CRC check on serial communication data and determine whether the CRC check is correct;
[0040] The communication processing data module is used to process the serial communication data and return it directly if the CRC check is correct;
[0041] The error accumulation module is used to accumulate the number of errors if the CRC check is wrong, and determine whether to turn on the adaptive baud rate processing according to the relationship between the accumulated number of errors and the preset number of consecutive errors;
[0042] The adaptive baud rate module is used to perform adaptive baud rate processing on serial communication data when the adaptive baud rate processing is turned on, so as to change the serial port parameter settings of the serial communication data.
[0043] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the method for adaptively selecting communication parameter matching of the above-mentioned first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0044] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the method for adaptively selecting communication parameter matching according to the first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0046] Figure 1 is a flow chart of a method for adaptively selecting communication parameter matching according to an embodiment of the present invention;
[0047] Figure 2 is a flow chart of another method for adaptively selecting communication parameter matching according to an embodiment of the present invention;
[0048] Figure 3 is a flow chart of another method for adaptively selecting communication parameter matching according to an embodiment of the present invention;
[0049] Figure 4 is a flow chart of another method for adaptively selecting communication parameter matching according to an embodiment of the present invention;
[0050] Figure 5 is a schematic diagram of connecting functional modules of a serial communication system for implementing adaptive selection of communication parameter matching according to an embodiment of the present invention;
[0051] Figure 6 is a schematic diagram of serial communication data in a preset data format according to an embodiment of the present invention;
[0052] Figure 7 is a structural block diagram of a device for adaptively selecting communication parameter matching according to an embodiment of the present invention;
[0053] Figure 8 is a schematic diagram of the connection of internal functional modules of the adaptive baud rate module according to an embodiment of the present invention;
[0054] Fig. 9 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0055] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0056] According to an embodiment of the present invention, a method embodiment for adaptively selecting communication parameter matching is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0057] In this embodiment, a method for adaptively selecting communication parameter matching is provided, which can be used in a serial communication system including a host computer and a slave computer, such as Figure 5 As shown, the serial communication system includes a host computer and a slave computer, wherein the slave computer includes a slave computer receiving data module, a CRC check module, a communication processing data module, an error accumulation module and an adaptive baud rate module. Figure 1 is a flow chart of a method for adaptively selecting communication parameter matching according to an embodiment of the present invention. Figure 1 As shown, the process includes the following steps:
[0058] Step S101, receiving serial communication data in a preset data format.
[0059] Specifically, the preset data format can be set to hexadecimal format, such as Figure 5 As shown, the host computer includes a host computer data sending module and a host computer data receiving module. The host computer data sending module sends data to the lower computer data receiving module in a preset data format through a serial communication step, and the lower computer data receiving module receives the serial communication data data in the preset data format. If the lower computer receives the serial communication data data, step S102 is performed. If the serial communication data data is not received, wait until the serial communication data is received.
[0060] Step S102, performing CRC check on the serial communication data, and determining whether the CRC check is correct.
[0061] Specifically, CRC (Cyclic Redundancy Check), also known as cyclic redundancy check, is a technology used to detect errors in data transmission or storage. CRC check generates a short fixed-bit check code through a certain mathematical operation, which is attached to the original serial communication data and sent together. After the lower computer receiving data module receives the data, it uses the same check code to verify the original serial communication data and the check code to determine whether the data is wrong during transmission. Figure 5 As shown, the lower computer receiving data module transmits the serial communication data to the CRC check module for CRC check. When the check code of the lower computer receiving data module is consistent with the check code of the original serial communication data, the CRC check is determined to be correct, otherwise the CRC check is wrong.
[0062] Step S103, if the CRC check is correct, the serial communication data is processed and then directly returned.
[0063] Specifically, Figure 5 As shown, the CRC check module is also connected to the communication processing data module, and the communication processing data module is connected to the host computer receiving data module. If the CRC check is correct, the serial communication data is transmitted to the communication processing data module for communication processing and then directly returned to the host computer receiving data module.
[0064] Step S104: If the CRC check is wrong, the number of errors is accumulated, and whether to start the adaptive baud rate processing is determined according to the relationship between the accumulated number of errors and the preset number of consecutive errors.
[0065] Specifically, Figure 5 As shown, the CRC check module is also connected to the error accumulation module, and the error accumulation module is connected to the adaptive baud rate module. If the CRC check is wrong, the error accumulation module accumulates the number of errors, and determines whether to turn on the adaptive baud rate module according to the relationship between the accumulated number of errors and the preset number of consecutive times, and has performed adaptive baud rate processing.
[0066] Step S105, when the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial communication data to change the serial port parameter settings of the serial communication data.
[0067] Specifically, when the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial communication data through the adaptive baud rate module to change the serial port parameter settings of the serial communication data to ensure that the serial communication between the upper computer and the lower computer proceeds normally.
[0068] The method for adaptively selecting communication parameter matching provided in the present embodiment determines whether to enter adaptive baud rate processing by checking whether serial port communication data is correct and the accumulated number of errors through CRC. When adaptive baud rate processing is turned on, adaptive baud rate processing is performed on the serial port communication data to change the serial port parameter settings of the serial port communication data, thereby adaptively selecting communication parameter matching in the case of an unknown communication protocol, so that the serial port parameters of the serial port communication data can be set, ensuring the normal progress of serial port communication, reducing the management and maintenance costs of communication, and solving the problem that in the case of an unknown communication protocol, the communication parameter matching cannot be adaptively selected, resulting in the inability to perform serial port communication normally.
[0069] In this embodiment, a method for adaptively selecting communication parameter matching is provided, which can be used in the above-mentioned serial communication system. Figure 2 is a flow chart of a method for adaptively selecting communication parameter matching according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:
[0070] Step S201, receiving serial communication data in a preset data format.
[0071] Specifically, the above step S201 includes:
[0072] a. Receive serial communication data bit by bit in the order of start bit, data bit, check bit and stop bit, and the data bit uses the preset number of bits.
[0073] Specifically, the preset number of bits is set to 8 bits. The host computer data sending module sends the serial communication data data in hexadecimal format, and each serial communication data is sent bit by bit in the order of the start bit, data bit, check bit and stop bit, and the data bit uses 8 data bits. Figure 6 As shown in the figure, the start bit, data bit, check bit and stop bit are described as follows:
[0074] Start bit: Before the data frame is sent, the bus is in an idle state, that is, it maintains a high level. When the host computer data sending module is ready to send data, it sends the start bit, thereby changing the bus from an idle state to an active state, and the level changes from a high level to a low level.
[0075] Data bit: The data bit contains the actual information to be transmitted, usually 8 bits. Figure 6 In it, LSB (least significant bit, LSB) is the least significant bit, and MSB (most significant bit, MSB) is the most significant bit.
[0076] Check bit: The check bit is located between the data bit and the stop bit. It can be divided into no check, odd check, and even check. If there is no check bit, it is directly the stop bit. If there is a check bit, it is checked by calculating the number of "1"s in the binary of the data bit. When the number of "1"s in the data is odd, if it is odd check, "0" is added after the data bit, and if it is even check, "1" is added after the data bit.
[0077] Stop bit: It is used to mark the end of the data frame in serial communication. It is divided into 1 stop bit, 1.5 stop bits, and 2 stop bits. Generally, the default is 1 stop bit.
[0078] Step S202, performing CRC check on the serial communication data, and determining whether the CRC check is correct.
[0079] Specifically, the above step S202 includes:
[0080] b. Obtain the CRC check code of the serial communication data at the time of sending, and compare the CRC check code of the serial communication data at the time of sending with the CRC check code of the received serial communication data in the preset data format. If the comparison is consistent, the CRC check is determined to be correct, otherwise the CRC check is determined to be wrong.
[0081] Step S203: If the CRC check is correct, the serial communication data is processed and then returned directly. Figure 1 Step S103 of the illustrated embodiment will not be described in detail here.
[0082] Step S204: If the CRC check is wrong, the number of errors is accumulated, and whether to start the adaptive baud rate processing is determined according to the relationship between the accumulated number and the preset continuous number.
[0083] Specifically, the preset number of consecutive times in this embodiment is set according to actual conditions and is not specifically limited here. The preset number of consecutive times in this embodiment is set to three times, and the above step S204 includes:
[0084] Step S2041, if the accumulated times do not exceed the preset continuous times, the adaptive baud rate processing is not started, the accumulated times are cleared to zero, the serial port communication data is processed, and then the process returns directly.
[0085] Specifically, if the CRC check of the received serial communication data data is wrong, the error accumulation module is used to accumulate the number of errors to obtain the accumulated number N. If the accumulated number N is not three consecutive times, the adaptive baud rate processing is not enabled. After the accumulated number N is cleared, the serial communication data is sent to the communication processing data module for communication processing to obtain the data txd_data, and the data txd_data is directly returned to the host computer receiving data module. If the host computer receiving data module receives the data txd_data, the serial communication ends.
[0086] Step S2042: if the accumulated times exceed the preset consecutive times, the adaptive baud rate processing is started.
[0087] Specifically, if the cumulative number N exceeds three consecutive times, the adaptive baud rate module is enabled to perform adaptive baud rate processing.
[0088] Step S205, when the adaptive baud rate processing is turned on, the serial communication data is adaptively processed at the baud rate to change the serial port parameter settings of the serial communication data. Figure 1 Step S105 of the illustrated embodiment will not be described in detail here.
[0089] The method for adaptively selecting communication parameter matching provided by the present embodiment receives serial communication data bit by bit in the order of the start bit, data bit, check bit and stop bit, and the data bit adopts a preset number of bits. The serial communication data is received according to the set data format to achieve the unification and standardization of the data format, and provide conditions for the subsequent CRC check of the serial communication data. Through the CRC check, it is possible to detect whether the serial communication data is wrong during the transmission process, which provides a prerequisite for whether to perform adaptive baud rate processing. Further, it is determined whether to start the adaptive baud rate processing based on the size relationship between the cumulative number of errors and the preset number of consecutive times, which improves the accuracy when entering the adaptive baud rate processing and reduces the management and maintenance costs of starting the adaptive baud rate.
[0090] In this embodiment, a method for adaptively selecting communication parameter matching is provided, which can be used in the above-mentioned serial communication system. Figure 3 is a flow chart of a method for adaptively selecting communication parameter matching according to an embodiment of the present invention. Figure 3 As shown, the process includes the following steps:
[0091] Step S301, receiving serial communication data in a preset data format. Figure 2 Step S201 of the illustrated embodiment will not be described in detail here.
[0092] Step S302, perform CRC check on the serial communication data and determine whether the CRC check is correct. Figure 2 Step S202 of the illustrated embodiment will not be described in detail here.
[0093] Step S303: If the CRC check is correct, the serial communication data is processed and then returned directly. Figure 2 Step S203 of the illustrated embodiment will not be described in detail here.
[0094] Step S304: If the CRC check is wrong, the number of errors is accumulated, and whether to start the adaptive baud rate processing is determined according to the relationship between the accumulated number and the preset number of consecutive times. Figure 2 Step S204 of the illustrated embodiment will not be described in detail here.
[0095] Step S305, when the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial communication data to change the serial port parameter settings of the serial communication data.
[0096] Specifically, Figure 4 Shown and Figure 8 As shown, the above step S305 includes:
[0097] Step S3051, using a timer to collect the high level and low level of the corresponding pin when serial communication data is collected and counting the number of high level and low level times to obtain a high level and low level count array.
[0098] Specifically, if the adaptive baud rate module is turned on, when the lower computer receives the low level of the serial communication data data pin, the timer is turned on to count the high and low levels received by the serial communication data data pin. When the count values are all low levels for many times (to prevent counting errors caused by interference), the high and low level count values are recorded. When the timer receives the high level for a long time, the timer count is stopped, thereby obtaining the high and low level count array Data. That is, the high and low level count array Data[0] is low level, Data[1] is high level, Data[2] is low level, and so on.
[0099] Step S3052, accumulate the count values in the high level and low level count arrays to obtain the count sum, query the preset count and baud rate relationship table based on the count sum, obtain the baud rate corresponding to the serial communication data transmission, and calculate the count value of sending one bit based on the baud rate, that is, the minimum bit width count value.
[0100] Specifically, the control unit MCU (Microcontroller Unit, MCU )) Accumulate the elements in the high level and low level count array Data to obtain the count and SUM, query the count and baud rate relationship table based on the count and SUM to obtain the baud rate BAUD corresponding to the host computer sending serial communication data, and calculate the minimum bit width count value BIT in combination with the sampling clock frequency f. For example, the corresponding baud rate BAUD = 1200, the sampling clock frequency f = 28800Hz, then the minimum bit width count value BIT = sampling clock frequency f / baud rate BAUD = 28800 / 1200 = 24.
[0101] It should be noted that, since the accumulated counts of some baud rates are similar, a low baud rate is selected by default to proceed to the next step.
[0102] Step S3053, decoding based on the minimum bit width count value to obtain a data timing diagram, and obtaining a stop bit and a check bit according to the data timing diagram.
[0103] In some optional implementations, the above step S3053 includes:
[0104] Step a1, decoding the high level and low level count arrays based on the minimum bit width count value to obtain a data timing diagram, and storing the data timing diagram as a binary array.
[0105] Specifically, since the serial communication data data is sent bit by bit, when the count value of one bit is known, the number of bits contained in each data in the array high level and low level count array Data can be calculated, thereby obtaining the timing diagram of the high level and low level count array data, and saving the data timing diagram in the binary array Data1.
[0106] Step a2, calculating the total number of bits of the serial communication data based on the count sum and the baud rate.
[0107] Specifically, the total number of bits LEN for sending a data is calculated based on the count and SUM and the baud rate BAUD. Since the minimum bit width count value BIT = sampling clock frequency f / baud rate BAUD, the total number of bits LEN for sending a data is obtained based on the count and SUM and BIT divided by 8. For example, SUM = 1901, the minimum bit width count value BIT = 24, then LEN = SUM / BIT / 8 = 1901 / 24 / 8 = 10 bits.
[0108] Step a3, determining whether the binary array includes a check bit based on the total number of bits and the preset data format, and if the check bit is included, determining whether the check bit is an odd check bit or an even check bit according to the number of data bits.
[0109] Specifically, according to the data format of the serial communication data, the parity bit is after the data bit. The binary array Data1 is traversed to determine whether it contains a parity bit. If there is a parity bit, it is determined whether it is an odd parity or an even parity based on the number of "1"s in the binary array data bits. When the number of "1"s in the binary array data bits is an odd number, it is an odd parity, and when the number of "1"s in the binary array data bits is an even number, it is an even parity. If it is an odd parity, "0" is added after the data bit, and if it is an even parity, "1" is added after the data bit.
[0110] Step a4, obtaining a stop bit based on the total number of bits, the preset data format, and the odd parity bit or the even parity bit.
[0111] Specifically, after the check bit is determined, the number of stop bits is determined. The number of stop bits = the total number of serial communication data bits - data bits - parity bits - start bits.
[0112] For example, in the decoded data timing diagram, that is, in the binary array, if there are multiple high-level pulse widths that are X.5 times the minimum bit width count value BIT (X is any value from 0 to 10), the stop bit is judged to be 1.5 stop bits.
[0113] The method for adaptively selecting communication parameter matching provided in this embodiment realizes the adaptation of data parity check bit and stop bit parameters through a binary array, reduces the cost of communication management and maintenance, and provides conditions for changing the serial port parameter setting of serial port communication data by using baud rate, stop bit and check bit.
[0114] Step S3054, converting the binary array into a hexadecimal array; performing CRC check on the hexadecimal array, and when the CRC check is correct, changing the serial port parameter settings of the serial port communication data using the baud rate, stop bit and check bit; when the CRC check is wrong, re-determining the baud rate corresponding to the serial port communication data when sending.
[0115] Specifically, the binary array Data1 is converted into the hexadecimal array Data2, and the hexadecimal array Data2 is subjected to CRC check. During the CRC check, the CRC check code in the hexadecimal array Data2 is also compared with the CRC check code of the serial communication data at the time of sending. If the comparison is consistent, the CRC check is determined to be correct. If correct, the serial port parameter settings are changed using the obtained baud rate, stop bit, and check bit. If the CRC check is incorrect, the baud rate BAUD is reselected.
[0116] This embodiment converts a binary array into a hexadecimal array; performs a CRC check on the hexadecimal array, and when the CRC check is correct, uses the baud rate, stop bit and check bit to change the serial port parameter settings of the serial port communication data; when the CRC check is wrong, redetermines the baud rate corresponding to the serial port communication data when it is sent, thereby ensuring the accuracy of the serial port communication data after the serial port parameter settings are changed, and providing conditions for the normal serial port communication.
[0117] Step S3055, using the baud rate, stop bit and check bit to change the serial port parameter settings of the serial port communication data. Specifically, the original serial port communication data is initialized using the acquired baud rate, stop bit and check bit, the serial port parameter settings of the serial port communication data are changed, the changed serial port communication data is sent to the communication processing data module for communication processing to obtain data txd_data, and the data txd_data is directly returned to the host computer receiving data module. If the host computer receiving data module receives the data txd_data, the serial port communication ends. The normal serial port communication between the host computer and the slave computer is realized.
[0118] The method for adaptively selecting communication parameter matching provided in the present embodiment uses a timer to collect the high level and low level of the serial communication data pin, obtains the baud rate of the asynchronous serial communication by looking up the table based on the high level and low level count values, and calculates the count value of sending one bit, that is, the minimum bit width count value, according to the baud rate, and decodes the minimum bit width count value to obtain the data timing diagram, and finally determines the parameters of the stop bit and the parity bit according to the data format. The adaptive baud rate can be achieved without sending a fixed feature code or performing additional encoding on the data. Compared with the prior art, except for abnormal judgment, the serial port parameters of all serial communication data can be adjusted only once to ensure normal serial communication.
[0119] As one or more specific application embodiments of the present invention, Figure 4 The method for adaptively selecting communication parameter matching provided by the present invention is further described in detail as follows:
[0120] Example 1: Sampling clock frequency f=28800 Hz, initial baud rate of the lower computer / chip serial port is 9600, 8 data bits, 1 stop bit, odd parity. The upper computer parameters are unknown.
[0121] The host computer parameters are unknown, such as Figure 4As shown, the upper computer data sending module starts to send serial communication data. The serial communication data number is, for example, 81 04 9C 41 00 01 50 4E. The lower computer / chip receives the serial communication data and the data is received normally. It enters the CRC check module. The CRC check code is (50, 4E), which is equal to the last two digits of the sent serial communication data. If the CRC check is correct, it enters the communication processing data module. The communication processing data module returns the received serial communication data directly to the upper computer receiving data module. The upper computer receiving data module receives 81 04 9C 41 00 01 50 4E.
[0122] If the CRC check code is not equal to (50, 4E), the error accumulation module is entered to accumulate the number of check errors. When the check is correct after resending the instruction, the number of check errors is cleared to 0. If the CRC check code is incorrect after sending three consecutive times, the adaptive baud rate module is turned on and the timer is turned on at the same time to collect the high and low levels of the data. When the count values are all low for many times, the data is recorded. When the timer receives a high level for a long time, it stops timing, and the high and low level count arrays Data obtained are: [14 23 14323 22 23 71 23 119 47 71 71 46 23 23 23 23 23 119 2347 23 238 23 23 23 167 47118 23 23 23 47 23 47 71 47 23 46 51]. Since the timer starts recording data at a low level, Data[0] is the data recorded at a low level, Data[1] is the data recorded at a high level, Data[2] is the data recorded at a low level, Data[3] is the data recorded at a high level, and so on. The timer also filters the high and low level count arrays to improve the anti-interference performance of the high and low level count arrays Data.
[0123] Then, the elements of the high-level and low-level count array Data are accumulated to obtain a count sum SUM of 2086. According to the relationship table between the count value and the baud rate (Table 1), it can be known that its value is in the range of 1840-2304, and its corresponding baud rate BAUD=1200. According to the sampling clock frequency f and the baud rate BAUD, the minimum bit width count value is calculated as BIT=24, and the minimum bit width count value is used to decode the high-level and low-level count array Data to obtain the binary array Data1 (less than one bit width is processed as one bit width), such as Data[0] is 1 low level, Data[1] is 1 high level, Data[2] is 6 low levels, Data[3] is 1 high level, etc. Then the array Data1 is [0 1 0 0 0 0 0 0 1 0 1……]. According to the count sum SUM and the minimum bit width count value BIT divided by 8, a data LEN=11 bits are sent, and the sorted array Data1 is shown in Table 2 below.
[0124] Table 1 Relationship between count value and baud rate
[0125]
[0126] Table 2 Binary array Data1 after sorting
[0127]
[0128] After getting the binary array Data1, start calculating the stop bit and check bit in the data. Since the parity bit is located after the data bit, if there is no check bit, the bit after the data bit will remain unchanged and be high level. By observing the sorted Table 2, the value after the data bit changes, then the serial communication data contains a check bit, and then determine whether it is odd or even by judging the number of "1" in the data bit in each serial communication data. As shown in Table 2, it can be directly judged that the check bit in the sent data belongs to even check. After the parity bit is judged, through stop bit = total number of serial communication data bits - data bits - parity bit - start bit, it can be known that the stop bit is 1 bit.
[0129] Since the binary array has been decomposed, the acquired data bits are converted from binary to hexadecimal. The hexadecimal array is:
[0130] [0x81, 0x04, 0x9C, 0x41, 0x00, 0x01, 0x50, 0x4e], perform CRC check on it, if its CRC check result is equal to the last two data of conversion, then it represents that CRC check is correct. Through calculation, it can be known that its result is 0x50 and 0x4e. If the check is correct, modify the corresponding serial port parameter settings, i.e. baud rate 1200, data bit 8, stop bit 1, even check. And the received data is directly returned to the host computer receiving data module after being processed by the communication processing data module, so as to realize the normal communication between the host computer and the slave computer.
[0131] Example 2: Sampling clock frequency f=28800 Hz, initial baud rate of the lower computer / chip serial port is 9600, 8 data bits, 1 stop bit, odd parity. The upper computer parameters are unknown.
[0132] When there is no return after sending three times, the adaptive baud rate module is entered and the high and low level count array Data obtained is: [14 23 143 47 71 23 119 23 71 71 46 47 23 23 119 23 23 23 215 2323 23167 23 118 23 23 23 23 23 47 23 47 71 23 51].
[0133] like Figure 8 As shown, the accumulation and table lookup unit is entered to accumulate the elements of the high-level and low-level count array Data to obtain SUM of 1901. According to the relationship table between the count value and the baud rate (Table 1), it can be known that its value is in the range of 1840-2304, and its corresponding baud rate BAUD=1200. The minimum bit width is BIT=24. The minimum bit width is used to decode Data to obtain the binary array Data1 (less than one bit width is processed as one bit width), such as Data[0] is 1 low level, Data[1] is 1 high level, Data[2] is 6 low levels, Data[3] is 2 high levels, etc. Then the binary array Data1 is [0 1 00 0 0 0 0 11 0……]. According to SUM and BIT divided by 8, a data LEN=10 bits are sent, and the sorted binary array Data1 is shown in Table 3.
[0134] Table 3 Binary array Data1 after sorting
[0135]
[0136] The calculation stop bit and check bit unit is used to judge the check bit and stop bit. Since the parity bit is located after the data bit, if there is no check bit, the bit after the data bit will remain unchanged and be high level. By observing the sorted Table 3, the value after the data bit has not changed, so there is no check bit. At the same time, the stop bit is 1 bit. Perform a binary to hexadecimal conversion operation on the acquired data bits. The hexadecimal array Data2 is obtained as [0x81, 0x04, 0x9C, 0x41, 0x00, 0x01, 0x50, 0x4e]. CRC check is performed on it. Through calculation, it can be known that the check is correct. If the check is correct, the corresponding serial port settings are modified by the modification parameter unit, that is, the baud rate is 1200, the data bit is 8, the stop bit is 1, and there is no check. And the received data is directly returned to the upper computer receiving data module after being processed by the communication processing data module to realize normal communication between the upper computer and the lower computer.
[0137] The method for adaptively selecting communication parameter matching provided in this embodiment can realize adaptive baud rate without sending fixed feature codes or additionally encoding data. It realizes the adaptation of data parity bit and stop bit parameters, and reduces the cost of communication management and maintenance. Compared with other solutions, except for abnormal judgment, all parameters can be adjusted only once.
[0138] In the present embodiment, a device for adaptively selecting communication parameter matching is also provided, and the device is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware is also possible and conceived.
[0139] This embodiment provides a device for adaptively selecting communication parameter matching, such as Figure 7 As shown, including:
[0140] The lower computer data receiving module 701 is used to receive serial communication data in a preset data format.
[0141] The CRC check module 702 is used to perform CRC check on the serial communication data and determine whether the CRC check is correct.
[0142] The communication processing data module 703 is used to perform communication processing on the serial communication data and return it directly if the CRC check is correct.
[0143] The error accumulation module 704 is used to accumulate the number of errors if the CRC check is wrong, and determine whether to start the adaptive baud rate processing according to the relationship between the accumulated number of errors and the preset number of consecutive errors.
[0144] The adaptive baud rate module 705 is used to perform adaptive baud rate processing on the serial port communication data when the adaptive baud rate processing is turned on, so as to change the serial port parameter settings of the serial port communication data.
[0145] In some optional implementations, the lower computer data receiving module 701 includes:
[0146] The data receiving unit is used to receive serial communication data bit by bit in the order of start bit, data bit, check bit and stop bit, and the data bit adopts a preset bit number.
[0147] In some optional implementations, the CRC check module 702 includes:
[0148] The CRC check unit is used to obtain the CRC check code of the serial communication data at the time of sending, and compare the CRC check code of the serial communication data at the time of sending with the CRC check code of the serial communication data in the preset data format received. If the comparison is consistent, it is determined that the CRC check is correct, otherwise it is determined that the CRC check is wrong.
[0149] In some optional implementations, the error accumulation module 704 includes:
[0150] The first error accumulation unit is used for not starting the adaptive baud rate processing if the accumulated times do not exceed the preset continuous times, clearing the accumulated times to zero, performing communication processing on the serial port communication data and then returning directly.
[0151] The second error accumulation unit is used to start the adaptive baud rate processing if the accumulated times exceed the preset consecutive times.
[0152] In some optional embodiments, such as Figure 8 As shown, the adaptive baud rate module 705 includes:
[0153] The timer collects data and filters the unit, which uses the timer to collect the high level and low level of the corresponding pin when serial communication data is collected and counts the number of high level and low level times to obtain a high level and low level count array.
[0154] The accumulation and table lookup and calculation unit is used to accumulate the count values in the high-level and low-level count arrays to obtain the count sum, query the preset count and baud rate relationship table based on the count sum, obtain the baud rate corresponding to the serial communication data when sending, and calculate the count value of sending one bit based on the baud rate, that is, the minimum bit width count value.
[0155] The decoding, stop bit and check bit calculation unit is used to obtain a data timing diagram by decoding based on the minimum bit width count value, and obtain a stop bit and a check bit according to the data timing diagram.
[0156] The parameter modification unit is used to change the serial port parameter settings of the serial port communication data using the baud rate, stop bit and parity bit.
[0157] In some optional embodiments, such as Figure 8 As shown, the decoding, stop bit and check bit calculation unit includes:
[0158] The decoding subunit is used to decode the high level and low level count arrays based on the minimum bit width to obtain a data timing diagram, and store the data timing diagram as a binary array.
[0159] The total number of bits subunit is used to calculate the total number of bits of serial communication data based on the count sum and the baud rate.
[0160] The check bit calculation subunit is used to determine whether the binary array contains a check bit based on the total number of bits and the preset data format. If the check bit is contained, it is determined whether the check bit is an odd check bit or an even check bit according to the number of data bits.
[0161] The stop bit calculation subunit is used to obtain the stop bit based on the total number of bits, the preset data format, and the odd parity bit or the even parity bit.
[0162] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0163] The device for adaptively selecting communication parameter matching in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.
[0164] The embodiment of the present invention also provides a computer device having the above Figure 7 and Figure 8 The adaptive selection shown matches the communication parameters of the device.
[0165] See also Fig. 9 , Fig. 9 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Fig. 9As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Fig. 9 A processor 10 is taken as an example.
[0166] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0167] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.
[0168] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0169] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0170] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Fig. 9 The example of connecting through bus is taken in the following.
[0171] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device can be a touch screen.
[0172] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0173] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A method for adaptively selecting communication parameter matching, characterized in that: The method comprises: Receive serial communication data in a preset data format; Performing CRC check on the serial communication data, and determining whether the CRC check is correct; If the CRC check is correct, the serial communication data is directly returned after communication processing; If the CRC check is wrong, the number of errors is accumulated, and whether to turn on the adaptive baud rate processing is determined according to the relationship between the accumulated number of errors and the preset number of consecutive errors; When the adaptive baud rate processing is turned on, the adaptive baud rate processing is performed on the serial port communication data to change the serial port parameter settings of the serial port communication data.
2. The method according to claim 1, characterized in that The receiving of serial communication data in a preset data format comprises: The serial communication data is received bit by bit in the order of a start bit, a data bit, a check bit and a stop bit, and the data bit adopts a preset number of bits.
3. The method according to claim 1, characterized in that Performing CRC check on the serial communication data and determining whether the CRC check is correct includes: Obtain the CRC check code of the serial communication data at the time of sending, and compare the CRC check code of the serial communication data at the time of sending with the CRC check code of the serial communication data in the preset data format received. If the comparison is consistent, it is determined that the CRC check is correct, otherwise it is determined that the CRC check is wrong.
4. The method according to claim 1, characterized in that: The determining whether to start the adaptive baud rate processing according to the relationship between the accumulated number of times and the preset continuous number of times includes: If the accumulated times do not exceed the preset continuous times, the adaptive baud rate processing is not started, the accumulated times are cleared to zero, the serial communication data is processed and then directly returned; If the accumulated times exceed the preset continuous times, the adaptive baud rate processing is started.
5. The method according to claim 1, characterized in that When the adaptive baud rate processing is turned on, the adaptive baud rate processing of the serial communication data includes: When using a timer to collect serial communication data, the high level and low level of the corresponding pin are counted and the number of high level and low level times is counted to obtain a high level and low level count array; Accumulate the count values in the high level and low level count arrays to obtain a count sum, query a preset count and baud rate relationship table based on the count sum, obtain the baud rate corresponding to the serial communication data transmission, and calculate the count value of sending one bit based on the baud rate, that is, the minimum bit width count value; Decoding the minimum bit width count value to obtain a data timing diagram, and obtaining a stop bit and a check bit according to the data timing diagram; The baud rate, stop bit and check bit are used to change the serial port parameter settings of the serial port communication data.
6. The method according to claim 5, characterized in that Decoding based on the minimum bit width count value to obtain a data timing diagram, and obtaining a stop bit and a check bit according to the data timing diagram includes: Decoding the high level and low level count arrays based on the minimum bit width to obtain a data timing diagram, and storing the data timing diagram as a binary array; Calculate the total number of bits of serial communication data based on the count sum and baud rate; Determine whether the binary array includes a check bit based on the total number of bits and the preset data format, and if the check bit is included, determine whether the check bit is an odd check bit or an even check bit according to the number of data bits; A stop bit is obtained based on the total number of bits, the preset data format, and an odd parity bit or an even parity bit.
7. The method according to claim 6, characterized in that Before changing the serial port parameter settings of the serial port communication data using the baud rate, stop bit and check bit, the method further includes: The binary array is converted into a hexadecimal array, and a CRC check is performed on the hexadecimal array. When the CRC check is correct, the serial port parameter setting of the serial port communication data is changed using the baud rate, stop bit and check bit; when the CRC check is wrong, the baud rate corresponding to the serial port communication data is re-determined.
8. A device for adaptively selecting communication parameter matching, characterized in that: The device comprises: The lower computer data receiving module is used to receive serial communication data in a preset data format; A CRC check module is used to perform a CRC check on the serial communication data and determine whether the CRC check is correct; The communication processing data module is used to directly return the serial communication data after communication processing if the CRC check is correct; An error accumulation module is used to accumulate the number of errors if the CRC check is wrong, and determine whether to turn on the adaptive baud rate processing according to the relationship between the accumulated number of errors and the preset number of consecutive errors; The adaptive baud rate module is used to perform adaptive baud rate processing on the serial port communication data when the adaptive baud rate processing is turned on, so as to change the serial port parameter settings of the serial port communication data.
9. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the method for adaptively selecting communication parameter matching according to any one of claims 1 to 7 by executing the computer instructions.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the method for adaptively selecting communication parameter matching according to any one of claims 1 to 7.
Citation Information
Cited By
Baud rate self-adaption method and tool of lighting equipment and medium
CN119696734A
Flow meter communication method and device, computer equipment and storage medium
CN119854390A
Bootloader baud rate detection method and system
CN121478363A