Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main(int argc, char *argv[])
{
HANDLE hComm;
DCB dcbSerialParams;
char initstr[20];
hComm = CreateFile( "COM3",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hComm == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
fputs("Error: Serial port does not exist\n", stderr);
}
return EXIT_FAILURE;
}
fputs ("Port opened successfully\n", stdout);
ZeroMemory(&dcbSerialParams, sizeof(dcbSerialParams));
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
/*
* See
* http://forums.codeguru.com/showthread.php?43432-I-finally-found-it!-BuildCommDCB-bug!
*/
strcpy(initstr, "9600,n,8,1,x");
if (BuildCommDCB(initstr, &dcbSerialParams))
{
if (SetCommState(hComm, &dcbSerialParams))
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 20;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 100;
if (SetCommTimeouts(hComm, &timeouts))
{
char szRXBuff[100] = {0};
char szTXBuff[100] = "Test";
DWORD dwBytesRead = 0;
DWORD dwCommEvent;
DWORD dwRead;
char chRead;
if (!WriteFile(hComm, szTXBuff, strlen(szTXBuff), &dwBytesRead, NULL))
{
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "Error: Could not send TX buffer\n%s", lastError);
}
fprintf(stdout, "\nBytes Written: %d\n", (int)dwBytesRead);
/* Recommended way
if (SetCommMask(hComm, EV_RXCHAR))
{
if (WaitCommEvent(hComm, &dwCommEvent, NULL))
{
do {
if (ReadFile(hComm, &chRead, 1, &dwRead, NULL))
putc(chRead,stdout);
// A byte has been read; process it.
else
// An error occurred in the ReadFile call.
break;
} while (dwRead);
}
else
{
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "Error: Problem with WaitCommEvent\n%s", lastError);
}
}
else
{
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "Error: Could not set up Comm Mask\n%s", lastError);
}*/
FlushFileBuffers(hComm);
/* Not recommeded way, but will do for this test */
if (!ReadFile(hComm, szRXBuff, (DWORD)(sizeof(szRXBuff)-1), &dwBytesRead, NULL))
{
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "Error: Could not receive RX buffer\n%s", lastError);
}
printf("\n%d:%x%x%x%x%x", (int)dwBytesRead, szRXBuff[0],szRXBuff[1],szRXBuff[2],
szRXBuff[3],szRXBuff[4]);
}
else
{
// Couldn't build the DCB. Usually a problem
// with the communications specification string.
fputs("Error: Could not set serial port timeouts\n", stderr);
}
}
else
{
fputs("Error: Could not set DCB settings (such as baud rate, parity, ect...)\n", stderr);
}
}
else
{
// Couldn't build the DCB. Usually a problem
// with the communications specification string.
fputs("Error: Could not build DCB settings (such as baud rate, parity, ect...)\n", stderr);
}
if (!CloseHandle(hComm))
{
fputs("Error: Could not close handle\n", stderr);
}
return EXIT_SUCCESS;
}
Here is the output when Rx and Tx of the RS-232 are short circuited (output looped straight back into the input):