Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C++
#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);
}

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

Source:
VB
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!
Posted
Updated 26-Sep-18 2:37am

1 solution

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.
 
Share this answer
 
Comments
w8Ball 26-Sep-18 10:55am    
Thank you for your reply. That is the answer I'm looking for. I just realized the best solution to mark the message "complete" is matching it with a Regular Expression pattern.
I don't know much about networks, as you can tell. The internet is full of related topics, but if you do not mind at all, I'm open to sites.

Cheers!
Dave Kreskowiak 26-Sep-18 11:42am    
There's plenty of examples on the web.
Google: "C# tcp/ip client[^]

Bluetooth works the same way.
w8Ball 27-Sep-18 13:30pm    
Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900