LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ConnectToTCPServer callback in different .c file not working.

Hello,

I have problem with ConnectToTCPServer in my program. Basically i have files scanner.c and scanner.h where ConnectToTCPServer is covered as below:

 

int EstablishScannerConnection(int scannerHandle, char *ip, int port)  {

int status=0;

strcpy (scannerObj[scannerHandle].ip, ip);     // kopiuj ip

scannerObj[scannerHandle].port = port;        // kopiuj port

scannerObj[scannerHandle].connect = 0;

status = ConnectToTCPServer(&scannerObj[scannerHandle].handle, scannerObj[scannerHandle].port, scannerObj[scannerHandle].ip, TCPCallback, 0, 2000);

 

if(status < 0) {

scannerObj[scannerHandle].errorMsg = GetTCPErrorString(status); scannerObj[scannerHandle].error = status;

if(scannerObj[scannerHandle].errorCallback) scannerObj[scannerHandle].errorCallback();

else

ErrorMSG( scannerHandle ); } else scannerObj[scannerHandle].connect = 1;

return scannerObj[scannerHandle].connect;

}

 

also in this file i have got TCPcallback:

 

 

int CVICALLBACK TCPCallback (unsigned handle, int event, int error,  void *callbackData) {
int status, i;

// szukaj numeru uchwytu
for (i=0; i< MAX_SCANNERS; i++) {
if(handle == scannerObj[i].handle) break;
}

switch (event) {
case TCP_DATAREADY:

scannerObj[i].dataLen = 0;
scannerObj[i].errorMsg = 0;

status = ClientTCPRead (handle, scannerObj[i].data , sizeof(scannerObj[i].data), 1000);

if ( status < 0) scannerObj[i].errorMsg = GetTCPErrorString(status);
else scannerObj[i].dataLen = strlen(scannerObj[i].data);

//scannerGetDataCB(i);
if(scannerObj[i].getDataCallback) scannerObj[i].getDataCallback(handle);
break;


case TCP_DISCONNECT:

CloseScannerConnection(i);
char outputMsg[1024]="0";
Fmt (outputMsg, "Scanner#%d\n\nIP: %d:%d:%d:%d\n\n%s\n\nDisconnected ", i, scannerObj[i].ip[0], scannerObj[i].ip[1], scannerObj[i].ip[2], scannerObj[i].ip[3]);
MessagePopup("Scanner", outputMsg);
if(scannerObj[i].closeConnCallback) scannerObj[i].closeConnCallback();
break;
}

return 0;
}

 

In main file I have:

 

#include "scanner.h"  

 

int main (int argc, char *argv[]) {

...

if(EstablishScannerConnection(SCANNER_OP130, SCANNER_OP130_IP, SCANNER_OP130_PORT))  {
   SetCtrlVal (panelHandle, PANEL_LED_SCANNER, 1);
} else {
   SetCtrlVal (panelHandle, PANEL_LED_SCANNER, 0);
}

}

 

My problem is that I have got connection with TCPServer by my TCPCallback is not working - i do not have callback TCP_DATAREADY or any different when data is ready. When I put ConnectToTCPServer as below into main it is working (I have callback for TCP_DATAREADY, but  TCP_DISCONNECT is not working😞

 

int main (int argc, char *argv[]) {

...

if(ConnectToTCPServer(&scanerHandle, scannerObj[scanerHandle].port, scannerObj[scanerHandle].ip, TCPCallback, 0, 2000)==0) {
   SetCtrlVal (panelHandle, PANEL_LED_SCANNER, 1);
} else {
   SetCtrlVal (panelHandle, PANEL_LED_SCANNER, 0);
}

}

 

I am using one thread, and ProcessSystemEvents(); in that thread:

CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadMain, NULL, &threadMainHandle);

 

I will be appreciable to give me direction how to fix it.

 

 

 

 

0 Kudos
Message 1 of 9
(3,922 Views)

Hi,

 

