日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

vb.net利用SerialPort进行读取串口操作

發(fā)布時間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vb.net利用SerialPort进行读取串口操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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() '設置串口參數(shù)
SerialPort1.BaudRate = Val(baudratebox.Text) '波特率
SerialPort1.PortName = portnamebox.Text '串口名稱
SerialPort1.DataBits = 8 '數(shù)據(jù)位
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


'發(fā)送數(shù)據(jù)
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

'觸發(fā)接收事件
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)) '調用接收數(shù)據(jù)函數(shù)
End Sub

'接收數(shù)據(jù)
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 '讀取緩沖區(qū)中的數(shù)據(jù)
SerialPort1.DiscardInBuffer()
receivebox.Text
= strIncoming
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class

轉載于:https://www.cnblogs.com/jiaxiaoai/archive/2011/08/09/2132502.html

總結

以上是生活随笔為你收集整理的vb.net利用SerialPort进行读取串口操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。