Developing Serial communication unitlity.
Can you please suggest me which is best way to receive the data on com port? I have created "Non -Overlapped" com Port.
Let me tell you, where I am. Created NON-OVERLAPPED port, set the baud rate to 38400 and STOP bit by SetCommState(). sending data is perfect. But while receiving it used to splits the bytes in first ReadFile() and captures in next ReadFile, which is definately not acceptable as we wanted response to have some sensible data for the command sent.
Here is pseudocode for Receive.
Code:
timeouts.ReadIntervalTimeout = 3;
timeouts.ReadTotalTimeoutMultiplier = 3;
timeouts.ReadTotalTimeoutConstant = 2;
timeouts.WriteTotalTimeoutMultiplier = 3;
timeouts.WriteTotalTimeoutConstant = 2;
if ( !SetCommTimeouts(hComm,&timeouts) )
printf("Error Setting Communication Timeouts\n");
while (1)
{
bResult = ReadFile(hComm,Recvbuffer,MAX_MESSAGE,&nbytes,NULL);
if ( 0 == bResult )
{
printf("Reading of serial communication has problem.");
return FALSE;
}
if ( nbytes )
{
printf("String = %s and length = %d\n",Recvbuffer,nbytes);
break;
}
}
Since above the command, I tried to implement code from Allen Denver in MSDN. But this seems to be not working. In the sense, control is not coming our of for loop. Could you please tell, what could be wrong?
Code:
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if ( !SetCommTimeouts(hComm,&timeouts) )
printf("Error Setting Communication Timeouts\n");
DWORD dwCommEvent;
DWORD dwRead;
char chRead;
if (!SetCommMask(hComm, EV_RXCHAR))
// Error setting communications event mask
char chRead,i=0;
for ( ; ; )
{
if ( WaitCommEvent(hComm, &dwCommEvent, NULL ) )
{
do
{
bResult = ReadFile(hComm, // handle of file to read
&chRead, // buffer
1, // number of bytes to read
&nbytes, // pointer to number of bytes read
NULL); // pointer to structure for data
if ( bResult )
{
Recvbuffer[i++] = chRead;
//printf("%c",chRead);
}
else
break;
} while (nbytes);
}
else
break;
}
Recvbuffer[i] = '\0';
printf("String = %s and length = %d\n",Recvbuffer,i);
I have dropped this option and increased the ReadTimeout value to 20 from 3 in my eralier code. That seems to be working, but want to know, why second section of code in notworking. Anybody faced similar problem and fixed later.
Thanks,