Some bytes are lost, not the whole data block missed. Bytes seem to be missed completely, not received incorrectly.
A complete operation involves transfer of data between PC and PIC: the PC initiates all transactions and expects an ASCII reply to each block of data it sends. The ASCII blocks end with a newline. The blocks start with a message number and a space. Thousands of characters transfer satisfactorily, then (for no apparent reason) the end of a message is not received. The receive routine waits for a newline and times out if it does not get one.
Here is the code for the receive routine. As you can see, I've added diagnostic bits to try to find out what is happening.
When a timeout occurs, GetComStat() returns 0000, GetComLineStatus() returns 0x30 and ReturnRS232Err() returns 0.
static CHAR Rxstr[MAXCOLS+1], pos;
int RxMssg (void)
{
int ich, error;
double starttime, time;
SetMouseCursor (VAL_HOUR_GLASS_CURSOR);
starttime = Timer ();
while (TRUE)
{
while (GetInQLen(Port) >= 1)
{
ich = ComRdByte (Port);
if (ich == '\n')
{
if (pos < 2)
{
strcpy (MssgStr, "Received ");
if (pos)
sprintf (MssgStr+strlen(MssgStr), "%02xh ", *Rxstr);
sprintf (MssgStr+strlen(MssgStr), "%02xh ", ich);
SetMouseCursor (VAL_DEFAULT_CURSOR);
return NEGERROR;
}
MssgNo = *Rxstr - '0';
if ((MssgNo >= 0) && (MssgNo < PROG_NMSSGS))
{
*(Rxstr+pos) = '\0';
strcpy (MssgStr, Rxstr+2);
pos = 0;
SetMouseCursor (VAL_DEFAULT_CURSOR);
return OK;
}
else
pos=0;
}
else if ((ich > 0x1f) && (ich < 0x80))
{
*(Rxstr+pos) = ich;
if (pos < MAXCOLS)
pos++;
}
}
time = Timer ();
if ((time - starttime) > 5)
{
*(Rxstr+pos) = '\0';
strcpy (MssgStr, Rxstr);
pos = 0;
// 0001 Input character were lost because the input queue filled.
// 0002 Internal input queue error.
// 0010 Parity error detected.
// 0020 Overrun error detected.
// 0040 Framing error detected.
// 0080 Break signal was detected.
// 1000 The remote system has sent an XOFF character.
// If XON/XOFF mode has been enabled, characters will not be moved
// from the output queue to the RS-232 port until XON is received from the remote system.
// 4000 An XOFF character was sent to the remote system.
error = GetComStat (Port);
sprintf (MssgStr+strlen(MssgStr), "\nComm Status = %04xh", error);
//kRS_CTS_ON 0x10 The CTS (clear-to-send) signal is on.
//kRS_DSR_ON 0x20 The DSR (data-set-ready) signal is on.
//kRS_RING_ON 0x40 The ring indicator signal is on.
//kRS_RLSD_ON 0x80 The RLSD (receive-line-signal-detect) signal is on.
error = GetComLineStatus (Port);
sprintf (MssgStr+strlen(MssgStr), "\nLine Status = %04xh", error);
error = ReturnRS232Err ();
if (error)
sprintf (MssgStr+strlen(MssgStr), "\nRS232 error %d: %s", error, GetRS232ErrorString (error));
SetMouseCursor (VAL_DEFAULT_CURSOR);
return NEGERROR;
}
}
}