You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Mesnac.DoUtils/DoControl.cs

168 lines
4.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Mesnac.DoUtils.enumInfo;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Mesnac.DoUtils
{
/// <summary>
/// 报警灯控制类
/// </summary>
public sealed class DoControl
{
public SerialPort serialPort = new SerialPort("COM5");
private static readonly Lazy<DoControl> lazy = new Lazy<DoControl>(() => new DoControl());
public static DoControl Instance => lazy.Value;
public bool AlarmState { get; private set; } = false;
private DoControl()
{
serialPort.BaudRate = 115200;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
}
/// <summary>
/// 串口启动
/// </summary>
public void ComOn()
{
try
{
if (!serialPort.IsOpen)
{
serialPort.Open();
ICSharpCode.Core.LoggingService<DoControl>.Debug($"打开报警器串口成功,串口:{serialPort.PortName};波特率:{serialPort.BaudRate}");
}
}catch (Exception ex)
{
ICSharpCode.Core.LoggingService<DoControl>.Error($"报警器串口打开异常,串口:{serialPort.PortName};波特率:{serialPort.BaudRate};异常信息:{ex.Message}");
}
}
/// <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启停控制
/// 如果收到红灯亮的信号AlarmState会变为True; 收到红灯灭的信号AlarmState设置为false
/// </summary>
/// <param name="dOName">DegitalOut接口名称</param>
/// <param name="dOOnOff">启停状态</param>
public void DOControl(DOName dOName, DOOnOff dOOnOff)
{
if(dOName.Equals(DOName.Red) && dOOnOff.Equals(DOOnOff.On))
{
AlarmState = true;
}
if(dOName.Equals(DOName.Red) && dOOnOff.Equals(DOOnOff.Off))
{
AlarmState = false;
}
if (serialPort.IsOpen)
{
serialPort.Write(new byte[] { 0xE3, 0x01, 0x09, (byte)dOName, (byte)dOOnOff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0, 12);
}
else
{
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);
}
}
}