I have been recently working with TCP communication in CVI and was able to get it working (multi client tcp server). But i was creating TCPserver in the CVI program. TCP clients should also work similarly. Generally from what i have understood, you have to poll for events in the same thread in which the TCP connection was established in order to capture the TCP events for the client/sever. Since you have already mentioned that you only have a single thread, that should not be the problem. If you could share your main.c, scanner.c and scanner.h files, i will try to figure out the problem.

 

Best Regards

Deepu

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 2 of 9
(3,897 Views)

It seems to me that you are running in a multithreaded environment spawning a separate thread with your call to CmtScheduleThreadPoolFunction. In this scenario, you must be sure that all communications functions are executed in the same thread, otherwise they won't work. Since you mention that moving the connect function to the main makes the program work, you probably have TCP functions spreaded across multiple threads in the program. Try moving all functions to one single thread, either the main one or the spawned one.

When multithreading TCP communications, you may have problems when you need to send some message originated from the other thread (i.e. following some interactive user action): in this case saving the thread handle and using PostDeferredCallToThread function may be of help.

 

In every case, creating a reduced, single-threaded program to test communications may help you in trimming the functions before moving them to the final application.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 9
(3,889 Views)

Hi,

 

thank you for replay.

 

Please find files in attachment.

 

Additionally I wrote simple program "test tcpServer.zip" to test my scanner file. It is working using "EstablishScannerConnection(0, "192.168.0.16", 23);" in main file.   

 

BR

M

Download All
0 Kudos
Message 4 of 9
(3,886 Views)

Hi,

 

I just had a look at your code files. You have declared the 'TCPCallback' function in the scanner.h as well as in scanner.c. It should only be declared once and that should be in the scanner.h file. you can delete the line 49 from the scanner.c file and try again.

 

int CVICALLBACK TCPCallback (unsigned handle, int event, int error, void *callbackData);   

Another thing is that you don't need to 'ProcessSystemEvents()' in the ThreadMain because this is already done by the RunUserInterface function in the main function. So you should remove processing events in ThreadMain.

As Roberto pointed out, the TCP callback will be processed by the main function where the connections are established. So the processing of events should happen in the main thread. This is being implicitly done by RunUserInterface.

 

I hope it works.

 

BR

Deepu

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 5 of 9
(3,880 Views)

Just to clarify the threading argument, your program has actually two threads: the main thread, which processes events in RunIserInterface as Deepu pointed out, and a second, independent thread which actually does nothing. In this respect, calling that second thread MainThread is misleading and may ingenerate confusion.

Additionally, you don't need that thread to run your application, which can be compiled and executed even commenting it out. This is important in order to reduce unnecessary complexity. (I see that you actually using the threaded function in your OC130 source file, but you should keep all UI operations in the main thread instead, leaving secondary threads for other, non UI-related activities)



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 9
(3,874 Views)

I have delete line 49 in scanner.c and even comment

// CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadMain, NULL, &threadMainHandle);  

and using 

EstablishScannerConnection(SCANNER_OP130, SCANNER_OP130_IP, SCANNER_OP130_PORT)

from my scanner.c file dose not get callback from TCP.

 

In program I am using 3 more threads depending what I am doing in software. Basically the threads are taking care of machine states for each program functionality. 

I do not have problems with TCP callback when I am using in main.c connection with TCP server:

ConnectToTCPServer(&scanerHandle, scannerObj[scanerHandle].port, scannerObj[scanerHandle].ip, TCPCallback, 0, 2000);

 

I have written  "test tcpServer" to test my scanner file (attached before) it is working. I am just wondering why one project is working and second is not working.

 

 

 

 

0 Kudos
Message 7 of 9
(3,864 Views)

are you processing events in any of those additional threads?

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 8 of 9
(3,848 Views)

In each thread I have:

while(condition_state != EXIT)  {
ProcessSystemEvents();

....

}

I am using only one extra thread at one time.

 

0 Kudos
Message 9 of 9
(3,844 Views)