我使用了此代码的另一种变体,通过tcp / ip和udp将箭头按下作为数据发送。我正在尝试将箭头按压信息发送到arduino over serial。这个代码过去对我来说很好,但每当我尝试编译我的vb.net项目时,我都会收到以下错误消息“错误1'SerialPort1'未被声明。由于其保护级别,它可能无法访问.C:\用户\ chris.FTKNOCKS \ Documents \ Visual Studio 2013 \ Projects \ WindowsApplication8 \ WindowsApplication8 \ Form1.vb 10 9 WindowsApplication8“如果有人有任何想法或建议,我会在程序中的每个Serial1实例中收到其中一个错误我会很感激它
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com14" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Right Then
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
e.Handled = True
ElseIf e.KeyCode = Keys.Left Then
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
e.Handled = True
ElseIf e.KeyCode = Keys.Down Then
SerialPort1.Open()
SerialPort1.Write("2")
SerialPort1.Close()
e.Handled = True
ElseIf e.KeyCode = Keys.Up Then
SerialPort1.Open()
SerialPort1.Write("3")
SerialPort1.Close()
e.Handled = True
End If
End Sub
Dim keydelay As Integer = 0
End Class