IPChannelConfig.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. namespace xicheji
  11. {
  12. public partial class IPChannelConfig : Form
  13. {
  14. public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIPParaCfgV40 = new CHCNetSDK.NET_DVR_IPPARACFG_V40();
  15. private CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo = new CHCNetSDK.NET_DVR_IPCHANINFO();
  16. private CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40 = new CHCNetSDK.NET_DVR_IPCHANINFO_V40();
  17. private CHCNetSDK.NET_DVR_IPC_PROTO_LIST m_struProtoList = new CHCNetSDK.NET_DVR_IPC_PROTO_LIST();
  18. private CHCNetSDK.NET_DVR_GET_STREAM_UNION m_struStreamUnion = new CHCNetSDK.NET_DVR_GET_STREAM_UNION();
  19. public int iIPDevIndex = -1;
  20. public int iChanIndex = 0;
  21. public uint dwUnionSize = 0;
  22. public Int32 m_lUserID = -1;
  23. public CHCNetSDK.NET_DVR_PROTO_TYPE m_struProtoType = new CHCNetSDK.NET_DVR_PROTO_TYPE();
  24. public IPChannelConfig()
  25. {
  26. InitializeComponent();
  27. dwUnionSize = (uint)Marshal.SizeOf(m_struStreamUnion);//联合体大小
  28. }
  29. private void btnSet_Click(object sender, EventArgs e)
  30. {
  31. /***********************************************************
  32. * 添加IP通道或者修改IP通道配置
  33. * **********************************************************/
  34. if (iIPDevIndex < 0)
  35. {
  36. //如果该IP通道空闲,新增IP通道,选择空闲设备ID
  37. for(int i=0;i<64;i++)
  38. if (m_struIPParaCfgV40.struIPDevInfo[i].byEnable==0)
  39. {
  40. iIPDevIndex = i;
  41. break;
  42. }
  43. }
  44. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].byEnable = 1;//该IP设备是否启用
  45. //设备(厂家)协议类型,通过NET_DVR_GetIPCProtoList获取
  46. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].byProType = (byte)m_struProtoList.struProto[comboBoxFactory.SelectedIndex].dwType;
  47. //前端设备IP地址
  48. byte[] byIpV4 = System.Text.Encoding.Default.GetBytes(textBoxIPAddr.Text);
  49. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].struIP.sIpV4 = new byte[16];
  50. byIpV4.CopyTo(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].struIP.sIpV4, 0);
  51. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].wDVRPort = UInt16.Parse(textBoxPort.Text);//前端设备端口
  52. //前端设备用户名
  53. byte[] byUserName = System.Text.Encoding.Default.GetBytes(textBoxUserName.Text);
  54. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sUserName = new byte[32];
  55. byUserName.CopyTo(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sUserName, 0);
  56. //前端设备密码
  57. byte[] byPsw = System.Text.Encoding.Default.GetBytes(textBoxPsw.Text);
  58. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sPassword = new byte[16];
  59. byPsw.CopyTo(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sPassword, 0);
  60. switch (m_struIPParaCfgV40.struStreamMode[iChanIndex].byGetStreamType)
  61. {
  62. case 0://直接从设备取流,对应联合体中结构NET_DVR_IPCHANINFO
  63. m_struChanInfo.byEnable = 1;
  64. m_struChanInfo.byIPID = Convert.ToByte((iIPDevIndex + 1) % 256);
  65. m_struChanInfo.byChannel = byte.Parse(textBoxChannel.Text);
  66. IntPtr ptrChanInfo = Marshal.AllocHGlobal((Int32)dwUnionSize);
  67. Marshal.StructureToPtr(m_struChanInfo, ptrChanInfo, false);
  68. m_struIPParaCfgV40.struStreamMode[iChanIndex].uGetStream = (CHCNetSDK.NET_DVR_GET_STREAM_UNION)Marshal.PtrToStructure(ptrChanInfo, typeof(CHCNetSDK.NET_DVR_GET_STREAM_UNION));
  69. Marshal.FreeHGlobal(ptrChanInfo);
  70. break;
  71. case 6: //直接从设备取流(扩展),对应联合体中结构NET_DVR_IPCHANINFO_V40
  72. m_struChanInfoV40.byEnable = 1;
  73. m_struChanInfoV40.wIPID = Convert.ToUInt16(iIPDevIndex + 1);
  74. m_struChanInfoV40.dwChannel = Convert.ToUInt32(textBoxChannel.Text);
  75. m_struChanInfoV40.byTransProtocol = (byte)comboBoxProtocol.SelectedIndex;
  76. m_struChanInfoV40.byTransMode = (byte)comboBoxStream.SelectedIndex;
  77. IntPtr ptrChanInfoV40 = Marshal.AllocHGlobal((Int32)dwUnionSize);
  78. Marshal.StructureToPtr(m_struChanInfoV40, ptrChanInfoV40, false);
  79. m_struIPParaCfgV40.struStreamMode[iChanIndex].uGetStream = (CHCNetSDK.NET_DVR_GET_STREAM_UNION)Marshal.PtrToStructure(ptrChanInfoV40, typeof(CHCNetSDK.NET_DVR_GET_STREAM_UNION));
  80. Marshal.FreeHGlobal(ptrChanInfoV40);
  81. break;
  82. default:
  83. break;
  84. }
  85. int nSize = Marshal.SizeOf(m_struIPParaCfgV40);
  86. IntPtr ptrIPParaCfgV40 = Marshal.AllocHGlobal(nSize);
  87. Marshal.StructureToPtr(m_struIPParaCfgV40, ptrIPParaCfgV40, false);
  88. int iGroupNo = 0;
  89. if (!CHCNetSDK.NET_DVR_SetDVRConfig(m_lUserID, CHCNetSDK.NET_DVR_SET_IPPARACFG_V40, iGroupNo, ptrIPParaCfgV40, (UInt32)nSize))
  90. {
  91. uint iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  92. string str = "NET_DVR_SET_IPPARACFG_V40 failed, error code= " + iLastErr;
  93. MessageBox.Show(str);
  94. //获取IP资源配置信息失败,输出错误号 Failed to get configuration of IP channels and output the error code
  95. }
  96. else
  97. {
  98. MessageBox.Show("IP通道配置成功!");
  99. }
  100. Marshal.FreeHGlobal(ptrIPParaCfgV40);
  101. }
  102. private void IPChannelConfig_Load(object sender, EventArgs e)
  103. {
  104. int i = 0;
  105. //获取设备支持IPC协议列表
  106. if (CHCNetSDK.NET_DVR_GetIPCProtoList(m_lUserID, ref m_struProtoList))
  107. {
  108. for (i = 0; i < (int)m_struProtoList.dwProtoNum; i++)
  109. {
  110. comboBoxFactory.Items.Add(m_struProtoList.struProto[i].byDescribe);
  111. }
  112. }
  113. comboBoxStreamType.SelectedIndex = 0;
  114. comboBoxFactory.SelectedIndex = 0;
  115. comboBoxStream.SelectedIndex = 0;
  116. comboBoxProtocol.SelectedIndex = 0;
  117. /***********************************************************
  118. * 显示当前IP通道配置
  119. * **********************************************************/
  120. if (iIPDevIndex < 0)
  121. {
  122. //该通道未添加有设备,空闲状态
  123. return;
  124. }
  125. comboBoxStreamType.SelectedIndex = m_struIPParaCfgV40.struStreamMode[iChanIndex].byGetStreamType;//取流方式
  126. textBoxIPAddr.Text = System.Text.Encoding.UTF8.GetString(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].struIP.sIpV4);//前端设备IP地址
  127. textBoxPort.Text = Convert.ToString(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].wDVRPort);//前端设备端口
  128. textBoxUserName.Text = System.Text.Encoding.UTF8.GetString(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sUserName);//前端设备用户名
  129. textBoxPsw.Text = System.Text.Encoding.UTF8.GetString(m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].sPassword);//前端设备密码
  130. for (i = 0; i < (int)m_struProtoList.dwProtoNum; i++)
  131. {
  132. //设备(厂家)协议类型,通过NET_DVR_GetIPCProtoList获取
  133. if (m_struProtoList.struProto[i].dwType == m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex].byProType)
  134. {
  135. comboBoxFactory.SelectedIndex = i;
  136. }
  137. }
  138. switch (m_struIPParaCfgV40.struStreamMode[iChanIndex].byGetStreamType)
  139. {
  140. case 0: //直接从设备取流,对应联合体中结构NET_DVR_IPCHANINFO
  141. IntPtr ptrChanInfo = Marshal.AllocHGlobal((Int32)dwUnionSize);
  142. Marshal.StructureToPtr(m_struIPParaCfgV40.struStreamMode[iChanIndex].uGetStream, ptrChanInfo, false);
  143. m_struChanInfo = (CHCNetSDK.NET_DVR_IPCHANINFO)Marshal.PtrToStructure(ptrChanInfo, typeof(CHCNetSDK.NET_DVR_IPCHANINFO));
  144. textBoxChannel.Text = Convert.ToString(m_struChanInfo.byChannel);
  145. Marshal.FreeHGlobal(ptrChanInfo);
  146. break;
  147. case 6://直接从设备取流(扩展),对应联合体中结构NET_DVR_IPCHANINFO_V40
  148. IntPtr ptrChanInfoV40 = Marshal.AllocHGlobal((Int32)dwUnionSize);
  149. Marshal.StructureToPtr(m_struIPParaCfgV40.struStreamMode[iChanIndex].uGetStream, ptrChanInfoV40, false);
  150. m_struChanInfoV40 = (CHCNetSDK.NET_DVR_IPCHANINFO_V40)Marshal.PtrToStructure(ptrChanInfoV40, typeof(CHCNetSDK.NET_DVR_IPCHANINFO_V40));
  151. textBoxChannel.Text = Convert.ToString(m_struChanInfoV40.dwChannel);
  152. comboBoxStream.SelectedIndex = m_struChanInfoV40.byTransMode;
  153. comboBoxProtocol.SelectedIndex = m_struChanInfoV40.byTransProtocol;
  154. Marshal.FreeHGlobal(ptrChanInfoV40);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. private void btnExit_Click(object sender, EventArgs e)
  161. {
  162. this.Close();
  163. this.Dispose();
  164. }
  165. private void btnDel_Click(object sender, EventArgs e)
  166. {
  167. if (iIPDevIndex < 0)
  168. {
  169. return; //该通道未添加有设备,空闲状态
  170. }
  171. m_struIPParaCfgV40.struStreamMode[iChanIndex].uGetStream.Init(); //初始化通道参数为0
  172. m_struIPParaCfgV40.struIPDevInfo[iIPDevIndex] = new CHCNetSDK.NET_DVR_IPDEVINFO_V31();//初始化设备参数为0
  173. int nSize = Marshal.SizeOf(m_struIPParaCfgV40);
  174. IntPtr ptrIPParaCfgV40 = Marshal.AllocHGlobal(nSize);
  175. Marshal.StructureToPtr(m_struIPParaCfgV40, ptrIPParaCfgV40, false);
  176. int iGroupNo = 0;
  177. if (!CHCNetSDK.NET_DVR_SetDVRConfig(m_lUserID, CHCNetSDK.NET_DVR_SET_IPPARACFG_V40, iGroupNo, ptrIPParaCfgV40, (UInt32)nSize))
  178. {
  179. uint iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  180. string str = "NET_DVR_SET_IPPARACFG_V40 failed, error code= " + iLastErr;
  181. MessageBox.Show(str);
  182. //获取IP资源配置信息失败,输出错误号 Failed to get configuration of IP channels and output the error code
  183. }
  184. else
  185. {
  186. MessageBox.Show("IP通道删除成功!");
  187. }
  188. Marshal.FreeHGlobal(ptrIPParaCfgV40);
  189. }
  190. }
  191. }