标签:
通讯事件类
public class ComEvent { /// <summary> /// 数据接收事件 /// </summary> /// <param name="sender"></param> /// <param name="data"></param> public delegate void DataReceivedHandler(object sender, string data); /// <summary> /// 发送数据事件 /// </summary> /// <param name="data"></param> public delegate void DataSendHandler(string data); }
Com接口类
[Guid(ComHelper.InterfaceId)] public interface IComHelper { void Open(string _port, int _baud, int _stopBits, int _databits, int _parity); void Open(); event ComEvent.DataReceivedHandler DataReceived; event ComEvent.DataSendHandler DataSend; void Close(); void Send(string data); string Read(); bool IsComOpen(); }
串口通讯类
public class ComHelper : IComHelper { public const string ClassId = "881F4CC7-EAD4-4185-A91E-612DEFCE1E83"; public const string InterfaceId = "F8B914CC-6696-4f5e-BD27-D77780092261"; #region Port Settings #region 定义 private SerialPort sp = new SerialPort(); private string m_port = "COM2"; private int m_baud = 9600; private int m_stopBits = 1; private int m_parity = 0; private int m_dataBits = 8; private string m_ComMesage; private bool m_ComIsOpen; #endregion #region 属性 /// <summary> ///串口 /// </summary> /// <returns></returns> public SerialPort SerialPort { get { return sp; } set { sp = value; } } /// <summary> ///端口号 /// </summary> /// <returns></returns> public string port { get { return m_port; } set { m_port = value; } } /// <summary> ///波特率 /// </summary> /// <returns></returns> public int baudRate { get { return m_baud; } set { m_baud = value; } } /// <summary> ///停止位 /// </summary> /// <returns></returns> public int stopBits { get { return m_stopBits; } set { m_stopBits = value; } } /// <summary> ///奇偶校验 /// </summary> /// <returns></returns> public int parity { get { return m_parity; } set { m_parity = value; } } /// <summary> ///字节数 /// </summary> /// <returns></returns> public int dataBits { get { return m_dataBits; } set { m_dataBits = value; } } /// <summary> ///端口异常信息 /// </summary> /// <returns></returns> public string ComMesage { get { return m_ComMesage; } set { m_ComMesage = value; } } /// <summary> ///端口打开状态 /// </summary> /// <returns></returns> public bool ComIsOpen { get { return m_ComIsOpen; } set { m_ComIsOpen = value; } } #endregion #endregion #region Settings public bool IsComOpen() { return m_ComIsOpen; } /// <summary> ///串口通讯类 默认端口COM2 波特率9600 停止位1 奇偶校验0 数据位8 /// </summary> /// <returns></returns> public ComHelper() { sp = new SerialPort(); m_port = "COM2"; m_baud = 9600; m_stopBits = 1; m_parity = 0; m_dataBits = 8; m_ComMesage = ""; m_ComIsOpen = false; } /// <summary> ///串口通讯类 /// </summary> /// <returns></returns> public ComHelper(string _port, int _baud, int _stopBits, int _databits, int _parity) { m_port = _port; m_baud = _baud; m_stopBits = _stopBits; m_parity = _parity; m_dataBits = _databits; } #endregion /// <summary> ///打开串口 /// </summary> /// <returns></returns> public void Open(string _port, int _baud, int _stopBits, int _databits, int _parity) { m_port = _port; m_baud = _baud; m_stopBits = _stopBits; m_parity = _parity; m_dataBits = _databits; Open(); } /// <summary> ///打开串口 /// </summary> /// <returns></returns> public void Open() { if (!sp.IsOpen) { switch (m_parity) { case 0: sp.Parity = Parity.None; break; case 1: sp.Parity = Parity.Odd; break; case 2: sp.Parity = Parity.Even; break; } switch (m_stopBits) { case 0: sp.StopBits = StopBits.None; break; case 1: sp.StopBits = StopBits.One; break; case 2: sp.StopBits = StopBits.OnePointFive; break; case 3: sp.StopBits = StopBits.Two; break; } sp.PortName = m_port; sp.BaudRate = m_baud; sp.DataBits = m_dataBits; sp.ReadTimeout = 3000; sp.WriteTimeout = 5000; try { sp.Open(); ComMesage = sp.PortName + " Open the Success!"; sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); } catch (Exception ee) { ComMesage = "Error opening " + m_port + ": " + ee.Message; } } else { ComMesage = "Port Already in use by another application"; } m_ComIsOpen = sp.IsOpen; } /// <summary> ///串口数据接收事件 /// </summary> /// <returns></returns> public event ComEvent.DataReceivedHandler DataReceived; /// <summary> ///串口数据发送事件 /// </summary> /// <returns></returns> public event ComEvent.DataSendHandler DataSend; /// <summary> ///关闭串口 /// </summary> /// <returns></returns> public void Close() { if (sp.IsOpen) { try { sp.Close(); } catch (Exception err) { ComMesage = "Error closing " + sp.PortName + ": " + err.Message; } ComMesage = sp.PortName + " closed Success"; } else { ComMesage = sp.PortName + " is not open"; } m_ComIsOpen = sp.IsOpen; } /// <summary> ///发送 /// </summary> /// <returns></returns> public void Send(string data) { if (sp.IsOpen) { try { data = data + "\r\n"; sp.WriteLine(data); DataSend(data); // sp.DiscardOutBuffer(); } catch { } } else { ComMesage = sp.PortName + " is not open"; } } /// <summary> ///读取 /// </summary> /// <returns></returns> public string Read() { byte[] data = new byte[sp.BytesToRead]; string buf = ""; if (sp.IsOpen) { try { //System.Threading.Thread.Sleep(50); sp.Read(data, 0, data.Length); buf = ASCIItoString(data); while (buf.IndexOf("\r\n") == -1) { data = new byte[sp.BytesToRead]; sp.Read(data, 0, data.Length); string temp = ASCIItoString(data); buf = buf + temp; } } catch (Exception eee) { buf = sp.PortName + eee.Message; } } else { ComMesage = sp.PortName + " is not open"; } sp.DiscardInBuffer(); return buf; } /// <summary> ///ASCII码转字符 /// <param name="string"></param> /// <returns></returns> private string ASCIItoString(byte[] asciiCode) { string aa = Encoding.ASCII.GetString(asciiCode); return aa; } /// <summary> ///去空格运算 /// </summary> /// <param name="string"></param> /// <returns></returns> private string ReplaceSpace(string str) { string NewStr = null; string[] word = str.Split(new char[] { ‘ ‘ }); foreach (string temp in word) NewStr += temp; return NewStr; } private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { string ReDatas = Read(); DataReceived(this, ReDatas);//输出数据 } }
标签:
原文地址:http://www.cnblogs.com/MachineVision/p/5775025.html