You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.2 KiB
148 lines
4.2 KiB
3 months ago
|
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 = CommonClassLib.AppSettings.dbo;//20130510
|
||
|
//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;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|