12-23-2005 08:07 AM
12-23-2005 09:26 AM
12-23-2005 10:02 AM
12-23-2005 10:02 AM
12-23-2005 10:02 AM
12-23-2005 10:06 AM
12-28-2005 02:28 AM
Hi, k1_ke. I am sorry for this late response: it's Christmas time here and I was not in the office in the previous days.
What do yuo mean with "I am still loosing data from the serial"?
Looking at your read_data function I noticed that after deciding how may characters and which termination character to wait for it enters an endless loop that exits only for a certain character in second position in the received message. You are not checking inside the loop if an error or timeot has occurred, so you may be out of sybc with your transmission without knowing it.
I would suggest you to test bytes_read value immediately after ComRdTerm and discriminate these situations:
- bytes_read < 0 => an error has occurred
- bytes_read < read_cnt => timeout during read
If you communication times out you may need to alter the timeout time with SetComTime.
Second item, as you can see in the documentation for ComReadTerm,
"If the termination byte equals a carriage return and the character immediately following the carriage return is a linefeed, then both the carriage return and linefeed are discarded.
If the termination byte equals a linefeed and the character immediately following the linefeed is a carriage return, then both the linefeed and the carriage return are discarded."
So be sure that you don't have a pair of 0xC-0xA (or 0xA-0xC) bytes in your transmission otherwise ComRdTerm is discarding a character more than you expected from the buffer this way altering the syncronization between transmitter and receiver
I hope these notes can help you to solve your problems and whish you a very good and prosperous new year
Roberto
01-02-2006 08:10 AM
01-03-2006 04:06 AM - edited 01-03-2006 04:06 AM
K1_ke, since your messages end with a fixed termination character that is never used inside the message, you could try to install a callback on the com port instead of using and endless loop for managing the serial communication. Using InstallComCallback (1, LWRS_RXFLAG, 0, 10, ComCallback, 0); you can install a callback on the serial port that is fired every time on the buffer a linefeed is present on the serial port. When the callback is fired, read the data on the port and manage your message as in your ReadData function, without need for do loops inside. With this architecture your UIR is never stuck in loops and you can deinstall the callback to stop serial comm. Be careful to check input queue lenght after reading since in some case the callback can be fired even if there are no more characters in the queue (there is a notice in the function help on this). Also, checking queue lenght before ending the callback can be safe in case a new message is received before the callback has ended (I don't remember if in this case the callback is fired again or not).
Another way to manage your application is to structure it in two threads, the main one that manages the user interface and spawns a second thread when serial communication is needed. The second thread can be structured in an endless loop with a global variable that manages loop break. For a reference on multithreading take a look at MultithreadingOverview.pdf document that you can find in <cvidir>\bin directory and to all documents found in the online help on multithreading.
In my opinion the com callback approach is easier to run and can be satisfactory if your communication rate is not too high (say some messages per second).
Message Edited by Roberto Bozzolo on 01-03-2006 11:11 AM
01-03-2006 12:15 PM - edited 01-03-2006 12:15 PM
Hi Roberto,
Thanks for your reply. I implemented the InstallComCallBack, now it works fine....i am still doing some tests to check i am not missing any data from the serial port but i have noticed that now my quit button takes a while to react. Sometimes it quick to close, other times it takes long, it just freezes for abit n closes. I dont know why. I haven't touched the Quit callback.
This is what i did
int CVICALLBACK ReadCallBack (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
StopButtonFlag=0;
ResetTextBox (panelhandle, MAIN_TBOX_READ, "\0");
SetCtrlAttribute (panelhandle, MAIN_READ, ATTR_DIMMED, 1);
SetCtrlAttribute (panelhandle, MAIN_STOP, ATTR_DIMMED, 0);
SetCtrlAttribute (panelhandle, MAIN_CONFIG, ATTR_DIMMED, 1);
GetCtrlVal (panelhandle, MAIN_READ_COUNT, &read_cnt);
GetCtrlIndex (panelhandle, MAIN_READTERM, &read_term_index);
FlushInQ (comport);
switch (read_term_index)
{
case 0:
read_term = 0;
break;
case 1:
read_term = 13;
break;
case 2:
read_term = 10;
break;
}
InstallComCallback (1, LWRS_RXFLAG, 0, read_term, ComCallback, 0);
case EVENT_RIGHT_CLICK :
break;
}
return 0;
}
void ComCallback(int portNo, int eventMask, void *data)
{
if(!StopButtonFlag)
{
ReadData(); //reads data from Com Port
}
FlushInQ (comport);
}
Message Edited by k1_ke on 01-03-2006 01:16 PM