使用的是http://www.cnblogs.com/liuy/archive/2010/03/03/1677130.html
中的串口通讯类using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;namespace LY.FuelStationPOS.Protocol
{
/// <summary>
/// 提供对串口的统一访问
/// </summary>
public sealed class SerialPortDao
{
#region 事件和字段定义
public event PortDataReceivedEventHandle Received;
public SerialPort serialPort = null;
public bool ReceiveEventFlag = false; //接收事件是否有效 false表示有效 private static readonly SerialPortDao instance = new SerialPortDao(); #endregion #region 属性定义
private string protName;
public string PortName
{
get { return serialPort.PortName; }
set
{
serialPort.PortName = value;
protName = value;
}
}
#endregion
}我要在外部使用这个类怎么用?
中的串口通讯类using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;namespace LY.FuelStationPOS.Protocol
{
/// <summary>
/// 提供对串口的统一访问
/// </summary>
public sealed class SerialPortDao
{
#region 事件和字段定义
public event PortDataReceivedEventHandle Received;
public SerialPort serialPort = null;
public bool ReceiveEventFlag = false; //接收事件是否有效 false表示有效 private static readonly SerialPortDao instance = new SerialPortDao(); #endregion #region 属性定义
private string protName;
public string PortName
{
get { return serialPort.PortName; }
set
{
serialPort.PortName = value;
protName = value;
}
}
#endregion
}我要在外部使用这个类怎么用?
using LY.FuelStationPOS.Protocol;代码:
SerialPortDao spd = new SerialPortDao();
spd.Received += new PortDataReceivedEventHandler(SerialPortDao_Received);
spd.PortName = ...
...protected void SerialPortDao_Received(object Sender, EventArgs e) //假设这个委托的签名是这样的,具体你要根据实际情况调整
{}
代码
public partial class frmMain : Form
{
SerialPortDao serialport = new SerialPortDao(); //不可访问,受保护级限制 public frmMain()
{
InitializeComponent();
}
}
C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?
C# 串口操作系列(3) -- 协议篇,二进制协议数据解析 看看博客中的方法是否能给你点启发。