using CommonFunc.Tools; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XGLFinishPro.Tools { internal class SerialHelper { public SerialPort serialPort = new SerialPort(Utils.GetAppSetting("SerialPort")); public SerialHelper() { //serialPort.PortName = "COM5"; serialPort.BaudRate = 115200; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; serialPort.Parity = Parity.None; } /// /// 串口启动 /// public void ComOn() { if (!serialPort.IsOpen) { serialPort.Open(); } } /// /// 串口关闭 /// public void ComOff() { if (serialPort.IsOpen) { serialPort.Close(); } } /// /// DO1设备启动(红灯) /// public void redLightOn() { DOControl(DOName.Red, DOOnOff.On); } /// /// DO2设备启动(绿灯) /// public void greenLightOn() { DOControl(DOName.Green, DOOnOff.On); } /// /// DO3设备启动(黄灯) /// public void yellowLightOn() { DOControl(DOName.Yellow, DOOnOff.On); } /// /// DO1设备关闭(红灯) /// public void redLightOff() { DOControl(DOName.Red, DOOnOff.Off); } /// /// DO2设备关闭(绿灯) /// public void greenLightOff() { DOControl(DOName.Green, DOOnOff.Off); } /// /// DO3设备关闭(黄灯) /// public void yellowLightOff() { DOControl(DOName.Yellow, DOOnOff.Off); } /// /// DO4设备开启(蜂鸣器) /// public void buzzerOn() { DOControl(DOName.Buzzer, DOOnOff.On); } /// /// DO4设备关闭(蜂鸣器) /// public void buzzerOff() { DOControl(DOName.Buzzer, DOOnOff.Off); } /// /// 重置串口和DO状态 /// public void Reset() { SetOff(); ComOff(); } /// /// 重置DO状态 /// public void SetOff() { foreach (DOName dOName in Enum.GetValues(typeof(DOName))) { DOControl(dOName, DOOnOff.Off); } } /// /// DO启停控制 /// /// DegitalOut接口名称 /// 启停状态 public void DOControl(DOName dOName, DOOnOff dOOnOff) { //ComOn(); serialPort.Write(new byte[] { 0xE3, 0x01, 0x09, (byte)dOName, (byte)dOOnOff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0, 12); } /// /// 文本框传输数据控制COM5串口 /// public void DOTestSend(string str) { //ComOn(); string[] strArray = str.Split(' '); byte[] bytes = new byte[strArray.Length]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = Convert.ToByte(strArray[i], 16); } serialPort.Write(bytes, 0, bytes.Length); } } public enum DOName { Red = 0x01, Green = 0x02, Yellow = 0x03, Buzzer = 0x04, dO_5 = 0x05, dO_6 = 0x06, dO_7 = 0x07, dO_8 = 0x08, } public enum DOOnOff { Off = 0x00, On = 0x01 } }