In windows (vc6), I believe you have to create a file with the name COM1, then you can use ReadFile and WriteFile to communicate.
Initialise serial comms : (f and hcom must be global variables defined somewhere in the project in this example)
extern HANDLE hcom;
extern FILE * f;
extern unsigned char page;
BOOL InitSerCom(char comport, int baudrate)
{
// return true;
page = 255;
char s;
sprintf(s,"COM%d",comport);
if ((hcom = CreateFile(s,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL)) != NULL)
{
DCB dcb;
sprintf(s,"baud=%d parity=O data=8 stop=1",baudrate);
GetCommState(hcom, &dcb);
BuildCommDCB(s, &dcb);
SetCommState(hcom, &dcb);
COMMTIMEOUTS comt;
comt.ReadIntervalTimeout = 20;
comt.ReadTotalTimeoutMultiplier = 20;
comt.ReadTotalTimeoutConstant = 20;
comt.WriteTotalTimeoutMultiplier = 20;
comt.WriteTotalTimeoutConstant = 20;
SetCommTimeouts(hcom, &comt);
GetCommState(hcom, &dcb);
GetCommTimeouts(hcom, &comt);
return true;
}
else
return false;
}
Sending:
DWORD nrsend;
char senddata = 'q';
WriteFile(hcom,&senddata,1,&nrsend, NULL);
//check nrsend variable to check it was actually sent
Receive :
DWORD nrrecd;
char recdata;
ReadFile(hcom, &recdata, 100, &nrrecd, NULL); //100 is the buffersize!
//nrrecd will be the number of characters actually received
Hope this helps.
Stefaan