hinksj
December 24th, 2002, 09:19 PM
I am porting a DOS based Serial Com program over to VC++;
I wrote the low-level Dos drivers for the Custom Modem.
The Modem comes up in 1200 baud.
You do some talking to it. then command the Modem to 9600 baud.
Program switches computer's Serial Port to 9600 and continue talking at 9600.
Now I'm taking the program to VC NET.
Please find attached my code to Set the Com Port and Set the Baud Rate.
Code goes like this ....
OpenSerialPort(1); // set COM1
SetBaudRate(1200);
... do some communicating at 1200
.. Xmit a message to the Modem to goto 9600
... Obtain Positive reply from Modem (at 1200) to switch
SetBaudRate(9600); // function return with no errors
I receive NO MORE data from the Modem!!!!!!
I have a seperate thread, that acquires RCV'd data.
I have tried to Close the Port and Open it again with an initial
Baud rate of 9600 but that fails too.
I'm developing on WIN 2k.
What am I missing???
Thans in advance
// return 1 if error
int OpenSerialPort(int CommPort)
{
char *p;
int i;
switch(CommPort)
{
case 1:
p = "COM1";
break;
case 2:
p = "COM2";
break;
case 3:
p = "COM3";
break;
case 4:
p = "COM4";
break;
default:
return 1;
}
SPort = CreateFile(p,
(GENERIC_READ | GENERIC_WRITE),
0, 0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if(SPort == INVALID_HANDLE_VALUE)
{
SPort = 0;
return 1;
}
// set Serial Comm Timeouts
TimeOuts.ReadIntervalTimeout = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 10;
TimeOuts.WriteTotalTimeoutMultiplier = 100;
SetCommTimeouts(SPort, &TimeOuts);
// Set Comm Event Mask ...
SetCommMask(SPort, (EV_ERR));
return 0;
}
// return 1 if error
int SetBaudRate(int Speed)
{
static int OldSpeed = 0;
int r;
dword BR;
FillMemory(&dcb, sizeof(dcb), 0);
switch(Speed)
{
case 1200:
BR = CBR_1200;
break;
case 9600:
BR = CBR_9600;
break;
default:
return 1;
}
// fill out struct
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = BR;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.XonLim = 2048;
dcb.XoffLim = 512;
dcb.XonChar = 17;
dcb.XoffChar = 19;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
r = SetCommState(SPort, &dcb);
if(r)
return 0; // ok
return GetLastError();
}
I wrote the low-level Dos drivers for the Custom Modem.
The Modem comes up in 1200 baud.
You do some talking to it. then command the Modem to 9600 baud.
Program switches computer's Serial Port to 9600 and continue talking at 9600.
Now I'm taking the program to VC NET.
Please find attached my code to Set the Com Port and Set the Baud Rate.
Code goes like this ....
OpenSerialPort(1); // set COM1
SetBaudRate(1200);
... do some communicating at 1200
.. Xmit a message to the Modem to goto 9600
... Obtain Positive reply from Modem (at 1200) to switch
SetBaudRate(9600); // function return with no errors
I receive NO MORE data from the Modem!!!!!!
I have a seperate thread, that acquires RCV'd data.
I have tried to Close the Port and Open it again with an initial
Baud rate of 9600 but that fails too.
I'm developing on WIN 2k.
What am I missing???
Thans in advance
// return 1 if error
int OpenSerialPort(int CommPort)
{
char *p;
int i;
switch(CommPort)
{
case 1:
p = "COM1";
break;
case 2:
p = "COM2";
break;
case 3:
p = "COM3";
break;
case 4:
p = "COM4";
break;
default:
return 1;
}
SPort = CreateFile(p,
(GENERIC_READ | GENERIC_WRITE),
0, 0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if(SPort == INVALID_HANDLE_VALUE)
{
SPort = 0;
return 1;
}
// set Serial Comm Timeouts
TimeOuts.ReadIntervalTimeout = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 10;
TimeOuts.WriteTotalTimeoutMultiplier = 100;
SetCommTimeouts(SPort, &TimeOuts);
// Set Comm Event Mask ...
SetCommMask(SPort, (EV_ERR));
return 0;
}
// return 1 if error
int SetBaudRate(int Speed)
{
static int OldSpeed = 0;
int r;
dword BR;
FillMemory(&dcb, sizeof(dcb), 0);
switch(Speed)
{
case 1200:
BR = CBR_1200;
break;
case 9600:
BR = CBR_9600;
break;
default:
return 1;
}
// fill out struct
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = BR;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.XonLim = 2048;
dcb.XoffLim = 512;
dcb.XonChar = 17;
dcb.XoffChar = 19;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
r = SetCommState(SPort, &dcb);
if(r)
return 0; // ok
return GetLastError();
}