Purging the serial port buffers

G

Guest

Hello,

I am trying to set up a program that communicates with an RF modem working
at 19200 bps via RS232 using Microsoft SDK. First of all i was trying to
understand how to work with the SDK fuctions, so i made a test program. The
modem always answers "+++" with "OK<carriage return>"

So the test program is structured in the following manner (i've omitted
declaration of variables, validation and error handling for simplicity):

/*open the COM port*/
hSerial=CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

/*define the port settings*/
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);

/*writes the "+++" and sends it through the rs232*/
sprintf(sendstr, "+++");
WriteFile(hSerial, sendstr, 3, &dwBytesRead, NULL))
printf("sendstr= %s\n", sendstr);

/*supposedly cleans the communication device buffers*/
PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR );

/*reads the answer*/
ReadFile(hSerial, recstr, 9, &dwBytesRead, NULL))
printf("bytesread= %d\n", dwBytesRead);
recstr[dwBytesRead]='\0';
printf("recstr= %s\n", recstr);

CloseHandle(hSerial);


This is what i get as output:
sendstr= +++
bytesread= 4
recstr= OK

So apparently everything works out fine, except the purgeComm. I thought
that it cleared all the buffers of the serial port. Why does the answer from
the modem is still there when i do ReadFile?

Is there another way of clearing the buffers?

Because i wanted to make a program that would detect at which baud rate the
modem was configured, and the only way of doing this is to send the "+++" at
all the possible baud rates and then check for an "OK<CR>". It happens that
when the modem is working at 19200bps, it will still answer the "+++" not
with the "OK<CR>", but with "ERROR 41" which will turn into gibberish since
the two serial ports are woking at different baud rates. SO i get all this
data garbage i dont know how to get rid of...
 
K

KM

Joao,

First of all, this seems to be the wrong newsgroup you posted to. This NG is for Windows XP Embedded product support. You'd rather
want to post it to a Win32 programming NG.

Anyway, just a thought after a quick glance at the code you posted.
First, you may want to add error handling to your code. Specifically, see if the PurgeComm returned TRUE. If not, then you obviously
can analize the GetLastError code.

Also, add a few second Sleep after you write a command to the port but before the PurgeComm call. This will make sure the buffers
are process by the modem driver. (especially after the +++ comand)
(calling to FlushFileBuffers may also be a good idea)

--
=========
Regards,
KM

Hello,

I am trying to set up a program that communicates with an RF modem working
at 19200 bps via RS232 using Microsoft SDK. First of all i was trying to
understand how to work with the SDK fuctions, so i made a test program. The
modem always answers "+++" with "OK<carriage return>"

So the test program is structured in the following manner (i've omitted
declaration of variables, validation and error handling for simplicity):

/*open the COM port*/
hSerial=CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

/*define the port settings*/
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);

/*writes the "+++" and sends it through the rs232*/
sprintf(sendstr, "+++");
WriteFile(hSerial, sendstr, 3, &dwBytesRead, NULL))
printf("sendstr= %s\n", sendstr);

/*supposedly cleans the communication device buffers*/
PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR );

/*reads the answer*/
ReadFile(hSerial, recstr, 9, &dwBytesRead, NULL))
printf("bytesread= %d\n", dwBytesRead);
recstr[dwBytesRead]='\0';
printf("recstr= %s\n", recstr);

CloseHandle(hSerial);


This is what i get as output:
sendstr= +++
bytesread= 4
recstr= OK

So apparently everything works out fine, except the purgeComm. I thought
that it cleared all the buffers of the serial port. Why does the answer from
the modem is still there when i do ReadFile?

Is there another way of clearing the buffers?

Because i wanted to make a program that would detect at which baud rate the
modem was configured, and the only way of doing this is to send the "+++" at
all the possible baud rates and then check for an "OK<CR>". It happens that
when the modem is working at 19200bps, it will still answer the "+++" not
with the "OK<CR>", but with "ERROR 41" which will turn into gibberish since
the two serial ports are woking at different baud rates. SO i get all this
data garbage i dont know how to get rid of...
 
G

Guest

First of all, thank you for the quick reply. Sorry for the posting in the
wrong section, but i found a similar topic here with 44 answers so i thought
this was the right place (lemmings syndrome :( ).
As for the program, i had error handling already, i just omitted it for
posting simplicity, but you solved my problem. It was all in the sleep. As
you said the modem didnt have time to answer before i purged the buffer, so i
ended up receiving the answer anyway.

Thank you very much.

KM said:
Joao,

First of all, this seems to be the wrong newsgroup you posted to. This NG is for Windows XP Embedded product support. You'd rather
want to post it to a Win32 programming NG.

Anyway, just a thought after a quick glance at the code you posted.
First, you may want to add error handling to your code. Specifically, see if the PurgeComm returned TRUE. If not, then you obviously
can analize the GetLastError code.

Also, add a few second Sleep after you write a command to the port but before the PurgeComm call. This will make sure the buffers
are process by the modem driver. (especially after the +++ comand)
(calling to FlushFileBuffers may also be a good idea)

--
=========
Regards,
KM

Hello,

I am trying to set up a program that communicates with an RF modem working
at 19200 bps via RS232 using Microsoft SDK. First of all i was trying to
understand how to work with the SDK fuctions, so i made a test program. The
modem always answers "+++" with "OK<carriage return>"

So the test program is structured in the following manner (i've omitted
declaration of variables, validation and error handling for simplicity):

/*open the COM port*/
hSerial=CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

/*define the port settings*/
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);

