about serial port communication and new operator
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.
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;
}
// 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;
}
////for reading from port
char * buff = new char[1024];
int j=0;
DWORD i;
CString GreatBuffer;
while(1)
{
memset(buff,0,512);
j = ReadFile(hCom,buff,512,&i,NULL);
GreatBuffer += buff;
if(i<512)
break;
}
MessageBox(GreatBuffer,"GreatBuffer");
delete [] buff;
buff=NULL;