#include "StdAfx.h"
#include "serialport.h"
// default constructer... not much to do right now but set our handle (hComm) to NULL
SerialPort::SerialPort()
{
hComm = NULL;
}
// destructor which will automatically close the serial port incase the user forgot to do so manually...
SerialPort::~SerialPort(void)
{
Close();
}
// will close the serial port if it is open
void SerialPort::Close()
{
if(hComm)
{
CloseHandle(hComm);
hComm = NULL;
}
}
// The following will open the serial port using the settings specified within COMPort (see SerialPort.h for more info on this)
bool SerialPort::InitSerialPort(SetupPort COMPort)
{
hComm = CreateFile(_T("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hComm == INVALID_HANDLE_VALUE)
return false; // Something went wrong, so quit
DCB options;
if(GetCommState(hComm, &options) == 0) // try to get the current settings for the COM port
{
MessageBox(NULL, _T("InitSerialPort Error 0x02"),_T("Error"),MB_OK); // didn't work, so quit
CloseHandle(hComm);
hComm = NULL;
return false;
}
// OK, so now change these settings to the ones specified by COMPort
options.BaudRate = COMPort.BaudRate;
options.ByteSize = COMPort.ByteSize;
options.Parity = COMPort.Parity;
options.StopBits = COMPort.StopBits;
options.fBinary = TRUE;
options.fDsrSensitivity = false;
options.fParity = COMPort.fParity;
options.fOutX = false;
options.fInX = false;
options.fNull = false;
options.fAbortOnError = true;
options.fOutxCtsFlow = TRUE;
options.fOutxDsrFlow = false;
options.fDtrControl=DTR_CONTROL_ENABLE;
options.fDsrSensitivity=false;
options.fRtsControl=RTS_CONTROL_HANDSHAKE;
options.fOutxCtsFlow=false;
// And set the settings:
if(SetCommState(hComm, &options) == 0)
{
MessageBox(NULL, _T("InitSerialPort Error 0x03"),_T("Error"),MB_OK);
CloseHandle(hComm);
hComm = NULL;
return false;
}
// setup the timeouts:
COMMTIMEOUTS timeOuts;
if((GetCommTimeouts (hComm, &timeOuts))==0)
return false;
timeOuts.ReadIntervalTimeout = 0;
timeOuts.ReadTotalTimeoutConstant = 10;
timeOuts.ReadTotalTimeoutMultiplier = 10;
timeOuts.WriteTotalTimeoutConstant = 1000;
timeOuts.WriteTotalTimeoutMultiplier= 10;
if(SetCommTimeouts (hComm, &timeOuts) == 0)
{
MessageBox(NULL, _T("InitSerialPort Error 0x04"),_T("Error"),MB_OK);
CloseHandle(hComm);
hComm = NULL;
return false;
}
return true; // done and all went well!
}
// This will send a single byte of data over the serial port
bool SerialPort::SendByte(char data)
{
if(!hComm)
return false; // COM port not open, so quit
DWORD bytesWritten = 0;
if(WriteFile(hComm, &data, 1, &bytesWritten, NULL) == 0)
return false;
return true;
}
// This will check to see if there is anything to read in from the COM port
// If there is data to read, that byte is stored in data, and this returns a TRUE
// Otherwise a FALSE is returned and data is unaffected
bool SerialPort::ReadByte(char *data)
{
BYTE b;
DWORD bytesTransferred=0;
if(ReadFile(hComm, &b, 1, &bytesTransferred, 0))
{
if (bytesTransferred == 1)
{
*data=b;
return true;
}
}
return false;
}
|