开发者

c# Serialport problem: data are send in "clumps"

I am trying to remote control a lego mindstorms NXT robot using a bluetooth serial connection. The program connects without any problem but when i send single commands they do not 开发者_开发技巧appear before several other commands has been send. Then they all appear at once on the nxt.

I have tried everything (i can think of or google has told me to) but i cannot seam to get the buffer to flush after a command is send.

Anyone having a idea about what i can do? Here is my code

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();
    }
}
}

Hope somone can help. Thanks in advance kind regards - kenneth


    comm.DiscardOutBuffer();

That's very bad. You are throwing away the bytes you have just written. The WriteLine() method writes the command to the output buffer from which they are slowly written to the serial port. Only if you debug the code, single stepping through the code, would the serial port driver have enough of a chance to actually send something. It would be hit or miss if the chip itself has a FIFO buffer. Just delete the DiscardOutBuffer() call, it does nothing but harm.

Beyond that, you are really complaining about a problem with receiving a response. But didn't show any code that makes any Read call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