2013-05-21 85 views
0

我在做一個使用串口的類。 這一切都很安靜,直到通過串口接收數據的項目,班級在主應用程序中引發一個事件。SerialPort C#自定義DataReceived委託

我的問題是:如何將參數傳遞給委託並在我的類中使用它,因爲我的類非常獨立。

以下來源和我喜歡花費代表的地方。

類控制串口:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO.Ports; 

namespace TCCExterna.Lib 
{ 
    public class PortaSerial //: IDisposable 
    { 
     private SerialPort serialPort; 
     private Queue<byte> recievedData = new Queue<byte>(); 

     public PortaSerial() 
     { 
      serialPort = new SerialPort();    
      serialPort.DataReceived += serialPort_DataReceived;    
     } 

     public void Abrir(string porta, int velocidade) 
     { 
      serialPort.PortName = porta; 
      serialPort.BaudRate = velocidade; 
      serialPort.Open(); 
     } 

     public string[] GetPortas() 
     { 
      return SerialPort.GetPortNames(); 
     } 

     public string[] GetVelocidades() 
     { 
      return new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" }; 

     } 

     void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e) 
     { 
      byte[] data = new byte[serialPort.BytesToRead]; 
      serialPort.Read(data, 0, data.Length); 
      data.ToList().ForEach(b => recievedData.Enqueue(b)); 
      processData();    
      // like this use LineReceivedEvent or LineReceived 

     } 

     private void processData() 
     { 
      // Determine if we have a "packet" in the queue 
      if (recievedData.Count > 50) 
      { 
       var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());     
      } 
     } 

     public void Dispose() 
     { 
      if (serialPort != null) 
       serialPort.Dispose(); 
     } 
    } 
} 

計劃:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 
using TCCExterna.Lib; 

namespace TCCExterna 
{ 
    public partial class FormPrincipal : Form 
    { 
     PortaSerial sp1 = new PortaSerial(); // like this command passed LineReceivedEvent or LineReceived 

     public delegate void LineReceivedEvent(string line); 
     public void LineReceived(string line) 
     { 
      //What to do with the received line here    
     } 

     public FormPrincipal() 
     { 
      InitializeComponent(); 
      cmbPortas.Items.AddRange(sp1.GetPortas()); 
      cmbVelocidade.Items.AddRange(sp1.GetVelocidades()); 
     } 
    } 
} 
+0

你到底想要做什麼?將參數從**程序**傳遞給**串行類** –

+0

我喜歡通過代表使用串行類參數... –

+0

卡拉....tádifícilentender o que tu quer rsrsr –

回答

2

如果我很清楚,你要的是這樣的:(SE quiser頗得explicar MELHOR EM葡萄牙語,depois一個GENTE traduz親網站)。

//delcare an event args clas 
public class LineReceivedEventArgs : EventArgs 
{ 
    //Data to pass to the event 
    public string LineData{get; private set;} 

    public LineReceivedEventArgs(string lineData) 
    { 
     this.LineData = lineData 
    } 
} 

//declare a delegate 
public delegate void LineReceivedEventHandler(object sender, LineReceivedEventArgs Args); 

public class PortaSerial //: IDisposable 
{ 
    private SerialPort serialPort; 
    private Queue<byte> recievedData = new Queue<byte>(); 

    //add event to class 
    public event LineReceivedEventHandler LineReceived; 

    public PortaSerial() 
    { 
     serialPort = new SerialPort(); 
     serialPort.DataReceived += serialPort_DataReceived; 
    } 

    public void Abrir(string porta, int velocidade) 
    { 
     serialPort.PortName = porta; 
     serialPort.BaudRate = velocidade; 
     serialPort.Open(); 
    } 

    public string[] GetPortas() 
    { 
     return SerialPort.GetPortNames(); 
    } 

    public string[] GetVelocidades() 
    { 
     return new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" }; 

    } 

    void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e) 
    { 
     byte[] data = new byte[serialPort.BytesToRead]; 
     serialPort.Read(data, 0, data.Length); 
     data.ToList().ForEach(b => recievedData.Enqueue(b)); 
     processData(); 

     //raise event here 
     if (this.LineReceived != null) 
      LineReceived(this, new LineReceivedEventArgs("some line data")); 


    } 

    private void processData() 
    { 
     // Determine if we have a "packet" in the queue 
     if (recievedData.Count > 50) 
     { 
      var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue()); 
     } 
    } 

    public void Dispose() 
    { 
     if (serialPort != null) 
      serialPort.Dispose(); 
    } 
} 


public partial class FormPrincipal : Form 
{ 
    PortaSerial sp1 = new PortaSerial(); // like this command passed LineReceivedEvent or LineReceived 

    // event handler method 
    void sp1_LineReceived(object sender, LineReceivedEventArgs Args) 
    { 
     //do things with line 
     MessageBox.ShowDialog(Args.LineData); 
    } 

    public FormPrincipal() 
    { 
     InitializeComponent(); 
     //add handler to event 
     sp1.LineReceived += new LineReceivedEventHandler(sp1_LineReceived); 
     cmbPortas.Items.AddRange(sp1.GetPortas()); 
     cmbVelocidade.Items.AddRange(sp1.GetVelocidades()); 
    } 


} 
+0

完美...這是我需要的......但你通過我的解決方案可以瞭解如何代表...... tks –