03-24-2006 09:43 AM
Hi everybody!!!!
I need support to solve a thread/TCP/user-interface communication problem. After schedule a thread pool function I establish TCP communication with 8 units. Then I start data acquisition from the units. The problem occurs when I want to stop the process and start again using the user-interface. After, I am getting corrupted data and I cannot reestablish communication with the units. I try to discard the thread and reinitialize the TCP comm. every time I stop the data acquisition but I did not have any success. I have to restart the program to set up a reliable TCP communication. Do you have any hint to overcome this problem? I appreciated your help. I am using LabWindows/CVI 6.0 version.
Thanks.
03-28-2006 07:22 AM
03-28-2006 09:30 AM
Hi Wendy,
Thanks to reply. I am trying a different direction now. I am using the ProcessSystemEvents() function to able to interact with the user interface. I added this function throughout the application and it seems more stable but I still have some problems with reinitializing TCP communication. After establish TCP communication with the units, using ConnectToTCPServer function I start data acquisition. The problem occurs when I want to stop and restart the process using the user-interface. When I stop the process I disconnect every TCP connections (DisconnectFromTCPServer())and on my start I will reestablish connection again using the same technique I used at program startup. The program stops at the second unit TCP connection initialization. The ClientTCPRead() status value return -12 (kTCP_NoConnectionEstablished) on the CVICALLBACK ClientTCPCB() function. I checked if there were problems on DisconnectFromTCPServer() but I do not get any error value from this fuction.
Thanks again Wendy. I appreciated you support.
Ben Mignano
03-28-2006 10:32 AM
Hi,
On the ConnectToTCPServer() function the conversation handle gets a different value from the previous initialization. Ex. I have three units connected. The conversation handle value for the first unit is 0 at the first initialization. After reinitialize the TCP the same first unit gets the value 3 for the conversation handle. Should it get 0 again?
Ben
04-04-2006 01:38 PM
Hi Ben,
I have a few questions for you. How do you have the your TCP worker thread setup? When I use a TCP worker thread arrangement, I like to use a thread function to get things going, and then a thread callback to clean things up. It helps to tell the thread scheduling function to call the callback in the same thread that executes the thread function. I also like to leave my thread function in a state that allows me to shut it down without using thread terminate functions. This is handled by my simple wait loop. I reset the stop_tcp variable (= 0) immediately before I schedule the thread, and then the gui thread can set stop_tcp (= 1) when it wants the TCP thread to shutdown gracefully. You must not forget to reset it when you get ready to restart the TCP worker thread. This wait look may draw some flak from others on the board, but it works fine. Here is a rough example of what works for me:
//TCP worker thread function
thrf_tcp(...) {
//preparation stuff goes here
//all listen or connect functions go here
// ...will be using tcpx_client(...) for the callback
//wait loop
while (!stop_tcp) {
ProcessSystemEvents( );
Sleep(100); //<--Don't use Delay( )
}
return 0;
}
//TCP callback
tcpx_client(...) {
switch(xType) {
case TCP_DISCONNECT:
//...whatever
break;
case TCP_DATAREADY:
//...whatever
break;
}
return 0;
}
//TCP worker thread callback
thrx_tcp(...) {
//disconnect all remaining TCP connections here
//free memory allocated in preparation are of thrf_tcp( )
return;
}
The next question is why do you need to sprinkle ProcessSystemEvents() all over your program? There are situations where ProcessSystemEvents() is basically required, but if the stability of the program is improved by randomly sprinkling them around, then the code design isn't very solid yet. I would be worried that if you move to faster or slower hardware that the program might become more or less stable than your development environment.
My final question is related to the fact that you are using your external DAQ devices as TCP servers. Is it possible that your client program's shutdown literally gives the DAQ devices ample time to detect that the TCP connections are all dropped. If that is the case, simply start a timer after the disconnect or TCP thread stop button and don't allow the TCP worker thread to reconnect until a few seconds of idle time passes.
Orlan