为什么arduino没有像这样通过HC-06蓝牙模块传输整个数据,并且是否有“修复”功能。 ? [英] Why is arduino not transfering whole data through HC-06 bluetooth module like this and is there a "fix" ?

查看:176
本文介绍了为什么arduino没有像这样通过HC-06蓝牙模块传输整个数据,并且是否有“修复”功能。 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个小的Arduino草图,每隔5秒通过蓝牙模块(我使用的是HC-06)发送随机数据,我有一个接收和管理数据的VB.NET Windows应用程序。数据看起来像例如(255 ** 255 ** 255)



我的VB.Net应用程序接收蓝牙模块发送的当前数据,弹出一个带有当前接收数据的消息框,并将数据添加到RichTextBox控件。所以整个过程应该是这样的:



1.字符串:(100 ** 390 ** 222)通过以下方式发送Arduino代码中的蓝牙

2.消息框弹出当前数据(100 ** 390 ** 222)

3。字符串被添加到RichTextBox控件

...



相反,它实际上并不是在一个实例上发送整个字符串,这里是实际过程:



1.字符串:(100 ** 390 ** 222)通过Arduino代码中的蓝牙发送

2.消息框弹出当前数据

3.字符串被添加到RichTextBox控件

4.消息框弹出当前数据100 ** 390 ** 222

5.字符串被添加到RichTextBox控件

...



有时,它会发送前两个字符,然后是其余字符。为什么不将整个字符串发送到一个实例并且有解决方法呢?



我尝试过:



这是我的Arduino草图:

Hello. I have a tiny Arduino sketch that sends random data via Bluetooth Module (I am using HC-06) every 5 seconds and I have a VB.NET windows Application that receives and manages the data. The data would look like this for example: "(255**255**255)"

My VB.Net Application receives current data being sent by the Bluetooth Module, pops up a message box with the current received data and adds the data to a RichTextBox control. So the whole process should look like this:

1. String: "(100**390**222)" gets sent via Bluetooth in Arduino code
2. Message Box pops up with the present data "(100**390**222)"
3. String gets added to the RichTextBox control
...

Instead, it doesn't actually send the whole string at one instance, here's the actual process:

1. String: "(100**390**222)" gets sent via Bluetooth in Arduino code
2. Message Box pops up with the present data "("
3. String gets added to the RichTextBox control
4. Message Box pops up with the present data "100**390**222)"
5. String gets added to the RichTextBox control
...

Sometimes, it sends the first two characters then the rest. Why doesn't it send the whole string at one instance and is there a fix to that?

What I have tried:

Here is my Arduino sketch:

#include <SoftwareSerial.h>

SoftwareSerial BTserial(15, 17);

uint8_t SomeFirstNumber, SomeSecondNumber, SomeThirdNumber;

void setup()
{
    BTserial.begin(38400);
}

void loop()
{
    SomeFirstNumber = random(255);
    SomeSecondNumber = random(255);
    SomeThirdNumber = random(255);
    
    BTserial.print("(" + String(SomeFirstNumber) + "**" + String(SomeSecondNumber) + "**" + String(SomeThirdNumber) + ")");

    delay(5000);
}



这是我的VB.Net应用程序:

https://images2.imgbox.com/9f/54/djv2xbck_o.png [ ^ ]



来源:


Here's my VB.Net Application:
https://images2.imgbox.com/9f/54/djv2xbck_o.png[^]

Source:

Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub Open_Port(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_OpenPort.Click
    Dim bException As Boolean = False

    SerialPort1.PortName = "COM3"
    SerialPort1.BaudRate = 38400
    SerialPort1.Parity = IO.Ports.Parity.None
    SerialPort1.StopBits = IO.Ports.StopBits.One
    SerialPort1.DataBits = 8

    Try
        SerialPort1.Open()
    Catch ex As Exception
        bException = True
        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
    End Try

    If Not bException Then
        MsgBox("Success!", MsgBoxStyle.Information)
    End If
End Sub

Private Sub Close_Port(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_ClosePort.Click
    SerialPort1.Close()
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    ReceivedText(SerialPort1.ReadExisting())
End Sub

Private Sub ReceivedText(ByVal [text] As String)
    If RichTextBox_Output.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})
    Else
        MsgBox([text])
        RichTextBox_Output.Text += [text]
        RichTextBox_Output.ScrollToCaret()

        SerialPort1.DiscardOutBuffer()
    End If
End Sub





我尽力解释。提前谢谢!



I tried explaining as best as I could. Thank you in advance!

推荐答案

编写代码是为了期望一个接收数据包中的整个消息,但这不是通过任何网络进行通信的方式。



您的接收代码必须缓冲任何获取的内容并继续添加到后续接收事件的缓冲内容,直到收到完整的消息。这取决于您的接收代码。数据中有什么可以知道是的,我收到了一条完整的消息?在你的情况下,它看起来像一个)字符。知道新消息正在启动的标记是(字符。当您收到完整消息时,您可以将该消息发送到应用程序中要处理它的任何代码。
Your code is written to expect an entire message in one "receive packet" but that's not how communication, over ANY network, works.

Your receive code has to buffer whatever is gets and keeps on adding to the buffered content on subsequent receive events until a complete message has been received. That is determined by your receive code. What does it look for in the data to know that "Yes, I have received an entire message"? In your case, it looks like a ")" character. The marker to know that a new message is starting is the "(" character. When you receive a complete message, you can send that message to whatever code in your application is going to process it.


这篇关于为什么arduino没有像这样通过HC-06蓝牙模块传输整个数据,并且是否有“修复”功能。 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