/*writes the "+++" and sends it through the rs232*/
sprintf(sendstr, "+++");
WriteFile(hSerial, sendstr, 3, &dwBytesRead, NULL))
printf("sendstr= %s\n", sendstr);

/*supposedly cleans the communication device buffers*/
PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR );

/*reads the answer*/
ReadFile(hSerial, recstr, 9, &dwBytesRead, NULL))
printf("bytesread= %d\n", dwBytesRead);
recstr[dwBytesRead]='\0';
printf("recstr= %s\n", recstr);

CloseHandle(hSerial);


This is what i get as output:
sendstr= +++
bytesread= 4
recstr= OK

So apparently everything works out fine, except the purgeComm. I thought
that it cleared all the buffers of the serial port. Why does the answer from
the modem is still there when i do ReadFile?

Is there another way of clearing the buffers?

Because i wanted to make a program that would detect at which baud rate the
modem was configured, and the only way of doing this is to send the "+++" at
all the possible baud rates and then check for an "OK<CR>". It happens that
when the modem is working at 19200bps, it will still answer the "+++" not
with the "OK<CR>", but with "ERROR 41" which will turn into gibberish since
the two serial ports are woking at different baud rates. SO i get all this
data garbage i dont know how to get rid of...
 
K

KM

Ahh.. Sometimes I think we have touched too many areas in this NG :)

Anyway, glad my suggestions worked for you.

--
=========
Regards,
KM


Joao Pio said:
First of all, thank you for the quick reply. Sorry for the posting in the
wrong section, but i found a similar topic here with 44 answers so i thought
this was the right place (lemmings syndrome :( ).
As for the program, i had error handling already, i just omitted it for
posting simplicity, but you solved my problem. It was all in the sleep. As
you said the modem didnt have time to answer before i purged the buffer, so i
ended up receiving the answer anyway.

Thank you very much.

KM said:
Joao,

First of all, this seems to be the wrong newsgroup you posted to. This NG is for Windows XP Embedded product support. You'd
rather
want to post it to a Win32 programming NG.

Anyway, just a thought after a quick glance at the code you posted.
First, you may want to add error handling to your code. Specifically, see if the PurgeComm returned TRUE. If not, then you
obviously
can analize the GetLastError code.

Also, add a few second Sleep after you write a command to the port but before the PurgeComm call. This will make sure the buffers
are process by the modem driver. (especially after the +++ comand)
(calling to FlushFileBuffers may also be a good idea)

--
=========
Regards,
KM

Hello,

I am trying to set up a program that communicates with an RF modem working
at 19200 bps via RS232 using Microsoft SDK. First of all i was trying to
understand how to work with the SDK fuctions, so i made a test program. The
modem always answers "+++" with "OK<carriage return>"

So the test program is structured in the following manner (i've omitted
declaration of variables, validation and error handling for simplicity):

/*open the COM port*/
hSerial=CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

/*define the port settings*/
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);

/*writes the "+++" and sends it through the rs232*/
sprintf(sendstr, "+++");
WriteFile(hSerial, sendstr, 3, &dwBytesRead, NULL))
printf("sendstr= %s\n", sendstr);

/*supposedly cleans the communication device buffers*/
PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR );

/*reads the answer*/
ReadFile(hSerial, recstr, 9, &dwBytesRead, NULL))
printf("bytesread= %d\n", dwBytesRead);
recstr[dwBytesRead]='\0';
printf("recstr= %s\n", recstr);

CloseHandle(hSerial);


This is what i get as output:
sendstr= +++
bytesread= 4
recstr= OK

So apparently everything works out fine, except the purgeComm. I thought
that it cleared all the buffers of the serial port. Why does the answer from
the modem is still there when i do ReadFile?

Is there another way of clearing the buffers?

Because i wanted to make a program that would detect at which baud rate the
modem was configured, and the only way of doing this is to send the "+++" at
all the possible baud rates and then check for an "OK<CR>". It happens that
when the modem is working at 19200bps, it will still answer the "+++" not
with the "OK<CR>", but with "ERROR 41" which will turn into gibberish since
the two serial ports are woking at different baud rates. SO i get all this
data garbage i dont know how to get rid of...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top