148 lines
4.2 KiB
C#
148 lines
4.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.IO.Ports;
|
||
using System.Data;
|
||
using DBFactory;
|
||
namespace SimensSerialPort
|
||
{
|
||
/// <summary>
|
||
/// 包含事件数据的类
|
||
/// </summary>
|
||
public class PortDataReciveEventArgs : EventArgs
|
||
{
|
||
private byte[] _data;
|
||
public byte[] Data
|
||
{
|
||
get { return _data; }
|
||
set { _data = value; }
|
||
}
|
||
public PortDataReciveEventArgs()
|
||
{
|
||
this._data = null;
|
||
}
|
||
|
||
public PortDataReciveEventArgs(byte[] data)
|
||
{
|
||
this._data = data;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 托管的串口数据接收事件的句柄
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
|
||
/// <summary>
|
||
/// 串口通讯类
|
||
/// </summary>
|
||
public class PortData
|
||
{
|
||
public event PortDataReceivedEventHandle Received;
|
||
public event SerialErrorReceivedEventHandler Error;
|
||
public SerialPort port;
|
||
public volatile bool ReceiveEventFlag = false; //接收事件是否有效 false表示有效
|
||
DBOperator dbo = new DBOperator();
|
||
//int _fid, mti;
|
||
public PortData(string sPortName, int baudrate, Parity parity)
|
||
{
|
||
port = new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);
|
||
port.WriteBufferSize = 4096;
|
||
port.RtsEnable = true;
|
||
port.ReadTimeout = 3000;
|
||
port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
|
||
port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);
|
||
//DataReceived 事件发生前内部输入缓冲区中的字节数
|
||
port.ReceivedBytesThreshold = 16;
|
||
dbo.Open();
|
||
}
|
||
|
||
~PortData()
|
||
{
|
||
Close();
|
||
}
|
||
public void Open()
|
||
{
|
||
if (!port.IsOpen)
|
||
{
|
||
port.Open();
|
||
}
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
if (port.IsOpen)
|
||
{
|
||
port.Close();
|
||
}
|
||
}
|
||
//数据发送
|
||
public void SendData(byte[] data)
|
||
{
|
||
if (port.IsOpen)
|
||
{
|
||
port.Write(data, 0, data.Length);
|
||
}
|
||
}
|
||
public void SendData(byte[] data, int offset, int count)
|
||
{
|
||
if (port.IsOpen)
|
||
{
|
||
port.Write(data, offset, count);
|
||
}
|
||
}
|
||
//发送命令
|
||
public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime)
|
||
{
|
||
if (port.IsOpen)
|
||
{
|
||
ReceiveEventFlag = true; //关闭接收事件
|
||
port.DiscardInBuffer(); //清空接收缓冲区
|
||
port.DiscardOutBuffer(); //清空发送缓冲区
|
||
|
||
port.Write(SendData, 0, SendData.Length);
|
||
int num = 0, ret = 0;
|
||
|
||
while (num++ < Overtime)
|
||
{
|
||
if (port.BytesToRead >= ReceiveData.Length) break;
|
||
System.Threading.Thread.Sleep(1);
|
||
}
|
||
|
||
if (port.BytesToRead >= ReceiveData.Length)
|
||
ret = port.Read(ReceiveData, 0, ReceiveData.Length);
|
||
ReceiveEventFlag = false; //打开事件
|
||
return ret;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
|
||
{
|
||
if (Error != null) Error(sender, e);
|
||
}
|
||
//数据接收
|
||
public void DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||
{
|
||
//禁止接收事件时直接退出
|
||
if (ReceiveEventFlag) return;
|
||
//是否应该是DataReceived 事件发生前内部输入缓冲区中的字节数
|
||
//ReceivedBytesThreshold = 16而不是port.BytesToRead???
|
||
byte[] data = new byte[port.BytesToRead];
|
||
port.Read(data, 0, data.Length);
|
||
if (Received != null)
|
||
{
|
||
Received(sender, new PortDataReciveEventArgs(data));
|
||
}
|
||
}
|
||
|
||
public bool IsOpen()
|
||
{
|
||
return port.IsOpen;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
}
|