代码拉取完成,页面将自动刷新
同步操作将从 热火/modbus协议库 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
using System;
using System.IO.Ports;
namespace SerialPortLibrary
{
/// <summary>
/// 串行通讯服务
/// </summary>
/// <remarks></remarks>
internal class SerialPort : DataPublish, ISerialPort
{
public long SendBytes { get; set; }
public long ReceiveBytes { get; set; }
private System.IO.Ports.SerialPort ComHandle;
/// <summary>
/// 发送超时时间 秒
/// </summary>
public int SendTimeOut
{
get { return ComHandle.WriteTimeout; }
set { ComHandle.WriteTimeout = value; }
}
/// <summary>
/// 接收超时时间 秒
/// </summary>
public int ReceiveTimeOut
{
get { return ComHandle.ReadTimeout; }
set { ComHandle.ReadTimeout = value; }
}
/// <summary>
/// 发送缓冲区大小
/// </summary>
public int ReadBufferSize
{
get { return ComHandle.ReadBufferSize; }
set { ComHandle.ReadBufferSize = value; }
}
/// <summary>
/// 接受缓冲区大小
/// </summary>
public int WriteBufferSize
{
get { return ComHandle.WriteBufferSize; }
set { ComHandle.WriteBufferSize = value; }
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="COM"></param>
/// <param name="BaudRate"></param>
/// <param name="Parity"></param>
/// <param name="DataBits"></param>
/// <param name="StopBit"></param>
internal SerialPort(string COM, int BaudRate, Parity Parity, int DataBits, StopBits StopBit)
{
ComHandle = new System.IO.Ports.SerialPort(COM, BaudRate, Parity, DataBits, StopBit) { ReadBufferSize = 4096, WriteBufferSize = 4096, ReceivedBytesThreshold = 1 };
ComHandle.DataReceived += ComHandle_DataReceived;
ComHandle.ErrorReceived += ComHandle_ErrorReceived;
}
private void ComHandle_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
serialdata.Error.ErrorType = 2;
serialdata.Error.ErrorMessage = e.EventType.ToString();
DataPublishEventArgs args = new DataPublishEventArgs(serialdata);
OnDataPublishEvent(args);
}
/// <summary>
/// 启动发送命令
/// </summary>
/// <param name="list"></param>
public void Send(ISerialData data)
{
if (data.Data == null || data.Data.Length < 1) { return; }
if (!ComHandle.IsOpen) { return; }
serialdata = data;
serialdata.ComID = ComHandle.PortName;
byte[] by = data.Data;
SendBytes += by.Length;
ComHandle.DiscardOutBuffer(); //清空缓冲区
ComHandle.DiscardInBuffer();
try
{
ComHandle.Write(by, 0, by.Length);
}
catch (Exception Error)
{
serialdata.Error.ErrorType = 1;
serialdata.Error.ErrorMessage = Error.Message;
DataPublishEventArgs args = new DataPublishEventArgs(serialdata);
OnDataPublishEvent(args);
}
}
/// <summary>
/// 接收事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComHandle_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (e.EventType != System.IO.Ports.SerialData.Chars) { return; }
try
{
System.IO.Ports.SerialPort port = sender as System.IO.Ports.SerialPort;
int ReceiveCount = port.BytesToRead;
byte[] by = new byte[ReceiveCount];
port.Read(by, 0, by.Length);
ReceiveBytes += by.Length;
serialdata.Data = by;
}
catch (Exception Error)
{
serialdata.Error.ErrorType = 3;
serialdata.Error.ErrorMessage = Error.Message;
}
finally
{
DataPublishEventArgs args = new DataPublishEventArgs(serialdata);
OnDataPublishEvent(args);
}
}
/// <summary>
/// 获取串口状态
/// </summary>
public bool IsOpen
{
get { return ComHandle.IsOpen; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ClosePort()
{
try
{
if (ComHandle.IsOpen)
{
serialdata.Error.ErrorType = -1;
DataPublishEventArgs args = new DataPublishEventArgs(serialdata);
OnDataPublishEvent(args);
ComHandle.Close();
}
return true;
}
catch
{
return false;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool OpenPort()
{
try
{
if (!ComHandle.IsOpen)
{
ComHandle.Open();
}
else
{
return false;
}
return true;
}
catch
{
return false;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。