2022年6月17日 星期五

vb.net利用SerialPort進行讀取串口操作

 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

沒有留言:

張貼留言