/*--------------------------------------------------------------------------------------------
// Copyright (C) 2014 FOXCONN SHZBG自动化设备开发处
// 版权所有。
//
// 档案名:SerialPortClass.cs
// 档功能描述:在系统SerialPort类的基础上封装的串口通讯类
//
//
// 创建标志:2014-12-12 edited by hua
//
// 修改标志:
// 修改描述:
//
--------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;//
namespace HUASPACE.SerialPortCommunicationBase
{
/// <summary>
/// 是否接受到数据
/// </summary>
/// <param name="data">数据</param>
/// <param name="comPort">串口名</param>
public delegate void IsRecievedSerialDataHandler(string data, string comPort);
/// <summary>
/// 串口读写操作
/// </summary>
public class CSerialPort : IDisposable
{
/// <summary>
/// 接受到数据事件
/// </summary>
public event IsRecievedSerialDataHandler IsRecievedSerialData;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sp">串口类</param>
public CSerialPort(SerialPort sp)
{
_comm = sp;
if (_comm != null) _comm.DataReceived += DataReceived;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="nPort">端口</param>
/// <param name="nBaud">波特率</param>
public CSerialPort(int nPort, int nBaud)
{
_nPort = nPort;
_nBaud = nBaud;
_comm = new SerialPort();
_comm.NewLine = "\r\n";
if (_comm != null) _comm.DataReceived += DataReceived;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="nPort">端口</param>
/// <param name="nBaud">波特率</param>
/// <param name="nParity">奇偶校验</param>
public CSerialPort(int nPort, int nBaud, int nParity)
{
_nPort = nPort;
_nBaud = nBaud;
_nParity = nParity;
_comm = new SerialPort();
if (_comm != null) _comm.DataReceived += DataReceived;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="nPort">端口</param>
/// <param name="nBaud">波特率</param>
/// <param name="nParity">奇偶校验</param>
/// <param name="nDataBits">数据位</param>
public CSerialPort(int nPort, int nBaud, int nParity, int nDataBits)
{
_nPort = nPort;
_nBaud = nBaud;
_nParity = nParity;
_nDataBits = nDataBits;
_comm = new SerialPort();
if (_comm != null) _comm.DataReceived += DataReceived;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="nPort">端口</param>
/// <param name="nBaud">波特率</param>
/// <param name="nParity">奇偶校验</param>
/// <param name="nDataBits">数据位</param>
/// <param name="nStopBits">停止位</param>
public CSerialPort(int nPort, int nBaud, int nParity, int nDataBits, int nStopBits)
{
_nPort = nPort;
_nBaud = nBaud;
_nParity = nParity;
_nDataBits = nDataBits;
_nStopBits = nStopBits;
_comm = new SerialPort();
if (_comm != null) _comm.DataReceived += DataReceived;
}
/// <summary>
/// 析构函数
/// </summary>
~CSerialPort()
{
try
{
if (_comm != null)
{
_comm.Close();
}
}
catch (System.Exception)
{
throw new Exception("异常信息: " +�
评论0
最新资源