LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

thread CVI5.0 ComRd

Hi everybody,
 
I am new with CVI. It's fun, but I have a big problem that's why I'm here 😉 !
 
I have an EVA Board with a microcontroleur Siemens C167 which generates signals, and a GUI on my computer to configure the signals I want. THe 2 entities are linked by the RS232 serial port. The new protocol I'm trying to set for the synchronization of both is asynchronous. At the begining, my microcontroler send a byte to "ping" the computer. I send back an ack, and at this reception, the GUI set a variable. I would like to do a thread which listens the communication from the µC, because it's not the only frame the GUI has to receive.
 
It looks like (I am sorry I am using CVI 5.0 😞 , so threads are hard) :
 
static volatile int exiting = 0; //variable of end
static int received;
int main (int argc, char *argv[])
{
  static DWORD idThreadEcoute; //id of the listening thread
  static HANDLE hThread;
  [...]
 if (InitComPort ())    //Inits Com                                                      
       return 1;
      
       //creation of the thread
       hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadEcoute, NULL, 0,  &idThreadEcoute); 
 
       while(received!=TRAMECONTACTOK);
       received=TRAMEKO;
       RunUserInterface ();      
       return 0;
}
 
static int CVICALLBACK ThreadEcoute(DWORD dParameter)
{
 unsigned char toSend ;
 unsigned char Ack;
 
 while (!exiting)
 {
   if (ComRd (COMPORT, &Ack, 1) == 1)
   {
    switch (Ack)
    {
     case 0xAA :  // reception of a ping from the PSG LIght
      toSend= 0xC3;
      if (ComWrt (COMPORT, &toSend, 1)!=1)
       return 1;
      break;
      
     case 0xF1 :  // reception of an acknoledge for a first contact
      received=TRAMECONTACTOK;
      break;
 ....
}
}
}
}
 
It doesn't work, and i can't debug with CVI. I break at createThread and after on the while, and it loops on this one. Aftter, when I stop the debug, I have this nice message : "An asynchronous callback thread caused an exception. Program will be terminated."
What am i wrong ?
thanks a lot !
Julie
0 Kudos
Message 1 of 4
(3,763 Views)
Julie-

Your file-scope static variable "received" is initialized to 0 by the C runtime environment when the program is loaded.  You do not seem to be assigning a value ***prior to*** checking it with the while statement "while(received!=TRAMECONTACTOK);" in main().  So, unless TRAMECONTACTOK is defined to be zero, it would seem to me that your main would loop forever in this while statement in main().

What are the definiations of TRAMECONTACTOK and
TRAMEKO?

Bob
0 Kudos
Message 2 of 4
(3,756 Views)

Hi Bob,

Thanks for your answer.

TRAMEKO=0 and TRAMECONTACTOK=1. I've tried to initialize received to TRAMEKO but it is still not working ! But I've another question : what does the error message mean : "An asynchronous callback thread caused an exception. Program will be terminated." ? Ok, my thread wait for a communication, but i can do that ?

Julie

Message Edited by JulieF on 07-08-2005 02:17 AM

0 Kudos
Message 3 of 4
(3,754 Views)
Julie -

Some more thoughts:

If you initialize recieved to TRAMEKO, your main code will hang up at the "while (received!=TRAMECONTACTOK);" statement and loop forever, since the condition will always be true and you have no mechanism to cause it to ever be false.

Also, why do you use CVICALLBACK for ThreadEcoute()?  This function is not a CVICALLBACK function:  it is a thread function.  This may account for the error message you are receiving.  You should not have installed this function as a callback function on the com port, at least not the way you've written this code.  A CVI callback is "synchronous" but you have created a separate thread for querying the com port (asynchrounous to the main thread).

You could perhaps use a callback function to solve this problem.  By using InstallComCallback() you can have the com port cause a "synchronous" call to a callback function.    You can configure the conditions under which the callback is invoked, for example the arrival of a char in the input queue.  If you use a synchronous callback installed by InstallComCallback, you will not need to create a separate thread for the callback:  the callback will run on the main thread of your program. 

Let me know what you find out.

Bob


Message 4 of 4
(3,708 Views)