Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serial Loopback Code

0 views
Skip to first unread message

John Flynn

unread,
Feb 4, 2004, 3:20:52 PM2/4/04
to
Hi,

I am trying to write an application that will test a serial loopback.

Initially I had two applications one to write to the serial port and
the other to read from it. This did not work since the read app could
not open the port as it was already opened.

So I brought in the read routine into the man app and created a thread
from this. The thread basically waits for a read event using

WaitCommEvent (hPort, &dwCommModemStatus, 0);

After creating this thread the main application loop then goes on to
write characters to the port. I expect to see the read thread being
activated and print out the characters. But this never happens. The
main routines are listed below:-

***********************************************************************/
void PortWrite (BYTE Byte)
{
DWORD dwError,
dwNumBytesWritten;

if (!WriteFile (hPort, // Port handle
&Byte, // Pointer to the data to write
1, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of
bytes
// written
NULL)) // Must be NULL for Windows CE
{
// WriteFile failed. Report error.
dwError = GetLastError ();
}
}


/***********************************************************************

PortRead()

This function loops continously simple to demonstrate serial reading
in a specific apllication of reading from the serial port a quit
method should be implemented.

***********************************************************************/
DWORD PortReadThread ()
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;

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

while (hPort != INVALID_HANDLE_VALUE)
{
printf ("WaitCommEvent\n");

// Wait for an event to occur for the port.
WaitCommEvent (hPort, &dwCommModemStatus, 0);

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

if (dwCommModemStatus & EV_RXCHAR)
{
// Loop for waiting for the data.
do
{
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);

// Display the data read.
if (dwBytesTransferred == 1)
printf("%c", Byte );

} while (dwBytesTransferred == 1);
}

// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);

}

return 0;
}


if (hReadThread = CreateThread (NULL, 0,
(LPTHREAD_START_ROUTINE)PortReadThread,
0, 0,
&dwThreadID))
{
CloseHandle (hReadThread);
}

else
{
// Could not create the read thread.
MessageBox (NULL, TEXT("Unable to create the read thread"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}


for (;;)
{
int i;

/* Print all the printable ASCII characters from 0x30 -> 7A */
for (i = '0' ; i < 'z' ; i++)
{
PortWrite(i);

}
}


Can anyone suggest why this mechanism odes not work.

Thanks,
J

Brad Chen

unread,
Feb 10, 2004, 3:02:42 AM2/10/04
to
This mechanism should work fine. You should check if your serial adapter is
good or not. Use Hyper terminal to make sure.

Brad

"John Flynn" <query_...@yahoo.co.uk>
???????:761ef898.04020...@posting.google.com...

0 new messages