|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SerialPort = System.IO.Ports.SerialPort;
|
|
|
|
|
|
|
|
|
|
namespace ZWGXPICK_SYS.SerialPort
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 串口通讯工具
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SerialPortUtils
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private System.IO.Ports.SerialPort SerialPort;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 串口名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ComName { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 波特率
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int ComBaud { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据位
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int DataBits { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接收数据长度
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int ReceivedBytesThreshold { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 停止位
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StopBits StopBits { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 校验位
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Parity Parity { get; private set; }
|
|
|
|
|
|
|
|
|
|
public event EventHandler<byte[]> SerialDataReceived;
|
|
|
|
|
|
|
|
|
|
public event EventHandler<MessageEventArgs> SerialMessage;
|
|
|
|
|
|
|
|
|
|
#region builder
|
|
|
|
|
private SerialPortUtils(SerialPortUtilsBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
ComName = builder.comName;
|
|
|
|
|
ComBaud = builder.comBaud;
|
|
|
|
|
DataBits = builder.dataBits;
|
|
|
|
|
StopBits = builder.stopBits;
|
|
|
|
|
Parity = builder.parity;
|
|
|
|
|
ReceivedBytesThreshold = builder.receivedBytesThreshold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SerialPortUtilsBuilder Builder(string comName)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return new SerialPortUtilsBuilder(comName);
|
|
|
|
|
}
|
|
|
|
|
public class SerialPortUtilsBuilder
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
internal string comName;
|
|
|
|
|
|
|
|
|
|
internal int comBaud = 9600;
|
|
|
|
|
|
|
|
|
|
internal int dataBits = 8;
|
|
|
|
|
|
|
|
|
|
internal StopBits stopBits = System.IO.Ports.StopBits.One;
|
|
|
|
|
|
|
|
|
|
internal int receivedBytesThreshold = 1;
|
|
|
|
|
|
|
|
|
|
public Parity parity = Parity.None;
|
|
|
|
|
|
|
|
|
|
public SerialPortUtilsBuilder(string comName)
|
|
|
|
|
{
|
|
|
|
|
this.comName = comName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SerialPortUtilsBuilder ComBaud(int comBaud)
|
|
|
|
|
{
|
|
|
|
|
this.comBaud = comBaud;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
public SerialPortUtilsBuilder DataBits(int dataBits)
|
|
|
|
|
{
|
|
|
|
|
this.dataBits = dataBits;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
public SerialPortUtilsBuilder StopBits(StopBits stopBits)
|
|
|
|
|
{
|
|
|
|
|
this.stopBits = stopBits;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
public SerialPortUtilsBuilder StopBits(Parity parity)
|
|
|
|
|
{
|
|
|
|
|
this.parity = parity;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
public SerialPortUtilsBuilder ReceivedBytesThreshold(int receivedBytesThreshold)
|
|
|
|
|
{
|
|
|
|
|
this.receivedBytesThreshold = receivedBytesThreshold;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SerialPortUtils Build()
|
|
|
|
|
{
|
|
|
|
|
return new SerialPortUtils(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion builder
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前电脑的串口列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string[] GetPortNames()
|
|
|
|
|
{
|
|
|
|
|
return System.IO.Ports.SerialPort.GetPortNames();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Open()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SerialPort == null || !SerialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
SerialPort = new System.IO.Ports.SerialPort();
|
|
|
|
|
SerialPort.PortName = ComName;
|
|
|
|
|
SerialPort.BaudRate = ComBaud;
|
|
|
|
|
SerialPort.DataBits = DataBits;
|
|
|
|
|
SerialPort.StopBits = StopBits;
|
|
|
|
|
SerialPort.Parity = Parity;
|
|
|
|
|
SerialPort.ReceivedBytesThreshold = ReceivedBytesThreshold;
|
|
|
|
|
SerialPort.Open();
|
|
|
|
|
//串口数据接收事件实现
|
|
|
|
|
SerialPort.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintMessage(true, $"打开串口连接异常:", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SerialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
SerialPort.Close();
|
|
|
|
|
|
|
|
|
|
PrintMessage(true, $"关闭串口连接:");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintMessage(true, $"关闭串口连接异常:", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsOpen
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return SerialPort.IsOpen;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReceiveData(object sender, SerialDataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.IO.Ports.SerialPort _SerialPort = sender as System.IO.Ports.SerialPort;
|
|
|
|
|
|
|
|
|
|
int _bytesToRead = _SerialPort.BytesToRead;
|
|
|
|
|
byte[] recvData = new byte[_bytesToRead];
|
|
|
|
|
|
|
|
|
|
_SerialPort.Read(recvData, 0, _bytesToRead);
|
|
|
|
|
|
|
|
|
|
SerialDataReceived?.Invoke(this, recvData);
|
|
|
|
|
|
|
|
|
|
PrintMessage(false, $"接收到数据【{e.EventType}】:" + BitConverter.ToString(recvData));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintMessage(true, $"接收数据异常:", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SendData(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (SerialPort == null || !SerialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
Open();
|
|
|
|
|
}
|
|
|
|
|
if (!SerialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
PrintMessage(true, $"发送数据异常,串口连接没有正常打开");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SerialPort.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
PrintMessage(false, $"发送数据:" + BitConverter.ToString(data));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintMessage(true, $"发送数据异常:", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//触发消息事件
|
|
|
|
|
private void PrintMessage(bool isError, string title, Exception ex = null)
|
|
|
|
|
{
|
|
|
|
|
string info = $"端口{ComName},波特率{ComBaud},{title}{(ex == null ? "" : "")}{ex?.Message}";
|
|
|
|
|
|
|
|
|
|
SerialMessage?.Invoke(this, new MessageEventArgs(isError: isError, message: info));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|