|
|
using Mesnac.DoUtils.enumInfo;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO.Ports;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
namespace Mesnac.DoUtils
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 报警灯控制类
|
|
|
/// </summary>
|
|
|
public sealed class DoControl
|
|
|
{
|
|
|
public SerialPort serialPort;
|
|
|
|
|
|
private static readonly Lazy<DoControl> lazy = new Lazy<DoControl>(() => new DoControl());
|
|
|
public static DoControl Instance
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return lazy.Value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private DoControl()
|
|
|
{
|
|
|
serialPort.BaudRate = 115200;
|
|
|
serialPort.DataBits = 8;
|
|
|
serialPort.StopBits = StopBits.One;
|
|
|
serialPort.Parity = Parity.None;
|
|
|
serialPort = new SerialPort("COM5");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 串口启动
|
|
|
/// </summary>
|
|
|
public void ComOn()
|
|
|
{
|
|
|
if (!serialPort.IsOpen)
|
|
|
{
|
|
|
serialPort.Open();
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 串口关闭
|
|
|
/// </summary>
|
|
|
public void ComOff()
|
|
|
{
|
|
|
if (serialPort.IsOpen)
|
|
|
{
|
|
|
serialPort.Close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO1设备启动(红灯)
|
|
|
/// </summary>
|
|
|
public void redLightOn()
|
|
|
{
|
|
|
DOControl(DOName.Red, DOOnOff.On);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO2设备启动(绿灯)
|
|
|
/// </summary>
|
|
|
public void greenLightOn()
|
|
|
{
|
|
|
DOControl(DOName.Green, DOOnOff.On);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO3设备启动(蜂鸣器)
|
|
|
/// </summary>
|
|
|
public void buzzerOn()
|
|
|
{
|
|
|
DOControl(DOName.Buzzer, DOOnOff.On);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO1设备关闭(红灯)
|
|
|
/// </summary>
|
|
|
public void redLightOff()
|
|
|
{
|
|
|
DOControl(DOName.Red, DOOnOff.Off);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO2设备关闭(绿灯)
|
|
|
/// </summary>
|
|
|
public void greenLightOff()
|
|
|
{
|
|
|
DOControl(DOName.Green, DOOnOff.Off);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO3设备关闭(蜂鸣器)
|
|
|
/// </summary>
|
|
|
public void buzzerOff()
|
|
|
{
|
|
|
DOControl(DOName.Buzzer, DOOnOff.On);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 重置串口和DO状态
|
|
|
/// </summary>
|
|
|
public void Reset()
|
|
|
{
|
|
|
SetOff();
|
|
|
ComOff();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 重置DO状态
|
|
|
/// </summary>
|
|
|
public void SetOff()
|
|
|
{
|
|
|
foreach (DOName dOName in Enum.GetValues(typeof(DOName)))
|
|
|
{
|
|
|
DOControl(dOName, DOOnOff.Off);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DO启停控制
|
|
|
/// </summary>
|
|
|
/// <param name="dOName">DegitalOut接口名称</param>
|
|
|
/// <param name="dOOnOff">启停状态</param>
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 文本框传输数据控制COM5串口
|
|
|
/// </summary>
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
} |