July 09, 2011
by Noter
|
There's not a lot of simple about using the serial port from C or C++ under windows ... here's some functions to make it easier:
DCB dcb;
HANDLE hCom;
COMMTIMEOUTS cTimeout;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM1"); // Most systems have a COM1 port
void PrintCommState(DCB dcb, char *port){
// Print some of the DCB structure values
printf("\nPort = %s, BaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n",
port,
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits
);
}
int OpenCommPort(char *port, char *baud){
// Open a handle to the specified com port.
hCom = CreateFile((LPCTSTR) port,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Initialize the DCB structure.
ZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// Build on the current configuration by first retrieving all current
// settings.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
//PrintCommState(dcb); // Output to console
// Fill in some DCB values and set the com state:
// 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = atol(baud); // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
dcb.fBinary=true;
dcb.fNull=false;
dcb.fTXContinueOnXoff=false;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fDsrSensitivity=false;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fOutxCtsFlow=false;
dcb.fOutxDsrFlow=false;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
// Get the comm config again.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb,port); // Output to console
//printf ("Serial port %s successfully reconfigured.\n", port);
return 0;
}
int SetCommTimeout(int timeout_ms){
cTimeout.ReadIntervalTimeout = MAXDWORD;
cTimeout.ReadTotalTimeoutConstant = timeout_ms;
cTimeout.ReadTotalTimeoutMultiplier = MAXDWORD;
cTimeout.WriteTotalTimeoutConstant = 0;
cTimeout.WriteTotalTimeoutMultiplier = 0;
fSuccess=SetCommTimeouts(hCom, &cTimeout);
if (!fSuccess){
printf ("SetCommTimeouts failed with error %d.\n", GetLastError());
return (1);
}
return 0;
}
int CloseCommPort(){
fSuccess=CloseHandle(hCom);
if (!fSuccess){
printf ("CloseHandle failed with error %d.\n", GetLastError());
return (1);
}
return 0;
}
int PutCommChar(UCHAR chrOut){
unsigned long bytesWritten;
fSuccess=WriteFile(hCom,&chrOut,1,&bytesWritten,0);
if (bytesWritten!=1){
printf ("WriteFile wrote %d bytes.\n", bytesWritten);
return (2);
}
if (!fSuccess){
printf ("WriteFile failed with error %d.\n", GetLastError());
return (1);
}
// printf(">>%02X\n",chrOut);
return 0;
}
int GetCommChar(UCHAR *chrIn){
unsigned long bytesRead;
fSuccess=ReadFile(hCom,chrIn,1,&bytesRead,0);
if (!fSuccess){
printf ("ReadFile failed with error %d.\n", GetLastError());
return (1);
}
if (bytesRead!=1){
printf ("timeout on read port %d\n",bytesRead);
return (2);
}
// printf("<<%02X\n",*chrIn);
return 0;
}
|