Signal.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.IO.Ports;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using TouchSocket.SerialPorts;
  12. namespace TeamAAS_VP.Core
  13. {
  14. /// <summary>
  15. /// 扭力测量仪
  16. /// </summary>
  17. public class Signal
  18. {
  19. //public static List<TouchSocket.SerialPorts.SerialPortClient> sp = new List<TouchSocket.SerialPorts.SerialPortClient>();
  20. SerialPortClient client;
  21. public static string BGSignalConfigPath = "..//Config//扭力测量仪配置文件.json";
  22. SignalRS232Config _bGModbus;
  23. public static string com;
  24. public static string baudRate = "9600";
  25. public static int parity = 0;
  26. public static string dataBits = "8";
  27. public static string stopBites = "1";
  28. public static int readTimeOut = 1000;
  29. public static bool isopen;
  30. private static SerialPort sp = new SerialPort();
  31. public async Task InitSignal()
  32. {
  33. if (File.Exists(BGSignalConfigPath))
  34. {
  35. try
  36. {
  37. var res = FileHelper.ReadJsonFile<SignalRS232Config>(BGSignalConfigPath);
  38. if (res != null)
  39. {
  40. foreach (var port in res.PortName)
  41. {
  42. com = port;
  43. OpenSerial(port, baudRate, parity, dataBits, stopBites, readTimeOut);
  44. }
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. LogHelper.WriteLogError("读取后台ModbusTCP通讯文件时出错!", ex);
  50. }
  51. }
  52. else
  53. {
  54. _bGModbus = new SignalRS232Config();
  55. _bGModbus.PortName.Add("COM4");
  56. FileHelper.WriteJsonFile(_bGModbus, BGSignalConfigPath);
  57. }
  58. }
  59. private static bool OpenSerial(string strPortName, string strBaudRate, int parity, string strDataBits, string strStopBits, int ReadTimeout)
  60. {
  61. try
  62. {
  63. sp.PortName = strPortName;
  64. sp.BaudRate = int.Parse(strBaudRate);
  65. sp.DataBits = int.Parse(strDataBits);
  66. sp.StopBits = (StopBits)int.Parse(strStopBits);
  67. sp.ReadTimeout = ReadTimeout;
  68. sp.Parity = (Parity)parity;
  69. sp.Open();
  70. return isopen = true;
  71. }
  72. catch (Exception ex)
  73. {
  74. LogHelper.WriteLogError("扭力测量仪连接失败", ex);
  75. return isopen = false;
  76. }
  77. }
  78. public static string SerialPortSendAndRecive(string strPortName, string strBaudRate, int parity, string strDataBits, string strStopBits, int ReadTimeout, string Sendstring)
  79. {
  80. try
  81. {
  82. if (isopen)
  83. {
  84. string result = SendAndRecive(Sendstring);
  85. return result;
  86. }
  87. return "Open SerialPort Fail";
  88. }
  89. catch (Exception ex)
  90. {
  91. return "Send Fail" + ex.Message;
  92. }
  93. }
  94. private static string SendAndRecive(string str)
  95. {
  96. try
  97. {
  98. byte[] array = strToHexByte(str.Trim());
  99. sp.Write(array, 0, array.Length);
  100. Thread.Sleep(100);
  101. int bytesToRead = sp.BytesToRead;
  102. byte[] array2 = new byte[bytesToRead];
  103. sp.Read(array2, 0, bytesToRead);
  104. string text = byteToHexStr(array2);
  105. return text;
  106. }
  107. catch (Exception ex)
  108. {
  109. return "EOROR" + ex.Message;
  110. }
  111. }
  112. public static string byteToHexStr(byte[] bytes)
  113. {
  114. string text = "";
  115. if (bytes != null)
  116. {
  117. for (int i = 0; i < bytes.Length; i++)
  118. {
  119. text = text + bytes[i].ToString("X2") + " ";
  120. }
  121. }
  122. return text;
  123. }
  124. private static byte[] strToHexByte(string hexString)
  125. {
  126. hexString = hexString.Replace(" ", "");
  127. if (hexString.Length % 2 != 0)
  128. {
  129. hexString += " ";
  130. }
  131. byte[] array = new byte[hexString.Length / 2];
  132. for (int i = 0; i < array.Length; i++)
  133. {
  134. array[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
  135. }
  136. return array;
  137. }
  138. public double SendAndReceive()
  139. {
  140. try
  141. {
  142. var data = SerialPortSendAndRecive(com, baudRate, parity, dataBits, stopBites, readTimeOut, "01 04 00 00 00 01 31 CA");
  143. if (data == "Open SerialPort Fail")
  144. {
  145. MessageBox.Show("扭力测量仪连接失败");
  146. }
  147. else if (data.Contains("Send Fail"))
  148. {
  149. MessageBox.Show("扭力测量发送数据失败");
  150. }
  151. string[] parts = data.Split(' ');
  152. string mystr = string.Join(" ", parts, 3, 4);
  153. string hex = mystr.Replace(" ", ""); // 16 进制字符串
  154. int decimalValue = Convert.ToInt32(hex, 16); // 转换为 10 进制整数
  155. double response = decimalValue / 100.0;
  156. return response;
  157. }
  158. catch (Exception ex)
  159. {
  160. LogHelper.WriteLogError("扭力测量仪接收信息时出错!", ex);
  161. return 0;
  162. }
  163. }
  164. }
  165. public class SignalRS232Config
  166. {
  167. public ObservableCollection<string> PortName { get; set; }
  168. public SignalRS232Config()
  169. {
  170. PortName = new ObservableCollection<string>();
  171. }
  172. }
  173. }