vb读取串口 存入mysql_VB.net 利用SerialPort进行读取串口操作
发布日期:2021-08-20 05:18:33 浏览次数:46 分类:技术文章

本文共 2557 字,大约阅读时间需要 8 分钟。

Imports System

Imports System.IO.Ports

Public 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

'关闭串口连接

Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click

Try

SerialPort1.Close() '关闭串口

Label3.Text = SerialPort1.IsOpen

If SerialPort1.IsOpen = False Then

statuslabel.Text = "串口未连接"

statuslabel.ForeColor = Color.Red

receivebox.Text = ""

receivebytes.Text = ""

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

'打开串口连接

Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click

Try

SerialPort1.Open() '打开串口

Label3.Text = SerialPort1.IsOpen

If SerialPort1.IsOpen = True Then

statuslabel.Text = "串口已连接"

statuslabel.ForeColor = Color.Green

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

'发送数据

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

SerialPort1.Write(sendbox.Text)

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

'触发接收事件

Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数

End Sub

'接收数据

Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)

Dim strIncoming As String

Try

receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead)

If SerialPort1.BytesToRead > 0 Then

Threading.Thread.Sleep(100) '添加的延时

strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据

SerialPort1.DiscardInBuffer()

receivebox.Text = strIncoming

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

End Class

转载地址:https://blog.csdn.net/weixin_33501587/article/details/113644525 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python3文件处理_Python3 文件处理相关脚本
下一篇:python在线翻译脚本_python写的翻译脚本详解

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月04日 15时16分50秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章