using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.LogNet;
namespace WIDESEAWCS_Communicator
{
///
///
///
public class SerialPortCommunicator : BaseSerialPortCommunicator
{
///
/// 当前通讯器是否已连接到PLC。
///
private bool _connected;
///
/// PLC名称
///
private string _name;
private ILogNet _logNet;
private List _buffers = new List();
private string _serialPortName;
private SerialPort _serialPort;
private int _readCount;
private bool _isReadToEnd;
///
/// 日志记录器
///
public override ILogNet LogNet => _logNet;
///
/// 通讯器名称
///
public override string Name => _name;
///
/// 当前通讯器是否已连接到PLC。
///
public override bool IsConnected => _connected;
///
/// 缓冲区
///
public override List Buffers => _buffers;
///
/// 串口名称
///
public override string SerialPortName => _serialPortName;
///
/// 读取数据的长度。
///
public override int ReadCount => _readCount;
///
/// 读取数据时是否读取到数据末尾。
///
public override bool IsReadToEnd => _isReadToEnd;
///
///
///
public override SerialPort SerialPort { get { return _serialPort; } set { _serialPort = value; } }
///
/// 构造函数
///
public SerialPortCommunicator(string serialPortName, int port, string name)
{
string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}";
_logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100);
_serialPortName = serialPortName;
_name = name;
_serialPort = new SerialPort(serialPortName);
_serialPort.DataReceived += DataReceived;
}
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
_buffers.Clear();
_buffers = new List();
byte[] data = new byte[1024];
while (true)
{
if (_serialPort.BytesToRead < 1)
break;
if (_serialPort.BytesToRead > data.Length)
{
_isReadToEnd = false;
}
else
{
_isReadToEnd = true;
}
_readCount = _serialPort.Read(data, 0, Math.Min(_serialPort.BytesToRead, data.Length));
_buffers.AddRange(data.SelectBegin(_readCount));
}
}
catch (Exception ex)
{
}
}
///
///
///
///
///
public override bool Connect()
{
try
{
_serialPort.Open();
return true;
}
catch (Exception ex)
{
LogNet.WriteException(Name, $"串口打开失败,串口号:{_serialPortName}", ex);
//连接异常时抛出自定义异常类
throw new CommunicationException(ex.Message, CommunicationErrorType.ConnectionFailed, innerException: ex);
}
}
///
/// 断开与工业设备的连接。
///
///
///
public override bool Disconnect()
{
try
{
_serialPort.Close();
return true;
}
catch (Exception ex)
{
LogNet.WriteException(Name, $"串口关闭失败,串口号:{_serialPortName}", ex);
//连接异常时抛出自定义异常类
throw new CommunicationException(ex.Message, CommunicationErrorType.ConnectionFailed, innerException: ex);
}
}
///
/// 释放资源。
///
///
public override void Dispose()
{
Disconnect();
GC.SuppressFinalize(this);
}
///
/// 将缓冲区中的数据转换为字符串
///
///
///
public override string ToString(Encoding encoding)
{
try
{
return encoding.GetString(Buffers.ToArray());
}
catch (Exception ex)
{
return "";
}
}
///
/// 发送数据
///
public override void Write(byte[] data)
{
_serialPort.Write(data, 0, data.Length);
}
///
/// 发送数据
///
///
public override void Write(string data)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
_serialPort.Write(bytes, 0, bytes.Length);
}
///
/// 发送数据
///
///
///
public override void Write(string data, string endStr)
{
byte[] bytes = Encoding.UTF8.GetBytes(data + endStr);
_serialPort.Write(bytes, 0, bytes.Length);
}
}
}