Skip to content

Instantly share code, notes, and snippets.

Last active Oct 13, 2020
What would you like to do?
VB.NET Serial RecivingSample Program Form1.vb
'Serial Port Recieve Sample Program
'Form1 Object must create ,button1,2 & textbox 1,2 
Imports System.IO.Ports
Public Class Form1
Delegate Sub DataDelegate(ByVal sdata As String)
Private Sub PrintData(ByVal sdata As String)
TextBox2.Text = sdata
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.PortName = TextBox1.Text 'オープンするポート名を格納
SerialPort1.Open() 'ポートオープン
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then 'ポートオープン済み
SerialPort1.Close() 'ポートクローズ
End If
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim ReceivedData As String = " " '受信データ用変数を宣言します
ReceivedData = SerialPort1.ReadLine 'データを受信します
Catch ex As Exception
ReceivedData = ex.Message '例外処理を行います
End Try
Dim adre As New DataDelegate(AddressOf PrintData)
Me.Invoke(adre, ReceivedData)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment