VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.net使用SerialPort类访问串口

 

	
  1. Public Class Form1
  2.  
    Dim mySerialPort As New System.IO.Ports.SerialPort
  3.  
    Delegate Sub DataShow(ByVal myString As String)
  4.  
     
  5.  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.  
    mySerialPort.Write("at+wmsn" & vbCrLf)
  7.  
    End Sub
  8.  
     
  9.  
    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  10.  
    If mySerialPort.IsOpen Then
  11.  
    mySerialPort.Close()
  12.  
    End If
  13.  
    End Sub
  14.  
     
  15.  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  16.  
    With mySerialPort
  17.  
    .PortName = "COM3"
  18.  
    .DataBits = 8
  19.  
    .StopBits = IO.Ports.StopBits.One
  20.  
    .Parity = IO.Ports.Parity.None
  21.  
    .BaudRate = 115200
  22.  
    .RtsEnable = True
  23.  
    .ReceivedBytesThreshold = 1
  24.  
    AddHandler mySerialPort.DataReceived, AddressOf SerialPort_DataReceived
  25.  
    .Open()
  26.  
    End With
  27.  
    End Sub
  28.  
     
  29.  
    Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
  30.  
    Dim sp As IO.Ports.SerialPort = sender
  31.  
    Dim s As String = sp.ReadExisting
  32.  
    Invoke(New DataShow(AddressOf SerialPort_DataShow), New Object() {s})
  33.  
    End Sub
  34.  
     
  35.  
    Private Sub SerialPort_DataShow(ByVal myString As String)
  36.  
    TextBox1.Text = TextBox1.Text & myString
  37.  
    End Sub
  38.  
    End Class
 


相关教程