Public Class Form1
    '無線機と電話機間の通信を転送します。
    '2009/830 7M4MON
    '起動時の処理
    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 
        MyBase.Shown()
        SerialPort1.NewLine = vbCr
        SerialPort2.NewLine = vbCr
        SerialPort1Open()
        SerialPort2Open()
    End Sub
    'COMポートを変更した場合の処理1
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        SerialPort1Open()
    End Sub
    'COMポートを変更した場合の処理2
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        SerialPort2Open()
    End Sub
    'COMポートを開きます1
    Private Sub SerialPort1Open()
        If SerialPort1.IsOpen = True Then
            'If SerialPort& port & .IsOpen = True Then
            SerialPort1.Close()
        End If
        Try
            SerialPort1.PortName = TextBox1.Text
            SerialPort1.Open()
            Label1.Enabled = True
        Catch ex As Exception
            Label1.Enabled = False
        End Try
    End Sub
    'COMポートを開きます2
    Private Sub SerialPort2Open()
        If SerialPort2.IsOpen = True Then
            SerialPort2.Close()
        End If
        Try
            SerialPort2.PortName = TextBox2.Text
            SerialPort2.Open()
            Label2.Enabled = True
            CheckBox1.Enabled = True
        Catch ex As Exception
            Label2.Enabled = False
            CheckBox1.Enabled = False
        End Try
    End Sub
    '終了時にはCOMポートを閉じます。
    Private Sub Form1_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
        If SerialPort2.IsOpen = True Then
            SerialPort2.Close()
        End If
    End Sub
    'Phone->RIG
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.
SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim readBuff1 As StringC:\Documents and Settings\7m4mon\My Documents\Visual Studio 2008\Projects\D-TEL_S\D-TEL_S\Form1.vb
        readBuff1 = SerialPort1.ReadLine
        SerialPort2.WriteLine(readBuff1) '文字を転送します。
        Debug.Print(readBuff1)
    End Sub
    'RIG->Phone
    Private Sub SerialPort2_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.
SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
        Dim readBuff2 As String
        readBuff2 = SerialPort2.ReadLine
        SerialPort1.WriteLine(readBuff2)
        Control.CheckForIllegalCrossThreadCalls = False 'CheckBox1を直接操作するため。
        'PTTを操作して音声転送処理を行います。
        If readBuff2 = "ATH" Then            '切断コマンドで
            CheckBox1.Checked = False
            'SerialPort2.RtsEnable = False   '転送終了
        ElseIf readBuff2 = "ATA" OrElse readBuff2.Contains("ATD") Then        '応答/発信コマンドで
            CheckBox1.Checked = True
            'SerialPort2.RtsEnable = True    '転送開始
        End If
        Debug.Print(readBuff2)
    End Sub
    'PTT操作
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            SerialPort2.RtsEnable = True
            Debug.Print("ON")
        Else
            SerialPort2.RtsEnable = False
            Debug.Print("OFF")
        End If
    End Sub
End Class