AXClient_t.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // Ignore Spelling: timestamp Cmp
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IACommService4CSharp
  8. {
  9. public class AXClient_t : IDisposable
  10. {
  11. private IClient4CSharp m_client;
  12. private bool m_bSymbolNameEncode;
  13. private bool m_disposed;
  14. private IReadCompleteCallback_t m_readCompleteCallback;
  15. private IWriteCompleteCallback_t m_writeCompleteCallback;
  16. private IDataChangeCallback_t m_dataChangeCallback;
  17. private readonly object m_locker = new object();
  18. private Dictionary<uint, string[]> m_symbolListCache = new Dictionary<uint, string[]>();
  19. public static uint BrowseAsync(string gatewayIP, string gatewayPort, IBrowseDeviceCallback_t browseCallback)
  20. {
  21. if (null == browseCallback || (null == gatewayIP && null != gatewayPort) || (null != gatewayIP && null == gatewayPort))
  22. return ErrorCode.InvalidArgument;
  23. DataValue_t gatewayInfos = null;
  24. if (gatewayIP != null && gatewayPort != null)
  25. {
  26. gatewayInfos = new DataValue_t(DType.Complex);
  27. var gatewayInfo = new DataValue_t(DType.String);
  28. gatewayInfo.SetValue(gatewayIP);
  29. gatewayInfos.SetValue("GatewayIPAddress", gatewayInfo);
  30. gatewayInfo.SetValue(gatewayPort);
  31. gatewayInfos.SetValue("GatewayPort", gatewayInfo);
  32. gatewayInfo.Dispose();
  33. }
  34. var clientMgr = CommClientManager as ICommClientManager24CSharp;
  35. if (null == clientMgr)
  36. return ErrorCode.NotSupported;
  37. var status = clientMgr.BrowseAsync("AXDriver", gatewayInfos == null ? null : gatewayInfos.Internal, browseCallback);
  38. if (gatewayInfos != null)
  39. gatewayInfos.Dispose();
  40. return status;
  41. }
  42. /* Parameter bool 'bSymbolNameEncode' :
  43. 'false' is mean that the instance of AXClient_t adapt to the symbol name encoding of DIADesigner-AX 1.5 or later,
  44. or other AX designer which install CODOSYS communication 4.3 or later add-on.
  45. 'true' is mean that the instance of AXClient_t adapt to the symbol name encoding of DIADesigner-AX 1.4 or below,
  46. and which did not install CODOSYS communication 4.3 or later add-on.
  47. */
  48. public AXClient_t(bool bSymbolNameEncode = false)
  49. {
  50. m_client = CommClientManager.CreateClient("AXDriver");
  51. if (null == m_client)
  52. throw new IACommServiceException(ErrorCode.InternalError);
  53. m_bSymbolNameEncode = bSymbolNameEncode;
  54. m_disposed = false;
  55. }
  56. ~AXClient_t()
  57. {
  58. Dispose(false);
  59. }
  60. public uint Connect(string axIPAddr, string port = "11740", string userName = null, string password = null)
  61. {
  62. if (null == m_client)
  63. return ErrorCode.InvalidState;
  64. if (null == axIPAddr || null == port)
  65. return ErrorCode.InvalidArgument;
  66. GeneralConfig_t config = new GeneralConfig_t();
  67. config.SetParameter("IPAddress", axIPAddr);
  68. config.SetParameter("Port", port);
  69. if (userName != null)
  70. config.SetParameter("User", userName);
  71. if (password != null)
  72. config.SetParameter("Password", password);
  73. config.SetParameter("SymbolNameEncode", m_bSymbolNameEncode ? "Yes" : "No");
  74. return m_client.Connect(config.Internal);
  75. }
  76. public uint ConnectViaGateway(string gatewayIPAddr, string gatewayPort, string axIPAddr, string port = "11740", string userName = null, string password = null)
  77. {
  78. if (null == m_client)
  79. return ErrorCode.InvalidState;
  80. if (null == gatewayIPAddr || null == gatewayPort || null == axIPAddr || null == port)
  81. return ErrorCode.InvalidArgument;
  82. GeneralConfig_t config = new GeneralConfig_t();
  83. //Add gateway connector configuration
  84. config.SetParameter("GatewayEnable", "Enabled");
  85. config.SetParameter("GatewayName", "Gateway1");
  86. config.SetParameter("GatewayIpAddress", gatewayIPAddr);
  87. config.SetParameter("GatewayPort", gatewayPort);
  88. //Add AX Device connect configuration
  89. config.SetParameter("IPAddress", axIPAddr);
  90. config.SetParameter("Port", port);
  91. if (userName != null)
  92. config.SetParameter("User", userName);
  93. if (password != null)
  94. config.SetParameter("Password", password);
  95. return m_client.Connect(config.Internal);
  96. }
  97. public uint ConnectByAddress(string axAddress /*logical address*/, string userName = null, string password = null)
  98. {
  99. if (null == m_client)
  100. return ErrorCode.InvalidState;
  101. if (null == axAddress)
  102. return ErrorCode.InvalidArgument;
  103. GeneralConfig_t config = new GeneralConfig_t();
  104. config.SetParameter("Address", axAddress);
  105. config.SetParameter("ConnectType", "Logical_Address");
  106. if (userName != null)
  107. config.SetParameter("User", userName);
  108. if (password != null)
  109. config.SetParameter("Password", password);
  110. return m_client.Connect(config.Internal);
  111. }
  112. public uint ConnectViaGatewayByAddress(string gatewayIPAddr, string gatewayPort, string axAddress /*logical address*/, string userName = null, string password = null)
  113. {
  114. if (null == m_client)
  115. return ErrorCode.InvalidState;
  116. if (null == gatewayIPAddr || null == gatewayPort || null == axAddress)
  117. return ErrorCode.InvalidArgument;
  118. GeneralConfig_t config = new GeneralConfig_t();
  119. //Add gateway connector configuration
  120. config.SetParameter("GatewayEnable", "Enabled");
  121. config.SetParameter("GatewayName", "Gateway1");
  122. config.SetParameter("GatewayIpAddress", gatewayIPAddr);
  123. config.SetParameter("GatewayPort", gatewayPort);
  124. //Add AX Device connect configuration
  125. config.SetParameter("Address", axAddress);
  126. config.SetParameter("ConnectType", "Logical_Address");
  127. if (userName != null)
  128. config.SetParameter("User", userName);
  129. if (password != null)
  130. config.SetParameter("Password", password);
  131. return m_client.Connect(config.Internal);
  132. }
  133. public uint Disconnect()
  134. {
  135. return m_client.Disconnect();
  136. }
  137. public uint Read(string symbolName, ref DataItem_t dataItem)
  138. {
  139. if (null == symbolName)
  140. return ErrorCode.InvalidArgument;
  141. if (dataItem is null)
  142. dataItem = new DataItem_t();
  143. var addressList = new string[1] { symbolName };
  144. var dataItemList = new DataItem_t[1] { dataItem };
  145. return Read(addressList, ref dataItemList);
  146. }
  147. public uint Read(string[] symbolNameList, ref DataItem_t[] dataItemList)
  148. {
  149. if (null == symbolNameList)
  150. return ErrorCode.InvalidArgument;
  151. if (0 == symbolNameList.Length)
  152. return ErrorCode.NothingToDo;
  153. if (dataItemList is null)
  154. {
  155. dataItemList = new DataItem_t[symbolNameList.Length];
  156. }
  157. else if(dataItemList.Length < symbolNameList.Length)
  158. {
  159. var tempList = dataItemList.ToList<DataItem_t>();
  160. while(tempList.Count < symbolNameList.Length)
  161. {
  162. tempList.Add(new DataItem_t());
  163. }
  164. dataItemList = tempList.ToArray<DataItem_t>();
  165. }
  166. else if(dataItemList.Length > symbolNameList.Length)
  167. {
  168. var tempList = dataItemList.ToList<DataItem_t>();
  169. tempList.RemoveRange(symbolNameList.Length, dataItemList.Length - symbolNameList.Length);
  170. dataItemList = tempList.ToArray<DataItem_t>();
  171. }
  172. var dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  173. for (int index = 0; index < dataItemList.Length; index++)
  174. {
  175. if (dataItemList[index] is null)
  176. dataItemList[index] = new DataItem_t();
  177. dataItemInternals[index] = dataItemList[index].Internal;
  178. }
  179. return m_client.Read(symbolNameList, dataItemInternals);
  180. }
  181. public void SetReadAsyncCallback(IReadCompleteCallback_t callback)
  182. {
  183. m_readCompleteCallback = callback;
  184. }
  185. public uint ReadAsync(string[] symbolNameList)
  186. {
  187. if (null == symbolNameList)
  188. return ErrorCode.InvalidArgument;
  189. if (0 == symbolNameList.Length)
  190. return ErrorCode.NothingToDo;
  191. uint transactionId = 0;
  192. if (m_readCompleteCallback != null) m_readCompleteCallback.Lock();
  193. uint status = m_client.ReadAsync(symbolNameList, m_readCompleteCallback, ref transactionId);
  194. if (ErrorCode.Good == status)
  195. {
  196. m_readCompleteCallback.AddRequestCache(transactionId, symbolNameList);
  197. }
  198. if (m_readCompleteCallback != null) m_readCompleteCallback.Unlock();
  199. return status;
  200. }
  201. public uint Write(string symbolName, DataItem_t dataItem, ref uint result)
  202. {
  203. if (null == symbolName || null == dataItem)
  204. return ErrorCode.InvalidArgument;
  205. var addressList = new string[1] { symbolName };
  206. var dataItemList = new DataItem_t[1] { dataItem };
  207. uint[] resultList = null;
  208. var status = Write(addressList, dataItemList, ref resultList);
  209. if (ErrorCode.Good == status)
  210. result = resultList[0];
  211. return status;
  212. }
  213. public uint Write(string[] symbolNameList, DataItem_t[] dataItemList, ref uint[] resultList)
  214. {
  215. if (null == symbolNameList || null == dataItemList)
  216. return ErrorCode.InvalidArgument;
  217. if (0 == symbolNameList.Length)
  218. return ErrorCode.NothingToDo;
  219. if (symbolNameList.Length != dataItemList.Length)
  220. return ErrorCode.InvalidArgument;
  221. IDataItem4CSharp[] dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  222. for (int index = 0; index < dataItemList.Length; index++)
  223. {
  224. dataItemInternals[index] = dataItemList[index] != null ? dataItemList[index].Internal : null;
  225. }
  226. resultList = new uint[symbolNameList.Length];
  227. return m_client.Write(symbolNameList, dataItemInternals, resultList);
  228. }
  229. public void SetWriteAsyncCallback(IWriteCompleteCallback_t callback)
  230. {
  231. m_writeCompleteCallback = callback;
  232. }
  233. public uint WriteAsync(string[] symbolNameList, DataItem_t[] dataItemList)
  234. {
  235. if (null == symbolNameList || null == dataItemList)
  236. return ErrorCode.InvalidArgument;
  237. if (0 == symbolNameList.Length)
  238. return ErrorCode.NothingToDo;
  239. if (symbolNameList.Length != dataItemList.Length)
  240. return ErrorCode.InvalidArgument;
  241. IDataItem4CSharp[] dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  242. for (int index = 0; index < dataItemList.Length; index++)
  243. {
  244. dataItemInternals[index] = dataItemList[index] != null ? dataItemList[index].Internal : null;
  245. }
  246. uint transactionId = 0;
  247. if (m_writeCompleteCallback != null) m_writeCompleteCallback.Lock();
  248. uint status = m_client.WriteAsync(symbolNameList, dataItemInternals, m_writeCompleteCallback, ref transactionId);
  249. if (ErrorCode.Good == status)
  250. m_writeCompleteCallback.AddRequestCache(transactionId, symbolNameList);
  251. if (m_writeCompleteCallback != null) m_writeCompleteCallback.Unlock();
  252. return status;
  253. }
  254. public void SetDataChangeCallback(IDataChangeCallback_t callback)
  255. {
  256. m_dataChangeCallback = callback;
  257. }
  258. public uint Subscribe(string[] symbolNameList, ref uint samplingInterval, ref uint publishingInterval, ref uint handler)
  259. {
  260. if (null == symbolNameList)
  261. return ErrorCode.InvalidArgument;
  262. if (0 == symbolNameList.Length)
  263. return ErrorCode.NothingToDo;
  264. return m_client.Subscribe(symbolNameList, m_dataChangeCallback, ref samplingInterval, ref publishingInterval, ref handler);
  265. }
  266. public uint Unsubscribe(uint handler)
  267. {
  268. return m_client.Unsubscribe(handler);
  269. }
  270. public uint ReadDeviceInfo(ref AXDeviceInfo_t axDeviceInfo)
  271. {
  272. var axClient = m_client as IAXClient4CSharp;
  273. if (null == axClient)
  274. return ErrorCode.NotSupported;
  275. DataValue_t dataValue = new DataValue_t();
  276. string[] propertyNames =
  277. {
  278. "Name",
  279. "Address",
  280. "Vendor",
  281. "Version",
  282. "SeriesName",
  283. "SerialNumber",
  284. "DeviceID",
  285. "DeviceType",
  286. "ChannelNumber",
  287. "EncryptedCommunication",
  288. "Ethernet"
  289. };
  290. IDataValue4CSharp[] propertyValues = new IDataValue4CSharp[propertyNames.Length];
  291. for (uint index = 0; index < propertyValues.Length; index++)
  292. {
  293. propertyValues[index] = DataValueFactory4CSharp.Create(DType.Undefined, null);
  294. }
  295. var status = axClient.ReadDeviceProperties(propertyNames, propertyValues);
  296. if (status == ErrorCode.Good) {
  297. if (null == axDeviceInfo)
  298. axDeviceInfo = new AXDeviceInfo_t();
  299. for (uint index = 0; index < propertyNames.Length; index++)
  300. {
  301. axDeviceInfo.Internal.SetValue(propertyNames[index], propertyValues[index]);
  302. }
  303. }
  304. for (uint index = 0; index < propertyValues.Length; index++)
  305. {
  306. DataValueFactory4CSharp.Destroy(propertyValues[index]);
  307. }
  308. return status;
  309. }
  310. public uint BrowseSymbol(string address, ref ArrayBase_t<SymbolNodeInfo_t> childSymbolNodes)
  311. {
  312. if (null == address)
  313. return ErrorCode.InvalidArgument;
  314. var axClient = m_client as IAXClient4CSharp;
  315. if (null == axClient)
  316. return ErrorCode.NotSupported;
  317. if (null == childSymbolNodes)
  318. childSymbolNodes = new ArrayBase_t<SymbolNodeInfo_t>();
  319. return axClient.BrowseSymbol(address, null, childSymbolNodes.Internal);
  320. }
  321. public uint GetSymbolProperty(string address, ref SymbolNodeInfo_t symbolProperty)
  322. {
  323. if (null == address)
  324. return ErrorCode.InvalidArgument;
  325. var axClient = m_client as IAXClient4CSharp;
  326. if (null == axClient)
  327. return ErrorCode.NotSupported;
  328. if (null == symbolProperty)
  329. symbolProperty = new SymbolNodeInfo_t();
  330. return axClient.GetSymbolProperty(address, symbolProperty.Internal);
  331. }
  332. public uint GetConnectionState(ref ConnectionState connState)
  333. {
  334. if (null == m_client)
  335. return ErrorCode.InvalidState;
  336. var outputArgument = new DataValue_t(DType.Int32);
  337. var state = m_client.DoCommand("GetConnectionState", null, outputArgument.Internal);
  338. if(ErrorCode.Good == state)
  339. {
  340. int intValue = 0;
  341. outputArgument.GetValue(ref intValue);
  342. connState = (ConnectionState)intValue;
  343. }
  344. outputArgument.Dispose();
  345. return state;
  346. }
  347. public uint SetConnectionCallback(IConnectionCallback_t callback)
  348. {
  349. var axClient2 = m_client as IAXClient24CSharp;
  350. if (null == axClient2)
  351. return ErrorCode.NotSupported;
  352. return axClient2.SetConnectionCallback(callback);
  353. }
  354. public uint CreateGroup(string[] symbolNameList, ref uint groupId, ref uint[] resultList)
  355. {
  356. var axClient2 = m_client as IAXClient24CSharp;
  357. if (null == axClient2)
  358. return ErrorCode.NotSupported;
  359. resultList = new uint[symbolNameList.Length];
  360. uint status = axClient2.CreateGroup(symbolNameList, ref groupId, resultList);
  361. if (ErrorCode.Good == status)
  362. {
  363. lock (m_locker)
  364. {
  365. m_symbolListCache.Add(groupId, symbolNameList);
  366. }
  367. }
  368. return status;
  369. }
  370. public uint GetSymbolCount(uint groupId, ref uint count)
  371. {
  372. var axClient2 = m_client as IAXClient24CSharp;
  373. if (null == axClient2)
  374. return ErrorCode.NotSupported;
  375. return axClient2.GetSymbolCount(groupId, ref count);
  376. }
  377. public uint GetSymbolAddress(uint groupId, uint index, ref string address)
  378. {
  379. var axClient2 = m_client as IAXClient24CSharp;
  380. if (null == axClient2)
  381. return ErrorCode.NotSupported;
  382. return axClient2.GetSymbolAddress(groupId, index, ref address);
  383. }
  384. public uint DeleteGroup(uint groupId)
  385. {
  386. var axClient2 = m_client as IAXClient24CSharp;
  387. if (null == axClient2)
  388. return ErrorCode.NotSupported;
  389. uint status = axClient2.DeleteGroup(groupId);
  390. if (ErrorCode.Good == status)
  391. {
  392. lock (m_locker)
  393. {
  394. m_symbolListCache.Remove(groupId);
  395. }
  396. }
  397. return status;
  398. }
  399. public uint Read(uint groupId, ref DataItem_t[] dataItemList)
  400. {
  401. var axClient2 = m_client as IAXClient24CSharp;
  402. if (null == axClient2)
  403. return ErrorCode.NotSupported;
  404. uint count = 0;
  405. uint status = axClient2.GetSymbolCount(groupId, ref count);
  406. if (status != ErrorCode.Good)
  407. return status;
  408. if (dataItemList is null)
  409. dataItemList = new DataItem_t[count];
  410. IDataItem4CSharp[] dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  411. for (int index = 0; index < dataItemList.Length; index++)
  412. {
  413. if (dataItemList[index] is null)
  414. dataItemList[index] = new DataItem_t();
  415. dataItemInternals[index] = dataItemList[index].Internal;
  416. }
  417. return axClient2.Read(groupId, dataItemInternals);
  418. }
  419. public uint ReadAsync(uint groupId)
  420. {
  421. var axClient2 = m_client as IAXClient24CSharp;
  422. if (null == axClient2)
  423. return ErrorCode.NotSupported;
  424. string[] symbolNameList = null;
  425. lock (m_locker)
  426. {
  427. if (!m_symbolListCache.ContainsKey(groupId))
  428. return ErrorCode.NotFound;
  429. symbolNameList = m_symbolListCache[groupId];
  430. }
  431. uint transactionId = 0;
  432. if (m_readCompleteCallback != null) m_readCompleteCallback.Lock();
  433. uint status = axClient2.ReadAsync(groupId, m_readCompleteCallback, ref transactionId);
  434. if (ErrorCode.Good == status)
  435. {
  436. m_readCompleteCallback.AddRequestCache(transactionId, symbolNameList);
  437. }
  438. if (m_readCompleteCallback != null) m_readCompleteCallback.Unlock();
  439. return status;
  440. }
  441. public uint Write(uint groupId, DataItem_t[] dataItemList, ref uint[] resultList)
  442. {
  443. if (null == dataItemList)
  444. return ErrorCode.InvalidArgument;
  445. var axClient2 = m_client as IAXClient24CSharp;
  446. if (null == axClient2)
  447. return ErrorCode.NotSupported;
  448. uint count = 0;
  449. uint status = axClient2.GetSymbolCount(groupId, ref count);
  450. if (status != ErrorCode.Good)
  451. return status;
  452. if (dataItemList.Length != count)
  453. return ErrorCode.InvalidArgument;
  454. IDataItem4CSharp[] dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  455. for (int index = 0; index < dataItemList.Length; index++)
  456. {
  457. dataItemInternals[index] = dataItemList[index] != null ? dataItemList[index].Internal : null;
  458. }
  459. resultList = new uint[dataItemList.Length];
  460. return axClient2.Write(groupId, dataItemInternals, resultList);
  461. }
  462. public uint WriteAsync(uint groupId, DataItem_t[] dataItemList)
  463. {
  464. if (null == dataItemList)
  465. return ErrorCode.InvalidArgument;
  466. var axClient2 = m_client as IAXClient24CSharp;
  467. if (null == axClient2)
  468. return ErrorCode.NotSupported;
  469. string[] symbolNameList = null;
  470. lock (m_locker)
  471. {
  472. if (!m_symbolListCache.ContainsKey(groupId))
  473. return ErrorCode.NotFound;
  474. symbolNameList = m_symbolListCache[groupId];
  475. }
  476. if (symbolNameList.Length != dataItemList.Length)
  477. return ErrorCode.InvalidArgument;
  478. IDataItem4CSharp[] dataItemInternals = new IDataItem4CSharp[dataItemList.Length];
  479. for (int index = 0; index < dataItemList.Length; index++)
  480. {
  481. dataItemInternals[index] = dataItemList[index] != null ? dataItemList[index].Internal : null;
  482. }
  483. uint transactionId = 0;
  484. if (m_writeCompleteCallback != null) m_writeCompleteCallback.Lock();
  485. uint status = axClient2.WriteAsync(groupId, dataItemInternals, m_writeCompleteCallback, ref transactionId);
  486. m_writeCompleteCallback.AddRequestCache(transactionId, symbolNameList);
  487. if (m_writeCompleteCallback != null) m_writeCompleteCallback.Unlock();
  488. return status;
  489. }
  490. public void Dispose()
  491. {
  492. Dispose(true);
  493. GC.SuppressFinalize(this);
  494. }
  495. protected virtual void Dispose(bool disposeManaged)
  496. {
  497. if (!m_disposed)
  498. {
  499. if (disposeManaged)
  500. {
  501. //do nothing
  502. }
  503. CommClientManager.DestroyClient(m_client);
  504. m_client = null;
  505. m_disposed = true;
  506. }
  507. }
  508. private static ICommClientManager4CSharp CommClientManager
  509. {
  510. get
  511. {
  512. if (!IACommService_t.IsInitialised)
  513. {
  514. IACommService_t.Initialise();
  515. }
  516. IIACommService4CSharp commService = IACommService_t.Instance();
  517. return commService != null ? commService.ClientManager() : null;
  518. }
  519. }
  520. }
  521. }