using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace QM.Exchange.Adapters.COM
{
///
/// 串口操作
///
public class SerialPortAdapter:IDisposable
{
SerialPort _serialPort = null;
//定义委托
public delegate void SerialPortDataReceiveEvent(object sender, SerialDataReceivedEventArgs e, byte[] bits);
public delegate void SerialErrorReceivedEvent(object sender, SerialErrorReceivedEventArgs e);
//定义接收数据事件
public event SerialPortDataReceiveEvent DataReceived;
//定义接收数据出错事件
public event SerialErrorReceivedEvent ErrorReceived;
#region 构造函数
///
/// 默认构造函数,操作COM1,速度为9600,没有奇偶校验,8位字节,停止位为1
///
public SerialPortAdapter()
{
_serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
SetSerialPort();
}
///
/// 构造函数,
///
///
public SerialPortAdapter(string comPortName)
{
_serialPort = new SerialPort(comPortName);
SetSerialPort();
}
///
/// 构造函数,可以自定义串口的初始化参数
///
/// 需要操作的COM口名称
/// COM的速度
/// 奇偶校验位
/// 数据长度
/// 停止位
public SerialPortAdapter(string comPortName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
{
_serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits);
SetSerialPort();
}
#endregion
#region 管理
///
/// 设置串口资源,还需重载多个设置串口的函数
///
void SetSerialPort()
{
if (_serialPort != null)
{
//设置触发DataReceived事件的字节数为1
_serialPort.ReceivedBytesThreshold = 1;
//接收到一个字节时,也会触发DataReceived事件
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
//接收数据出错,触发事件
_serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(_serialPort_ErrorReceived);
//打开串口
OpenPort();
}
}
///
/// 打开串口资源
///
public bool OpenPort()
{
bool result = false;
//如果串口是打开的,先关闭
if (_serialPort.IsOpen)
_serialPort.Close();
try
{
//打开串口
_serialPort.Open();
result = true;
}
catch (Exception Ex)
{
throw Ex;
}
return result;
}
///
/// 关闭串口资源,操作完成后,一定要关闭串口
///
public void ClosePort()
{
//如果串口处于打开状态,则关闭
if (_serialPort.IsOpen)
_serialPort.Close();
}
///
/// 释放
///
public void Dispose()
{
ClosePort();
}
///
/// 获取所有已连接短信猫设备的串口
///
///
public string[] SerialsIsConnected()
{
List lists = new List();
string[] seriallist = GetSerials();
return lists.ToArray();
}
///
/// 获得当前电脑上的所有串口资源
///
///
public string[] GetSerials()
{
return SerialPort.GetPortNames();
}
#endregion
#region 事件
///
/// 接收串口数据事件
///
///
///
public void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (DataReceived != null)
{
byte[] _data = new byte[_serialPort.BytesToRead];
_serialPort.Read(_data, 0, _data.Length);
DataReceived(sender, e, _data);
}
}
///
/// 接收数据出错事件
///
///
///
public void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
if (ErrorReceived != null)
{
ErrorReceived(sender,e);
}
}
#endregion
#region 写入
///
/// 写入文本数据
///
///
public void WriteData(string data)
{
//发送数据,并加加车符
_serialPort.Write(data);
}
///
/// 写入二进制数据
///
///
public void WriteData(byte[] data)
{
//发送数据
_serialPort.Write(data,0,data.Length);
}
#endregion
#region 字符处理工具
///
/// 把字节型转换成十六进制字符串
///
///
///
public static string ByteToString(byte[] InBytes)
{
string StringOut = "";
foreach (byte InByte in InBytes)
{
StringOut = StringOut + String.Format("{0:X2} ", InByte);
}
return StringOut;
}
///
/// 把十六进制字符串转换成字节型
///
///
///
public static byte[] StringToByte(string InString)
{
string[] ByteStrings;
ByteStrings = InString.Split(" ".ToCharArray());
byte[] ByteOut;
ByteOut = new byte[ByteStrings.Length - 1];
for (int i = 0; i == ByteStrings.Length - 1; i++)
{
ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
}
return ByteOut;
}
#endregion
}
}