Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multi SerialPort basic Test pgm
'Seril Port Recieve Sample Program
'Form1 Object must create ,button1,2 & textbox 1,2 
Imports System.IO.Ports
Public Class Form1
Dim ReceivedDataTotal As String = ""
Dim ReceivedData1 As String = " " '受信データ用変数を宣言します
Dim ReceivedData2 As String = " " '受信データ用変数を宣言します
Dim ReceivedData3 As String = " " '受信データ用変数を宣言します
Dim ReceivedData4 As String = " " '受信データ用変数を宣言します
Dim ReceivedData5 As String = " " '受信データ用変数を宣言します
Delegate Sub DataDelegate(ByVal sdata As String)
Private Sub PrintData(ByVal sdata As String)
ListBox1.Items.Add(sdata)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.PortName = TextBox1.Text 'オープンするポート名を格納
SerialPort2.PortName = TextBox2.Text 'オープンするポート名を格納
'SerialPort3.PortName = TextBox3.Text 'オープンするポート名を格納
'SerialPort4.PortName = TextBox4.Text 'オープンするポート名を格納
'SerialPort5.PortName = TextBox5.Text 'オープンするポート名を格納
SerialPort1.Open() 'ポート1オープン
SerialPort2.Open() 'ポート2オープン
'SerialPort3.Open() 'ポート3オープン
'SerialPort4.Open() 'ポート4オープン
'SerialPort5.Open() 'ポート5オープン
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then 'ポートオープン済み
SerialPort1.Close() 'ポートクローズ
SerialPort2.Close()
'SerialPort3.Close()
'SerialPort4.Close()
'SerialPort5.Close()
End If
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
ReceivedData1 = SerialPort1.ReadLine 'データを受信します
Catch ex As Exception
ReceivedData1 = ex.Message '例外処理を行います
End Try
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
Dim adre As New DataDelegate(AddressOf PrintData)
Me.Invoke(adre, ReceivedData1)
End Sub
Private Sub SerialPort2_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
ReceivedData2 = SerialPort2.ReadLine 'データを受信します
Catch ex As Exception
ReceivedData2 = ex.Message '例外処理を行います
End Try
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
Dim adre As New DataDelegate(AddressOf PrintData)
Me.Invoke(adre, ReceivedData2)
End Sub
'Private Sub SerialPort3_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
' Try
' ReceivedData3 = SerialPort3.ReadLine 'データを受信します
' Catch ex As Exception
' ReceivedData3 = ex.Message '例外処理を行います
' End Try
' 'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
' Dim adre As New DataDelegate(AddressOf PrintData)
' Me.Invoke(adre, ReceivedData3)
'End Sub
'Private Sub SerialPort4_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
' Try
' ReceivedData4 = SerialPort4.ReadLine 'データを受信します
' Catch ex As Exception
' ReceivedData4 = ex.Message '例外処理を行います
' End Try
' 'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
' Dim adre As New DataDelegate(AddressOf PrintData)
' Me.Invoke(adre, ReceivedData4)
'End Sub
'Private Sub SerialPort5_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
' Try
' ReceivedData5 = SerialPort4.ReadLine 'データを受信します
' Catch ex As Exception
' ReceivedData5 = ex.Message '例外処理を行います
' End Try
' 'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
' Dim adre As New DataDelegate(AddressOf PrintData)
' Me.Invoke(adre, ReceivedData5)
'End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "COM6"
TextBox2.Text = "COM8"
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.