如何在 Visual Basic 中显示来自 Arduino 的简单值?
How to diplay a simple value from Arduino in Visual Basic?
希望你一切顺利!
事实上,我正在尝试实现一个监控系统。它的一部分包括每当其中一台机器停止时,我们将使用键盘输入代码并将其显示在文本框中(在 visual basic 中),然后它将发送到我的 MySQL 数据库。
我现在的问题是在通信级别 Arduino-VB,我可以在 Arduino 监控中看到这些值,但我的文本框总是空的。
这是我的原型 Arduino 示例:
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
非常感谢!
问题是 TextBox1 正在从与创建它的线程不同的线程(windows 形式的普通线程)访问它,第二个线程(serial.datareceived ) 需要征求许可,看看这样做是否安全才能访问它
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
希望你一切顺利! 事实上,我正在尝试实现一个监控系统。它的一部分包括每当其中一台机器停止时,我们将使用键盘输入代码并将其显示在文本框中(在 visual basic 中),然后它将发送到我的 MySQL 数据库。 我现在的问题是在通信级别 Arduino-VB,我可以在 Arduino 监控中看到这些值,但我的文本框总是空的。 这是我的原型 Arduino 示例:
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
非常感谢!
问题是 TextBox1 正在从与创建它的线程不同的线程(windows 形式的普通线程)访问它,第二个线程(serial.datareceived ) 需要征求许可,看看这样做是否安全才能访问它
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