I am trying to read out some serial data from my STM32 board to GUI, and also, I have trouble closing down my serial communication port.
I am using 2013 Visual Studio and chose Visual Basic as my programming language. What I wanted to send from the microcontroller is some serial data that I sent byte-by-byte. So, for example, I will be sending "<abcde>"
from my microcontroller with "<"
and ">"
as my start and stop bit; and hopefully my visual basic GUI will read and display it.
I have tried to display it at visual basic using serialport.readexisting() with no problem, but then I need to separate each bytes by itself. In the end, I wanted my program to read and process each byte, which will be some sensor values and can display it in the VB program.
I have tried using both serialport.read() and serialport.readbyte() but I don't know why, it does not show the correct output. What I meant of not correct is that the ascii code nor characters does not represent on what I sent from the microcontroller.
Here is the time when I use readbyte().
'Visual Basic --> for serialport.readbyte()
Dim buff_rx As Byte
Private Sub SerialPort1_DataReceived_1(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
SerialPort1.ReadTimeout = 20
Do While SerialPort1.BytesToRead > 0
Try
buff_rx = SerialPort1.ReadByte
Me.Invoke(New EventHandler(AddressOf update_dat))
Catch ex As Exception
End Try
Loop
End Sub
Public Sub update_dat(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i As Integer = buff_rx
Dim s As String = ""
s = i.ToString("X2") 'if wanted to be in hex, use "X2"
Rx_text.Text = Rx_text.Text & " " & i
End Sub
Here is the part when I tried using the read() part.
'Visual Basic --> for serialport1.read()
Private Sub SerialPort1_DataReceived_1(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim bytestoread As Int16
bytestoread = SerialPort1.BytesToRead
Dim buff(bytestoread) As Byte
ReceivedText(SerialPort1.Read(buff, 0, bytestoread - 1))
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.Rx_text.InvokeRequired Then 'form 1 is Me'
Dim k As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(k, New Object() {(text)})
Else
Me.Rx_text.Text &= [text]
End If
End Sub
This part below is when I send the data from microcontroller
//Microcontroller side, data sending
tx3_buff[0] = '<'; //ascii code: 60
tx3_buff[1] = 'b'; //ascii code: 98
tx3_buff[2] = 'c'; //ascii code: 99
tx3_buff[3] = 'd'; //ascii code: 100
tx3_buff[4] = 'e'; //ascii code: 101
tx3_buff[5] = 'f'; //ascii code: 102
tx3_buff[6] = 'g'; //ascii code: 103
tx3_buff[7] = 'h'; //ascii code: 104
tx3_buff[8] = 'i'; //ascii code: 105
tx3_buff[9] = '>'; //ascii code: 62
The data that I was intended to send from the microcrontroller are shown on the code as well. As I mentioned before, when I used the serialport.readexisting(), I can correctly read "". But when I use either serialport.readbyte() and serialport.read() it reads:
160 16 161 33 198 18 52 68 84 200 232 16 40 200
and so on and so on which is definitely incorrect and made nonsense.
In terms of the close() problem, I put the serialport.close() inside the button function. I already tried from some forums that stated we should use begininvoke instead of only using invoke (like here: https://blogs.msdn.microsoft.com/bclteam/2006/10/10/top-5-serialport-tips-kim-hamilton/)
But it still won't work. Here is the Close button part:
'Visual Basic --> for disconnect button
Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
SerialPort1.Close()
End Sub
Everytime after I pressed this button, it will always be stuck and hanged. I need to restart the program so this problem will be solved. Any ideas for this part?
To easily summarize, here are my problems:
I have literally no idea how to use the serialport.readbyte() and serialport.read(). I really need this as I wanted to separate the data byte-by-byte so I can process it easily.
The serialport.close() button always hangs and I don't know why.
I hope someone can help me with these problems. Thank you in advance for all your help and sorry if my thread is messy!
Thank you again!
Buff_Rx is a byte array and can't be assigned to an integer:
Dim i As Integer = buff_rx