InovanceTcp.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using HandyControl.Controls;
  2. using HslCommunication;
  3. using HslCommunication.Profinet.Inovance;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace LogoForceTestApp.Modules.MainModule
  11. {
  12. public class InovanceTcp//汇川H5U
  13. {
  14. public InovanceTcpNet tcpNet;
  15. public bool Connection(string ip, int port)
  16. {
  17. tcpNet = new InovanceTcpNet(ip, port, 1);//ip,端口,站号
  18. //tcpNet.Station = 1;
  19. //tcpNet.AddressStartWithZero = true;
  20. //tcpNet.IsStringReverse = false;
  21. tcpNet.Series = HslCommunication.Profinet.Inovance.InovanceSeries.H5U;
  22. //tcpNet.DataFormat = HslCommunication.Core.DataFormat.CDAB;
  23. //tcpNet.Series = InovanceSeries.H5U;
  24. OperateResult connect = tcpNet.ConnectServer();
  25. if (connect.IsSuccess)
  26. {
  27. //连接成功
  28. return true;
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. }
  35. public void ConnectionClose()
  36. {
  37. tcpNet.ConnectClose();
  38. }
  39. public void WriteDate<T>(string address, T t)
  40. {
  41. dynamic temp = t;
  42. OperateResult result = tcpNet.Write(address, temp);//WriteDate("D200",10);WriteDate("W5.0",true);
  43. Thread.Sleep(500);
  44. }
  45. public bool[] Read(string add,ushort lenght=1)
  46. {
  47. bool[] obj = tcpNet.ReadBool(add, lenght).Content;
  48. return obj;
  49. }
  50. public Tuple<bool,bool> ReadBool(string add)
  51. {
  52. var obj = tcpNet.ReadBool(add);
  53. return new Tuple<bool, bool>(obj.IsSuccess, obj.Content);
  54. }
  55. public int ReadInt(string add)//short
  56. {
  57. var result = tcpNet.ReadInt16(add);
  58. var obj = result.Content;
  59. if (result.IsSuccess)
  60. {
  61. return obj;
  62. }
  63. else
  64. {
  65. return -1;
  66. }
  67. }
  68. //public int ReadInt(string add)
  69. //{
  70. // var obj = tcpNet.ReadInt32(add).Content;
  71. // return obj;
  72. //}
  73. public void WriteInt(string add,int val)
  74. {
  75. tcpNet.Write(add, val);
  76. }
  77. public void WriteBool(string add, bool val)
  78. {
  79. tcpNet.Write(add, val);
  80. }
  81. }
  82. }