0

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 !

1 Answers1

0

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
Keith Mifsud
  • 725
  • 5
  • 16
  • So it's done with the Invoke instruction ? I'll try it right now – HamzaLamfaouar May 28 '15 at 08:58
  • No results :/ !! I tried it at first with the Arduino monitor with a simple example (showing the keys pressed) and it works. But when I run my VB program and trying to do it again, it shows me an error when openning the Serial Port, It can't be opened when we use the arduino monitor, so, I closed it and run the vb program again, but the textbox still empty ! – HamzaLamfaouar May 28 '15 at 09:31
  • Add a breakpoint in the datareceived event and see what is going on with `Chaine` – Keith Mifsud May 28 '15 at 10:24
  • I did it, but when debugging, everything was in my opinion correct. Also, I tried to change the type of `Chaine` from String to Byte and change the Invoke instruction to this : `TextBox1.Text = Chaine.ToString` and this is the error that i've got when I press 'A' for example (The conversion of "A" to "Byte" is invalid). – HamzaLamfaouar May 28 '15 at 13:48
  • OK this leads me to believe that data is actually coming in from Arduino. What I'm failing to understand is what is the value of `Chaine` before you do the `Invoke`. If you leave it as a string, whats its value just before you call the invoke? – Keith Mifsud May 29 '15 at 07:10
  • Before the `Invoke`, `Chaine` is a byte variable. After calling it, and to make sure to display all the data coming from the Arduino as strings, I added the instruction `ToString` to convert it and affect it to my textbox! I hope I could explain it well ! – HamzaLamfaouar May 29 '15 at 10:24
  • care to share :)? I've decided to look and see if there are other ways to read data from the serialPort like for example this post: http://stackoverflow.com/questions/11590945/how-to-display-the-data-read-in-datareceived-event-handler-of-serialport The solution is in c# and it is using the same concept with the invoke but what he is doing is using `SerialPort1.ReadExisting()' and it returns a string – Keith Mifsud May 29 '15 at 11:04
  • Glad I was able to help :) – Keith Mifsud May 29 '15 at 11:17
  • IT WORKS :D.. OH GOD, IT REALLY WORKS ! I tried it with a very simple code that sends a integer with Delay, and Its displaying like with the Arduino Monitor ! What's the tip behind the `&=` ? – HamzaLamfaouar May 29 '15 at 11:18
  • it is like `TextBox1.Text = TextBox1.Text & Chaine` so the text does not replace itself but adds to what there was previously – Keith Mifsud May 29 '15 at 11:20
  • Just like append then ! Thank you very much for your help ! I really really appreciate it and I hope I didn't bother you ! I'll try to apply this to arrays :) – HamzaLamfaouar May 29 '15 at 11:21
  • yes exactly like append :) No problems. I've been coding for years now and I got an arduino Uno recently so its good for me to see what other people are doing with it and problems they encounter :) – Keith Mifsud May 29 '15 at 11:23
  • Me too, I've just bought it for a project and I've just learned coding with VB .NET, and I'm really happy with it! In fact, I wasn't that good in this, but now, you really motivated me and pushed me to do my best and keep searching for the information, and sooner or later, i'll make it ! That's why I really thank you very much ! Good luuck for your projects and hope we'll stay in touch :) .. Also, I'll tell if there's something new in the array's treatment code :) – HamzaLamfaouar May 29 '15 at 11:31