Tôi có một dữ liệu gửi dữ liệu Mirocontroller Atmel mà tôi muốn nhận trên PC của mình qua COM1.Tôi nhận được thư rác trên cổng nối tiếp
Khi tôi đính kèm chương trình đầu cuối, dữ liệu được nhận chính xác (tất cả đều là ascii, tất cả có thể in ngoại trừ \ n).
Tuy nhiên, mã của tôi dường như đang nhận được thư rác (ký tự không phải ascii). Ai có thể nhìn thấy những gì tôi đang làm sai? Cảm ơn
Gửi mã, chỉ để biết
// USART options.
static const usart_options_t USART_CONSOLE_OPTIONS =
{
.baudrate = 115200,
.charlength = 8,
.paritytype = USART_NO_PARITY,
.stopbits = USART_1_STOPBIT,
.channelmode = USART_NORMAL_CHMODE
};
Tiếp nhận đang
E_boolean OpenCom1(void)
{
COMMTIMEOUTS timeouts;
comPortHandle = CreateFile("COM1", // Specify port device: default "COM1"
GENERIC_READ | GENERIC_WRITE, // Specify mode that open device.
0, // the device isn't shared.
NULL, // the object gets a default security.
OPEN_EXISTING, // Specify which action to take on file.
0, // default (not overlapped i/o).
NULL); // default (hTemplate must be NULL for COM devices).
if (comPortHandle == INVALID_HANDLE_VALUE)
return False;
deviceControlBlock.DCBlength = sizeof(deviceControlBlock);
if((GetCommState(comPortHandle, &deviceControlBlock) == 0))
{
// CodeMe: do what?
return False;
}
deviceControlBlock.BaudRate = CBR_115200;
deviceControlBlock.StopBits = ONESTOPBIT;
deviceControlBlock.Parity = NOPARITY;
deviceControlBlock.ByteSize = DATABITS_8;
deviceControlBlock.fRtsControl = 0;
if (!SetCommState(comPortHandle, &deviceControlBlock))
{
// CodeMe: do what?
return False;
}
// set short timeouts on the comm port.
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 1000; // oen second
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
if (!SetCommTimeouts(comPortHandle, &timeouts))
{
// CodeMe: do what?
return False;
}
FlushFileBuffers(comPortHandle);
PurgeComm (comPortHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return True;
}//OpenCom1()
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
void ReadCharacterFromCom1(INPUT char *theCharacter)
{
DWORD numBytesRead;
numBytesRead = 0;
while (numBytesRead == 0)
{
ReadFile(comPortHandle, // handle of file to read
theCharacter, // store read data here
sizeof(char), // number of bytes to read
&numBytesRead, // pointer to number of bytes actually read
NULL);
}
return;
}//ReadCharacterFromCom1()
Ý anh là gì bởi "rác"? Làm thế nào để bạn biết "rác" không phải là do tiếng ồn điện? –
@HotLicks +1 Tôi có nghĩa là không phải ascii ký tự và kể từ khi một chương trình thiết bị đầu cuối nhận được một cách chính xác tôi nghiêng để đổ lỗi cho mã của riêng tôi. – Mawg
Bạn đã thử in giá trị thập lục phân của mỗi byte nhận được thông qua 'theCharacter'? – Tuxdude