简体   繁体   English

稳定蓝牙串行通信的波特率

[英]Baud rates for stable Bluetooth serial communication

I have an Arduino mega communicating over Bluetooth (bluesmirf gold device) to a C# application that I wrote.我有一个 Arduino mega 通过蓝牙(bluesmirf gold 设备)与我编写的 C# 应用程序进行通信。 The Arduino is constantly sending a serial signal of 32 characters, the first always being an "S" and the last an "E". Arduino 不断发送 32 个字符的串行信号,第一个总是“S”,最后一个总是“E”。 Using putty I can confirm that this signal is being sent correctly 99% of the time.使用腻子,我可以确认该信号在 99% 的时间内被正确发送。

Now I want to read this signal with my C# application, which I am doing with the following code:现在我想用我的 C# 应用程序读取这个信号,我正在使用以下代码:

    public string receiveCommandHC()
    {
        
        string messageHC = "";
        if (serialHC.IsOpen)
        {

            while (serialHC.ReadChar() != 'S')
            {

            }
            messageHC = serialHC.ReadTo("E");
            serialHC.DiscardInBuffer();
        }
        return messageHC;
         
    }

serialHC is of the serial class. serialHC 属于串行类。

Sometimes this works perfectly but other times I'm having problems, I cannot find out why it works sometimes but others not.有时这很有效,但有时我会遇到问题,我不知道为什么它有时会起作用,但其他人却没有。

The problem that I seem to be having is that sometimes I get a rather large Lag in the data that I am reading from the arduino.我似乎遇到的问题是,有时我从 arduino 读取的数据中出现相当大的滞后。 I notice this because I am sending button states and they only change a few seconds after I actually press or release the button on the Arduino.我注意到这一点是因为我正在发送按钮状态,并且它们仅在我实际按下或释放 Arduino 上的按钮后几秒钟发生变化。 I used the standard baud rate of the Bluetooth device, which is 115200, and was wondering if changing that to a much lower rate could yield better results?我使用了蓝牙设备的标准波特率,即 115200,并想知道将其更改为更低的速率是否会产生更好的结果? What if any advantage would that have?如果这有什么好处呢? I do not need hight communication rates, even updating the state 4-5 times a second would be acceptable for my application.我不需要很高的通信速率,即使每秒更新 4-5 次状态对于我的应用程序也是可以接受的。

Is it possible the lag is coming from my code?延迟是否可能来自我的代码? I think it may be from the while loop that is waiting for the incoming "S" but then I don't see why it should hang there since there are new signals always coming in at a high rate.我认为它可能来自等待传入的“S”的 while 循环,但是我不明白为什么它应该挂在那里,因为新信号总是以很高的速度进入。

I'm using the DiscardInBuffer() because I do not care about outdated data and just want to skip over that.我使用 DiscardInBuffer() 是因为我不关心过时的数据,只想跳过它。 It is much more important that I am reading current up to date data and acting on that fresh data.更重要的是我正在阅读最新的数据并根据这些新数据采取行动。

Thank you for your help!谢谢您的帮助!

Best regards,最好的祝福,

Bender本德尔

Update:更新:

Just found out a bit more information while debugging.刚刚在调试时发现了更多信息。 The problem only seems to appear:问题似乎只出现在:

  1. When connected over Bluetooth (over USB cable there is absolutely NO Lag)通过蓝牙连接时(通过 USB 电缆绝对没有延迟)
  2. When a second Bluetooth connection is established from the PC to another device (different COM port and different baud rate)当从 PC 到另一个设备建立第二个蓝牙连接时(不同的 COM 端口和不同的波特率)

Does anybody have any experience running two different devices off the same Bluetooth dongle on the PC?有没有人有在 PC 上通过同一个蓝牙加密狗运行两个不同设备的经验? I can manage to connect to both no problem but still having the lag issue mentioned before.我可以设法连接到两者都没有问题,但仍然存在前面提到的滞后问题。

Thanks for any help谢谢你的帮助

You are not really using a physical serial port here.您在这里并没有真正使用物理串行端口。 The BlueTooth driver merely emulates one.蓝牙驱动程序只是模拟一个。 This is common, the Windows API has a well defined set of api functions to talk to a serial port.这很常见,Windows API 有一组定义良好的 api 函数来与串行端口通信。 Emulating one makes the interface to the driver simple, the vendor doesn't have to supply an interface DLL or document a complicated DeviceIoControl() protocol.模拟一个使驱动程序的接口变得简单,供应商不必提供接口 DLL 或记录复杂的 DeviceIoControl() 协议。

Which means for one thing that the actual communication settings don't matter.这意味着一方面,实际的通信设置并不重要。 Baudrate is meaningless in this scenario, it is the BlueTooth radio signaling that sets the transfer rate.在这种情况下,波特率是没有意义的,它是设置传输速率的蓝牙无线电信号。 The driver will accept whatever you select but will otherwise ignore it.驱动程序将接受您选择的任何内容,否则将忽略它。 Handshake signals might be interpreted, it's up to the driver to implement them.握手信号可能会被解释,这取决于驱动程序来实现它们。 Communication error reporting is very rarely implemented, BlueTooth has an error correcting protocol, unlike a real serial port.通信错误报告很少实现,蓝牙有一个纠错协议,不像真正的串口。

No, the loss of data here is entirely self induced.不,这里的数据丢失完全是自我引起的。 Clearly the driver does implement DiscardInBuffer().很明显,驱动程序确实实现了 DiscardInBuffer()。 Which accomplishes nothing but throw away any data that the driver received.除了丢弃驱动程序接收到的任何数据外,什么都不做。 This goes wrong if your code runs a bit late or gets interrupted by a thread context switch.如果您的代码运行稍晚或被线程上下文切换中断,则会出错。

Delete the DiscardInBuffer() call.删除 DiscardInBuffer() 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM