#include "XNetSDKTest.h"
#include "SystemFunction.h"
#include "XNetSDKSyn.h"
#include "Uart_RS485.h"
#include "Uart_Comm.h"

USE_NS_NETSDK_CFG
int TestOpenTransChannel()
{
	printf("%s\r\n", __FUNCTION__);
	char c = 0;
	bool bOpen = false;
	bool bRS485 = false;
	bool bRS232 = false;
	int nResult = 0;

	char szOutBuffer[40960] = {0};
	int  nInOutSize = sizeof(szOutBuffer);
	char szOutBuffer1[4096] = {0};
	int  nInOutSize1 = sizeof(szOutBuffer1);
	char outbuffer[100] = {0};
	int len = 0;

	XSDK_HANDLE gTranshandle = 0;
	while((c = getchar()) != 'q')
	{
		switch(c)
		{
		case 'g':
			{
				///获取设备支持的串口类型

				memset(&szOutBuffer, 0, 40960);
				nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_SystemFunction, szOutBuffer, &nInOutSize, 4000, JK_SystemFunction_MsgId);
				
				if(nResult >= 0)
				{
					SystemFunction sysfunc;
					sysfunc.Parse(szOutBuffer);
					bRS485 = sysfunc.mCommFunction.CommRS485.ToBool();
					bRS232 = sysfunc.mCommFunction.CommRS232.ToBool();
				}
			}
			break;
			///配置485串口
		case 'a':
			{
				if(bRS485)
				{
					memset(&szOutBuffer, 0, 40960);
					memset(&szOutBuffer1, 0, 4096);
					memset(&outbuffer, 0, 100);

					/////获取当前485串口的配置
					nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Uart_RS485, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);

					if (nResult < 0)
					{
						break;
					}

					///获取串口支持的协议,对应Uart_RS485中的ProtocolName
					nResult = XSDK_DevGetSysConfigSyn(g_hDevice, "UartProtocol", szOutBuffer1, &nInOutSize1, 4000, EXCMD_ABILITY_GET);

					if (nResult < 0)
					{
						break;
					}

					JObjArrayObject<JStrObj> cfg(NULL, "UartProtocol");
					cfg.Parse(szOutBuffer1);

					for(int i = 0;i < cfg.objs.Size();i++)
					{
						printf("ComProtocol = %s\r\n", cfg.objs[i].ToString());
					}


					///对串口进行设置
					JObjArrayObject<Uart_RS485> uart(NULL, JK_Uart_RS485);
					uart.Parse(szOutBuffer);

					///打印获取到的当前实际波特率
					printf("BaudRate = %d\r\n", uart.objs[0].Attribute[0].ToInt());

					///设置校验位
					uart.objs[0].Attribute[1].SetValue(NOPARITY);

					const char *pCfg = uart.ToString();
					nResult = XSDK_DevSetSysConfigSyn(g_hDevice, JK_Uart_RS485, pCfg, strlen(pCfg),outbuffer, &len, 4000);

					if(nResult >= 0)
					{
						printf("Set Success!\r\n");
					}
				}
			}
			break;
			///配置232串口
		case 'b':
			{
				if(bRS232)
				{
					memset(&szOutBuffer, 0, 40960);
					memset(&szOutBuffer1, 0, 4096);
					memset(&outbuffer, 0, 100);
				
					nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Uart_Comm, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);

					if (nResult < 0)
					{
						break;
					}

					/////获取串口支持的协议,对应Uart_Comm中的ProtocolName
					nResult = XSDK_DevGetSysConfigSyn(g_hDevice, "ComProtocol", szOutBuffer1, &nInOutSize1, 4000, EXCMD_ABILITY_GET);

					if(nResult < 0)
					{
						break;
					}

					JObjArrayObject<JStrObj> cfg(NULL, "ComProtocol");
					cfg.Parse(szOutBuffer1);
					for(int i = 0;i < cfg.objs.Size();i++)
					{
						printf("ComProtocol = %s\r\n", cfg.objs[i].ToString());
					}

					///对串口进行设置
					JObjArrayObject<Uart_Comm> uart(NULL, JK_Uart_Comm);
					uart.Parse(szOutBuffer);

					//打印获取到的当前实际波特率
					printf("BaudRate = %d\r\n", uart.objs[0].Attribute[0].ToInt());

					///设置校验位
					uart.objs[0].Attribute[1].SetValue(ODDPARITY);

					const char *pCfg = uart.ToString();
					nResult = XSDK_DevSetSysConfigSyn(g_hDevice, JK_Uart_Comm, pCfg, strlen(pCfg),outbuffer, &len, 4000);

					if(nResult >= 0)
					{
						printf("Set Success!\r\n");
					}
				}
			}
			break;
			/////////打开透明串口功能：注使用该功能前需要将232或485串口配置好
		case 'c':
			{
				////232串口
				gTranshandle = XSDK_OpenTransCom(g_hDevice, S_XTRANS_COMM_RS232);		
			}	
			break;
		case 'd':
			{
				////485串口
				gTranshandle = XSDK_OpenTransCom(g_hDevice, S_XTRANS_COMM_RS485);
			}
			///打开透明串口功能之后向串口写数据
		case 'w':
			{
				char data[10] = "123456";
				XSDK_TransComWrite(g_hDevice, 1, data, strlen(data), 1);
			}
			break;
			///关闭透明串口功能
		case 't':
			{	
				XSDK_CloseTransCom(gTranshandle);
			}
			break;
		}
	}
	return 0;
}

///////接收串口数据
void OnTransCommCallback(XSDK_HANDLE hDevice, const char *szData)
{
	printf("OnTransCallback[Dev:%ld][%s]\r\n", hDevice, szData);
}