Here is the code. Thanks for taking the time to have a look at it.
Regards,
John
Code:
///////////////////////////////////
// Communications Class
// Communications.h
///////////////////////////////////
#define COMMUNICATIONS_H
#include <windows.h>
#include <iostream>
using namespace std;
class Communications
{
private:
HANDLE CommPortHandle;
DCB dcb;
DCB *pdcb;
public:
void OpenCommPort(void); // opens COM1:
void CloseCommPort(void); // closes COM1:
HANDLE GetCommPortHandle(void); // returns the COM1: handle
void BuildDCB(void); // sets DCB parameters
void BuildTimeOuts(void); // sets COM1: timeouts
bool SendTestString(void); // sends a test string to COM1:
};
#endif
///////////////////////////////////
// Communications Class
// Communications.cpp
///////////////////////////////////
#include "Communications.h"
/////////////////////////////////////////////////////////////////////////
void Communications::OpenCommPort(void)
// Opens COM1: for reading and writing. If CreateFile is successful,
// a pointer to the port is stored in CommPortHandle (private). Otherwise,
// the pointer is saved as zero. The function reports verbose indications
// of success or failure.
{
char comport[]="COM1:";
HANDLE x=0;
x = CreateFile(comport, // pointer to name of the file
GENERIC_READ || GENERIC_WRITE, // access (read-write) mode
0, // share mode
NULL, // pointer to security attributes
OPEN_EXISTING, // how to create
FILE_ATTRIBUTE_NORMAL, // port attributes
NULL); // handle to file with attributes to copy
if(!x)
{
cout<<"Error Opening COM1:"<<endl;
CommPortHandle=0;
}
else
{
cout<<"COM1: Opened Successfully"<<endl;
CommPortHandle=x;
}
}
/////////////////////////////////////////////////////////////////////////
HANDLE Communications::GetCommPortHandle(void)
// returns the handle to this COM port.
{
return(CommPortHandle);
// report back
cout<<"Handle Returned"<<endl;
}
/////////////////////////////////////////////////////////////////////////
void Communications::CloseCommPort(void)
// this function closes the comport and sets CommPortHandle=0
{
CloseHandle(CommPortHandle);
CommPortHandle=0;
// report back
cout<<"Port Closed"<<endl;
}
/////////////////////////////////////////////////////////////////////////
void Communications::BuildDCB(void)
// this function set DCB parameters
{
// create DCB objects
DCB dcb, *pdcb;
// set the dcb pointer
pdcb=&dcb;
// define the variables in the dcb object
dcb.DCBlength=sizeof(dcb);
dcb.BaudRate=300;
dcb.fBinary=true;
dcb.fParity=false;
dcb.fOutxCtsFlow=false;
dcb.fOutxDsrFlow=false;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity=false;
dcb.fTXContinueOnXoff=true;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fErrorChar=false;
dcb.fNull=true;
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.fAbortOnError=false;
// dcb.fDummy2 do not use
dcb.wReserved=0;
dcb.XonLim=1;
dcb.XoffLim=1;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=1;
dcb.XonChar=0;
dcb.XoffChar=0;
dcb.ErrorChar=0;
dcb.EofChar=0;
dcb.EvtChar='&';
// dcb.wReserved do not use
//set the COM1: according to dcb parameters
SetCommState(CommPortHandle,pdcb);
// report back
cout<<"DCB Built"<<endl;
}
/////////////////////////////////////////////////////////////////////////
void Communications::BuildTimeOuts(void)
// this function set the COM1: timeouts
{
// create COMMTIMEOUTS object
COMMTIMEOUTS cto;
// set comm timeout variables here
cto.ReadIntervalTimeout=MAXWORD;
cto.ReadTotalTimeoutConstant=0;
cto.ReadTotalTimeoutMultiplier=0;
cto.WriteTotalTimeoutConstant=2000;
cto.WriteTotalTimeoutMultiplier=0;
// set the COM1: timeouts
SetCommTimeouts(CommPortHandle,&cto);
// report back
cout<<"Timeouts Set"<<endl;
}
/////////////////////////////////////////////////////////////////////////
bool Communications::SendTestString(void)
{
bool result=false;
DWORD n, txresult;
unsigned long int byteswritten=0;
char *str;
str="This is a test of the serial communications port.";
n=strlen(str);
cout<<"The test string is "<<n<<" characters long\n";
cout<<"Started Writing...\n";
txresult=WriteFile(CommPortHandle,str,n,&byteswritten,0);
DWORD LE=GetLastError(); // always returns with 5 (ERROR_ACCESS_DENIED)
cout<<"GetLastError()="<<LE<<"\n";
cout<<"txresult is "<<txresult<<"\n";
return result;
}
///////////////////////////////////
// Communications Class Testbed
// commtest.cpp
///////////////////////////////////
#include "Communications.h"
#include <windows.h>
#include <iostream>
using namespace std;
void main()
{
// create a Communications object
Communications c;
// open the port
c.OpenCommPort();
// build the DCB
c.BuildDCB();
// build the timeouts
c.BuildTimeOuts();
// send a test string to COM1:
c.SendTestString();
// close the port
c.CloseCommPort();
}