Click to See Complete Forum and Search --> : Strange Input to the Console Port


BrainTease
May 17th, 2006, 03:44 PM
I am getting strange input to my console window when I am reading from the serial port on my PC. The following is what I am seeing in my console window:


A Test Port COM 1
X EXIT

SERIAL PORT TESTING - Enter Selection >

A
select = COM1
COM1 Port Received: = `
COM1 Port Received: = x
COM1 Port Received: = `
COM1 Port Received: =
COM1 Port Received: = µ
COM1 Port Received: = ~
COM1 Port Received: = ₧
COM1 Port Received: = `
COM1 Port Received: = ♠
COM1 Port Received: = ▲
COM1 Port Received: = å


What I'm expecting is the following:



A Test Port COM 1
X EXIT

SERIAL PORT TESTING - Enter Selection >

A
select = COM1
COM1 Port Received: = G
COM1 Port Received: = P
COM1 Port Received: = G
COM1 Port Received: = G
COM1 Port Received: = A
COM1 Port Received: = ,
COM1 Port Received: = 1
COM1 Port Received: = 2
COM1 Port Received: = 6
COM1 Port Received: = 7
COM1 Port Received: = 1


Why is it looking so screwed up? The following is what I believe is relevant code to this problem. My PortRead function calls ReadFile when it creates a thread.


ERR_CODE PortRead(CommPortClass *hCommPort)
{
HANDLE hThread; // handler for port read thread
DWORD IDThread;
DWORD Ret, ExitCode;
DWORD dTimeout = 5000; // define time out value: 5 sec.
ERR_CODE ecStatus = OK;

if (!(hThread = CreateThread(NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) ThreadFunc,
(LPVOID)hCommPort, // parameter to thread funciton
CREATE_SUSPENDED, // creation flag - suspended
&IDThread) ) ) // returns thread ID
{
msg("Create Read Thread failed");
return EC_CREATE_THREAD;
}
}


void WINAPI ThreadFunc(void* hCommPorts)
{
BYTE Byte;
DWORD dwError;
BOOL bResult;
int nTotRead = 0;
DWORD dwCommModemStatus, dwBytesTransferred;
CommPortClass* CommPorts;
ERR_CODE ecStatus = OK;

CommPorts = (CommPortClass* )hCommPorts;

// Specify a set of events to be monitored for the port.
SetCommMask(CommPorts->handlePort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);

// Wait for an event to occur for the port.
WaitCommEvent(CommPorts->handlePort, &dwCommModemStatus, 0);

// Re-specify the set of events to be monitored for the port.
SetCommMask(CommPorts->handlePort, EV_RXCHAR | EV_CTS | EV_DSR |EV_RLSD| EV_RING);

if (dwCommModemStatus & EV_RXCHAR||dwCommModemStatus & EV_RLSD) // received the char_event
{

// Read the data from the serial port.
bResult = ReadFile(CommPorts->handlePort,
&Byte,
1,
&dwBytesTransferred,
0);
}


Any ideas? Thanks in advance for the help!

JamesSchumacher
May 17th, 2006, 04:58 PM
From reading the following:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/setcommmask.asp

What I *think* maybe the problem is this line:


if (dwCommModemStatus & EV_RXCHAR||dwCommModemStatus & EV_RLSD) // received the char_event


When you do a logical 'or' and check to see if EV_RLSD bit is set.

According to the article, EV_RLSD *does not* place a character into the input buffer. So when you read from the COM port if this bit has been set (yet EV_RXCHAR has not been set) you read a character from the port when there isn't any in the buffer. >.>

EDIT....

Now from reading more.... Is EV_RLSD (receive line signal detect) supposed to be when you receive a new line signal? If so when you receive this signal you will have to output a 'new line' yourself and not read from the COM port.

hanhao
February 23rd, 2007, 12:06 AM
could be your baud rate,stop bit , data bit , parity settings

sreehari
February 26th, 2007, 04:41 AM
The baud rate at which the data is being sent is different from the baudrate you are expecting to receive the data.