我用 Visual Basic 编写了这个程序来控制一个小型机器人。程序本身基本上只是将我手的 X 和 Y 坐标发送到控制机器人的 Arduino 微处理器。
出于某种原因,当波特率设置为 115200(我正在使用的 arduino 蓝牙板要求)时,应用程序一开始发送串行数据就会冻结。然而,当我将它设置为 9200 并使用我的 arduino diecimilia 时,应用程序并没有冻结。
我需要波特率为 115200,因为我想使用我的 arduino 蓝牙与我的机器人进行无线通信。关于如何阻止它冻结的任何想法?
向 Arduino 发送数据的代码
Private Sub ArduinoSetSerial()
Dim ArduinoCom As String = ComPort.text
_serialPort = New SerialPort()
_serialPort.PortName = "COM" + Trim(ComPort.Text) 'Gets the ComPort from the Text box in the GUI of the App
_serialPort.BaudRate = 9200 'When using any other Arduino PLC, this should be set to 9600. In this case, it is set to 115200 as the specififc Arduino PLC we are using requires it.
_serialPort.DataBits = 8
_serialPort.Handshake = 0
_serialPort.ReadTimeout = 500
_serialPort.WriteTimeout = 500
End Sub
Private Sub ArduinoOpenSerial()
If Not _serialPort.IsOpen Then 'Opens the serial port if it isn't already open.
_serialPort.Open()
Else
MsgBox("ARDUINO: SERIAL PORT CANNOT BE OPENED")
End If
_continue = True
End Sub
Private Sub ArduinoCloseSerial() 'Defines the CloseSerial Action executed on shutdown
If _serialPort.IsOpen Then
_serialPort.Close()
End If
End Sub
Private Sub ArduinoSendByte(ByVal kinect_x As Single, ByVal kinect_y As Single, ByVal kinect_z As Single, ByVal kinect_j As Integer)
Dim x, y, z, j As Byte
Dim sx, sy As Single
Dim HowOften As Integer
ComStatus.Text = "NA" 'Set ComStatus Text to NA when not sending data
x = Math.Abs(CByte(kinect_x)) 'The X co-ordinate of the requested JointID
y = Math.Abs(CByte(kinect_y)) 'The Y co-ordinate of the requested JointID
z = CByte(kinect_z) 'The Z co-ordinate of the requested JointID (Not used in this particular application)
j = CByte(kinect_j) 'JointID
x = x
Dim ArduinoBuffer() As Byte = {x, y, z, j}
If _serialPort.IsOpen Then
ComStatus.Text = "OK" 'Write OK inthe ComStatus text box in the GUI
_serialPort.Write(ArduinoBuffer, 0, ArduinoBuffer.Length) 'Actually sends all the information to the Arduino on the specified Serial Port
End If
End Sub