Click to See Complete Forum and Search --> : Problem Switching Baud Rate


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();

}

hinksj
December 25th, 2002, 09:38 AM
Additional Info so hopefully someone can enlighten me ....

I am using OVERLAPPED Reads.

Prior to issuing a call to set the Baud rate at 9600, there is a pending READ on ReadFile. I use a WaitForMultipleObjects to detect Read completion.

After the Baud rate switch, I do NOT get my Read Event being set to inform me that a byte is avail (I WriteFile a byte at 9600 which will reflect into the uart receiver) ... Not even getting that.

It appears the low-level driver has shut down.

I have called ClearCommError(...) and PurgeComm(..) after the Baud rate switch in attempt to clear any driver issue. still nothing

Any thoughts???