Dim UART1 as FormUART = New FormUART
Dim rootNo as integer = 0
Delegate Sub dlgAddText(ByVal GetText As String)
Private Sub Button1_click 〜
Dim Data as String = "ABC"
UART1.Send(Data)
End Sub
Public Sub AddText(ByVal strGetText As String)
Select Case rootNo
Case 1
Dim strTmp As String = ""
Dim strVer_Array() As String = Split(strGetText)
For i As Integer = 0 To strVer_Array.Length - 1
strTmp = strTmp + Chr(strVer_Array(i))
Next
TextBox1.Text = strTmp
Case 2
〜
End Select
End Sub
−−−−−−−−−−−−−−−
Class FormUART
Dim LByte As List(Of Byte) = New List(Of Byte)
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
〜(strInに取り込んだデータを格納)
Me.Invoke(New Form1.dlgAddText(AddressOf Form1.AddText), New Object() {strIn})
End Sub
'FormUARTから呼ばれる
Public Sub AddText(ByVal strGetText As String)
TextBox1.Text = TextBox1.Text + strGetText
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
UART1 = New FormUART()
UART1.Visible = True
UART1.SetForm(Me) 'Form1のインスタンスを渡す
End Sub
End Class
Public Class FormUART
Dim F1 As Form1
Delegate Sub dlgAddText(ByVal GetText As String)
Public Sub AddText(ByVal strText As String)
F1.AddText(strText)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim inStr As String = SerialPort1.ReadChar()
Me.Invoke(New dlgAddText(AddressOf Me.AddText), _
New Object() {inStr})
End Sub
Public Sub SetForm(ByVal f As Form1)
F1 = f
End Sub
Private Sub FormUART_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.Open()
End Sub
End Class