我正在开发一个通过笔记本电脑/PC 的 USB 端口连接 RS232 的项目。我已经创建了 vb.net 应用程序。当应用程序第一次加载时,它需要检测串口。到目前为止,我手动将端口名放入串行端口的属性中,但是如果我部署我的应用程序并且如果我使用其他笔记本电脑/PC,则会出现错误:运行我的 GUI 时出现 System.IO.IOException。我想对串口的自动检测进行编程,但我是 vb.net 中串口编程的新手。
谁能帮我?谢谢!
这是我的程序的一部分:
Imports System.IO.Ports
Public Class Form1
'Dim myPort As Array
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'myPort = IO.Ports.SerialPort.GetPortNames()
SerialPort1.Open()
Timer1.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'==zigbee sent data to the app====
Console.Beep(3000, 1000) 'high tone buzzer whenever there is a notification received
MsgBox("THERE IS A NOTIFICATION RECEIVED!")
uart_rx = Me.SerialPort1.ReadExisting
toDisplay = toDisplay + uart_rx
flag = 1 'there is a notification sent
End Sub
....
在 myPort = IO.Ports.SerialPort.GetPortNames() 之后,我不知道下一步该做什么。