2011-12-14 62 views
2

我的應用程序主動使用串行端口與設備進行交互式通信。我需要的是添加某種形式的串口發送和接收數據的可視化工具(兩個閃爍的燈,類似於系統托盤中網絡活動的兩臺閃爍計算機)。 我應該遵循什麼方法來正確實現此功能?串行端口活動的展示臺(用於發送和接收數據)

+1

到目前爲止你在做什麼? http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/是串口的好起點,通常你會聽DataReceived傳入數據。 – 2011-12-14 14:39:27

回答

1

最簡單的方法是創建一個StatusStrip,並添加一個專用於這些指標的部分。接下來,選擇個別狀態的圖像,並在每次有活動時設置適當的圖像。這裏是一些粗略的代碼。

private System.Drawing.Image StateImageX { get; set; } 
private System.Drawing.Image StateImageY { get; set; } 

public delegate void DelegateRefreshControls(); 
public DelegateRefreshControls RefreshControlHandler; 

public Form1() 
{ 
    this.InitializeComponent(); 

    this.StateImageX = System.Drawing.Image.FromFile(@"??\ImageX.png"); 
    this.StateImageY = System.Drawing.Image.FromFile(@"??\ImageY.png"); 

    this.RefreshControlHandler += new DelegateRefreshControls(this.RefreshControls); 
} 

private void Form1_FormClosing (object sender, FormClosingEventArgs e) 
{ 
    this.RefreshControlHandler -= new DelegateRefreshControls(this.RefreshControls); 
} 

private void SerialPort_Activity (object sender, FormClosingEventArgs e) 
{ 
    this.Invoke(this.RefreshControlHandler); 
} 

public void RefreshControls() 
{ 
    if (state == x) this.ToolStripStatusLabel.Image = this.StateImageX; 
    if (state == y) this.ToolStripStatusLabel.Image = this.StateImageY; 
} 
相關問題