LabWindows中TCPip函数分析与实例

更新时间:2024-01-18 16:22:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

LabWindows CVI TCP程序说明

1 本系统框图如下:

服务器sever 路由交换机 终端Client 2 TCP函数说明

2.1 Client Functions客户端

ClientTCPRead (unsigned int conversationHandle, void *dataBuffer, size_t dataSize, unsigned int timeOut);

参量1:操作柄、参量2:读入数据、参量3:读入数据长度、参量4:溢出时间。 Return:读出数据的字节长度(可用来校验数据)。

ClientTCPWrite (unsigned int conversationHandle, void *dataPointer, size_t dataSize, unsigned int timeOut);

参量1:操作柄、参量2:输出数据、参量3:读入数据长度、参量4:溢出时间。 Return:写入数据的字节长度(可用来校验数据)。

ConnectToTCPServer (unsigned int **conversationHandle, unsigned int portNumber, char serverHostName[], tcpFuncPtr callbackFunction, void *callbackData, unsigned int timeOut);

参量1:操作柄、参量2:当前端口、参量3:服务器地址、参量4:回调函数、参量5:溢出时间

Return:写入数据的字节长度(可用来校验数据)。

DisconnectFromTCPServer (unsigned int conversationHandle); 参量:操作柄

2.2 Sever Functions服务器端

DisconnectTCPClient (unsigned int conversationHandle); 参量:操作柄

RegisterTCPServer (unsigned int portNumber, tcpFuncPtr callbackFunction, void *callbackData);

参量1:当前端口、参量2:回调函数、参量3:返回数据(NULL)

ServerTCPRead (unsigned int conversationHandle, void *dataBuffer, size_t dataSize, unsigned int timeOut);

参量1:操作柄、参量2:读入数据、参量3:读入数据长度、参量4:溢出时间。 Return:读出数据的字节长度(可用来校验数据)。

ServerTCPWrite (unsigned int conversationHandle, void *dataPointer, size_t dataSize, unsigned int timeOut);

参量1:操作柄、参量2:输出数据、参量3:读入数据长度、参量4:溢出时间。 Return:写入数据的字节长度(可用来校验数据)。

UnregisterTCPServer (unsigned int portNumber);

UnregisterTCPServerEx (unsigned int portNumber, const char localhostAddress[]);

2.3 Support Functions 支持函数

GetAllTCPHostAddresses (char ****addresses, int **numberOfAddresses); 参量1:network interface address、参量2:number of address

GetHostTCPSocketHandle (unsigned int connectionHandle, intptr_t **socketHandle); 获取当前操作柄

GetTCPHostAddr (char buffer[], size_t bufferSize); 获取本机的IP Address

GetTCPHostConnectionAddr (unsigned int connectionHandle, char buffer[], size_t bufferSize);

参量1:操作柄、参量2:输出地址、参量3:数据长度。

GetTCPHostName (char buffer[], size_t bufferSize); 获取主机(本机)用户名。

GetTCPPeerAddr (unsigned int connectionHandle, char buffer[], size_t bufferSize); 参量1:操作柄、参量2:输出地址、参量3:数据长度。 获取对方的IP地址

GetTCPPeerName (unsigned int connectionHandle, char buffer[], size_t bufferSize); 参量1:操作柄、参量2:输出地址、参量3:数据长度。 获取对方的用户名

SetTCPDisconnectMode (unsigned int conversationHandle, int disconnectMode); 参量1:操作柄、参量2:模式选择。

2.4 Other 其它函数

*GetTCPErrorString (int errorNumber); 根据当前错误代码,调出当前错误类型。

char *GetTCPSystemErrorString (void); 获取当前错误代码。

int ProcessTCPEvents (void); 获取当前TCP处理事件。

TCPFreeMemory (void *pointerToMemory); 释放内存。

3 例程说明

3.1 服务器sever

