串行通信期间读取的未知字节
Unknown bytes read during serial communications
我正在编写代码以使用串行端口和 MCU 的 UART 与 stm32 设备 (stm32l412kb) 进行通信。该代码的目的是 MCU 将向主机发送一个 instruction/request 字节。主机将根据该指令执行操作。例如,发送的第一条指令是 ACK(确认)字节 = 0x79。当计算机收到此消息时,会发回一个 ACK。
第一个字节完美无缺。但是,要发送的第二个字节是 VERSION_REQUEST 字节 = 0x01。当 stm32 代码循环等待响应时,stm32 不断发送这个字节并且只发送这个字节。问题出在主机端,无论延迟如何,计算机正在读取字节模式:1B、08、D4、9F、79(作为 = ACK 执行)。计算机将循环读取这些内容,不会出现 01。
由于stm32和host端都有代码,不知道是哪里出了问题。主机端的代码(宏和 main())是:
#include <windows.h>
#include <cstdio>
#include <stdint.h>
HANDLE hSerial;
/*Macros*/
#define ACK 0x79 //Acknowledge byte used in UART communication
#define NACK 0x1F //Not acknowledged byte used in UART communication
//Manually change the version number of the latest update
#define VERSION_NUMBER 0x0001
/*Instruction Macros*/
#define VERSION_REQUEST 0x01
#define DOWNLOAD_REQUEST 0x02
/*Function Prototypes*/
char receiveInstruction();
int sendACK();
int sendVersionNumber();
void initialiseSerialPort();
void readData(char Rx_Buffer[], int numberOfBytes);
void writeData(char Tx_Buffer[], int numberOfBytes);
int main(){
/*Error Handling*/
char lastError[1024];
initialiseSerialPort();
while (1) {
uint8_t instruction = 0;
printf("Searching for incoming instruction...\n");
//This odd while requirements were to try and enclose the problem
//Wait until familiar instruction is read
while ((instruction != ACK) && (instruction != VERSION_REQUEST)) {
instruction = receiveInstruction();
printf("Received Instruction: %X \n", instruction);
}
printf("Received Instruction: %X \n", instruction);
if (instruction == ACK) {
sendACK();
}
else if (instruction == VERSION_REQUEST) {
sendVersionNumber();
}
else {
printf("Unknown request received.\n");
}
Sleep(100);
}
/*Close Down*/
CloseHandle(hSerial); //Without closing - may not be able to access the port until reboot
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
return 0 ;
}
readData 和 readInstruction 函数是:
void readData(char Rx_Buffer[], int numberOfBytes)
{
/*Clear Buffer*/
for (int i = 0; i < numberOfBytes; i++) {
Rx_Buffer[i] = 0 ;
}
DWORD dwBytesRead = 0;
/*Read Bytes*/
if (!ReadFile(hSerial, Rx_Buffer, numberOfBytes, &dwBytesRead, NULL)) {
//error occurred. Report to user.
}
}
char receiveInstruction()
{
//Initialise reading buffer, this decides which mode of transmission to use.
char Rx_Buffer[1];
Rx_Buffer[0] = 0;
//While the buffer is empty, read for an instruction
while (Rx_Buffer[0] == 0) {
readData(Rx_Buffer, 1);
Sleep(100);
}
return Rx_Buffer[0];
}
如果 stm32 端需要代码,我可以提供,但它看起来只是循环发送版本号请求(主机未收到)。
抱歉,这是一个很模糊的问题,我真的不知道去哪里找。如果需要更多详细信息,我可以提供。
一如既往地感谢您花时间提供帮助,非常感谢。
- 您可以尝试从主机向具有特定位模式的 STM 字节发送,每次发送之间至少等待 500 毫秒以避免合并字节。
0x01
、0x55
、0xFF
记下您从 STM 中读取的内容。
- 然后尝试从 STM 到主机的相同操作。
记下您从主机读取的内容
- 然后你可以从差异中推导出一些东西:
如果您确定始终从一侧发送一个字节,而从另一侧(如您的模式)获得不同的读数,则这很像是同步问题。含义:波特率,而不是奇偶校验位或停止位,因为在那种情况下,模式将始终与 0x01 转换为 0x10 相同。
但是用逻辑分析仪会容易得多:)
我也可能是噪音。
当您查看模式字节时,它与 0x01:
有很大不同
0x01 : 0b00000001
0x1B : 0b00011011
0x08 : 0b00001000
0xD4 : 0b11010100
0x9F : 0b10011111
0x79 : 0b01111001
我正在编写代码以使用串行端口和 MCU 的 UART 与 stm32 设备 (stm32l412kb) 进行通信。该代码的目的是 MCU 将向主机发送一个 instruction/request 字节。主机将根据该指令执行操作。例如,发送的第一条指令是 ACK(确认)字节 = 0x79。当计算机收到此消息时,会发回一个 ACK。
第一个字节完美无缺。但是,要发送的第二个字节是 VERSION_REQUEST 字节 = 0x01。当 stm32 代码循环等待响应时,stm32 不断发送这个字节并且只发送这个字节。问题出在主机端,无论延迟如何,计算机正在读取字节模式:1B、08、D4、9F、79(作为 = ACK 执行)。计算机将循环读取这些内容,不会出现 01。
由于stm32和host端都有代码,不知道是哪里出了问题。主机端的代码(宏和 main())是:
#include <windows.h>
#include <cstdio>
#include <stdint.h>
HANDLE hSerial;
/*Macros*/
#define ACK 0x79 //Acknowledge byte used in UART communication
#define NACK 0x1F //Not acknowledged byte used in UART communication
//Manually change the version number of the latest update
#define VERSION_NUMBER 0x0001
/*Instruction Macros*/
#define VERSION_REQUEST 0x01
#define DOWNLOAD_REQUEST 0x02
/*Function Prototypes*/
char receiveInstruction();
int sendACK();
int sendVersionNumber();
void initialiseSerialPort();
void readData(char Rx_Buffer[], int numberOfBytes);
void writeData(char Tx_Buffer[], int numberOfBytes);
int main(){
/*Error Handling*/
char lastError[1024];
initialiseSerialPort();
while (1) {
uint8_t instruction = 0;
printf("Searching for incoming instruction...\n");
//This odd while requirements were to try and enclose the problem
//Wait until familiar instruction is read
while ((instruction != ACK) && (instruction != VERSION_REQUEST)) {
instruction = receiveInstruction();
printf("Received Instruction: %X \n", instruction);
}
printf("Received Instruction: %X \n", instruction);
if (instruction == ACK) {
sendACK();
}
else if (instruction == VERSION_REQUEST) {
sendVersionNumber();
}
else {
printf("Unknown request received.\n");
}
Sleep(100);
}
/*Close Down*/
CloseHandle(hSerial); //Without closing - may not be able to access the port until reboot
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
return 0 ;
}
readData 和 readInstruction 函数是:
void readData(char Rx_Buffer[], int numberOfBytes)
{
/*Clear Buffer*/
for (int i = 0; i < numberOfBytes; i++) {
Rx_Buffer[i] = 0 ;
}
DWORD dwBytesRead = 0;
/*Read Bytes*/
if (!ReadFile(hSerial, Rx_Buffer, numberOfBytes, &dwBytesRead, NULL)) {
//error occurred. Report to user.
}
}
char receiveInstruction()
{
//Initialise reading buffer, this decides which mode of transmission to use.
char Rx_Buffer[1];
Rx_Buffer[0] = 0;
//While the buffer is empty, read for an instruction
while (Rx_Buffer[0] == 0) {
readData(Rx_Buffer, 1);
Sleep(100);
}
return Rx_Buffer[0];
}
如果 stm32 端需要代码,我可以提供,但它看起来只是循环发送版本号请求(主机未收到)。
抱歉,这是一个很模糊的问题,我真的不知道去哪里找。如果需要更多详细信息,我可以提供。
一如既往地感谢您花时间提供帮助,非常感谢。
- 您可以尝试从主机向具有特定位模式的 STM 字节发送,每次发送之间至少等待 500 毫秒以避免合并字节。
0x01
、0x55
、0xFF
记下您从 STM 中读取的内容。
- 然后尝试从 STM 到主机的相同操作。
记下您从主机读取的内容
- 然后你可以从差异中推导出一些东西:
如果您确定始终从一侧发送一个字节,而从另一侧(如您的模式)获得不同的读数,则这很像是同步问题。含义:波特率,而不是奇偶校验位或停止位,因为在那种情况下,模式将始终与 0x01 转换为 0x10 相同。
但是用逻辑分析仪会容易得多:)
我也可能是噪音。 当您查看模式字节时,它与 0x01:
有很大不同0x01 : 0b00000001
0x1B : 0b00011011
0x08 : 0b00001000
0xD4 : 0b11010100
0x9F : 0b10011111
0x79 : 0b01111001