版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

HeightDev.cs 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9. namespace ProductionControl.Device
  10. {
  11. public class HeightDev:IDisposable
  12. {
  13. //public double HeightValue { private set; get; }
  14. public Action<WarningEnum, string> WarningEvent;
  15. /// <summary>
  16. /// 值<值>
  17. /// </summary>
  18. public Action<double> HeightEvent;
  19. /// <summary>
  20. /// 是否打开设备成功
  21. /// </summary>
  22. public bool IsInit { get; private set; } = false;
  23. private System.Timers.Timer timer = new System.Timers.Timer();
  24. private Socket socketObj;
  25. private double decimalNum=-1;
  26. private bool _isDebug = false;
  27. public HeightDev(bool isDebug)
  28. {
  29. _isDebug = isDebug;
  30. }
  31. public HeightDev()
  32. {
  33. }
  34. public bool start(string net_ip, int net_port)
  35. {
  36. try
  37. {
  38. // 1、创建socket对象
  39. socketObj = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  40. // 2、字符串ip转网络ip, 端口号转int
  41. IPAddress ip = IPAddress.Parse(net_ip);
  42. int port = Convert.ToInt32(net_port);
  43. // 3、连接服务器
  44. socketObj.Connect(ip, port);
  45. IsInit = true;
  46. if (_isDebug)
  47. {
  48. timer.Elapsed += Timer_Elapsed;
  49. timer.Interval = 500;
  50. timer.Start();
  51. }
  52. return true;
  53. }
  54. catch (Exception ex)
  55. {
  56. WarningEvent?.Invoke(WarningEnum.High,ex.Message);
  57. return false;
  58. }
  59. }
  60. public void stop()
  61. {
  62. if (!IsInit) return;
  63. //关闭客户端
  64. try
  65. {
  66. IsInit = false;
  67. if (_isDebug)
  68. {
  69. timer.Elapsed -= Timer_Elapsed;
  70. timer.Stop();
  71. }
  72. socketObj.Close();
  73. }
  74. catch { }
  75. }
  76. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  77. {
  78. if (!IsInit) return;
  79. timer.Stop();
  80. getHeight();
  81. timer.Start();
  82. }
  83. public double getHeight()
  84. {
  85. try
  86. {
  87. if (!IsInit) return -1;
  88. if (decimalNum == -1)
  89. {
  90. decimalNum = getDecimal();
  91. if (decimalNum < 0)
  92. throw new Exception("HeightDev start Err!");
  93. }
  94. // 发送消息
  95. socketObj.Send(Encoding.Default.GetBytes("SR,01,037\r\n"));
  96. // 接收消息
  97. byte[] buffer = new byte[1024];
  98. socketObj.Receive(buffer);
  99. string res = System.Text.Encoding.Default.GetString(buffer);
  100. if (res.IndexOf("SR,01,037,") != 0)
  101. return -1;
  102. res = res.Replace("SR,01,037,", "");
  103. double val;
  104. if (decimalNum < 1)
  105. val = Convert.ToDouble(res);
  106. else
  107. val = Convert.ToDouble(res) / decimalNum;
  108. val = Math.Abs(val) * 1000;
  109. HeightEvent?.Invoke(val);
  110. return val;
  111. }
  112. catch(Exception ex)
  113. {
  114. WarningEvent?.Invoke(WarningEnum.Low, ex.Message);
  115. return -1;
  116. }
  117. }
  118. private double getDecimal()
  119. {
  120. if (!IsInit) return -1;
  121. // 发送消息
  122. socketObj.Send(Encoding.Default.GetBytes("FR,01,037\r\n"));
  123. // 接收消息
  124. byte[] buffer = new byte[1024];
  125. socketObj.Receive(buffer);
  126. string res = System.Text.Encoding.Default.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.None)[0];
  127. if (res.IndexOf("FR,01,037,") != 0)
  128. return -1;
  129. res= res.Replace("FR,01,037,","");
  130. return Math.Pow(10,Convert.ToInt32(res));
  131. }
  132. public bool reset()
  133. {
  134. if (!IsInit) return false;
  135. // 发送消息
  136. socketObj.Send(Encoding.Default.GetBytes("SW,01,001,+000000001\r\n"));
  137. // 接收消息
  138. byte[] buffer = new byte[1024];
  139. socketObj.Receive(buffer);
  140. string res = System.Text.Encoding.Default.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.None)[0];
  141. return res == "SW,01,001";
  142. }
  143. public void Dispose()
  144. {
  145. stop();
  146. }
  147. }
  148. }