Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Last active October 13, 2020 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/bd3b2f1e58db04f231bbbcc7aa6715eb to your computer and use it in GitHub Desktop.
Save dj1711572002/bd3b2f1e58db04f231bbbcc7aa6715eb to your computer and use it in GitHub Desktop.
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 = " " '受信データ用変数を宣言します
Try
ReceivedData = SerialPort1.ReadLine 'データを受信します
Catch ex As Exception
ReceivedData = ex.Message '例外処理を行います
End Try
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
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