Reason for writing: Are you idle recently? None! A bug occurred in the project code co-developed by a colleague yesterday. His PC sends me a command every MS to read some data from my PC, in this process, you will press the button to perform other operations and return data to the user. (Communication passes)
1. He regularly sends back commands to me Ms. I should return the strings to him:
@ Vumultiget (out_1,-INF dbfs, normal, out_2,-INF dbfs, normal, out_3,-INF dbfs, normal, out_4,-INF dbfs, normal, out_5, -INF dbfs, normal, out_6,-INF dbfs, normal, out_7,-47.6 dbfs, normal, out_8,-INF dbfs, normal)
2. Randomly press the button. In addition to other operations, I should return the string to him: @ load (1)
When a message is returned to him at regular intervals, the following error occurs when a random key is pressed:
@ Vumultiget (out_1,-INF dbfs, normal, out_2,-INF dbfs, normal, out_3,-INF dbfs, normal, out_4,-INF dbfs, normal, out_5, -INF dbfs, normal, out_6,-INF dbfs, normal, out_7,-47.6 dbfs, normal, out_8,-INF dbfs, normal) @ load (1) ultiget (out_1,
-INF dbfs, normal, out_2,-INF dbfs, normal, out_3,-INF dbfs, normal, out_4,-INF dbfs, normal, out_5,-INF dbfs, normal, out_6, -INF dbfs, normal, out_7,-47.6 dbfs, normal, out_8,-INF dbfs, normal) @ vumultiget (out_1,-INF dbfs, normal, out_2,-INF dbfs,
Normal, out_3,-INF dbfs, normal, out_4,-INF dbfs, normal, out_5,-INF dbfs, normal, out_6,-INF dbfs, normal, out_7,-47.6 dbfs, normal, out_8,-INF dbfs, normal )@
Have you understood the red part above? See the error? The sending information in the Code is as follows:
The serial sending code is as follows: // write formatted data to a string buffer Len = sprintf (BUF, "@ vumultiget (% S % d, %. 1f dbfs, % s) ", am_chnstrtable [I + layout_input_size], I + 1, VU. level, am_vuclipstrtable [vu. clip]); usart_send (uint8 *) BUF, Len); press the button to return information. The serial port sending code is as follows: usart_send (uint8 *) "@ load (0 )", sizeof ("@ load (0 )"));
The cause of the Bug found during final debugging is: the string sent by pressing the key does not exist.TerminatorCause error;
The reason why the serial communication fails to send and receive messages is summarized as follows:
Unable to receive data. Cause: 1. Use an oscilloscope to check whether data is sent from the sending pin;
2. Use an oscilloscope to check whether data is received by the receiving pipe foot;
3. baud rate problem-whether the communications port is consistent;
The received data is incorrect because: 1. it may be whether the external clock is used correctly. The default external crystal oscillator of stm32 is 8 MHz. If the incorrect connection is 12 MHz, modify the program configuration. Otherwise, the serial clock is incorrect and the baud rate is incorrect;
(For example: the surface with 19200 baud rate communication, the actual clock is wrong, the baud rate is not actually 19200) the relationship between the baud rate and the clock look at my blog: http://blog.csdn.net/yx_l128125/article/details/9197787
2. Whether there is an ending character when "string" is used for communication;
3. Check whether the serial port is correctly connected when using the dual-serial port. (Serial Port 1 and Serial Port 2 are mixed up)
4. There is another possibility to see my blog: http://blog.csdn.net/yx_l128125/article/details/7914102
Stm32 cainiao growth record-RS485 communication protocol Application