Device.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "stdafx.h"
  2. #include ".\device.h"
  3. #include "Global.h"
  4. CDevice::CDevice(void)
  5. {
  6. // m_iVarId = 0; //变量ID
  7. m_iID = 0; //设备的ID
  8. m_iPort = 0; //设备串口号
  9. m_iDeviceType = 0; //设备类型
  10. m_iBaudrate = 0; //波特率
  11. m_iDatabit = 0; //数据位
  12. m_iStopbit = 0; //停止位
  13. m_iParitybit = 0; //奇偶校验位
  14. m_iRsmode = 0; //串口通信方式,0 - RS232;1 - RS422; 2 - RS485
  15. m_iIpport = 0; //ip端口号
  16. m_iCommmode = 0; //通信的方式,有 0 - 串口方式;...
  17. m_iDeviceflag = -1; //该设备是否采集器 ;...
  18. m_iDevideaddr = 0; //设备地址
  19. m_iTimeout = 0; //通讯超时
  20. m_iIsuse = 0; //是否启用设备1:启用,0:禁用
  21. m_strUid =_T(""); //设备UID
  22. m_strDeviceName = _T(""); //设备名称
  23. m_strIp = _T(""); //ip地址号
  24. m_strProtocoldllname = _T("");//协议DLL名称
  25. m_bySpecialdevice = 0; //Modbus Rtu;Modbus Ascii;Modbus Tcp;Snmp;RS232;
  26. m_dwOnlineTick = 0;
  27. m_bCommStatus = TRUE;
  28. //===============================================
  29. // 新增成员变量 in 2010-04-07
  30. m_strIniName = _T(""); //配置文件名称
  31. m_strReserved1 = _T("");
  32. m_strReserved2 = _T("");
  33. m_strReserved3 = _T("");
  34. m_strReserved4 = _T("");
  35. m_strReserved5 = _T("");
  36. m_strReserved6 = _T("");
  37. m_strReserved7 = _T("");
  38. m_strReserved8 = _T("");
  39. m_strReserved9 = _T("");
  40. m_strReserved10 = _T("");
  41. m_nReserved1 = 0;
  42. m_nReserved2 = 0;
  43. m_nReserved3 = 0;
  44. m_nReserved4 = 0;
  45. m_nReserved5 = 0;
  46. m_nReserved6 = 0;
  47. m_nReserved7 = 0;
  48. m_nReserved8 = 0;
  49. m_nReserved9 = 0;
  50. m_nReserved10 = 0;
  51. m_bReserved1 = false;
  52. m_bReserved2 = false;
  53. m_bReserved3 = false;
  54. m_bReserved4 = false;
  55. m_bReserved5 = false;
  56. m_bReserved6 = false;
  57. m_bReserved7 = false;
  58. m_bReserved8 = false;
  59. m_bReserved9 = false;
  60. m_bReserved10 = false;
  61. MTVERIFY( m_hSemNet = CreateEvent( NULL, TRUE, TRUE, 0 ) );
  62. }
  63. CDevice::CDevice(const CDevice & other)
  64. {
  65. // m_iVarId = other.m_iVarId; //变量ID
  66. m_iID = other.m_iID; //设备的ID
  67. m_iPort = other.m_iPort; //设备串口号
  68. m_iDeviceType = other.m_iDeviceType; //设备类型
  69. m_iBaudrate = other.m_iBaudrate; //波特率
  70. m_iDatabit = other.m_iDatabit; //数据位
  71. m_iStopbit = other.m_iStopbit; //停止位
  72. m_iParitybit = other.m_iParitybit; //奇偶校验位
  73. m_iRsmode = other.m_iRsmode; //串口通信方式,0 - RS232;1 - RS422; 2 - RS485
  74. m_iIpport = other.m_iIpport; //ip端口号
  75. m_iCommmode = other.m_iCommmode; //通信的方式,有 0 - 串口方式;...
  76. m_iDeviceflag = other.m_iDeviceflag; //该设备是否采集器 ;...
  77. m_iDevideaddr = other.m_iDevideaddr; //设备地址
  78. m_iTimeout = other.m_iTimeout; //通讯超时
  79. m_iIsuse = other.m_iIsuse; //是否启用设备1:启用,0:禁用
  80. m_strUid = other.m_strUid; //设备UID
  81. m_strDeviceName = other.m_strDeviceName; //设备名称
  82. m_strIp = other.m_strIp; //ip地址号
  83. m_strProtocoldllname = other.m_strProtocoldllname;//协议DLL名称
  84. m_bySpecialdevice = other.m_bySpecialdevice; //Modbus Rtu;Modbus Ascii;Modbus Tcp;Snmp;RS232;
  85. m_dwOnlineTick = other.m_dwOnlineTick;
  86. m_bCommStatus = other.m_bCommStatus;
  87. //===============================================
  88. // 新增成员变量 in 2010-04-07
  89. m_strIniName = other.m_strIniName;
  90. m_strReserved1 = other.m_strReserved1;
  91. m_strReserved2 = other.m_strReserved2;
  92. m_strReserved3 = other.m_strReserved3;
  93. m_strReserved4 = other.m_strReserved4;
  94. m_strReserved5 = other.m_strReserved5;
  95. m_strReserved6 = other.m_strReserved6;
  96. m_strReserved7 = other.m_strReserved7;
  97. m_strReserved8 = other.m_strReserved8;
  98. m_strReserved9 = other.m_strReserved9;
  99. m_strReserved10 = other.m_strReserved10;
  100. m_nReserved1 = other.m_nReserved1;
  101. m_nReserved2 = other.m_nReserved2;
  102. m_nReserved3 = other.m_nReserved3;
  103. m_nReserved4 = other.m_nReserved4;
  104. m_nReserved5 = other.m_nReserved5;
  105. m_nReserved6 = other.m_nReserved6;
  106. m_nReserved7 = other.m_nReserved7;
  107. m_nReserved8 = other.m_nReserved8;
  108. m_nReserved9 = other.m_nReserved9;
  109. m_nReserved10 = other.m_nReserved10;
  110. m_bReserved1 = other.m_bReserved1;
  111. m_bReserved2 = other.m_bReserved2;
  112. m_bReserved3 = other.m_bReserved3;
  113. m_bReserved4 = other.m_bReserved4;
  114. m_bReserved5 = other.m_bReserved5;
  115. m_bReserved6 = other.m_bReserved6;
  116. m_bReserved7 = other.m_bReserved7;
  117. m_bReserved8 = other.m_bReserved8;
  118. m_bReserved9 = other.m_bReserved9;
  119. m_bReserved10 = other.m_bReserved10;
  120. }
  121. CDevice &CDevice::operator =(const CDevice & other)
  122. {
  123. if (this == &other)
  124. return *this;
  125. // m_iVarId = other.m_iVarId; //变量ID
  126. m_iID = other.m_iID; //设备的ID
  127. m_iPort = other.m_iPort; //设备串口号
  128. m_iDeviceType = other.m_iDeviceType; //设备类型
  129. m_iBaudrate = other.m_iBaudrate; //波特率
  130. m_iDatabit = other.m_iDatabit; //数据位
  131. m_iStopbit = other.m_iStopbit; //停止位
  132. m_iParitybit = other.m_iParitybit; //奇偶校验位
  133. m_iRsmode = other.m_iRsmode; //串口通信方式,0 - RS232;1 - RS422; 2 - RS485
  134. m_iIpport = other.m_iIpport; //ip端口号
  135. m_iCommmode = other.m_iCommmode; //通信的方式,有 0 - 串口方式;...
  136. m_iDeviceflag = other.m_iDeviceflag; //该设备是否采集器 ;...
  137. m_iDevideaddr = other.m_iDevideaddr; //设备地址
  138. m_iTimeout = other.m_iTimeout; //通讯超时
  139. m_iIsuse = other.m_iIsuse; //是否启用设备1:启用,0:禁用
  140. m_strUid = other.m_strUid; //设备UID
  141. m_strDeviceName = other.m_strDeviceName; //设备名称
  142. m_strIp = other.m_strIp; //ip地址号
  143. m_strProtocoldllname = other.m_strProtocoldllname;//协议DLL名称
  144. m_bySpecialdevice = other.m_bySpecialdevice; //Modbus Rtu;Modbus Ascii;Modbus Tcp;Snmp;RS232;
  145. m_dwOnlineTick = other.m_dwOnlineTick;
  146. m_bCommStatus = other.m_bCommStatus;
  147. //===============================================
  148. // 新增成员变量 in 2010-04-07
  149. m_strIniName = other.m_strIniName;
  150. m_strReserved1 = other.m_strReserved1;
  151. m_strReserved2 = other.m_strReserved2;
  152. m_strReserved3 = other.m_strReserved3;
  153. m_strReserved4 = other.m_strReserved4;
  154. m_strReserved5 = other.m_strReserved5;
  155. m_strReserved6 = other.m_strReserved6;
  156. m_strReserved7 = other.m_strReserved7;
  157. m_strReserved8 = other.m_strReserved8;
  158. m_strReserved9 = other.m_strReserved9;
  159. m_strReserved10 = other.m_strReserved10;
  160. m_nReserved1 = other.m_nReserved1;
  161. m_nReserved2 = other.m_nReserved2;
  162. m_nReserved3 = other.m_nReserved3;
  163. m_nReserved4 = other.m_nReserved4;
  164. m_nReserved5 = other.m_nReserved5;
  165. m_nReserved6 = other.m_nReserved6;
  166. m_nReserved7 = other.m_nReserved7;
  167. m_nReserved8 = other.m_nReserved8;
  168. m_nReserved9 = other.m_nReserved9;
  169. m_nReserved10 = other.m_nReserved10;
  170. m_bReserved1 = other.m_bReserved1;
  171. m_bReserved2 = other.m_bReserved2;
  172. m_bReserved3 = other.m_bReserved3;
  173. m_bReserved4 = other.m_bReserved4;
  174. m_bReserved5 = other.m_bReserved5;
  175. m_bReserved6 = other.m_bReserved6;
  176. m_bReserved7 = other.m_bReserved7;
  177. m_bReserved8 = other.m_bReserved8;
  178. m_bReserved9 = other.m_bReserved9;
  179. m_bReserved10 = other.m_bReserved10;
  180. return *this;
  181. }
  182. CDevice::~CDevice(void)
  183. {
  184. if( m_hSemNet )
  185. {
  186. MTVERIFY( SetEvent(m_hSemNet) );
  187. MTVERIFY( CloseHandle( m_hSemNet ));
  188. }
  189. }
  190. int FindDevice(CString strDevUid)
  191. {
  192. int nRet = -1;
  193. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  194. for (int i = 0; i< nDeviceSize; i++)
  195. {
  196. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  197. if( pDev->m_strUid.Compare(strDevUid) == 0 )
  198. {
  199. nRet = i;
  200. }
  201. }
  202. return nRet;
  203. }
  204. BOOL FindVar(CString strDevUid, int nVarID, int &nDeviceIndex, int &nVarIndex)
  205. {
  206. BOOL bRet = FALSE;
  207. int i, j;
  208. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  209. for( i = 0; i < nDeviceSize; i++ )
  210. {
  211. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  212. int nVarSize = (int)pDev->m_Vars.GetSize();
  213. for( j = 0; j < nVarSize; j++ )
  214. {
  215. CBaseVar *pBaseVar = pDev->m_Vars[j];
  216. if( pBaseVar->m_nVarID == nVarID && pDev->m_strUid.Compare(strDevUid) == 0 )
  217. {
  218. nDeviceIndex = i;
  219. nVarIndex = j;
  220. bRet = TRUE;
  221. return bRet;
  222. }
  223. }
  224. }
  225. return bRet;
  226. }
  227. BOOL FindVar(CString strDevUid, CString strVarName, int &nDeviceIndex, int &nVarIndex)
  228. {
  229. BOOL bRet = FALSE;
  230. int i, j;
  231. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  232. for( i = 0; i < nDeviceSize; i++ )
  233. {
  234. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  235. int nVarSize = (int)pDev->m_Vars.GetSize();
  236. for( j = 0; j < nVarSize; j++ )
  237. {
  238. CBaseVar *pBaseVar = pDev->m_Vars[j];
  239. if( pBaseVar->m_strName.Compare(strVarName) == 0 )
  240. {
  241. nDeviceIndex = i;
  242. nVarIndex = j;
  243. bRet = TRUE;
  244. return bRet;
  245. }
  246. }
  247. }
  248. return bRet;
  249. }
  250. BOOL FindVar(CString &strDevUid, int &iVarID,CString strVarName, int &nDeviceIndex, int &nVarIndex)
  251. {
  252. BOOL bRet = FALSE;
  253. int i, j;
  254. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  255. for( i = 0; i < nDeviceSize; i++ )
  256. {
  257. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  258. int nVarSize = (int)pDev->m_Vars.GetSize();
  259. for( j = 0; j < nVarSize; j++ )
  260. {
  261. CBaseVar *pBaseVar = pDev->m_Vars[j];
  262. if( pBaseVar->m_strName.Compare(strVarName) == 0 )
  263. {
  264. strDevUid = pDev->m_strUid;
  265. iVarID = pBaseVar->m_nVarID;
  266. nDeviceIndex = i;
  267. nVarIndex = j;
  268. bRet = TRUE;
  269. return bRet;
  270. }
  271. }
  272. }
  273. return bRet;
  274. }
  275. CBaseVar* FindVar( CString sDevName,CString sVarName )
  276. {
  277. if( sVarName!="" )
  278. {
  279. for( int i = 0; i < (int)g_pDevicesManager->m_Devices.GetSize(); i++ )
  280. {
  281. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  282. if( pDev->m_strDeviceName==sDevName )
  283. {
  284. int nVarSize = (int)pDev->m_Vars.GetSize();
  285. for( int m = 0; m < nVarSize; m++ )
  286. {
  287. CBaseVar *pBaseVar = pDev->m_Vars[m];
  288. if( pBaseVar->m_strName == sVarName )
  289. {
  290. return pBaseVar;
  291. }
  292. }
  293. return NULL;
  294. }
  295. }
  296. }
  297. else
  298. {
  299. for( int i = 0; i < (int)g_pDevicesManager->m_Devices.GetSize(); i++ )
  300. {
  301. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  302. if( pDev->m_strDeviceName==sDevName )
  303. {
  304. int nVarSize = (int)pDev->m_Vars.GetSize();
  305. for( int m = 0; m < nVarSize; m++ )
  306. {
  307. CBaseVar *pBaseVar = pDev->m_Vars[m];
  308. if( pBaseVar->m_bReadOnly==0 && pBaseVar->m_nVarTypeID != ONLINE_VAR_ID && pBaseVar->m_nVarTypeID != HIGH_PRIORITY_ONLINE_ID )
  309. {
  310. return pBaseVar;
  311. }
  312. }
  313. return NULL;
  314. }
  315. }
  316. }
  317. return NULL;
  318. }
  319. BOOL IsExistVar( CString sVarName )//判断变量是否存在
  320. {
  321. if( sVarName=="" )
  322. return FALSE;
  323. BOOL bRet = FALSE;
  324. int i, j;
  325. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  326. for( i = 0; i < nDeviceSize; i++ )
  327. {
  328. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  329. int nVarSize = (int)pDev->m_Vars.GetSize();
  330. for( j = 0; j < nVarSize; j++ )
  331. {
  332. CBaseVar *pBaseVar = pDev->m_Vars[j];
  333. if( pBaseVar->m_strName.Compare(sVarName) == 0 )
  334. {
  335. bRet = TRUE;
  336. return bRet;
  337. }
  338. }
  339. }
  340. LOG4C((LOG_NOTICE, "报警联动表里有不存在的变量:sVarName=%s",sVarName));
  341. return bRet;
  342. }
  343. BOOL IsExistDev( CString sDevName )//判断设备是否存在
  344. {
  345. if( sDevName=="" )
  346. return FALSE;
  347. BOOL bRet = FALSE;
  348. int i, j;
  349. int nDeviceSize = (int)g_pDevicesManager->m_Devices.GetSize();
  350. for( i = 0; i < nDeviceSize; i++ )
  351. {
  352. CDevice *pDev = g_pDevicesManager->m_Devices[i];
  353. if( pDev->m_strDeviceName.Compare(sDevName) == 0 )
  354. {
  355. bRet = TRUE;
  356. return bRet;
  357. }
  358. }
  359. return bRet;
  360. }