So, I'm starting to have a problem with writing serial communication circuits, and it seems to be related to the timing. My embedded platform is the Rabbit Semiconductor BL2600, in which case it is a RS232 device. It sends a command to the device, and the device sends a response back to the BL2600, which is then processed.
My problem is that when I send a command and then wait for a response, I do not receive it. However, if I set a breakpoint at the right points and one step through the code, I often get an answer. I put my computer between the BL2600 and the device for listening to the RS232 stream (after watching the initial problem), and the response is sent regardless of whether I remain a breakpoint or not, but the BL2600 only sees it in its buffers if I stop immediately before that part, where I parse the code and try to find its start bit. If I stop when I just read the entire line after that, it will not find it.
So, it sounds like I’m not waiting enough, so now it’s just funny, I set the timeouts for checking the buffer to 1 second (and with a baud rate of 38400, it’s better to show in this window), and yet I NEVER get nothing but breakpoints and one-step.
The following is a significant portion of my code:
serCwrFlush();
while(serCwrUsed())
{
;
}
startwait = MS_TIMER;
while((serCrdUsed() > 1) && (device_timeout_check < 1000))
{
if (MS_TIMER < startwait)
{
device_timeout_check = MS_TIMER + (ULONG_MAX - startwait);
}
else
{
device_timeout_check = MS_TIMER - startwait;
}
serCrdFlush();
}
serCputs("mpcal=d\r");
while(serCwrUsed())
{
;
}
startwait = MS_TIMER;
while((serCrdUsed() < 11) && (device_timeout_check < 1000))
{
if (MS_TIMER < startwait)
{
device_timeout_check = MS_TIMER + (ULONG_MAX - startwait);
}
else
{
device_timeout_check = MS_TIMER - startwait;
}
}
temp=serCpeek();
i=0;
while((temp != 'H' ) && (i<100))
{
serCgetc();
temp=serCpeek();
i++;
}
c=serCread(comp_cal_string,20, 20);
I have a feeling that I’m reinventing the wheel and that someone probably did it in front of me, at least on another platform, so that it lingers long enough for the data to be received, but fast enough for it to really catch data.