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...
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...