1. 程式人生 > >【C#】串列埠操作實用類

【C#】串列埠操作實用類

publicclass PortData
    {
        
publicevent PortDataReceivedEventHandle Received;
        
publicevent SerialErrorReceivedEventHandler Error; 
        
public SerialPort port;
        
publicbool ReceiveEventFlag =false;  //接收事件是否有效 false表示有效public PortData(string sPortName, int baudrate,Parity parity,SerialInterface.SerialMode mode)
        {
            port 
=new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);
            port.RtsEnable 
=true;
            port.ReadTimeout 
=3000;
            port.DataReceived 
+=new SerialDataReceivedEventHandler(DataReceived);
            port.ErrorReceived 
+=new SerialErrorReceivedEventHandler(ErrorEvent);
        }

        
~PortData()
        {
            Close();
        }
        
publicvoid Open()
        {
            
if (!port.IsOpen)
            {           
                port.Open();
            }
        }

        
publicvoid Close()
        {
            
if (port.IsOpen)
            {
                port.Close();
            }
        }
        
//資料傳送publicvoid SendData(byte[] data)
        {
            
if (port.IsOpen)
            {
                port.Write(data, 
0, data.Length);
            }
        }
        
publicvoid SendData(byte[] data,int offset,int count)
        {
            
if (port.IsOpen)
            {
                port.Write(data, offset, count);
            }
        }
        
//傳送命令publicint SendCommand(byte[] SendData, refbyte[] ReceiveData,int Overtime)
        {

            
if(port.IsOpen)
            {
                ReceiveEventFlag 
=true;        //關閉接收事件                port.DiscardInBuffer();         //清空接收緩衝區                                 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;
        }

        
publicvoid ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
        {
            
if (Error !=null) Error(sender, e);
        }
        
//資料接收publicvoid DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
//禁止接收事件時直接退出if (ReceiveEventFlag) return;

            
byte[] data =newbyte[port.BytesToRead];
            port.Read(data, 
0, data.Length);
            
if (Received !=null) Received(sender, new PortDataReciveEventArgs(data));
        }

        
publicbool IsOpen()
        {
            
return port.IsOpen;
        }
    }
    
publicdelegatevoid PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
    
publicclass PortDataReciveEventArgs : EventArgs
    {
        
public PortDataReciveEventArgs()
        {
            
this.data =null;
        }

        
public PortDataReciveEventArgs(byte[] data)
        {
            
this.data = data;
        }

        
privatebyte[] data;

        
publicbyte[] Data
        {
            
get { return data; }
            
set { data = value; }
        }
    }