Re: about serial port communication and new operator
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 01 May 2006 18:09:59 -0400
Several issues... see below
On 29 Apr 2006 05:01:40 -0700, "harshalshete@xxxxxxxxx" <harshalshete@xxxxxxxxx> wrote:
hi group,****
i got two problems
1) related to serial port communication
2) related to new operator
problem 1)i am opening the com port for serial communication
COM1.then i am trying to communicate with device with this port.
on my computer i get a valid COM1 handle.then i have done GetComState
and SetComstate
and then i am issuing a read call on that port.
my problem is that for some time the program was running fine and then
suddenly it is not reading anything.
and ReadFile is returning 1 but the number of bytes read are zero i
have given the code below.
This is expected behavior from a serial port. Why does it surprise you?
*****
please help me if anyone knows about this problem.****
BOOL fSuccess;
CString Port = getPort();
DCB dcb;
hCom = CreateFile( Port,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
MessageBox("Can Not Open the port file","Error",MB_ICONERROR);
return 1;
And what, exactly, does "1" mean? This is a BOOL function. It returns TRUE or FALSE.
Values like "1" and "0" are nonsensical!
****
}****
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
MessageBox("GetCommstate failed","Error",MB_ICONERROR);
return 1;
}
// Fill in DCB: 9600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
MessageBox("SetCommstate failed","Error",MB_ICONERROR);
return 1;
By the way, at what point do you close the handle on an error?
****
}*****
////for reading from port
char * buff = new char[1024];
int j=0;
DWORD i;
CString GreatBuffer;
while(1)
Would it hurt to write readable code? What's "1" mean? Why not something meaningful,
like "TRUE"?
*****
{*****
memset(buff,0,512);
Useless and nonsensical. Why are you clearing out only half the buffer? Why are you not
use ::ZeroMemory? Why do you feel you have to clear it out at all?
*****
j = ReadFile(hCom,buff,512,&i,NULL);****
Have you ever heard of #define? What's with these nonsensical hardwired values like 1024
and 512 doing in a piece of code?
****
GreatBuffer += buff;****
Why do you think your CString is 8-bit characters, really? Why not something like
if(i == 0)
continue;
GreatBuffer += CString(buff, i);
*****
if(i<512)****
break;
Why 512? Why not 1024? Why not 729? Why break here? This requires you receive at least
512 characters? Why do you think this is going to happen? Communications channels can
drop characters, for example.
Doing serial communication reliably involves things like worrying about timeouts, never
working with fixed-size limits, and writing REALLY robust code. Note also that this would
block the main GUI thread while the data was being read, always a Really Bad Idea on a
device with unbounded blocking time.
****
}Joseph M. Newcomer [MVP]
MessageBox(GreatBuffer,"GreatBuffer");
delete [] buff;
buff=NULL;
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
.
- Follow-Ups:
- Re: about serial port communication and new operator
- From: harshalshete@xxxxxxxxx
- Re: about serial port communication and new operator
- Prev by Date: Re: about new operator
- Next by Date: Bug in COM Interop Sample?
- Previous by thread: Re: about new operator
- Next by thread: Re: about serial port communication and new operator
- Index(es):
Relevant Pages
|