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.

160 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using TouchSocket.Sockets;
namespace SlnMesnac.RfidUpload.Business
{
/// <summary>
/// 串口通讯类
/// </summary>
public class SerialPortBusiness
{
#region 单例实现
private static readonly Lazy<SerialPortBusiness> _lazy = new Lazy<SerialPortBusiness>(() => new SerialPortBusiness());
public static SerialPortBusiness Instance
{
get
{
return _lazy.Value;
}
}
#endregion
#region 委托事件
/// <summary>
/// 刷新标签解析
/// </summary>
public delegate void ReceivedData(byte[] buffer);
public event ReceivedData ReceivedDataEvent;
/// <summary>
/// 刷新日志内容
/// </summary>
public delegate void RefreshLogMessage(string message);
public event RefreshLogMessage RefreshLogMessageEvent;
#endregion
private SerialPort _serialPort;
private SerialPortBusiness()
{
}
/// <summary>
/// 打开串口
/// </summary>
//public void Open()
//{
// string port = System.IO.Ports.SerialPort.GetPortNames().FirstOrDefault();
// _serialPort = new SerialPort(port, 115200, Parity.None, 8, StopBits.One);
// _serialPort.DataReceived += SerialPortDataReceived;
// _serialPort.Open();
//}
/// <summary>
/// 打开串口
/// </summary>
/// <param name="serialPort">串口号</param>
/// <param name="baudRate">波特率</param>
/// <param name="parity">校验位None = 0,Odd = 1, Even = 2,Mark = 3,Space = 4,</param>
/// <param name="dataBits">数据位</param>
/// <param name="stopBits">停止位</param>
public void OpenSerialPort(string serialPort, int baudRate, int parity, int dataBits, int stopBits)
{
if (string.IsNullOrEmpty(serialPort))
{
throw new ArgumentException("串口号不能为空");
}
_serialPort = new SerialPort(serialPort, baudRate, (System.IO.Ports.Parity)parity, dataBits, (StopBits)stopBits);
_serialPort.DataReceived += SerialPortDataReceived;
_serialPort.Open();
}
/// <summary>
/// 关闭串口
/// </summary>
public void Close()
{
if (_serialPort != null)
{
_serialPort.Close();
}
else
{
throw new ArgumentException("_serialPort == null");
}
}
/// <summary>
/// 接收数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(200);
int bytesToRead = _serialPort.BytesToRead;
byte[] buffer = new byte[bytesToRead];
_serialPort.Read(buffer, 0, bytesToRead);
RefreshLogMessageEvent?.Invoke("Received data: " + BitConverter.ToString(buffer).Replace("-", " "));
if (CheckReceivedData(buffer))
{
ReceivedDataEvent?.Invoke(buffer);
}
else
{
//RefreshLogMessageEvent?.Invoke("心跳指令不进行逻辑处理");
//.Send(item.Id, "heartbeat");
_serialPort.WriteLine("heartbeat");
}
}
catch (Exception ex)
{
RefreshLogMessageEvent?.Invoke($"接收数据异常:{ex.Message}");
}
}
/// <summary>
/// 判断是否为业务数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private bool CheckReceivedData(byte[] data)
{
if (data.Length > 4)
{
return true;
}
return false;
}
/// <summary>
/// 获取系统内的串口
/// </summary>
/// <param name="serialPorts"></param>
public void GetSerialPorts(out List<string> serialPorts)
{
serialPorts = SerialPort.GetPortNames().ToList();
}
}
}