【C#】串口操作实用类

  做工业通信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但是目前却没有C#相对成熟的串口操作代码,最近用Moxa的设备开发基于WinCE5.0的串口操作代码,所以就扩充完善了一下串口操作,特别是SendCommand函数,这是我比较常用的主从通信代码,不喜欢用事件或线程接数据,在规定的超时时间内直接循环判断要接收的数据。

     下面是具体的代码:   

 

  
  
  
  
  1. public class PortData  
  2.    {  
  3.        public event PortDataReceivedEventHandle Received;  
  4.        public event SerialErrorReceivedEventHandler Error;   
  5.        public SerialPort port;  
  6.        public bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效  
  7.  
  8.        public PortData(string sPortName, int baudrate,Parity parity,SerialInterface.SerialMode mode)  
  9.        {  
  10.            port = new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);  
  11.            port.RtsEnable = true;  
  12.            port.ReadTimeout = 3000;  
  13.            port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);  
  14.            port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);  
  15.        }  
  16.  
  17.        ~PortData()  
  18.        {  
  19.            Close();  
  20.        }  
  21.        public void Open()  
  22.        {  
  23.            if (!port.IsOpen)  
  24.            {             
  25.                port.Open();  
  26.            }  
  27.        }  
  28.  
  29.        public void Close()  
  30.        {  
  31.            if (port.IsOpen)  
  32.            {  
  33.                port.Close();  
  34.            }  
  35.        }  
  36.        //数据发送  
  37.        public void SendData(byte[] data)  
  38.        {  
  39.            if (port.IsOpen)  
  40.            {  
  41.                port.Write(data, 0, data.Length);  
  42.            }  
  43.        }  
  44.        public void SendData(byte[] data,int offset,int count)  
  45.        {  
  46.            if (port.IsOpen)  
  47.            {  
  48.                port.Write(data, offset, count);  
  49.            }  
  50.        }  
  51.        //发送命令  
  52.        public int SendCommand(byte[] SendData, ref  byte[] ReceiveData,int Overtime)  
  53.        {  
  54.  
  55.            if(port.IsOpen)  
  56.            {  
  57.                ReceiveEventFlag = true;        //关闭接收事件  
  58.                port.DiscardInBuffer();         //清空接收缓冲区                   
  59.                port.Write(SendData, 0, SendData.Length);  
  60.                int num=0,ret=0;  
  61.                while (num++ < Overtime)  
  62.                {  
  63.                    if (port.BytesToRead >= ReceiveData.Length) break;  
  64.                    System.Threading.Thread.Sleep(1);   
  65.                }  
  66.                if (port.BytesToRead >= ReceiveData.Length)   
  67.                    ret = port.Read(ReceiveData, 0, ReceiveData.Length);  
  68.                ReceiveEventFlag = false;       //打开事件  
  69.                return ret;  
  70.            }  
  71.            return -1;  
  72.        }  
  73.  
  74.        public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)  
  75.        {  
  76.            if (Error != null) Error(sender, e);  
  77.        }  
  78.        //数据接收  
  79.        public void DataReceived(object sender, SerialDataReceivedEventArgs e)  
  80.        {  
  81.            //禁止接收事件时直接退出  
  82.            if (ReceiveEventFlag) return;  
  83.  
  84.            byte[] data = new byte[port.BytesToRead];  
  85.            port.Read(data, 0, data.Length);  
  86.            if (Received != null) Received(sender, new PortDataReciveEventArgs(data));  
  87.        }  
  88.  
  89.        public bool IsOpen()  
  90.        {  
  91.            return port.IsOpen;  
  92.        }  
  93.    }  
  94.    public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);  
  95.    public class PortDataReciveEventArgs : EventArgs  
  96.    {  
  97.        public PortDataReciveEventArgs()  
  98.        {  
  99.            this.data = null;  
  100.        }  
  101.  
  102.        public PortDataReciveEventArgs(byte[] data)  
  103.        {  
  104.            this.data = data;  
  105.        }  
  106.  
  107.        private byte[] data;  
  108.  
  109.        public byte[] Data  
  110.        {  
  111.            get { return data; }  
  112.            set { data = value; }  
  113.        }  
  114.    }  


【附注】1~9 串口的名称是 "COMx:",>9的以前用\\\\.\\COMx:比较好使,但是在moxa 661设备上却不行,要用如下格式"$device\\COM" + PortNo.ToString() + "\0",也许这是moxa修改了相应的串口驱动。

 

 

本文出自 “叶帆工作室” 博客,转载请与作者联系!

你可能感兴趣的:(职场,串口,休闲,实用类)