Search code examples
vb.netarduinoserial-communication

How to diplay a simple value from Arduino in Visual Basic?


hope u're doing well! I'm in fact trying to realize a monitoring system. A part of it consist in whenever one of the machines stops, we'll enter a code using a keypad and displaying it in a textbox (in visual basic), and then it will be sent to my MySQL database. My problem now is at the level of the communication Arduino-VB, I can see these values in the Arduino monitoring, but my textbox is always empty. Here is my prototype Arduino example:

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeComponent()
        SerialPort1.Open()
        SerialPort1.PortName = "COM21"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default

    End Sub

    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim bytes As Integer = 6
            Dim Chaine As String = ""
            Dim comBuffer As Byte() = New Byte(bytes - 1) {}
            Chaine = SerialPort1.Read(comBuffer, 0, bytes - 1)
            For i As Integer = 1 To (bytes - 1)
                Chaine = Chaine + comBuffer(i)
            Next

            TextBox1.Text = Chaine.ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Thank you very much !


Solution

  • The problem is that TextBox1 is being accessed from a different thread than the one it was created on (normal thread of windows forms) In order to access it, the second thread (serial.datareceived) needs to sort of asks permission and see if its safe to do so to access it

    Imports System.IO
    Imports System.IO.Ports
    Imports System.Threading
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            InitializeComponent()
            SerialPort1.PortName = "COM21"
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = Parity.None
            SerialPort1.StopBits = StopBits.One
            SerialPort1.Handshake = Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.Default
            SerialPort1.Open()
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim Chaine As String = ""
                Chaine = SerialPort1.ReadExisting();
    
                If TextBox1.InvokeRequired Then
                        TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= Chaine, MethodInvoker))
                    Else
                        TextBox1.Text &= Chaine
                    End if
                 Catch ex As Exception
                    MessageBox.Show(ex.Message)
                 End Try
            End Sub
        End Class