LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with rs-232 comunication

Hi.

I use for my aplication a comunication rs-232 whith a microcontroler.
At one moment I must receve from microcontroler datas in a large quatites(8000 bytes) and some bytes are lost. I divided this information in
packeges of 100 bytes but even so I lose data(10 bytes). The data loses apairs randomly and I have no idea why this hapens.

For receiving the data I use these functions:

InstallComCallback (comport, LWRS_RXCHAR, 0, (int)EventChar[0],SerialaCallback, 0);


void CVICALLBACK SerialaCallback (int portNo,int eventMask, void *callbackData)
{
if(strLen!=0)
{
strLen = GetInQLen (comport);
status=ComRd (comport, readBuf,strLen);
status=FlushInQ (comport);

switch(strLen)
{
case 8: Fmt (Str
ing, "%s<%1x[b1w2p0]",readBuf);
SetCtrlVal (autopont, AutoPont_TBOX_RECEPTION, String);
break;
}
}
}

Thanks.
0 Kudos
Message 1 of 4
(3,479 Views)
Don't call FlushInQ after calling ComRd. ComRd removes the bytes it read from the input queue. You don't need to and don't want to call FlushInQ to remove the data you just read. Flushing can throw away data you want to read the next time. Typically, you would call FlushInQ after initializing the port, or on an error, etc., but not while you're in a loop reading data.
In your callback, you're checking strLen before you set it. Maybe you just showed us a small chunk of your code and the part that sets strLen got left out, but make sure.
Take a look at the sample projects in ..\cvi\samples\rs232.
Message 2 of 4
(3,479 Views)
Another thing: using case statements on strLen can be risky. If you are getting large amounts of data at a time and if Windows is busy doing something else, you may miss the case where strLen=8. It is better to check if (strLen >= 8), then process the first 8 characters.
0 Kudos
Message 3 of 4
(3,479 Views)
I agree with Al S. Also, make sure when you open comport your input queue is large enough to hold data.
Also once you get call back try to wait few hundered milisecs and then read this will allow you to get all data which are available to read.

Sheetal
Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 4 of 4
(3,479 Views)