利用SerialPort进行读取串口操作
Imports System
Imports System.IO.PortsPublic
Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '获取计算机有效串口
Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口 Dim port As String
For Each port In ports
portnamebox.Items.Add(port) '向combobox中添加项
Next port '初始化界面
baudratebox.SelectedIndex() = 2
portnamebox.SelectedIndex() = 0
Serial_Port1() '初始化串口
Label3.Text = SerialPort1.IsOpen
statuslabel.Text = "串口未连接"
statuslabel.ForeColor = Color.Red
sendbox.Text = "123"
' baudratebox.Text = baudratebox.Items(0) 注释和不注释的地方可以替换
'portnamebox.Text = portnamebox.Items(0)
End Sub
Private Sub Serial_Port1() '设置串口参数
SerialPort1.BaudRate = Val(baudratebox.Text) '波特率
SerialPort1.PortName = portnamebox.Text '串口名称
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
End Sub
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载
- 1
- 2
前往页