Vb.net 如何在Visual Basic中显示Arduino中的简单值?
希望你做得很好! 事实上,我正试图实现一个监控系统。它的一部分在于,每当其中一台机器停止时,我们将使用键盘输入一个代码,并将其显示在文本框中(在visual basic中),然后它将被发送到我的MySQL数据库。 我现在的问题是Arduino VB的通信级别,我可以在Arduino监控中看到这些值,但我的文本框总是空的。 以下是我的原型Arduino示例:Vb.net 如何在Visual Basic中显示Arduino中的简单值?,vb.net,arduino,serial-communication,Vb.net,Arduino,Serial Communication,希望你做得很好! 事实上,我正试图实现一个监控系统。它的一部分在于,每当其中一台机器停止时,我们将使用键盘输入一个代码,并将其显示在文本框中(在visual basic中),然后它将被发送到我的MySQL数据库。 我现在的问题是Arduino VB的通信级别,我可以在Arduino监控中看到这些值,但我的文本框总是空的。 以下是我的原型Arduino示例: Imports System.IO Imports System.IO.Ports Imports System.Threading Pu
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
尝试并移动
SerialPort1。打开到Form1\u加载的末尾
它不工作:/。。这是已翻译的错误消息:操作线程间无效:控件“Textbox”已从创建它的线程以外的线程访问。好的,我知道您的问题。。。我正在写解决方案,是用Invoke指令完成的吗?我现在就试试看没有结果:/!!首先,我用Arduino监视器和一个简单的例子(显示按键)进行了尝试,结果成功了。但是当我运行我的VB程序并尝试再次运行时,它显示我在打开串行端口时出错,当我们使用arduino监视器时无法打开,因此,我关闭了它并再次运行VB程序,但文本框仍然为空!在datareceived事件中添加一个断点,然后查看Chaine
的情况。我这样做了,但在调试时,我认为一切都是正确的。此外,我尝试将Chaine
的类型从字符串更改为字节,并将Invoke指令更改为:TextBox1.Text=Chaine.ToString
,这就是我在按下“A”时遇到的错误(例如,“A”到“Byte”的转换无效).好的,这让我相信数据实际上来自Arduino。我未能理解的是,在执行调用之前,Chaine
的值是多少。如果将其保留为字符串,那么在调用调用之前它的值是多少?