'Copyright (C) 2002 Microsoft Corporation
'All rights reserved.
'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

'Requires the Trial or Release version of Visual Studio .NET Professional (or greater).

Option Strict On

Imports System.Text
Imports System.IO
'\Windows\System32\Drivers\FTD2XX.SYS
'C:\Windows\System32\Drivers\FTSER2K.SYS

Public Class frmMain
	Inherits System.Windows.Forms.Form

    ' Declare necessary class variables.
    Dim End_Reading_Flag As Boolean = False
    Private m_CommPort As New COMport_DirectUSB
    Dim Detected_Driver As String
    Private m_IsTCICFound As Boolean = False
    Private m_TCICPort As Integer = 0
    Const cBaudRate As Integer = 115200
    Public cMaxNumberOfPorts As Integer = 255
    Public RealMaxNumberOfPorts As Integer = 255
    Dim NumDevs As Integer
    Public Serial_Number(255) As String
    Dim PortType As String = "COM"
    Dim CommunicationStatus As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        ' So that we only need to set the title of the application once,
        ' we use the AssemblyInfo class (defined in the AssemblyInfo.vb file)
        ' to read the AssemblyTitle attribute.
        Dim ainfo As New AssemblyInfo

        Me.Text = ainfo.Title
        Me.mnuAbout.Text = String.Format("&About {0} ...", ainfo.Title)

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents mnuMain As System.Windows.Forms.MainMenu
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
    Friend WithEvents mnuHelp As System.Windows.Forms.MenuItem
    Friend WithEvents mnuAbout As System.Windows.Forms.MenuItem
    Friend WithEvents txtStatus As System.Windows.Forms.TextBox
    Friend WithEvents tmrReadCommPort As System.Windows.Forms.Timer
    Friend WithEvents clstPorts As System.Windows.Forms.CheckedListBox
    Friend WithEvents btnCheckForPorts As System.Windows.Forms.Button
    Friend WithEvents btnSendUserCommand As System.Windows.Forms.Button
    Friend WithEvents btnClear As System.Windows.Forms.Button
    Friend WithEvents GrUserCommand As System.Windows.Forms.GroupBox
    Friend WithEvents ChkWithRet As System.Windows.Forms.CheckBox
    Friend WithEvents txtUserCommand As System.Windows.Forms.TextBox
    Friend WithEvents btnCheckTCIC As System.Windows.Forms.Button
    Friend WithEvents txtSelectedTCIC As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.mnuMain = New System.Windows.Forms.MainMenu
        Me.mnuFile = New System.Windows.Forms.MenuItem
        Me.mnuExit = New System.Windows.Forms.MenuItem
        Me.mnuHelp = New System.Windows.Forms.MenuItem
        Me.mnuAbout = New System.Windows.Forms.MenuItem
        Me.btnCheckForPorts = New System.Windows.Forms.Button
        Me.txtStatus = New System.Windows.Forms.TextBox
        Me.tmrReadCommPort = New System.Windows.Forms.Timer(Me.components)
        Me.clstPorts = New System.Windows.Forms.CheckedListBox
        Me.btnCheckTCIC = New System.Windows.Forms.Button
        Me.txtSelectedTCIC = New System.Windows.Forms.TextBox
        Me.btnSendUserCommand = New System.Windows.Forms.Button
        Me.btnClear = New System.Windows.Forms.Button
        Me.GrUserCommand = New System.Windows.Forms.GroupBox
        Me.ChkWithRet = New System.Windows.Forms.CheckBox
        Me.txtUserCommand = New System.Windows.Forms.TextBox
        Me.GrUserCommand.SuspendLayout()
        Me.SuspendLayout()
        '
        'mnuMain
        '
        Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuHelp})
        Me.mnuMain.RightToLeft = CType(resources.GetObject("mnuMain.RightToLeft"), System.Windows.Forms.RightToLeft)
        '
        'mnuFile
        '
        Me.mnuFile.Enabled = CType(resources.GetObject("mnuFile.Enabled"), Boolean)
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuExit})
        Me.mnuFile.Shortcut = CType(resources.GetObject("mnuFile.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuFile.ShowShortcut = CType(resources.GetObject("mnuFile.ShowShortcut"), Boolean)
        Me.mnuFile.Text = resources.GetString("mnuFile.Text")
        Me.mnuFile.Visible = CType(resources.GetObject("mnuFile.Visible"), Boolean)
        '
        'mnuExit
        '
        Me.mnuExit.Enabled = CType(resources.GetObject("mnuExit.Enabled"), Boolean)
        Me.mnuExit.Index = 0
        Me.mnuExit.Shortcut = CType(resources.GetObject("mnuExit.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuExit.ShowShortcut = CType(resources.GetObject("mnuExit.ShowShortcut"), Boolean)
        Me.mnuExit.Text = resources.GetString("mnuExit.Text")
        Me.mnuExit.Visible = CType(resources.GetObject("mnuExit.Visible"), Boolean)
        '
        'mnuHelp
        '
        Me.mnuHelp.Enabled = CType(resources.GetObject("mnuHelp.Enabled"), Boolean)
        Me.mnuHelp.Index = 1
        Me.mnuHelp.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuAbout})
        Me.mnuHelp.Shortcut = CType(resources.GetObject("mnuHelp.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuHelp.ShowShortcut = CType(resources.GetObject("mnuHelp.ShowShortcut"), Boolean)
        Me.mnuHelp.Text = resources.GetString("mnuHelp.Text")
        Me.mnuHelp.Visible = CType(resources.GetObject("mnuHelp.Visible"), Boolean)
        '
        'mnuAbout
        '
        Me.mnuAbout.Enabled = CType(resources.GetObject("mnuAbout.Enabled"), Boolean)
        Me.mnuAbout.Index = 0
        Me.mnuAbout.Shortcut = CType(resources.GetObject("mnuAbout.Shortcut"), System.Windows.Forms.Shortcut)
        Me.mnuAbout.ShowShortcut = CType(resources.GetObject("mnuAbout.ShowShortcut"), Boolean)
        Me.mnuAbout.Text = resources.GetString("mnuAbout.Text")
        Me.mnuAbout.Visible = CType(resources.GetObject("mnuAbout.Visible"), Boolean)
        '
        'btnCheckForPorts
        '
        Me.btnCheckForPorts.AccessibleDescription = resources.GetString("btnCheckForPorts.AccessibleDescription")
        Me.btnCheckForPorts.AccessibleName = resources.GetString("btnCheckForPorts.AccessibleName")
        Me.btnCheckForPorts.Anchor = CType(resources.GetObject("btnCheckForPorts.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnCheckForPorts.BackgroundImage = CType(resources.GetObject("btnCheckForPorts.BackgroundImage"), System.Drawing.Image)
        Me.btnCheckForPorts.Dock = CType(resources.GetObject("btnCheckForPorts.Dock"), System.Windows.Forms.DockStyle)
        Me.btnCheckForPorts.Enabled = CType(resources.GetObject("btnCheckForPorts.Enabled"), Boolean)
        Me.btnCheckForPorts.FlatStyle = CType(resources.GetObject("btnCheckForPorts.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnCheckForPorts.Font = CType(resources.GetObject("btnCheckForPorts.Font"), System.Drawing.Font)
        Me.btnCheckForPorts.Image = CType(resources.GetObject("btnCheckForPorts.Image"), System.Drawing.Image)
        Me.btnCheckForPorts.ImageAlign = CType(resources.GetObject("btnCheckForPorts.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnCheckForPorts.ImageIndex = CType(resources.GetObject("btnCheckForPorts.ImageIndex"), Integer)
        Me.btnCheckForPorts.ImeMode = CType(resources.GetObject("btnCheckForPorts.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnCheckForPorts.Location = CType(resources.GetObject("btnCheckForPorts.Location"), System.Drawing.Point)
        Me.btnCheckForPorts.Name = "btnCheckForPorts"
        Me.btnCheckForPorts.RightToLeft = CType(resources.GetObject("btnCheckForPorts.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnCheckForPorts.Size = CType(resources.GetObject("btnCheckForPorts.Size"), System.Drawing.Size)
        Me.btnCheckForPorts.TabIndex = CType(resources.GetObject("btnCheckForPorts.TabIndex"), Integer)
        Me.btnCheckForPorts.Text = resources.GetString("btnCheckForPorts.Text")
        Me.btnCheckForPorts.TextAlign = CType(resources.GetObject("btnCheckForPorts.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnCheckForPorts.Visible = CType(resources.GetObject("btnCheckForPorts.Visible"), Boolean)
        '
        'txtStatus
        '
        Me.txtStatus.AccessibleDescription = resources.GetString("txtStatus.AccessibleDescription")
        Me.txtStatus.AccessibleName = resources.GetString("txtStatus.AccessibleName")
        Me.txtStatus.Anchor = CType(resources.GetObject("txtStatus.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.txtStatus.AutoSize = CType(resources.GetObject("txtStatus.AutoSize"), Boolean)
        Me.txtStatus.BackgroundImage = CType(resources.GetObject("txtStatus.BackgroundImage"), System.Drawing.Image)
        Me.txtStatus.Dock = CType(resources.GetObject("txtStatus.Dock"), System.Windows.Forms.DockStyle)
        Me.txtStatus.Enabled = CType(resources.GetObject("txtStatus.Enabled"), Boolean)
        Me.txtStatus.Font = CType(resources.GetObject("txtStatus.Font"), System.Drawing.Font)
        Me.txtStatus.ImeMode = CType(resources.GetObject("txtStatus.ImeMode"), System.Windows.Forms.ImeMode)
        Me.txtStatus.Location = CType(resources.GetObject("txtStatus.Location"), System.Drawing.Point)
        Me.txtStatus.MaxLength = CType(resources.GetObject("txtStatus.MaxLength"), Integer)
        Me.txtStatus.Multiline = CType(resources.GetObject("txtStatus.Multiline"), Boolean)
        Me.txtStatus.Name = "txtStatus"
        Me.txtStatus.PasswordChar = CType(resources.GetObject("txtStatus.PasswordChar"), Char)
        Me.txtStatus.ReadOnly = True
        Me.txtStatus.RightToLeft = CType(resources.GetObject("txtStatus.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.txtStatus.ScrollBars = CType(resources.GetObject("txtStatus.ScrollBars"), System.Windows.Forms.ScrollBars)
        Me.txtStatus.Size = CType(resources.GetObject("txtStatus.Size"), System.Drawing.Size)
        Me.txtStatus.TabIndex = CType(resources.GetObject("txtStatus.TabIndex"), Integer)
        Me.txtStatus.Text = resources.GetString("txtStatus.Text")
        Me.txtStatus.TextAlign = CType(resources.GetObject("txtStatus.TextAlign"), System.Windows.Forms.HorizontalAlignment)
        Me.txtStatus.Visible = CType(resources.GetObject("txtStatus.Visible"), Boolean)
        Me.txtStatus.WordWrap = CType(resources.GetObject("txtStatus.WordWrap"), Boolean)
        '
        'tmrReadCommPort
        '
        '
        'clstPorts
        '
        Me.clstPorts.AccessibleDescription = resources.GetString("clstPorts.AccessibleDescription")
        Me.clstPorts.AccessibleName = resources.GetString("clstPorts.AccessibleName")
        Me.clstPorts.Anchor = CType(resources.GetObject("clstPorts.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.clstPorts.BackgroundImage = CType(resources.GetObject("clstPorts.BackgroundImage"), System.Drawing.Image)
        Me.clstPorts.ColumnWidth = CType(resources.GetObject("clstPorts.ColumnWidth"), Integer)
        Me.clstPorts.Dock = CType(resources.GetObject("clstPorts.Dock"), System.Windows.Forms.DockStyle)
        Me.clstPorts.Enabled = CType(resources.GetObject("clstPorts.Enabled"), Boolean)
        Me.clstPorts.Font = CType(resources.GetObject("clstPorts.Font"), System.Drawing.Font)
        Me.clstPorts.HorizontalExtent = CType(resources.GetObject("clstPorts.HorizontalExtent"), Integer)
        Me.clstPorts.HorizontalScrollbar = CType(resources.GetObject("clstPorts.HorizontalScrollbar"), Boolean)
        Me.clstPorts.ImeMode = CType(resources.GetObject("clstPorts.ImeMode"), System.Windows.Forms.ImeMode)
        Me.clstPorts.IntegralHeight = CType(resources.GetObject("clstPorts.IntegralHeight"), Boolean)
        Me.clstPorts.Location = CType(resources.GetObject("clstPorts.Location"), System.Drawing.Point)
        Me.clstPorts.Name = "clstPorts"
        Me.clstPorts.RightToLeft = CType(resources.GetObject("clstPorts.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.clstPorts.ScrollAlwaysVisible = CType(resources.GetObject("clstPorts.ScrollAlwaysVisible"), Boolean)
        Me.clstPorts.Size = CType(resources.GetObject("clstPorts.Size"), System.Drawing.Size)
        Me.clstPorts.TabIndex = CType(resources.GetObject("clstPorts.TabIndex"), Integer)
        Me.clstPorts.TabStop = False
        Me.clstPorts.Visible = CType(resources.GetObject("clstPorts.Visible"), Boolean)
        '
        'btnCheckTCIC
        '
        Me.btnCheckTCIC.AccessibleDescription = resources.GetString("btnCheckTCIC.AccessibleDescription")
        Me.btnCheckTCIC.AccessibleName = resources.GetString("btnCheckTCIC.AccessibleName")
        Me.btnCheckTCIC.Anchor = CType(resources.GetObject("btnCheckTCIC.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnCheckTCIC.BackgroundImage = CType(resources.GetObject("btnCheckTCIC.BackgroundImage"), System.Drawing.Image)
        Me.btnCheckTCIC.Dock = CType(resources.GetObject("btnCheckTCIC.Dock"), System.Windows.Forms.DockStyle)
        Me.btnCheckTCIC.Enabled = CType(resources.GetObject("btnCheckTCIC.Enabled"), Boolean)
        Me.btnCheckTCIC.FlatStyle = CType(resources.GetObject("btnCheckTCIC.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnCheckTCIC.Font = CType(resources.GetObject("btnCheckTCIC.Font"), System.Drawing.Font)
        Me.btnCheckTCIC.Image = CType(resources.GetObject("btnCheckTCIC.Image"), System.Drawing.Image)
        Me.btnCheckTCIC.ImageAlign = CType(resources.GetObject("btnCheckTCIC.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnCheckTCIC.ImageIndex = CType(resources.GetObject("btnCheckTCIC.ImageIndex"), Integer)
        Me.btnCheckTCIC.ImeMode = CType(resources.GetObject("btnCheckTCIC.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnCheckTCIC.Location = CType(resources.GetObject("btnCheckTCIC.Location"), System.Drawing.Point)
        Me.btnCheckTCIC.Name = "btnCheckTCIC"
        Me.btnCheckTCIC.RightToLeft = CType(resources.GetObject("btnCheckTCIC.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnCheckTCIC.Size = CType(resources.GetObject("btnCheckTCIC.Size"), System.Drawing.Size)
        Me.btnCheckTCIC.TabIndex = CType(resources.GetObject("btnCheckTCIC.TabIndex"), Integer)
        Me.btnCheckTCIC.Text = resources.GetString("btnCheckTCIC.Text")
        Me.btnCheckTCIC.TextAlign = CType(resources.GetObject("btnCheckTCIC.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnCheckTCIC.Visible = CType(resources.GetObject("btnCheckTCIC.Visible"), Boolean)
        '
        'txtSelectedTCIC
        '
        Me.txtSelectedTCIC.AccessibleDescription = resources.GetString("txtSelectedTCIC.AccessibleDescription")
        Me.txtSelectedTCIC.AccessibleName = resources.GetString("txtSelectedTCIC.AccessibleName")
        Me.txtSelectedTCIC.Anchor = CType(resources.GetObject("txtSelectedTCIC.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.txtSelectedTCIC.AutoSize = CType(resources.GetObject("txtSelectedTCIC.AutoSize"), Boolean)
        Me.txtSelectedTCIC.BackgroundImage = CType(resources.GetObject("txtSelectedTCIC.BackgroundImage"), System.Drawing.Image)
        Me.txtSelectedTCIC.Dock = CType(resources.GetObject("txtSelectedTCIC.Dock"), System.Windows.Forms.DockStyle)
        Me.txtSelectedTCIC.Enabled = CType(resources.GetObject("txtSelectedTCIC.Enabled"), Boolean)
        Me.txtSelectedTCIC.Font = CType(resources.GetObject("txtSelectedTCIC.Font"), System.Drawing.Font)
        Me.txtSelectedTCIC.ImeMode = CType(resources.GetObject("txtSelectedTCIC.ImeMode"), System.Windows.Forms.ImeMode)
        Me.txtSelectedTCIC.Location = CType(resources.GetObject("txtSelectedTCIC.Location"), System.Drawing.Point)
        Me.txtSelectedTCIC.MaxLength = CType(resources.GetObject("txtSelectedTCIC.MaxLength"), Integer)
        Me.txtSelectedTCIC.Multiline = CType(resources.GetObject("txtSelectedTCIC.Multiline"), Boolean)
        Me.txtSelectedTCIC.Name = "txtSelectedTCIC"
        Me.txtSelectedTCIC.PasswordChar = CType(resources.GetObject("txtSelectedTCIC.PasswordChar"), Char)
        Me.txtSelectedTCIC.RightToLeft = CType(resources.GetObject("txtSelectedTCIC.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.txtSelectedTCIC.ScrollBars = CType(resources.GetObject("txtSelectedTCIC.ScrollBars"), System.Windows.Forms.ScrollBars)
        Me.txtSelectedTCIC.Size = CType(resources.GetObject("txtSelectedTCIC.Size"), System.Drawing.Size)
        Me.txtSelectedTCIC.TabIndex = CType(resources.GetObject("txtSelectedTCIC.TabIndex"), Integer)
        Me.txtSelectedTCIC.Text = resources.GetString("txtSelectedTCIC.Text")
        Me.txtSelectedTCIC.TextAlign = CType(resources.GetObject("txtSelectedTCIC.TextAlign"), System.Windows.Forms.HorizontalAlignment)
        Me.txtSelectedTCIC.Visible = CType(resources.GetObject("txtSelectedTCIC.Visible"), Boolean)
        Me.txtSelectedTCIC.WordWrap = CType(resources.GetObject("txtSelectedTCIC.WordWrap"), Boolean)
        '
        'btnSendUserCommand
        '
        Me.btnSendUserCommand.AccessibleDescription = resources.GetString("btnSendUserCommand.AccessibleDescription")
        Me.btnSendUserCommand.AccessibleName = resources.GetString("btnSendUserCommand.AccessibleName")
        Me.btnSendUserCommand.Anchor = CType(resources.GetObject("btnSendUserCommand.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnSendUserCommand.BackgroundImage = CType(resources.GetObject("btnSendUserCommand.BackgroundImage"), System.Drawing.Image)
        Me.btnSendUserCommand.Dock = CType(resources.GetObject("btnSendUserCommand.Dock"), System.Windows.Forms.DockStyle)
        Me.btnSendUserCommand.Enabled = CType(resources.GetObject("btnSendUserCommand.Enabled"), Boolean)
        Me.btnSendUserCommand.FlatStyle = CType(resources.GetObject("btnSendUserCommand.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnSendUserCommand.Font = CType(resources.GetObject("btnSendUserCommand.Font"), System.Drawing.Font)
        Me.btnSendUserCommand.Image = CType(resources.GetObject("btnSendUserCommand.Image"), System.Drawing.Image)
        Me.btnSendUserCommand.ImageAlign = CType(resources.GetObject("btnSendUserCommand.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnSendUserCommand.ImageIndex = CType(resources.GetObject("btnSendUserCommand.ImageIndex"), Integer)
        Me.btnSendUserCommand.ImeMode = CType(resources.GetObject("btnSendUserCommand.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnSendUserCommand.Location = CType(resources.GetObject("btnSendUserCommand.Location"), System.Drawing.Point)
        Me.btnSendUserCommand.Name = "btnSendUserCommand"
        Me.btnSendUserCommand.RightToLeft = CType(resources.GetObject("btnSendUserCommand.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnSendUserCommand.Size = CType(resources.GetObject("btnSendUserCommand.Size"), System.Drawing.Size)
        Me.btnSendUserCommand.TabIndex = CType(resources.GetObject("btnSendUserCommand.TabIndex"), Integer)
        Me.btnSendUserCommand.Text = resources.GetString("btnSendUserCommand.Text")
        Me.btnSendUserCommand.TextAlign = CType(resources.GetObject("btnSendUserCommand.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnSendUserCommand.Visible = CType(resources.GetObject("btnSendUserCommand.Visible"), Boolean)
        '
        'btnClear
        '
        Me.btnClear.AccessibleDescription = resources.GetString("btnClear.AccessibleDescription")
        Me.btnClear.AccessibleName = resources.GetString("btnClear.AccessibleName")
        Me.btnClear.Anchor = CType(resources.GetObject("btnClear.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnClear.BackgroundImage = CType(resources.GetObject("btnClear.BackgroundImage"), System.Drawing.Image)
        Me.btnClear.Dock = CType(resources.GetObject("btnClear.Dock"), System.Windows.Forms.DockStyle)
        Me.btnClear.Enabled = CType(resources.GetObject("btnClear.Enabled"), Boolean)
        Me.btnClear.FlatStyle = CType(resources.GetObject("btnClear.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnClear.Font = CType(resources.GetObject("btnClear.Font"), System.Drawing.Font)
        Me.btnClear.Image = CType(resources.GetObject("btnClear.Image"), System.Drawing.Image)
        Me.btnClear.ImageAlign = CType(resources.GetObject("btnClear.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnClear.ImageIndex = CType(resources.GetObject("btnClear.ImageIndex"), Integer)
        Me.btnClear.ImeMode = CType(resources.GetObject("btnClear.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnClear.Location = CType(resources.GetObject("btnClear.Location"), System.Drawing.Point)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.RightToLeft = CType(resources.GetObject("btnClear.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnClear.Size = CType(resources.GetObject("btnClear.Size"), System.Drawing.Size)
        Me.btnClear.TabIndex = CType(resources.GetObject("btnClear.TabIndex"), Integer)
        Me.btnClear.Text = resources.GetString("btnClear.Text")
        Me.btnClear.TextAlign = CType(resources.GetObject("btnClear.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnClear.Visible = CType(resources.GetObject("btnClear.Visible"), Boolean)
        '
        'GrUserCommand
        '
        Me.GrUserCommand.AccessibleDescription = resources.GetString("GrUserCommand.AccessibleDescription")
        Me.GrUserCommand.AccessibleName = resources.GetString("GrUserCommand.AccessibleName")
        Me.GrUserCommand.Anchor = CType(resources.GetObject("GrUserCommand.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.GrUserCommand.BackgroundImage = CType(resources.GetObject("GrUserCommand.BackgroundImage"), System.Drawing.Image)
        Me.GrUserCommand.Controls.Add(Me.ChkWithRet)
        Me.GrUserCommand.Controls.Add(Me.txtUserCommand)
        Me.GrUserCommand.Dock = CType(resources.GetObject("GrUserCommand.Dock"), System.Windows.Forms.DockStyle)
        Me.GrUserCommand.Enabled = CType(resources.GetObject("GrUserCommand.Enabled"), Boolean)
        Me.GrUserCommand.Font = CType(resources.GetObject("GrUserCommand.Font"), System.Drawing.Font)
        Me.GrUserCommand.ImeMode = CType(resources.GetObject("GrUserCommand.ImeMode"), System.Windows.Forms.ImeMode)
        Me.GrUserCommand.Location = CType(resources.GetObject("GrUserCommand.Location"), System.Drawing.Point)
        Me.GrUserCommand.Name = "GrUserCommand"
        Me.GrUserCommand.RightToLeft = CType(resources.GetObject("GrUserCommand.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.GrUserCommand.Size = CType(resources.GetObject("GrUserCommand.Size"), System.Drawing.Size)
        Me.GrUserCommand.TabIndex = CType(resources.GetObject("GrUserCommand.TabIndex"), Integer)
        Me.GrUserCommand.TabStop = False
        Me.GrUserCommand.Text = resources.GetString("GrUserCommand.Text")
        Me.GrUserCommand.Visible = CType(resources.GetObject("GrUserCommand.Visible"), Boolean)
        '
        'ChkWithRet
        '
        Me.ChkWithRet.AccessibleDescription = resources.GetString("ChkWithRet.AccessibleDescription")
        Me.ChkWithRet.AccessibleName = resources.GetString("ChkWithRet.AccessibleName")
        Me.ChkWithRet.Anchor = CType(resources.GetObject("ChkWithRet.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.ChkWithRet.Appearance = CType(resources.GetObject("ChkWithRet.Appearance"), System.Windows.Forms.Appearance)
        Me.ChkWithRet.BackgroundImage = CType(resources.GetObject("ChkWithRet.BackgroundImage"), System.Drawing.Image)
        Me.ChkWithRet.CheckAlign = CType(resources.GetObject("ChkWithRet.CheckAlign"), System.Drawing.ContentAlignment)
        Me.ChkWithRet.Dock = CType(resources.GetObject("ChkWithRet.Dock"), System.Windows.Forms.DockStyle)
        Me.ChkWithRet.Enabled = CType(resources.GetObject("ChkWithRet.Enabled"), Boolean)
        Me.ChkWithRet.FlatStyle = CType(resources.GetObject("ChkWithRet.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.ChkWithRet.Font = CType(resources.GetObject("ChkWithRet.Font"), System.Drawing.Font)
        Me.ChkWithRet.Image = CType(resources.GetObject("ChkWithRet.Image"), System.Drawing.Image)
        Me.ChkWithRet.ImageAlign = CType(resources.GetObject("ChkWithRet.ImageAlign"), System.Drawing.ContentAlignment)
        Me.ChkWithRet.ImageIndex = CType(resources.GetObject("ChkWithRet.ImageIndex"), Integer)
        Me.ChkWithRet.ImeMode = CType(resources.GetObject("ChkWithRet.ImeMode"), System.Windows.Forms.ImeMode)
        Me.ChkWithRet.Location = CType(resources.GetObject("ChkWithRet.Location"), System.Drawing.Point)
        Me.ChkWithRet.Name = "ChkWithRet"
        Me.ChkWithRet.RightToLeft = CType(resources.GetObject("ChkWithRet.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.ChkWithRet.Size = CType(resources.GetObject("ChkWithRet.Size"), System.Drawing.Size)
        Me.ChkWithRet.TabIndex = CType(resources.GetObject("ChkWithRet.TabIndex"), Integer)
        Me.ChkWithRet.Text = resources.GetString("ChkWithRet.Text")
        Me.ChkWithRet.TextAlign = CType(resources.GetObject("ChkWithRet.TextAlign"), System.Drawing.ContentAlignment)
        Me.ChkWithRet.Visible = CType(resources.GetObject("ChkWithRet.Visible"), Boolean)
        '
        'txtUserCommand
        '
        Me.txtUserCommand.AccessibleDescription = resources.GetString("txtUserCommand.AccessibleDescription")
        Me.txtUserCommand.AccessibleName = resources.GetString("txtUserCommand.AccessibleName")
        Me.txtUserCommand.Anchor = CType(resources.GetObject("txtUserCommand.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.txtUserCommand.AutoSize = CType(resources.GetObject("txtUserCommand.AutoSize"), Boolean)
        Me.txtUserCommand.BackgroundImage = CType(resources.GetObject("txtUserCommand.BackgroundImage"), System.Drawing.Image)
        Me.txtUserCommand.Dock = CType(resources.GetObject("txtUserCommand.Dock"), System.Windows.Forms.DockStyle)
        Me.txtUserCommand.Enabled = CType(resources.GetObject("txtUserCommand.Enabled"), Boolean)
        Me.txtUserCommand.Font = CType(resources.GetObject("txtUserCommand.Font"), System.Drawing.Font)
        Me.txtUserCommand.ImeMode = CType(resources.GetObject("txtUserCommand.ImeMode"), System.Windows.Forms.ImeMode)
        Me.txtUserCommand.Location = CType(resources.GetObject("txtUserCommand.Location"), System.Drawing.Point)
        Me.txtUserCommand.MaxLength = CType(resources.GetObject("txtUserCommand.MaxLength"), Integer)
        Me.txtUserCommand.Multiline = CType(resources.GetObject("txtUserCommand.Multiline"), Boolean)
        Me.txtUserCommand.Name = "txtUserCommand"
        Me.txtUserCommand.PasswordChar = CType(resources.GetObject("txtUserCommand.PasswordChar"), Char)
        Me.txtUserCommand.RightToLeft = CType(resources.GetObject("txtUserCommand.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.txtUserCommand.ScrollBars = CType(resources.GetObject("txtUserCommand.ScrollBars"), System.Windows.Forms.ScrollBars)
        Me.txtUserCommand.Size = CType(resources.GetObject("txtUserCommand.Size"), System.Drawing.Size)
        Me.txtUserCommand.TabIndex = CType(resources.GetObject("txtUserCommand.TabIndex"), Integer)
        Me.txtUserCommand.Text = resources.GetString("txtUserCommand.Text")
        Me.txtUserCommand.TextAlign = CType(resources.GetObject("txtUserCommand.TextAlign"), System.Windows.Forms.HorizontalAlignment)
        Me.txtUserCommand.Visible = CType(resources.GetObject("txtUserCommand.Visible"), Boolean)
        Me.txtUserCommand.WordWrap = CType(resources.GetObject("txtUserCommand.WordWrap"), Boolean)
        '
        'frmMain
        '
        Me.AccessibleDescription = resources.GetString("$this.AccessibleDescription")
        Me.AccessibleName = resources.GetString("$this.AccessibleName")
        Me.AutoScaleBaseSize = CType(resources.GetObject("$this.AutoScaleBaseSize"), System.Drawing.Size)
        Me.AutoScroll = CType(resources.GetObject("$this.AutoScroll"), Boolean)
        Me.AutoScrollMargin = CType(resources.GetObject("$this.AutoScrollMargin"), System.Drawing.Size)
        Me.AutoScrollMinSize = CType(resources.GetObject("$this.AutoScrollMinSize"), System.Drawing.Size)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = CType(resources.GetObject("$this.ClientSize"), System.Drawing.Size)
        Me.Controls.Add(Me.GrUserCommand)
        Me.Controls.Add(Me.btnClear)
        Me.Controls.Add(Me.btnSendUserCommand)
        Me.Controls.Add(Me.txtSelectedTCIC)
        Me.Controls.Add(Me.txtStatus)
        Me.Controls.Add(Me.btnCheckTCIC)
        Me.Controls.Add(Me.clstPorts)
        Me.Controls.Add(Me.btnCheckForPorts)
        Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)
        Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.ImeMode = CType(resources.GetObject("$this.ImeMode"), System.Windows.Forms.ImeMode)
        Me.Location = CType(resources.GetObject("$this.Location"), System.Drawing.Point)
        Me.MaximizeBox = False
        Me.MaximumSize = CType(resources.GetObject("$this.MaximumSize"), System.Drawing.Size)
        Me.Menu = Me.mnuMain
        Me.MinimumSize = CType(resources.GetObject("$this.MinimumSize"), System.Drawing.Size)
        Me.Name = "frmMain"
        Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.StartPosition = CType(resources.GetObject("$this.StartPosition"), System.Windows.Forms.FormStartPosition)
        Me.Text = resources.GetString("$this.Text")
        Me.GrUserCommand.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region " Standard Menu Code "
    ' <System.Diagnostics.DebuggerStepThrough()> has been added to some procedures since they are
    ' not the focus of the demo. Remove them if you wish to debug the procedures.
    ' This code simply shows the About form.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        ' Open the About form in Dialog Mode
        Dim frm As New frmAbout
        frm.ShowDialog(Me)
        frm.Dispose()
    End Sub

    ' This code will close the form.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        ' Close the current form
        Me.Close()
    End Sub
#End Region

    
    Private Sub btnCheckForPorts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckForPorts.Click
        Call CheckForPorts()
    End Sub
    Private Sub CheckForPorts()
        ' This subroutine checks for available ports on the local machine. It does 
        '   this by attempting to open ports 1 through 255.
        ' Check for Availability of each of the 256 Comm Ports, and
        '   place a check in the list box items that have openable ports.
        Dim i As Integer


        btnCheckForPorts.Enabled = False

        Dim iNumberOfPorts As Integer
        If m_CommPort.sPortType = COM_Port_Driver Then
            iNumberOfPorts = cMaxNumberOfPorts
            PortType = "COM"
        Else
            iNumberOfPorts = NumDevs
            PortType = "USB"
        End If

        For i = 1 To iNumberOfPorts
            clstPorts.Items.Add(PortType & i, False)
        Next


        For i = 1 To iNumberOfPorts
            WriteMessage("Testing " & PortType + i.ToString())
            If IsPortAvailable(i) Then
                ' Check the box for available ports.
                Me.clstPorts.SetItemChecked(i - 1, True)
                RealMaxNumberOfPorts = i
            Else
                ' Uncheck the box for unavailable ports.
                Me.clstPorts.SetItemChecked(i - 1, False)
            End If
        Next
        ' Enable the Find TCIC button.
        Me.btnCheckTCIC.Enabled = True

        Call SendCursorDown()
    End Sub

    ' This subroutine attempts to send an AT command to any active Comm Ports.
    '   If a response is returned then a usable TCIC has been detected
    '   on that port.
    Private Sub btnCheckTCIC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckTCIC.Click
        Dim i As Integer

        btnCheckTCIC.Enabled = False
        'For i = 0 To clstPorts.Items.Count - 1
        For i = 0 To RealMaxNumberOfPorts - 1
            If Me.clstPorts.GetItemChecked(i) Then
                ' Item is checked so it MIGHT be a valid port.
                ' Test for validity.
                If IsPortAvailable(i + 1) Then
                    ' Check if port responds to an AT command.
                    If Is_TCIC_Port(i + 1) Then
                        ' Set the class variables to the last TCIC found.
                        Me.m_IsTCICFound = True
                        Me.m_TCICPort = i + 1
                        ' Write message to the user.
                        WriteMessage("Port " + (i + 1).ToString() + _
                            " is a responsive TCIC.")
                    Else
                        ' Write message to the user.
                        WriteMessage("Port " + (i + 1).ToString() + _
                            " is not a responsive TCIC.")
                    End If
                End If
            End If
        Next
        ' If a TCIC was found, enable the rest of the buttons, so the user
        '   can interact with the TCIC.
        If Me.m_IsTCICFound Then
            Me.txtSelectedTCIC.Text = "Using TCIC on " & porttype & _
                Me.m_TCICPort.ToString()

            Me.txtUserCommand.Enabled = True
            Me.ChkWithRet.Enabled = True
        End If
        Call SendCursorDown()
    End Sub


    ' This subroutine clears the TextBox.
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtStatus.Clear()
    End Sub


    ' This subroutine sends an AT command to the TCIC, and records its response.
    '   
    Private Sub btnSendATCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        ' Always wrap up working with Comm Ports in exception handlers.
        Try
            ' Enable the timer.
            Me.tmrReadCommPort.Enabled = True
            ' Attempt to open the port.
            m_CommPort.Open(m_TCICPort, 115200, 8, COMport_DirectUSB.DataParity.Parity_None, _
                COMport_DirectUSB.DataStopBit.StopBit_1, 4096)

            ' Write an AT Command to the Port.
            m_CommPort.Write(Encoding.ASCII.GetBytes("AT" & Chr(13))) 'zel
            'm_CommPort.Write(Encoding.ASCII.GetBytes(txtUserCommand.Text & Chr(13))) 'zel
            ' Sleep long enough for the TCIC to respond and the timer to fire.
            System.Threading.Thread.Sleep(200)
            Application.DoEvents()
            m_CommPort.Close()

        Catch ex As Exception
            ' Warn the user.
            MessageBox.Show("Unable to communicate with Card")
        Finally
            ' Disable the timer.
            Me.tmrReadCommPort.Enabled = False
        End Try

    End Sub


    ' This subroutine sends a user specified command to the TCIC, and records its 
    '   response. It depends on the timer to do the reading of the response.
    Private Sub btnSendUserCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendUserCommand.Click
        Call SendUserCommand()
    End Sub
    Sub SendUserCommand()
        ' Always wrap up working with Comm Ports in exception handlers.
        Try
            ' Enable the timer.
            btnSendUserCommand.Enabled = False
            Me.tmrReadCommPort.Enabled = True
            ' Attempt to open the port.

            m_CommPort.Open(m_TCICPort, 115200, 8, COMport_DirectUSB.DataParity.Parity_None, COMport_DirectUSB.DataStopBit.StopBit_1, 4096)
            'Me.Text = (TakeTime() - StartTime).ToString
            'GoTo m_Cont
            ' Write an user specified Command to the Port.
            Dim StringForSend As String
            If ChkWithRet.Checked = True Then
                StringForSend = Me.txtUserCommand.Text & Chr(13)
            Else
                StringForSend = Me.txtUserCommand.Text
            End If

            Dim ch As String
            Dim i As Integer
            'StartTime = TakeTime()
            For i = 0 To StringForSend.Length - 1

                ch = StringForSend.Substring(i, 1)
                Try
                    CommunicationStatus = False
                    m_CommPort.Write(Encoding.ASCII.GetBytes(ch))
                    System.Threading.Thread.Sleep(1)

                Catch ex As Exception
                    sCurrentStopMessage = "Stop 2"
                    MessageBox.Show(sCurrentStopMessage, sCritical_Error, MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Try

            Next

            ' Sleep long enough for the TCIC to respond and the timer to fire.
            System.Threading.Thread.Sleep(70)
            Application.DoEvents()
m_Cont:
            m_CommPort.Close()
            btnSendUserCommand.Enabled = True
        Catch ex As Exception
            ' Warn the user.
            MessageBox.Show("Unable to communicate with TCIC")
        Finally
            ' Disable the timer.
            Me.tmrReadCommPort.Enabled = False
        End Try


    End Sub

    ' This subroutine is fired when the timer event is raised. It writes whatever
    '   is in the Comm Port buffer to the output window.
    Private Sub tmrReadCommPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadCommPort.Tick

        Try
            ' As long as there is information, read one byte at a time and 
            '   output it.

            While (m_CommPort.Read(1) <> -1)
                ' Write the output to the screen.
                WriteMessage(Chr(m_CommPort.InputStream(0)), False)

            End While

        Catch exc As Exception
            ' An exception is raised when there is no information to read.
            '   Don't do anything here, just let the exception go.
            If CommunicationStatus = False Then
                CommunicationStatus = True
                MessageBox.Show("Unable to communicate with TCIC")
                tmrReadCommPort.Enabled = False
                btnCheckForPorts.Enabled = True
                m_CommPort.ClearInputBuffer()
                m_CommPort.Close()
            Else
                End_Reading_Flag = True
                'Stop
            End If

        End Try

    End Sub

    ' This function checks to see if the port is a TCIC, by sending 
    '   an AT command to the port. If the port responds, it is assumed to 
    '   be a TCIC. The function returns a Boolean.
    Private Function Is_TCIC_Port(ByVal port As Integer) As Boolean

        ' Always wrap up working with Comm Ports in exception handlers.
        Try
            ' Attempt to open the port.
            If m_CommPort.sPortType = COM_Port_Driver Then
                m_CommPort.Open(port, 115200, 8, COMport_DirectUSB.DataParity.Parity_None, _
                    COMport_DirectUSB.DataStopBit.StopBit_1, 4096)

            Else
                m_CommPort.SerialNumber = Serial_Number(port - 1)
                m_CommPort.Open(port, cBaudRate, 8, COMport_DirectUSB.DataParity.Parity_None, _
                  COMport_DirectUSB.DataStopBit.StopBit_1, 4096)


            End If

            m_CommPort.Write(Encoding.ASCII.GetBytes(":a"))
            System.Threading.Thread.Sleep(200)
            m_CommPort.Write(Encoding.ASCII.GetBytes("V"))
            Dim CardAnswer As String

            ' Sleep long enough for the TCIC to respond.
            System.Threading.Thread.Sleep(200)
            Application.DoEvents()
            ' Try to get info from the Comm Port.
            Try

                ' Try to read a single byte. If you get it, then assume
                '   that the port contains a TCIC. Clear the buffer before 
                '   leaving.


                While (m_CommPort.Read(1) <> -1)

                    CardAnswer &= Chr(m_CommPort.InputStream(0))
                    Application.DoEvents()
                    If CardAnswer = "TCIC" Then
                        Exit While
                    End If

                End While

                m_CommPort.ClearInputBuffer()
                m_CommPort.Close()
                Return True
            Catch exc As Exception
                ' Nothing to read from the Comm Port, so set to False
                m_CommPort.Close()
                Return False
            End Try
        Catch exc As Exception
            ' Port could not be opened or written to.
            Me.clstPorts.SetItemChecked(port - 1, False)
            MsgBox("Could not open port.", MsgBoxStyle.OKOnly, Me.Text)
            Return False
        End Try


    End Function

    ' This function attempts to open the passed Comm Port. If it is
    '   available, it returns True, else it returns False. To determine
    '   availability a Try-Catch block is used.
    Private Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
        Try
            If m_CommPort.sPortType = COM_Port_Driver Then
                m_CommPort.Open(ComPort, cBaudRate, 8, COMport_DirectUSB.DataParity.Parity_None, _
                    COMport_DirectUSB.DataStopBit.StopBit_1, 4096)
                ' If it makes it to here, then the Comm Port is available.

            Else
                m_CommPort.SerialNumber = Serial_Number(ComPort - 1) 'USB
                m_CommPort.Open(ComPort, cBaudRate, 8, COMport_DirectUSB.DataParity.Parity_None, _
                   COMport_DirectUSB.DataStopBit.StopBit_1, 4096)
                ' If it makes it to here, then the Comm Port is available.
            End If



            m_CommPort.Close()
            Return True
        Catch
            ' If it gets here, then the attempt to open the Comm Port
            '   was unsuccessful.
            Return False
        End Try
    End Function

    ' This subroutine writes a message to the txtStatus TextBox.
    Private Sub WriteMessage(ByVal message As String)
        Me.txtStatus.Text += message + vbCrLf

    End Sub
    ' This subroutine writes a message to the txtStatus TextBox and allows
    '   the line feed to be suppressed.
    Private Sub WriteMessage(ByVal message As String, ByVal linefeed As Boolean)
        Me.txtStatus.Text += message
        If linefeed Then
            Me.txtStatus.Text += vbCrLf
        End If
        CommunicationStatus = True
        Call SendCursorDown()
        If End_Reading_Flag Then

            End_Reading_Flag = False

        End If

    End Sub

    Private Sub txtUserCommand_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUserCommand.KeyPress
        If e.KeyChar = Chr(13) Then
            Call SendUserCommand()
        End If

    End Sub

    Private Sub txtUserCommand_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUserCommand.TextChanged
        If txtUserCommand.Text <> "" Then
            Me.btnSendUserCommand.Enabled = True
        Else
            Me.btnSendUserCommand.Enabled = False
        End If
    End Sub
    Private Function DeSelectCardAddress() As Boolean

        Dim CurrentAddress, CardAnswer As String

        Call ReadWrite(":a", CardAnswer, 100)

        Call ReadWrite("z", CardAnswer, 100)
        If CardAnswer.Length >= 1 Then
            CurrentAddress = CardAnswer.Substring(0, 1)
        Else
            Return False
        End If

        If CurrentAddress = "a" Then
            Return True
        Else
            If CurrentAddress = "b" Then
                Call ReadWrite(":c", CardAnswer, 100)
                Return True
            Else
                Call ReadWrite(":b", CardAnswer, 100)

                Return True
            End If
        End If
    End Function
    Public Sub ReadWrite(ByVal sentStr As String, ByRef CardAnswer As String, _
                ByVal Delay As Integer)



        ' Always wrap up working with Comm Ports in exception handlers.
        m_CommPort.Close()

        Try
            m_CommPort.Open(m_TCICPort, cBaudRate, 8, COMport_DirectUSB.DataParity.Parity_None, COMport_DirectUSB.DataStopBit.StopBit_1, 4096)

            ' Write an user specified Command to the Port.
            CardAnswer = ""
            m_CommPort.Write(Encoding.ASCII.GetBytes(sentStr))
            Try

                ' As long as there is information, read one byte at a time and 
                '   output it.
                System.Threading.Thread.Sleep(Delay)

                While (m_CommPort.Read(1) <> -1)

                    CardAnswer &= Chr(m_CommPort.InputStream(0))
                    Application.DoEvents()

                End While

            Catch exc As Exception
                ' An exception is raised when there is no information to read.
                '   Don't do anything here, just let the exception go.
                Dim sErr As String = exc.ToString

            End Try
            '
            ' Sleep long enough for the TCIC to respond and the timer to fire.
            System.Threading.Thread.Sleep(50)
            Application.DoEvents()
            m_CommPort.Close()

        Catch ex As Exception
            ' Warn the user.
            MessageBox.Show("Unable to communicate with Card")
            m_CommPort.Close()
            End

        Finally


        End Try

    End Sub

    Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        If m_TCICPort > 0 Then
            If DeSelectCardAddress() = False Then
                MessageBox.Show("TCIC not deselected", "Attention!", MessageBoxButtons.OK)
            End If
        End If
    End Sub
    Private Sub SendCursorDown()
        Me.txtStatus.SelectAll()
        Me.txtStatus.ScrollToCaret()
        Me.txtStatus.Select(1, 1)

    End Sub
    Private Function GetTCIC_Driver_Type() As String
        Dim SysDir As String

        Try
            Dim i As Integer
            For i = 1 To 255
                SysDir &= "0"
            Next
            Dim nSize As Int32

            GetWindowsDirectory(SysDir, nSize)
            i = SysDir.IndexOf(Chr(0))
            Dim DRV_File_name As String
            SysDir = SysDir.Substring(0, i)
            Dim ln As Integer = SysDir.Length
            DRV_File_name = SysDir & "\System32\Drivers\FTSER2K.SYS"
            Dim DLL_File_name As String = SysDir & "\System32\Drivers\FTD2XX.SYS"

            If File.Exists(DLL_File_name) Then
                Detected_Driver = Detected_Direct_Driver
                Me.Text &= Detected_Driver
                Return Direct_Driver
            Else
                If File.Exists(DRV_File_name) Then

                    Detected_Driver = Detected_COM_Port_Driver
                    Me.Text &= Detected_Driver
                    Return COM_Port_Driver
                Else
                    Return Driver_NONE
                End If
            End If
        Catch ex As Exception
            sCurrentStopMessage = "Stop 1"
            MessageBox.Show(sCurrentStopMessage, sCritical_Error, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Function
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Driver_TYPE = GetTCIC_Driver_Type()
        Select Case Driver_TYPE
            Case Direct_Driver
                Call GetUSB_Serial_Numbers()

            Case COM_Port_Driver

            Case Else
                MessageBox.Show("TCIC driver is not installed.", "Sorry...", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End
        End Select
        m_CommPort.sPortType = Driver_TYPE

    End Sub
    Private Sub GetUSB_Serial_Numbers()
        Dim ReturnValue As Integer
        Dim Index As Integer
        Dim Flags As Integer
        Dim TheType As Integer
        Dim ID As Integer
        Dim LocId As Integer
        Dim SerialNumber As String
        Dim Description As String
        Dim Handle As Integer
        'Dim cd As Byte = Convert.ToByte(0)
        For Index = 1 To 256
            SerialNumber &= "_" 'cd
        Next
        Description = SerialNumber

        ReturnValue = m_CommPort.FT_CreateDeviceInfoList(NumDevs)

        For Index = 0 To NumDevs - 1
            ReturnValue = m_CommPort.FT_GetDeviceInfoDetail(Index, Flags, TheType, ID, LocId, SerialNumber, Description, Handle)
            Serial_Number(Index) = SerialNumber
        Next

    End Sub
End Class