PingPong専用Windowsプログラムのソース
PingPong専用Windowsプログラムは操作性よりも、コードの単純化とデバッグ性を優先しました。コードはForm.vbのみです。以下に全コードを示します。
Imports System.IO.Ports
Public Class Form1
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub UpdateCOMPortList()
Dim s As String
Dim i As Integer
Dim foundDifference As Boolean
i = 0
foundDifference = False
If lstCOMPorts.Items.Count = SerialPort.GetPortNames().Length Then
For Each s In SerialPort.GetPortNames()
If lstCOMPorts.Items(i).Equals(s) = False Then
foundDifference = True
End If
i = i + 1
Next s
Else
foundDifference = True
End If
If foundDifference = False Then
Exit Sub
End If
lstCOMPorts.Items.Clear()
For Each s In SerialPort.GetPortNames()
lstCOMPorts.Items.Add(s)
Next s
lstCOMPorts.SelectedIndex = 0
End Sub
Private Sub UpdateCOMPortList2()
Dim s As String
Dim i As Integer
Dim foundDifference As Boolean
i = 0
foundDifference = False
If lstCOMPorts2.Items.Count = SerialPort.GetPortNames().Length Then
For Each s In SerialPort.GetPortNames()
If lstCOMPorts2.Items(i).Equals(s) = False Then
foundDifference = True
End If
i = i + 1
Next s
Else
foundDifference = True
End If
If foundDifference = False Then
Exit Sub
End If
lstCOMPorts2.Items.Clear()
For Each s In SerialPort.GetPortNames()
lstCOMPorts2.Items.Add(s)
Next s
lstCOMPorts2.SelectedIndex = 0
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
UpdateCOMPortList()
UpdateCOMPortList2()
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
SerialPort1.PortName = lstCOMPorts.Items(lstCOMPorts.SelectedIndex).ToString()
SerialPort1.Open()
btnConnect.Enabled = False
lstCOMPorts.Enabled = False
btnClose.Enabled = True
txtDataReceived.Clear()
txtDataReceived.AppendText("Connected." + vbCrLf)
Catch ex As Exception
btnClose_Click(Me, e)
End Try
End Sub
Private Sub btnConnect2_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect2.Click
Try
SerialPort2.PortName = lstCOMPorts2.Items(lstCOMPorts2.SelectedIndex).ToString()
SerialPort2.Open()
btnConnect2.Enabled = False
lstCOMPorts2.Enabled = False
btnClose2.Enabled = True
txtDataReceived2.Clear()
txtDataReceived2.AppendText("Connected." + vbCrLf)
Catch ex As Exception
btnClose2_Click(Me, e)
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
btnClose.Enabled = False
btnConnect.Enabled = True
lstCOMPorts.Enabled = True
Try
SerialPort1.DiscardInBuffer()
SerialPort1.DiscardOutBuffer()
SerialPort1.Close()
Catch ex As Exception
End Try
End Sub
Private Sub btnClose2_Click(sender As System.Object, e As System.EventArgs) Handles btnClose2.Click
btnClose2.Enabled = False
btnConnect2.Enabled = True
lstCOMPorts2.Enabled = True
Try
SerialPort2.DiscardInBuffer()
SerialPort2.DiscardOutBuffer()
SerialPort2.Close()
Catch ex As Exception
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
SetText(SerialPort1.ReadExisting())
Catch ex As Exception
btnClose_Click(Me, e)
End Try
End Sub
Private Sub SerialPort2_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
Try
SetText2(SerialPort2.ReadExisting())
Catch ex As Exception
btnClose2_Click(Me, e)
End Try
End Sub
Private Sub SetText(ByVal [text] As String)
If txtDataReceived.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Invoke(d, New Object() {[text]})
Else
txtDataReceived.AppendText(text)
End If
End Sub
Private Sub SetText2(ByVal [text] As String)
If txtDataReceived2.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText2)
Invoke(d, New Object() {[text]})
Else
txtDataReceived2.AppendText(text)
End If
End Sub
Private Sub btnSendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendData.Click
Try
SerialPort1.Write(txtData.Text)
Catch ex As Exception
btnClose_Click(Me, e)
End Try
End Sub
Private Sub btnSendData2_Click(sender As System.Object, e As System.EventArgs) Handles btnSendData2.Click
Try
SerialPort2.Write(txtData2.Text)
Catch ex As Exception
btnClose2_Click(Me, e)
End Try
End Sub
End Class