You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.4 KiB

  1. #include "XNetSDKTest.h"
  2. #include "SystemFunction.h"
  3. #include "XNetSDKSyn.h"
  4. #include "Uart_RS485.h"
  5. #include "Uart_Comm.h"
  6. USE_NS_NETSDK_CFG
  7. int TestOpenTransChannel()
  8. {
  9. printf("%s\r\n", __FUNCTION__);
  10. char c = 0;
  11. bool bOpen = false;
  12. bool bRS485 = false;
  13. bool bRS232 = false;
  14. int nResult = 0;
  15. char szOutBuffer[40960] = {0};
  16. int nInOutSize = sizeof(szOutBuffer);
  17. char szOutBuffer1[4096] = {0};
  18. int nInOutSize1 = sizeof(szOutBuffer1);
  19. char outbuffer[100] = {0};
  20. int len = 0;
  21. XSDK_HANDLE gTranshandle = 0;
  22. while((c = getchar()) != 'q')
  23. {
  24. switch(c)
  25. {
  26. case 'g':
  27. {
  28. ///获取设备支持的串口类型
  29. memset(&szOutBuffer, 0, 40960);
  30. nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_SystemFunction, szOutBuffer, &nInOutSize, 4000, JK_SystemFunction_MsgId);
  31. if(nResult >= 0)
  32. {
  33. SystemFunction sysfunc;
  34. sysfunc.Parse(szOutBuffer);
  35. bRS485 = sysfunc.mCommFunction.CommRS485.ToBool();
  36. bRS232 = sysfunc.mCommFunction.CommRS232.ToBool();
  37. }
  38. }
  39. break;
  40. ///配置485串口
  41. case 'a':
  42. {
  43. if(bRS485)
  44. {
  45. memset(&szOutBuffer, 0, 40960);
  46. memset(&szOutBuffer1, 0, 4096);
  47. memset(&outbuffer, 0, 100);
  48. /////获取当前485串口的配置
  49. nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Uart_RS485, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);
  50. if (nResult < 0)
  51. {
  52. break;
  53. }
  54. ///获取串口支持的协议,对应Uart_RS485中的ProtocolName
  55. nResult = XSDK_DevGetSysConfigSyn(g_hDevice, "UartProtocol", szOutBuffer1, &nInOutSize1, 4000, EXCMD_ABILITY_GET);
  56. if (nResult < 0)
  57. {
  58. break;
  59. }
  60. JObjArrayObject<JStrObj> cfg(NULL, "UartProtocol");
  61. cfg.Parse(szOutBuffer1);
  62. for(int i = 0;i < cfg.objs.Size();i++)
  63. {
  64. printf("ComProtocol = %s\r\n", cfg.objs[i].ToString());
  65. }
  66. ///对串口进行设置
  67. JObjArrayObject<Uart_RS485> uart(NULL, JK_Uart_RS485);
  68. uart.Parse(szOutBuffer);
  69. ///打印获取到的当前实际波特率
  70. printf("BaudRate = %d\r\n", uart.objs[0].Attribute[0].ToInt());
  71. ///设置校验位
  72. uart.objs[0].Attribute[1].SetValue(NOPARITY);
  73. const char *pCfg = uart.ToString();
  74. nResult = XSDK_DevSetSysConfigSyn(g_hDevice, JK_Uart_RS485, pCfg, strlen(pCfg),outbuffer, &len, 4000);
  75. if(nResult >= 0)
  76. {
  77. printf("Set Success!\r\n");
  78. }
  79. }
  80. }
  81. break;
  82. ///配置232串口
  83. case 'b':
  84. {
  85. if(bRS232)
  86. {
  87. memset(&szOutBuffer, 0, 40960);
  88. memset(&szOutBuffer1, 0, 4096);
  89. memset(&outbuffer, 0, 100);
  90. nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Uart_Comm, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);
  91. if (nResult < 0)
  92. {
  93. break;
  94. }
  95. /////获取串口支持的协议,对应Uart_Comm中的ProtocolName
  96. nResult = XSDK_DevGetSysConfigSyn(g_hDevice, "ComProtocol", szOutBuffer1, &nInOutSize1, 4000, EXCMD_ABILITY_GET);
  97. if(nResult < 0)
  98. {
  99. break;
  100. }
  101. JObjArrayObject<JStrObj> cfg(NULL, "ComProtocol");
  102. cfg.Parse(szOutBuffer1);
  103. for(int i = 0;i < cfg.objs.Size();i++)
  104. {
  105. printf("ComProtocol = %s\r\n", cfg.objs[i].ToString());
  106. }
  107. ///对串口进行设置
  108. JObjArrayObject<Uart_Comm> uart(NULL, JK_Uart_Comm);
  109. uart.Parse(szOutBuffer);
  110. //打印获取到的当前实际波特率
  111. printf("BaudRate = %d\r\n", uart.objs[0].Attribute[0].ToInt());
  112. ///设置校验位
  113. uart.objs[0].Attribute[1].SetValue(ODDPARITY);
  114. const char *pCfg = uart.ToString();
  115. nResult = XSDK_DevSetSysConfigSyn(g_hDevice, JK_Uart_Comm, pCfg, strlen(pCfg),outbuffer, &len, 4000);
  116. if(nResult >= 0)
  117. {
  118. printf("Set Success!\r\n");
  119. }
  120. }
  121. }
  122. break;
  123. /////////打开透明串口功能:注使用该功能前需要将232或485串口配置好
  124. case 'c':
  125. {
  126. ////232串口
  127. gTranshandle = XSDK_OpenTransCom(g_hDevice, S_XTRANS_COMM_RS232);
  128. }
  129. break;
  130. case 'd':
  131. {
  132. ////485串口
  133. gTranshandle = XSDK_OpenTransCom(g_hDevice, S_XTRANS_COMM_RS485);
  134. }
  135. ///打开透明串口功能之后向串口写数据
  136. case 'w':
  137. {
  138. char data[10] = "123456";
  139. XSDK_TransComWrite(g_hDevice, 1, data, strlen(data), 1);
  140. }
  141. break;
  142. ///关闭透明串口功能
  143. case 't':
  144. {
  145. XSDK_CloseTransCom(gTranshandle);
  146. }
  147. break;
  148. }
  149. }
  150. return 0;
  151. }
  152. ///////接收串口数据
  153. void OnTransCommCallback(XSDK_HANDLE hDevice, const char *szData)
  154. {
  155. printf("OnTransCallback[Dev:%ld][%s]\r\n", hDevice, szData);
  156. }