Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class FrmTerminal
	Inherits System.Windows.Forms.Form
#Region "Windows フォーム デザイナによって生成されたコード"
	Public Sub New()
		MyBase.New()
		If m_vb6FormDefInstance Is Nothing Then
			If m_InitializingDefInstance Then
				m_vb6FormDefInstance = Me
			Else
				Try 
					'スタートアップ フォームについては、最初に作成されたインスタンスが既定インスタンスになります。
					If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
						m_vb6FormDefInstance = Me
					End If
				Catch
				End Try
			End If
		End If
		'この呼び出しは、Windows フォーム デザイナで必要です。
		InitializeComponent()
	End Sub
	'Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
	Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
		If Disposing Then
			If Not components Is Nothing Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(Disposing)
	End Sub
	'Windows フォーム デザイナで必要です。
	Private components As System.ComponentModel.IContainer
	Public ToolTip1 As System.Windows.Forms.ToolTip
	Public WithEvents ComCancel As System.Windows.Forms.Button
	Public WithEvents ComStop As System.Windows.Forms.Button
	Public WithEvents ComStart As System.Windows.Forms.Button
	Public WithEvents PortNo As System.Windows.Forms.ComboBox
	Public WithEvents ResponseText As System.Windows.Forms.TextBox
	Public WithEvents RecvTimer As System.Windows.Forms.Timer
	Public WithEvents Send As System.Windows.Forms.Button
	Public WithEvents SerialCmnd As System.Windows.Forms.TextBox
	Public WithEvents RecvText As System.Windows.Forms.TextBox
	'メモ : 以下のプロシージャは Windows フォーム デザイナで必要です。
	'Windows フォーム デザイナを使って変更できます。
	'コードエディタを使って修正しないでください。


	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmTerminal))
		Me.components = New System.ComponentModel.Container()
		Me.ToolTip1 = New System.Windows.Forms.ToolTip(components)
		Me.ToolTip1.Active = True
		Me.ComCancel = New System.Windows.Forms.Button
		Me.ComStop = New System.Windows.Forms.Button
		Me.ComStart = New System.Windows.Forms.Button
		Me.PortNo = New System.Windows.Forms.ComboBox
		Me.ResponseText = New System.Windows.Forms.TextBox
		Me.RecvTimer = New System.Windows.Forms.Timer(components)
		Me.Send = New System.Windows.Forms.Button
		Me.SerialCmnd = New System.Windows.Forms.TextBox
		Me.RecvText = New System.Windows.Forms.TextBox
		Me.Text = "VB Sample(タイマー処理）"
		Me.ClientSize = New System.Drawing.Size(549, 358)
		Me.Location = New System.Drawing.Point(4, 30)
		Me.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
		Me.BackColor = System.Drawing.SystemColors.Control
		Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable
		Me.ControlBox = True
		Me.Enabled = True
		Me.KeyPreview = False
		Me.MaximizeBox = True
		Me.MinimizeBox = True
		Me.Cursor = System.Windows.Forms.Cursors.Default
		Me.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.ShowInTaskbar = True
		Me.HelpButton = False
		Me.WindowState = System.Windows.Forms.FormWindowState.Normal
		Me.Name = "FrmTerminal"
		Me.ComCancel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		Me.ComCancel.Text = "受信中止"
		Me.ComCancel.Size = New System.Drawing.Size(73, 25)
		Me.ComCancel.Location = New System.Drawing.Point(376, 15)
		Me.ComCancel.TabIndex = 7
		Me.ComCancel.BackColor = System.Drawing.SystemColors.Control
		Me.ComCancel.CausesValidation = True
		Me.ComCancel.Enabled = True
		Me.ComCancel.ForeColor = System.Drawing.SystemColors.ControlText
		Me.ComCancel.Cursor = System.Windows.Forms.Cursors.Default
		Me.ComCancel.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.ComCancel.TabStop = True
		Me.ComCancel.Name = "ComCancel"
		Me.ComStop.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		Me.ComStop.Text = "通信終了"
		Me.ComStop.Size = New System.Drawing.Size(81, 25)
		Me.ComStop.Location = New System.Drawing.Point(272, 15)
		Me.ComStop.TabIndex = 2
		Me.ComStop.BackColor = System.Drawing.SystemColors.Control
		Me.ComStop.CausesValidation = True
		Me.ComStop.Enabled = True
		Me.ComStop.ForeColor = System.Drawing.SystemColors.ControlText
		Me.ComStop.Cursor = System.Windows.Forms.Cursors.Default
		Me.ComStop.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.ComStop.TabStop = True
		Me.ComStop.Name = "ComStop"
		Me.ComStart.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		Me.ComStart.Text = "通信開始"
		Me.ComStart.Size = New System.Drawing.Size(81, 25)
		Me.ComStart.Location = New System.Drawing.Point(168, 16)
		Me.ComStart.TabIndex = 1
		Me.ComStart.BackColor = System.Drawing.SystemColors.Control
		Me.ComStart.CausesValidation = True
		Me.ComStart.Enabled = True
		Me.ComStart.ForeColor = System.Drawing.SystemColors.ControlText
		Me.ComStart.Cursor = System.Windows.Forms.Cursors.Default
		Me.ComStart.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.ComStart.TabStop = True
		Me.ComStart.Name = "ComStart"
		Me.PortNo.Size = New System.Drawing.Size(137, 20)
		Me.PortNo.Location = New System.Drawing.Point(8, 16)
		Me.PortNo.TabIndex = 0
		Me.PortNo.BackColor = System.Drawing.SystemColors.Window
		Me.PortNo.CausesValidation = True
		Me.PortNo.Enabled = True
		Me.PortNo.ForeColor = System.Drawing.SystemColors.WindowText
		Me.PortNo.IntegralHeight = True
		Me.PortNo.Cursor = System.Windows.Forms.Cursors.Default
		Me.PortNo.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.PortNo.Sorted = False
		Me.PortNo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown
		Me.PortNo.TabStop = True
		Me.PortNo.Visible = True
		Me.PortNo.Name = "PortNo"
		Me.ResponseText.AutoSize = False
		Me.ResponseText.Size = New System.Drawing.Size(529, 57)
		Me.ResponseText.Location = New System.Drawing.Point(8, 88)
		Me.ResponseText.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal
		Me.ResponseText.WordWrap = False
		Me.ResponseText.TabIndex = 5
		Me.ResponseText.TabStop = False
		Me.ResponseText.AcceptsReturn = True
		Me.ResponseText.TextAlign = System.Windows.Forms.HorizontalAlignment.Left
		Me.ResponseText.BackColor = System.Drawing.SystemColors.Window
		Me.ResponseText.CausesValidation = True
		Me.ResponseText.Enabled = True
		Me.ResponseText.ForeColor = System.Drawing.SystemColors.WindowText
		Me.ResponseText.HideSelection = True
		Me.ResponseText.ReadOnly = False
		Me.ResponseText.Maxlength = 0
		Me.ResponseText.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.ResponseText.MultiLine = False
		Me.ResponseText.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.ResponseText.Visible = True
		Me.ResponseText.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
		Me.ResponseText.Name = "ResponseText"
		Me.RecvTimer.Enabled = False
		Me.RecvTimer.Interval = 100
		Me.Send.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		Me.Send.Text = "送信"
		Me.Send.Size = New System.Drawing.Size(81, 25)
		Me.Send.Location = New System.Drawing.Point(456, 48)
		Me.Send.TabIndex = 4
		Me.Send.BackColor = System.Drawing.SystemColors.Control
		Me.Send.CausesValidation = True
		Me.Send.Enabled = True
		Me.Send.ForeColor = System.Drawing.SystemColors.ControlText
		Me.Send.Cursor = System.Windows.Forms.Cursors.Default
		Me.Send.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.Send.TabStop = True
		Me.Send.Name = "Send"
		Me.SerialCmnd.AutoSize = False
		Me.SerialCmnd.Size = New System.Drawing.Size(433, 25)
		Me.SerialCmnd.Location = New System.Drawing.Point(8, 48)
		Me.SerialCmnd.TabIndex = 3
		Me.SerialCmnd.AcceptsReturn = True
		Me.SerialCmnd.TextAlign = System.Windows.Forms.HorizontalAlignment.Left
		Me.SerialCmnd.BackColor = System.Drawing.SystemColors.Window
		Me.SerialCmnd.CausesValidation = True
		Me.SerialCmnd.Enabled = True
		Me.SerialCmnd.ForeColor = System.Drawing.SystemColors.WindowText
		Me.SerialCmnd.HideSelection = True
		Me.SerialCmnd.ReadOnly = False
		Me.SerialCmnd.Maxlength = 0
		Me.SerialCmnd.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.SerialCmnd.MultiLine = False
		Me.SerialCmnd.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.SerialCmnd.ScrollBars = System.Windows.Forms.ScrollBars.None
		Me.SerialCmnd.TabStop = True
		Me.SerialCmnd.Visible = True
		Me.SerialCmnd.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
		Me.SerialCmnd.Name = "SerialCmnd"
		Me.RecvText.AutoSize = False
		Me.RecvText.Size = New System.Drawing.Size(529, 185)
		Me.RecvText.Location = New System.Drawing.Point(8, 160)
		Me.RecvText.MultiLine = True
		Me.RecvText.TabIndex = 6
		Me.RecvText.TabStop = False
		Me.RecvText.AcceptsReturn = True
		Me.RecvText.TextAlign = System.Windows.Forms.HorizontalAlignment.Left
		Me.RecvText.BackColor = System.Drawing.SystemColors.Window
		Me.RecvText.CausesValidation = True
		Me.RecvText.Enabled = True
		Me.RecvText.ForeColor = System.Drawing.SystemColors.WindowText
		Me.RecvText.HideSelection = True
		Me.RecvText.ReadOnly = False
		Me.RecvText.Maxlength = 0
		Me.RecvText.Cursor = System.Windows.Forms.Cursors.IBeam
		Me.RecvText.RightToLeft = System.Windows.Forms.RightToLeft.No
		Me.RecvText.ScrollBars = System.Windows.Forms.ScrollBars.None
		Me.RecvText.Visible = True
		Me.RecvText.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
		Me.RecvText.Name = "RecvText"
		Me.Controls.Add(ComCancel)
		Me.Controls.Add(ComStop)
		Me.Controls.Add(ComStart)
		Me.Controls.Add(PortNo)
		Me.Controls.Add(ResponseText)
		Me.Controls.Add(Send)
		Me.Controls.Add(SerialCmnd)
		Me.Controls.Add(RecvText)
	End Sub
