Hi to all:
i am trying to use TCP/IP to connect with ARM , which connection between server(PXI tester) and client(ARM) has been confirmed using ARM vendor program,it is OK!
so i want use LabWindows program to complete this function, but i confront some problems. the errcode is -12 that means no connection established, I list all my test program for you,please help me trap the problem (there is no *.UIR, USING CVI functionn directly).
I dont know how to get the right conversationHandle, when i invoke TCP_server_send() and the program breaks in ServerTCPWrite (), TCP_server_init() is OK
could you tell me how to resolve this problem.
THANK you very much
tcp_main.h:
int TCP_server_init(void) ;
int TCP_servr_terminated(void);
int TCP_server_send(void);
int TCP_server_receiver(void);
typedef struct
{
int x;
double y;
int z;
char c;
}XYZ ;
//=======================================
tcp_main.c:
#include <userint.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <tcpsupp.h>
#include "tcp_main.h"
#define SERVERPORT (2000)
static unsigned int conversationHandle;
int serverCallBack (unsigned handle, int event, int errCode, void *callbackData);
int main (int argc, char *argv[])
{
int iRtn = 0;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
iRtn = TCP_server_init() ;
iRtn = TCP_server_send();
iRtn = TCP_server_receiver();
iRtn = TCP_servr_terminated();
return 0;
}
//================================================
int TCP_server_init(void)
{
int iRtn = 0;
char buff[256] ={'0'};
iRtn = RegisterTCPServer (SERVERPORT, serverCallBack, 0);
ProcessTCPEvents();
if(iRtn < 0)
{
MessagePopup("TCP Server", "RegisterTCPServer FAIL!") ;
}
else
{
GetTCPHostAddr (buff, 256);
GetTCPHostName (buff, 256);
}
// ProcessSystemEvents();
// ProcessTCPEvents();
return 0;
}
//===================================================
int TCP_servr_terminated(void)
{
if (conversationHandle)
{
DisconnectTCPClient (conversationHandle);
}
conversationHandle = 0;
UnregisterTCPServer (SERVERPORT);
return 0;
}
//=================================================
int TCP_server_send(void)
{
char *err = NULL;
int iLength = 0;
XYZ xyz;
xyz.x = 1;
xyz.y =10.9;
xyz.z = 5;
xyz.c = 'c';
iLength = sizeof(xyz) ;
// ProcessTCPEvents();
if(ServerTCPWrite (conversationHandle,(char*)&xyz,iLength , 0)< 0)
{
MessagePopup("TCP server","TCP write error");
}
return 0;
}
//=================================================
int TCP_server_receiver(void)
{
int iRtn;
//char sbuffer[1000];
XYZ xyzRec;
iRtn = ServerTCPRead (conversationHandle, &xyzRec, sizeof(xyzRec), 0);
return iRtn;
}
//=======================================
int serverCallBack (unsigned handle, int event, int errCode, void *callbackData)
{
char recbuf[512];
char addBuf[31];
int readSize = 0;
char nameBuf[50];
int iRtn;
switch (event)
{
// ProcessTCPEvents();
case TCP_CONNECT:
conversationHandle = handle;
iRtn = GetTCPPeerAddr (conversationHandle, addBuf, 31);
// GetTCPPeerName (conversationHandle, nameBuf, 50);
SetTCPDisconnectMode (conversationHandle, TCP_DISCONNECT_AUTO);
break;
case TCP_DISCONNECT:
if (handle == conversationHandle)
{
conversationHandle = 0;
}
break;
case TCP_DATAREADY:
if((readSize = ServerTCPRead (conversationHandle, recbuf, 512, 0)) < 0)
{
MessagePopup("TCP server","TCP read ERR");
}
break;
}
ProcessTCPEvents();
return 0;
}