シリアル通信プログラム

Public Class SerialCommunication
    Dim TempStr As String ' 一時文字列

    Private Sub SerialCommunication_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .PortName = "COM1"
            .BaudRate = 1200
            .DataBits = 8
            .Parity = IO.Ports.Parity.None
            .StopBits = IO.Ports.StopBits.One
            .ReceivedBytesThreshold = 1
        End With

    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim value As Char
        Dim str As String
        value = e.KeyChar

        If value = vbCr Then
            str = TextBox1.Text.ToUpper() + vbCr
            If SerialPort1.IsOpen Then
                SerialPort1.Write(str)
            End If
            TextBox1.SelectAll()
        End If
    End Sub

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        If SerialPort1.IsOpen = False Then
            Try
                SerialPort1.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "エラー", _
                    MessageBoxButtons.OK, _
                    MessageBoxIcon.Error, _
                    MessageBoxDefaultButton.Button3)
            End Try
        Else
            MessageBox.Show("既にシリアルポートを開いています。", "エラー", _
                MessageBoxButtons.OK, _
                MessageBoxIcon.Warning, _
                MessageBoxDefaultButton.Button3)

        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If SerialPort1.IsOpen = True Then
            SerialPort1.Close()
        End If
    End Sub

    Private Sub SerialCommunication_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If SerialPort1.IsOpen = True Then
            SerialPort1.Close()
        End If
    End Sub

    Delegate Sub RecvDataDelegate(ByVal RecvStr As String)

    Private Sub RecvData(ByVal RecvStr As String)
        ' 受信欄更新
        TextBox2.Text = RecvStr

        ' 受信履歴更新
        RichTextBox1.Text = RichTextBox1.Text + RecvStr + vbCr

        ' 受信履歴の最下行表示
        RichTextBox1.SelectionStart = RichTextBox1.Text.Length - 1
        RichTextBox1.ScrollToCaret()
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim Recv As New RecvDataDelegate(AddressOf RecvData)
        Dim strReceived As String

        Try
            ' 受信データ読込
            strReceived = SerialPort1.ReadExisting()

            ' 受信文字列結合
            TempStr = TempStr + strReceived

            ' CR受信(受信完了)
            If strReceived(strReceived.Length - 1) = vbCr Then
                ' CR削除
                TempStr = TempStr.Remove(TempStr.Length - 1, 1)

                ' TextBox1からInvoke
                TextBox1.Invoke(Recv, TempStr)

                ' 一時文字列初期化
                TempStr = ""
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "受信エラー", _
                MessageBoxButtons.OK, _
                MessageBoxIcon.Error, _
                MessageBoxDefaultButton.Button3)
        End Try

    End Sub

End Class