#End Region 
#Region "アップグレード ウィザードのサポート コード"
	Private Shared m_vb6FormDefInstance As FrmTerminal
	Private Shared m_InitializingDefInstance As Boolean
	Public Shared Property DefInstance() As FrmTerminal
		Get
			If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
				m_InitializingDefInstance = True
				m_vb6FormDefInstance = New FrmTerminal()
				m_InitializingDefInstance = False
			End If
			DefInstance = m_vb6FormDefInstance
		End Get
		Set
			m_vb6FormDefInstance = Value
		End Set
	End Property
#End Region 
	
	' VB タイマー処理サンプル
	
	Private RecvType As Short
	Private ComId As Integer
	
	Private Sub ComCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComCancel.Click
		IT4xxx_ComCancel(ComId)
	End Sub
	
	Private Sub ComStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComStart.Click
        ComId = IT4xxx_PortOpen(PortNo.SelectedIndex + 1, IT4xxx_BAUD_9600, IT4xxx_DATABIT_8, IT4xxx_STOPBIT_1, IT4xxx_PARITY_NONE, IT4xxx_FLOW_RTSCTS)
		
		If ComId < 0 Then
			MsgBox("ポートオープンエラー")
			Exit Sub
		End If
		
		IT4xxx_TimeoutSet(ComId, -1, 100)
		
		PortNo.Enabled = False
		ComStart.Enabled = False
		ComStop.Enabled = True
		Send.Enabled = True
		ComCancel.Enabled = True
		
		RecvTimer.Enabled = True
		
		RecvType = 0
	End Sub
	
	Private Sub ComStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ComStop.Click
		If RecvType = 2 Then
			Exit Sub
		End If
		
		IT4xxx_PortClose(ComId)
		
		PortNo.Enabled = True
		ComStart.Enabled = True
		ComStop.Enabled = False
		Send.Enabled = False
		ComCancel.Enabled = False
		
		RecvTimer.Enabled = False
		
		PortNo.Focus()
	End Sub
	
	Private Sub FrmTerminal_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim Idx As Short
		
		PortNo.Enabled = True
		For Idx = 1 To 10
			PortNo.Items.Add("COM" & Str(Idx))
		Next 
		PortNo.SelectedIndex = 0
		
		ComStart.Enabled = True
		ComStop.Enabled = False
		Send.Enabled = False
		ComCancel.Enabled = False
		
		RecvTimer.Enabled = False
	End Sub
	
    Private Sub FrmTerminal_Closing(ByVal eventSender As System.Object, ByVal eventArgs As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If RecvType = 2 Then
            eventArgs.Cancel = True
        Else
            eventArgs.Cancel = False
        End If
    End Sub

    Private Sub FrmTerminal_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
        IT4xxx_PortClose(ComId)
    End Sub

    Private Sub RecvTimer_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles RecvTimer.Tick
        Const ResBufSize As Short = 1024
        Dim ResponseBuf As String = ""
        Dim recvsize As Integer
        Dim Idx As Integer
        Dim DispText As String
        Dim EditStr As String

        If IT4xxx_RecvChk(ComId) <= 0 Then
            Exit Sub
        End If

        RecvTimer.Enabled = False

        recvsize = 0
        Select Case RecvType
            Case 0 '   データ受信
                recvsize = vbIT4xxx_DataRecv(ComId, ResponseBuf, ResBufSize, "", "")
            Case 1 '   コマンド受信
                recvsize = vbIT4xxx_ResponseRecv(ComId, ResponseBuf, ResBufSize)
            Case 2 '   イメージ受信
                FrmProgress.DefInstance.ComId = ComId
                FrmProgress.DefInstance.Show()
                recvsize = vbIT4xxx_ImageRecv(ComId, "RecvImage.jpg", ResponseBuf, ResBufSize, IT4xxx_PROTOCOL_2P)
                FrmProgress.DefInstance.Close()
        End Select

        Send.Enabled = True

        If recvsize <= 0 Then
            ErrMessage(recvsize)
        Else
            DispText = ""
            For Idx = 1 To recvsize
                EditStr = Mid(ResponseBuf, Idx, 1)
                Select Case Asc(EditStr)
                    Case &H6S : EditStr = "<ACK>"
                    Case &H5S : EditStr = "<ENQ>"
                    Case &HAS : EditStr = "<LF>"
                    Case &HDS : EditStr = "<CR>"
                    Case &H15S : EditStr = "<NAK>"
                    Case &H16S : EditStr = "<SYN>"
                    Case Else
                        If Asc(EditStr) < &H20S Or Asc(EditStr) > &H7FS Then
                            EditStr = "<" & VB.Right("0" & Hex(Asc(EditStr)), 2) & ">"
                        End If
                End Select
                DispText = DispText & EditStr
            Next

            If RecvType = 0 Then
                RecvText.Text = RecvText.Text & DispText & Chr(&HDS) & Chr(&HAS)
            Else
                ResponseText.Text = DispText
            End If
        End If

        RecvType = 0
        RecvTimer.Enabled = True
    End Sub

    Private Sub Send_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Send.Click
        Dim ret As Integer
        Dim SendText As String

        ret = vbIT4xxx_CmndSend(ComId, SerialCmnd.Text)
        If ret <> IT4xxx_NORMAL_END Then
            ErrMessage(ret)
            Exit Sub
        End If

        SendText = UCase(SerialCmnd.Text)

        Send.Enabled = False
        If InStr(SendText, "IMG") > 0 And (InStr(SendText, "SHP") > 0 Or InStr(SendText, "BOX")) And InStr(SendText, "?") <= 0 And InStr(SendText, "^") <= 0 Then
            RecvType = 2
        Else
            RecvType = 1
        End If

        ComCancel.Enabled = True
    End Sub

    Private Sub ErrMessage(ByRef errcode As Object)
        Select Case errcode
            Case IT4xxx_ERR_NOTOPEN
                MsgBox("ポートオープンエラー")
            Case IT4xxx_ERR_FILE
                MsgBox("ファイルエラー")
            Case IT4xxx_ERR_TIMEOUT
                MsgBox("受信タイムアウト")
            Case IT4xxx_ERR_CANCEL
                MsgBox("キャンセル")
            Case IT4xxx_ERR_PARAMETER
                MsgBox("パラメータエラー")
            Case IT4xxx_ERR_COM
                MsgBox("通信エラー")
            Case IT4xxx_ERR_FULL
                MsgBox("バッファフル")
                IT4xxx_ComClear(ComId)
            Case Else
                MsgBox("エラー発生")
        End Select
    End Sub
End Class