I have an Atmel mirocontroller sending data that I want to receive on my PC via COM1.
When I connect the terminal program, the data is received correctly (this is all ascii, all for printing, except \ n).
However, my code seems to get unwanted (non-ascii characters). Can anyone see what I'm doing wrong? Thanks
Sending a code, for information only
static const usart_options_t USART_CONSOLE_OPTIONS =
{
.baudrate = 115200,
.charlength = 8,
.paritytype = USART_NO_PARITY,
.stopbits = USART_1_STOPBIT,
.channelmode = USART_NORMAL_CHMODE
};
Code retrieval
E_boolean OpenCom1(void)
{
COMMTIMEOUTS timeouts;
comPortHandle = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (comPortHandle == INVALID_HANDLE_VALUE)
return False;
deviceControlBlock.DCBlength = sizeof(deviceControlBlock);
if((GetCommState(comPortHandle, &deviceControlBlock) == 0))
{
return False;
}
deviceControlBlock.BaudRate = CBR_115200;
deviceControlBlock.StopBits = ONESTOPBIT;
deviceControlBlock.Parity = NOPARITY;
deviceControlBlock.ByteSize = DATABITS_8;
deviceControlBlock.fRtsControl = 0;
if (!SetCommState(comPortHandle, &deviceControlBlock))
{
return False;
}
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
if (!SetCommTimeouts(comPortHandle, &timeouts))
{
return False;
}
FlushFileBuffers(comPortHandle);
PurgeComm (comPortHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return True;
}
void ReadCharacterFromCom1(INPUT char *theCharacter)
{
DWORD numBytesRead;
numBytesRead = 0;
while (numBytesRead == 0)
{
ReadFile(comPortHandle,
theCharacter,
sizeof(char),
&numBytesRead,
NULL);
}
return;
}
source
share