VB_Net_串口通信程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Imports System.IO.Ports

Imports System.Text

Public Class Form1

Dim RS232Com As New SerialPort

Dim mPortName As String

Public Sub OpenCOM()

mPortName = Me.CmbCom.SelectedItem.ToString

RS232Com = New SerialPort(mPortName, 9600, Parity.None, 8, StopBits.One)

RS232Com.Encoding = Encoding.ASCII

If Not RS232Com.IsOpen Then RS232Com.Open()

End Sub

Public Sub CloseCOM()

If RS232Com.IsOpen Then RS232Com.Close()

End Sub

Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click

Call OpenCOM()

End Sub

Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click

Call CloseCOM()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

For Each sp As String In SerialPort.GetPortNames

Me.CmbCom.Items.Add(sp)

Next

Me.CmbCom.SelectedIndex = 0

End Sub

Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click

RS232Com.Write(Me.TextBox1.Text) '即写命令过去

End Sub

Private Sub BtnReceived_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReceived.Click

Dim sOut As String

sOut = RS232Com.ReadExisting 'sOut即读到的字符串End Sub

End Class

相关文档
最新文档