using GummingCommon; using NModbus; using System; using System.Collections.Generic; using System.IO; using System.IO.Ports; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace GummingControl { public class RS485 { private static SerialPort sp; public static void CreateLink() { if (!Global.IsRelease) { return; } if (sp == null) { sp = new SerialPort(); sp.PortName = "COM15"; sp.BaudRate = 9600; sp.DataBits = 8; sp.StopBits = StopBits.One; sp.Parity = Parity.None; sp.ReadTimeout = 1000; sp.WriteTimeout = 1000; sp.Open(); } } /// /// /// /// 0 /// 0 /// 0 /// public static double GetTemprature() { if (!Global.IsRelease || sp == null) { byte[] test = new byte[] { 0X01, 0X02, 0X33, 0X33, 0X30, 0X30, 0X30, 0X03, 0X3D, 0X32, 0X0D };//30.00 string temp = ""; for (int i = 3; i < 7; i++) { temp = string.Format("{0}{1}", temp, (test[i] - 0X30)); } int realTemp = Convert.ToInt32(temp); Global.CurrentCPTemprature = realTemp / 100.00; return Global.CurrentCPTemprature; } try { byte[] data = new byte[] { 0X01, 0X05, 0X33, 0X03, 0X33, 0X0D }; sp.Write(data, 0, data.Length); Thread.Sleep(1000); byte[] buffer = new byte[12]; int count = sp.Read(buffer, 0, buffer.Length); //校验 string demp = TypeHelper.IntToHex(buffer[3] + buffer[4] + buffer[5] + buffer[6]); string value = demp.Length > 1 ? demp.Substring(0, 1) : "0"; byte check1 = (byte)(0X30 + Convert.ToInt32(value, 16)); value = demp.Length > 1 ? demp.Substring(1, 1) : "0"; byte check2 = (byte)(0X30 + Convert.ToInt32(value, 16)); if (check1 != buffer[8] || check2 != buffer[9]) { return -1; } string temp = ""; for (int i = 3; i < 7; i++) { temp = string.Format("{0}{1}", temp, (buffer[i] >= 0X30 ? buffer[i] - 0X30 : 0)); } int realTemp = Convert.ToInt32(temp); Global.CurrentCPTemprature = realTemp / 100.00; return Global.CurrentCPTemprature; } catch { CreateLink(); return -1; } } public static bool SetTemprature(double temp) { if (!Global.IsRelease || sp == null) { return true; } try { byte[] data = new byte[] { 0X01, 0X02, 0X31, 0X33, 0X34, 0X35, 0X36, 0X03, 0X3D, 0X32, 0X0D }; string demp = temp.ToString("00.00"); string value = demp.Substring(0, 1); data[3] = (byte)(0X30 + Convert.ToByte(value)); value = demp.Substring(1, 1); data[4] = (byte)(0X30 + Convert.ToByte(value)); value = demp.Substring(3, 1); data[5] = (byte)(0X30 + Convert.ToByte(value)); value = demp.Substring(4, 1); data[6] = (byte)(0X30 + Convert.ToByte(value)); demp = TypeHelper.IntToHex(data[3] + data[4] + data[5] + data[6]); value = demp.Substring(0, 1); data[8] = (byte)(0X30 + Convert.ToInt32(value, 16)); value = demp.Substring(1, 1); data[9] = (byte)(0X30 + Convert.ToInt32(value, 16)); sp.Write(data, 0, data.Length); Thread.Sleep(1000); byte[] buffer = new byte[3];//0X01 0X06 0X0D int count = sp.Read(buffer, 0, buffer.Length); string result = ""; for (int i = 0; i < 3; i++) { result = string.Format("{0}{1}", result, buffer[i]); } int realTemp = Convert.ToInt32(result); return realTemp == 1613; } catch { CreateLink(); return false; } } /// /// /// /// 0 /// 0 /// 0 /// public static int GetWarning() { if (!Global.IsRelease || sp == null) { byte[] test = new byte[] { 0X01, 0X02, 0X34, 0X30, 0X30, 0X30, 0X30, 0X03, 0X3C, 0X30, 0X0D }; string temp = ""; for (int i = 3; i < 7; i++) { temp = string.Format("{0}{1}", temp, (test[i] - 0X30)); } int realTemp = Convert.ToInt32(temp); return realTemp; } { byte[] data = new byte[] { 0X01, 0X05, 0X34, 0X03, 0X34, 0X0D }; sp.Write(data, 0, data.Length); Thread.Sleep(1000); byte[] buffer = new byte[12]; int count = sp.Read(buffer, 0, buffer.Length); //校验 string demp = TypeHelper.IntToHex(buffer[3] + buffer[4] + buffer[5] + buffer[6]); string value = demp.Length > 1 ? demp.Substring(0, 1) : "0"; byte check1 = (byte)(0X30 + Convert.ToInt32(value, 16)); value = demp.Length > 1 ? demp.Substring(1, 1) : "0"; byte check2 = (byte)(0X30 + Convert.ToInt32(value, 16)); if (check1 != buffer[8] || check2 != buffer[9]) { return -1; } string temp = ""; for (int i = 3; i < 7; i++) { temp = string.Format("{0}{1}", temp, (buffer[i] >= 0X30 ? buffer[i] - 0X30 : 0)); } int realTemp = Convert.ToInt32(temp); return realTemp; } } public static bool SetStart() { if (!Global.IsRelease) { return true; } byte[] data = new byte[] { 0X01, 0X02, 0X39, 0X30, 0X30, 0X30, 0X31, 0X03, 0X3C, 0X31, 0X0D }; sp.Write(data, 0, data.Length); Thread.Sleep(1000); byte[] buffer = new byte[3];//0X01 0X06 0X0D int count = sp.Read(buffer, 0, buffer.Length); string temp = ""; for (int i = 0; i < 3; i++) { temp = string.Format("{0}{1}", temp, buffer[i]); } int realTemp = Convert.ToInt32(temp); return realTemp == 1613; } } }