| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using TouchSocket.SerialPorts;
- namespace TeamAAS_VP.Core
- {
- /// <summary>
- /// 扭力测量仪
- /// </summary>
- public class Signal
- {
- //public static List<TouchSocket.SerialPorts.SerialPortClient> sp = new List<TouchSocket.SerialPorts.SerialPortClient>();
- SerialPortClient client;
- public static string BGSignalConfigPath = "..//Config//扭力测量仪配置文件.json";
- SignalRS232Config _bGModbus;
- public static string com;
- public static string baudRate = "9600";
- public static int parity = 0;
- public static string dataBits = "8";
- public static string stopBites = "1";
- public static int readTimeOut = 1000;
- public static bool isopen;
- private static SerialPort sp = new SerialPort();
- public async Task InitSignal()
- {
- if (File.Exists(BGSignalConfigPath))
- {
- try
- {
- var res = FileHelper.ReadJsonFile<SignalRS232Config>(BGSignalConfigPath);
- if (res != null)
- {
- foreach (var port in res.PortName)
- {
- com = port;
- OpenSerial(port, baudRate, parity, dataBits, stopBites, readTimeOut);
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("读取后台ModbusTCP通讯文件时出错!", ex);
- }
- }
- else
- {
- _bGModbus = new SignalRS232Config();
- _bGModbus.PortName.Add("COM4");
- FileHelper.WriteJsonFile(_bGModbus, BGSignalConfigPath);
- }
- }
- private static bool OpenSerial(string strPortName, string strBaudRate, int parity, string strDataBits, string strStopBits, int ReadTimeout)
- {
- try
- {
- sp.PortName = strPortName;
- sp.BaudRate = int.Parse(strBaudRate);
- sp.DataBits = int.Parse(strDataBits);
- sp.StopBits = (StopBits)int.Parse(strStopBits);
- sp.ReadTimeout = ReadTimeout;
- sp.Parity = (Parity)parity;
- sp.Open();
- return isopen = true;
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("扭力测量仪连接失败", ex);
- return isopen = false;
- }
- }
- public static string SerialPortSendAndRecive(string strPortName, string strBaudRate, int parity, string strDataBits, string strStopBits, int ReadTimeout, string Sendstring)
- {
- try
- {
- if (isopen)
- {
- string result = SendAndRecive(Sendstring);
- return result;
- }
- return "Open SerialPort Fail";
- }
- catch (Exception ex)
- {
- return "Send Fail" + ex.Message;
- }
- }
- private static string SendAndRecive(string str)
- {
- try
- {
- byte[] array = strToHexByte(str.Trim());
- sp.Write(array, 0, array.Length);
- Thread.Sleep(100);
- int bytesToRead = sp.BytesToRead;
- byte[] array2 = new byte[bytesToRead];
- sp.Read(array2, 0, bytesToRead);
- string text = byteToHexStr(array2);
- return text;
- }
- catch (Exception ex)
- {
- return "EOROR" + ex.Message;
- }
- }
- public static string byteToHexStr(byte[] bytes)
- {
- string text = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- text = text + bytes[i].ToString("X2") + " ";
- }
- }
- return text;
- }
- private static byte[] strToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if (hexString.Length % 2 != 0)
- {
- hexString += " ";
- }
- byte[] array = new byte[hexString.Length / 2];
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
- }
- return array;
- }
- public double SendAndReceive()
- {
- try
- {
- var data = SerialPortSendAndRecive(com, baudRate, parity, dataBits, stopBites, readTimeOut, "01 04 00 00 00 01 31 CA");
- if (data == "Open SerialPort Fail")
- {
- MessageBox.Show("扭力测量仪连接失败");
- }
- else if (data.Contains("Send Fail"))
- {
- MessageBox.Show("扭力测量发送数据失败");
- }
- string[] parts = data.Split(' ');
- string mystr = string.Join(" ", parts, 3, 4);
- string hex = mystr.Replace(" ", ""); // 16 进制字符串
- int decimalValue = Convert.ToInt32(hex, 16); // 转换为 10 进制整数
- double response = decimalValue / 100.0;
- return response;
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("扭力测量仪接收信息时出错!", ex);
- return 0;
- }
- }
- }
- public class SignalRS232Config
- {
- public ObservableCollection<string> PortName { get; set; }
- public SignalRS232Config()
- {
- PortName = new ObservableCollection<string>();
- }
- }
- }
|