/**************************************************************************** >>>function :TcpConnect连接 TcpDisconnect断开 >>>intput >>>output >>>

*****************************************************************************/ int CVICALLBACK TcpConnect (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {

int status=0; char tcp_Port[10]; switch (event) {

case EVENT_COMMIT: GetCtrlVal (panelHandle, PANEL_STRING_SERVER_PORT, tcp_Port);// 获得

sscanf(tcp_Port, \

status = RegisterTCPServer (Tcpip.tcp_Port, TcpServerCall, 0); if(!status){ //注册成功 得到主机信息

Tcpip.tcp_Using=1; SetCtrlAttribute

(panelHandle,

PANEL_BUTTON_CONNECT, PANEL_BUTTON_DISCONNECT,

:N/A

:Tcpip.tcp_Port(600) TCP端口

TCP服务器端口号

ATTR_DIMMED, 1); SetCtrlAttribute (panelHandle, ATTR_DIMMED, 0); GetHostInfo();//获取主机信息 }

}

} break;

return 0;

/**************************************************************************** >>>function :TcpServerCall TCP接收数据处理部分(主机) >>>intput

>>>output : >>>

*****************************************************************************/

:Tcpip.client_Handle操作柄

int CVICALLBACK TcpServerCall(unsigned handle, int event, int error, void *callbackData) {

int i;

char client_Ip[50]; char rev_Data[200]; char Rev_Length[50]; switch (event)

{

case TCP_CONNECT:

Tcpip.client_Handle = handle;

// 获得

客户端句柄,赋给全局变量 GetTCPPeerAddr (Tcpip.client_Handle, client_Ip, 50); 地址

// 获得客户端 IP

SetCtrlVal (panelHandle, PANEL_STRING_CLIENT_IP, client_Ip); Client led亮为绿色

VAL_GREEN); led亮

SetCtrlVal (panelHandle, PANEL_LED_CLIENT, 1);

// Client

//

//

SetCtrlAttribute (panelHandle, PANEL_LED_CLIENT, ATTR_ON_COLOR,

设置 TCP 断开方式为自动

break;

SetTCPDisconnectMode (Tcpip.client_Handle, TCP_DISCONNECT_AUTO);

case TCP_DATAREADY: memset (rev_Data, '\\0', 200); 清0

// 数组

Tcpip.tcp_Rev_Length = ServerTCPRead (Tcpip.client_Handle, rev_Data, 200, 1000);// 从TCP服务器读数据

//de_protocol(data, read_len, ret_data, &ret_len); for(i=0;i

Tcpip.tcp_Rev_Data[i]=rev_Data[i];

// 协议解析

//取出数据

}

SetCtrlVal (panelHandle, PANEL_TEXTBOX_TCP_ASCII, \接收到数据:\SetCtrlVal (panelHandle, PANEL_TEXTBOX_TCP_ASCII, rev_Data); SetCtrlVal (panelHandle, PANEL_TEXTBOX_TCP_ASCII, \SetCtrlVal

(panelHandle,

PANEL_TEXTBOX_TCP_ASCII,

\

SetCtrlVal (panelHandle, PANEL_TEXTBOX_TCP_ASCII, \ sprintf(Rev_Length, \from TCP Client: %d

Bytes\

Tcpip.tcp_Rev_Length);

SetCtrlVal (panelHandle, PANEL_TEXTBOX_TCP_ASCII, Rev_Length); 显示接收数据长度 break;

case TCP_DISCONNECT:

if(handle==Tcpip.client_Handle){ // 因为断开方式被设置为自动,所以这里不需要调用DisconnectTCPClient Tcpip.client_Handle = 0; 时不需要再断了 led灭 IP 栏

// Disconnect

// Client // 清空

//

SetCtrlVal (panelHandle, PANEL_LED_CLIENT, 0);

SetCtrlVal (panelHandle, PANEL_STRING_CLIENT_IP, \

}

break; } return 0; }

注:发送数据、获取信息都是一些很简单的调用,不再介绍。

3.2 客户端Client

int CVICALLBACK connect (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {

char sever_Addr[20]; char sever_Name[20]; char sever_Port[10];

char message_Buffer[255]; int status=0; switch (event) {

case EVENT_COMMIT:

GetCtrlVal (panelHandle, PANEL_STRING_ADDRESS, sever_Addr); GetCtrlVal (panelHandle, PANEL_STRING_PORTNUMBER, sever_Port); sscanf(sever_Port, \

DisableBreakOnLibraryErrors ();

Tcpip.tcp_Port,

status=ConnectToTCPServer (&Tcpip.client_Handle, sever_Addr, tcpCallback, 0, 0);

EnableBreakOnLibraryErrors ();

if(!status){ Tcpip.tcp_Using=1;//链接标识位

GetTCPPeerName (Tcpip.client_Handle, sever_Name, 20); SetCtrlVal (panelHandle, PANEL_STRING_SERVERIP, sever_Addr); SetCtrlVal (panelHandle, PANEL_STRING_SERVERNAME,

(panelHandle,

PANEL_LED_SERVER,

sever_Name);

SetCtrlAttribute

ATTR_ON_COLOR, VAL_RED);

SetCtrlAttribute

SetCtrlVal (panelHandle, PANEL_LED_SERVER, 1);

(panelHandle, (panelHandle,

PANEL_CMD_CONNECT, PANEL_CMD_DISCONNECT,

ATTR_DIMMED, 1);

SetCtrlAttribute ATTR_DIMMED, 0); } else{//超时溢出 message: %s\

}

sprintf(message_Buffer, \library error message: %s\\nSystem error

GetTCPErrorString (status), GetTCPSystemErrorString()); MessagePopup (\

} break; }

return 0;

/************************************************************************** >>>function :tcpCallback tcp连接回调函数 >>>input :handle操作柄

>>>ouput:DATA传送到textbox上

***************************************************************************/

int tcpCallback(unsigned handle, int xType, int errCode, void *callbackData) {

char data[256]; char Rev_Length[50]; int tcp_Rev_Length;

switch(xType){ case TCP_DISCONNECT: SetCtrlVal (panelHandle, PANEL_LED_SERVER, 0);

SetCtrlVal (panelHandle, PANEL_STRING_SERVERIP, \

SetCtrlVal (panelHandle, PANEL_STRING_SERVERNAME, \ break;

case TCP_DATAREADY:

ClientTCPRead (handle, data, 200, 1); data[strlen(data)+1]='\\0';

SetCtrlVal (panelHandle, PANEL_TEXTBOX, data); SetCtrlVal (panelHandle, PANEL_TEXTBOX, \

sprintf(Rev_Length, \ %d Bytes\

//

SetCtrlVal (panelHandle, PANEL_TEXTBOX, Rev_Length);

显示接收数据长度

}

SetCtrlVal (panelHandle, PANEL_TEXTBOX, \

break;

}

return 0;

本文来源:https://www.bwwdw.com/article/ov5o.html

Top