vb.net 如何在vb.net中编写自动检测串口的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15183865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to program automatic detect serial port in vb.net
提问by Catherine Takishima
I am working on a project where RS232 is connected via USB port of laptop / PC. I already created the vb.net application. As the application loads for the first time, it needs to detect the serial port. As of now, I manually put the portname in the properties of the serialport but if I deploy my application and if I use other laptop / PC, there would be an error: System.IO.IOException as I run my GUI. I want to program the automatic detect of serialport but I am new to serial port programming in vb.net.
我正在开发一个通过笔记本电脑/PC 的 USB 端口连接 RS232 的项目。我已经创建了 vb.net 应用程序。应用程序首次加载时,需要检测串口。到目前为止,我手动将端口名放在串行端口的属性中,但是如果我部署我的应用程序并且如果我使用其他笔记本电脑/PC,则会出现错误:System.IO.IOException 当我运行我的 GUI 时。我想对串口的自动检测进行编程,但我是 vb.net 中串口编程的新手。
Can anyone help me? thanks!
谁能帮我?谢谢!
This is some part of my program:
这是我的程序的一部分:
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
....
After myPort = IO.Ports.SerialPort.GetPortNames() , I don't know what to do next.
在 myPort = IO.Ports.SerialPort.GetPortNames() 之后,我不知道接下来要做什么。
回答by w0051977
There is an example showing how this is done on the following webpage: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
以下网页上有一个示例显示了这是如何完成的:http: //msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
In the example the user is prompted for the serial port settings before the application communicates with the port.
在该示例中,在应用程序与端口通信之前提示用户进行串行端口设置。
SerialPort.GetPortNames (http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx) will get the names of the serial ports.
SerialPort.GetPortNames ( http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx) 将获得串行端口的名称。
回答by VLPATIL
I provide a Listbox and populate it with all ports on the system. User has to select the com port, after which you assign that port name to serial port.
Code is as follows
Dim SP as string
我提供了一个列表框并用系统上的所有端口填充它。用户必须选择 com 端口,然后将该端口名称分配给串行端口。
代码如下 Dim SP as string
Private Sub frmSelectPort_Load(sender as Object, e as
System. EventArgs) Handles Me. Load
GetSerialPortnames
End Sub
Sub GetSerialPortnames () For Each SP In My. Computer.
SerialPortNames
Listbox1. Items. Add(sp)
End Sub
(Under button click event)
SP =Listbox1. SelectedItem
SerialPort1. PortName=SP
SerialPort1. Open()

