我有一个 Atmel 微控制器发送我想通过 COM1 在我的 PC 上接收的数据。
当我附加一个终端程序时,数据被正确接收(它都是 ascii,除了 \n 之外都是可打印的)。
但是,我的代码似乎正在接收垃圾(非 ascii 字符)。谁能看到我做错了什么?谢谢
发送代码,仅供参考
// 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
};
接收代码
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()