2010-10-03 35 views
1

我想遠程控制一個lego mindstorms NXT機器人使用藍牙串行連接。程序連接沒有任何問題,但是當我發送單個命令時,它們在其他幾個命令發送之前不會出現。然後他們都立刻出現在nxt上。c#Serialport問題:數據發送「叢」

我已經嘗試了一切(我能想到或谷歌已告訴我),但我不能縫發送命令後得到緩衝區刷新。

任何人都知道我能做些什麼嗎? 這裏是我的代碼

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

namespace NXTBtRemote 
{ 
    public class BTHandler 
    { 
    SerialPort comm; 
    private string comPort; 
    public BTHandler() 
    { 
     comm = new SerialPort(); 
    } 

    public Boolean Connect(string _comPort) 
    { 
     this.comPort = _comPort; 
     comm.PortName = comPort; 
     comm.Open(); 

     if (comm.IsOpen) return true; 
     else return false; 
    } 

    public void sendCommand(Command command) 
    { 
     string msg = command.cmdType + "#" + command.arguments; 
     if (!comm.IsOpen) comm.Open(); 
     comm.WriteLine(msg); 
     comm.DiscardOutBuffer(); 
    } 
} 
} 

希望somone可以幫助。在此先感謝 親切的問候 - 肯尼斯

回答

1
comm.DiscardOutBuffer(); 

這是非常糟糕。您正在丟棄剛剛寫入的字節。 WriteLine()方法將命令寫入輸出緩衝區,從緩衝區緩慢寫入串行端口。只有在調試代碼時,單步執行代碼才能讓串口驅動程序有足夠的機會真正發送內容。如果芯片本身具有一個FIFO緩衝器,它將被擊中或錯過。只需刪除DiscardOutBuffer()調用,它只會造成傷害。

除此之外,你真的抱怨收到迴應的問題。但沒有顯示任何使任何Read調用的代碼。