Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a simple program to communicate via Bluetooth with an Arduino. The program opens a serial port, connects to the Bluetooth port on the Arduino and sends single characters (x,y,z)on keystrokes. Since the Arduino carries out the functions as determined by the keys, that part is working fine.

I wanted the board to return the character of the key that was pressed to deliver the command, so I have the Arduino Serial.write back, which it appears to do. I read the data with this code:

VB
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object,
                                         ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
        If SerialPort1.IsOpen Then
            Try
                readBuffer = SerialPort1.ReadLine()
                'data to UI thread 
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
            Catch ex As Exception
                MsgBox("read " & ex.Message)
            End Try
        End If
    End Sub


    ' update received string in UI 

    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)

        ' rtbMonitor.Text = SerialPort1.BytesToRead
        If readBuffer.Contains("y") Then

            rtbMonitor.Text = "contains the letter y"  'this is the result
        Else
            rtbMonitor.Text = readBuffer
        End If

If readBuffer.Equals("y") Then

            rtbMonitor2.Text = "the letter y"  
        Else
            rtbMonitor2.Text = readBuffer  ' y is the result
        End If


What I have tried:

I want to use the returned character for some other functions, however, the data type escapes me. Alternately, I am unsure of what is being sent back from the Arduino. I thought if I read the buffer as above, the simple string "y" would be returned. Apparently, the buffer contains a "y" but does not equal a "y". The y seems to be text (string); it is not read as char. So the question is what is in the buffer (readBuffer) other than "y" and how can I read it? There is very little data being sent. Just a keystroke every few seconds.
Posted
Updated 27-Jul-16 10:41am
Comments
Richard Deeming 27-Jul-16 15:17pm    
So debug your code and inspect the value of the variable. Is it a string of length 1, containing precisely a single lower-case "y", with no white-space or other characters?

We don't have access to your system, so we can't debug your code for you.
Sleeper 11888211 27-Jul-16 16:12pm    
Thanks. I will try to read it byte by byte and see what that brings. I figured there were other non-visible characters. Pointers are always welcome for this hobbyist.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Using ReadLine() imply getting a string, if you want chars, use a function that read 1 char at a time.
 
Share this answer
 
If you are using System.IO.Ports.SerialPort, then the SerialPort.ReadLine waits for a CR to be received. If no CR, then after the timeout period, a TimeoutException should be thrown.

Are you getting the TimeoutException?

Try using SerialPort.ReadByte instead in order to read a single byte. Check SerialPort.BytesToRead and loop reading all the bytes.

Also, look at SerialPort.ReceivedBytesThreshold - set it to 1 to get an event for each byte. If you do set to 1, your ability to process the bytes is limited so don't send very many bytes.

Sorry, VB.Net is not my primary programming language

VB.NET
If SerialPort1.IsOpen Then
    Try
        Dim byteCount as Int = SerialPort1.BytesToRead
        Dim i as Int
        Dim data as Int
        For i = 1 to byteCount
            data = SerialPort1.ReadByte()
            If data <> -1 Then
                'data to UI thread
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
            End If
        Next
    Catch ex As Exception
        MsgBox("read " & ex.Message)
    End Try
End If
 
Share this answer
 
v2
Comments
Sleeper 11888211 29-Jul-16 15:42pm    
Thanks! I'm not a professional- it's my hobby!

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



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