久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

vb.net利用SerialPort進(jìn)行讀取串口操作

 filedot 2011-12-08
vb.net利用SerialPort進(jì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
'獲取計(jì)算機(jī)有效串口
Dim ports As String() = SerialPort.GetPortNames() '必須用命名空間,,用SerialPort,獲取計(jì)算機(jī)的有效串口
Dim port As String
For Each port In ports
portnamebox.Items.Add(port)
'向combobox中添加項(xiàng)
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è)置串口參數(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 '校驗(yàn)位
End Sub

'關(guān)閉串口連接
Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click
Try
SerialPort1.Close()
'關(guān)閉串口
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

'打開(kāi)串口連接
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click
Try
SerialPort1.Open()
'打開(kāi)串口
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)) '調(diào)用接收數(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) '添加的延時(shí)
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

分類: VB編程

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多