SECS/GEM通信

以下只列出主要代码

1.将Secs.dll、secs.h、Secs.lib导入工程中

2.定义服务器和客户端的对象句柄、相关结构体

HSECS secs_Client;
HSECS secs_Server;

SECS_CFG settings;

3.启动服务器(或开启客户端)

        memset(&settings,0,sizeof(settings));
        settings.t3 = 3000; // 45000;
	settings.t5 = 10000;
	settings.t6 = 5000;
	settings.t7 = 10000;
	settings.t8 = 5000;

	settings.pfn_on_connect = OnConnect;
	settings.pfn_on_recv_message = OnRecvMessage;
	settings.pfn_on_close = OnClose;

	settings.device_id = 0;
	settings.mode = HSMS_PASSIVE;
	settings.local_ip = inet_addr("127.0.0.1"); 
	settings.local_port = 5000;
	settings.remote_ip = inet_addr("127.0.0.1")
	settings.remote_port = 5000;

	secs_Server = CreateSecs(&settings);
	if (secs_Server == NULL) 
	{
		return FALSE;
	}
	else
	{
		if(settings.mode = HSMS_PASSIVE)
		{
			BOOL bStartServerOK = FALSE;
			bStartServerOK = SecsStartupServer(secs_Server);
			if (!bStartServerOK)
			{
				DestroySecs(secs_Server);
				return FALSE;
			}
		}
		else
		{
			BOOL bStartServerOK = FALSE;
			bStartServerOK =SecsConnect(secs_Server);
			if(!bStartServerOK)
			{	
				DestroySecs(secs_Server);
				return FALSE;
				}
		}
	}
4.三个响应消息(链接、数据接收、断开链接)
BOOL WINAPI OnConnect(HSECS hServer,HSECS hSecs,USHORT usDevId)
{
	secs_Client = hSecs;
	return TRUE;
}
DWORD WINAPI OnRecvMessage(HSECS session_handle, unsigned long id,unsigned char stream,unsigned char function,SECS_ITEM* ptr_item)
{
	unsigned long proc_result = do_process_msg(id, stream, function, ptr_item);
	return proc_result;
}

VOID WINAPI OnClose(HSECS hSecs)
{
	secs_Client = NULL;
}
5.消息处理

unsigned long do_process_msg( DWORD msgid, UCHAR stream,UCHAR function,SECS_ITEM* ptr_item)
{
	// normal process
	int ret_code = SECS_SYS_SUCCESS;
	switch (stream)
	{
	case 1: // ========= S1
		switch (function)
		{
		case 1:  ret_code = on_s1_f1(ptr_item, msgid);	break;	// S1,F1
		default: ret_code = SECS_SYS_ERROR_UFN;		    break;
		}
		break;

	case 2: // ========= S2
		switch(function)
		{
			// 		case 33: ret_code = on_s2_f33(ptr_item, msgid);	break;	// S2,F33
			// 		case 35: ret_code = on_s2_f35(ptr_item, msgid);	break;	// S2,F35
			// 		case 37: ret_code = on_s2_f37(ptr_item, msgid);	break;	// S2,F37
		case 41: ret_code = on_s2_f41(ptr_item, msgid);	break;	// S2,F41
		default: ret_code = SECS_SYS_ERROR_UFN;         break;
		}
		break;

	default:
		ret_code = SECS_SYS_ERROR_USN;
		break;
	}

	return ret_code;
}
int on_s1_f1(SECS_ITEM* ptr_item, DWORD msgid)
{
	// reply S1,F2
	SECS_ITEM* item_s1f2 = SecsCreateItemList(2);
	SECS_ITEM* item_mdln = SecsCreateItemAscii(EQ_MDLN);
	SECS_ITEM* item_softrev = SecsCreateItemAscii(EQ_SOFTREV);
	SecsListItemAppend(item_s1f2, item_mdln);
	SecsListItemAppend(item_s1f2, item_softrev);
	::SecsReplyMessageItem(g_secs_Client,1, 2,item_s1f2, 0, msgid);
	SecsDeleteItem(item_s1f2);
	return SECS_SYS_SUCCESS;
}
int on_s2_f41(SECS_ITEM* ptr_item, DWORD msgid)
{
	// reply S1,F2
	SECS_ITEM* item_s1f2 = SecsCreateItemList(2);
	SECS_ITEM* item_mdln = SecsCreateItemAscii(EQ_MDLN);
	SECS_ITEM* item_softrev = SecsCreateItemAscii(EQ_SOFTREV);
	SecsListItemAppend(item_s1f2, item_mdln);
	SecsListItemAppend(item_s1f2, item_softrev);
	::SecsReplyMessageItem(g_secs_Client,1, 2,item_s1f2, 0, msgid);
	SecsDeleteItem(item_s1f2);
	return SECS_SYS_SUCCESS;
}
6.发送消息
SECS_ITEM* ptr_item_First = SecsCreateItemList(2);
	SECS_ITEM* ptr_item_First_1 = SecsCreateItemList(1);
	SECS_ITEM* ptr_item_First_1_1 = SecsCreateItemList(2);
	SECS_ITEM* ptr_item_First_1_1_1 = SecsCreateItemAscii("BIBStrip_1234567890");
	SECS_ITEM* ptr_item_First_1_1_2 = SecsCreateItemList(1);
	SECS_ITEM* ptr_item_First_1_1_2_1 = SecsCreateItemList(2);
	SECS_ITEM* ptr_item_First_1_1_2_1_1 = SecsCreateItemAscii("MapData");
	SECS_ITEM* ptr_item_First_1_1_2_1_2 = SecsCreateItemAscii("");
	SECS_ITEM* ptr_item_First_2 = SecsCreateItemList(2);
	SECS_ITEM* ptr_item_First_2_1 = SecsCreateItemU1(0);
	SECS_ITEM* ptr_item_First_2_2 = SecsCreateItemList(0);

	SecsListItemAppend(ptr_item_First,ptr_item_First_1);

	SecsListItemAppend(ptr_item_First_1,ptr_item_First_1_1);
	SecsListItemAppend(ptr_item_First_1_1,ptr_item_First_1_1_1);

	SecsListItemAppend(ptr_item_First_1_1,ptr_item_First_1_1_2);

	SecsListItemAppend(ptr_item_First_1_1_2,ptr_item_First_1_1_2_1);

	SecsListItemAppend(ptr_item_First_1_1_2_1,ptr_item_First_1_1_2_1_1);
	SecsListItemAppend(ptr_item_First_1_1_2_1,ptr_item_First_1_1_2_1_2);

	SecsListItemAppend(ptr_item_First,ptr_item_First_2);
	SecsListItemAppend(ptr_item_First_2,ptr_item_First_2_1);
	SecsListItemAppend(ptr_item_First_2,ptr_item_First_2_2);

	unsigned long msg_id = SecsSendMessageItem(g_secs_Client, 14, 1, ptr_item_First, SECS_FLAGS_NEED_REPLY);//发送消息主要函数
所发消息如下图
SECS/GEM通信_第1张图片
7.断开链接

if(settings.mode = HSMS_PASSIVE)
	{
	   SecsCloseServer(g_secs_Server);
	   DestroySecs(g_secs_Server);
	   g_secs_Server = NULL;
	}
	else
	{
	  SecsDisconnect(g_secs_Server);
	  DestroySecs(g_secs_Server);
	  g_secs_Server = NULL;
	}

你可能感兴趣的:(c/c++)