VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Demo : Reading 7080 Counter Value"
   ClientHeight    =   3600
   ClientLeft      =   1380
   ClientTop       =   1875
   ClientWidth     =   7170
   BeginProperty Font 
      Name            =   "MS Sans Serif"
      Size            =   9.75
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkMode        =   1  'Source
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   11070
   ScaleWidth      =   15240
   Begin VB.Frame Frame3 
      Caption         =   "Digital Output: "
      ForeColor       =   &H00FF0000&
      Height          =   852
      Left            =   3600
      TabIndex        =   24
      Top             =   1800
      Width           =   3372
      Begin VB.Image imgDigitalOut 
         Height          =   372
         Index           =   1
         Left            =   2160
         Top             =   360
         Width           =   492
      End
      Begin VB.Image imgDigitalOut 
         Height          =   372
         Index           =   0
         Left            =   600
         Top             =   360
         Width           =   492
      End
      Begin VB.Label Label10 
         Caption         =   "CH:1"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   1680
         TabIndex        =   26
         Top             =   360
         Width           =   492
      End
      Begin VB.Label Label6 
         Caption         =   "CH:0"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   25
         Top             =   360
         Width           =   492
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "Counter Gate Mode: "
      ForeColor       =   &H00FF0000&
      Height          =   612
      Left            =   3600
      TabIndex        =   20
      Top             =   1080
      Width           =   3372
      Begin VB.OptionButton Option1 
         Caption         =   "None"
         Height          =   252
         Index           =   2
         Left            =   2040
         TabIndex        =   23
         Top             =   240
         Value           =   -1  'True
         Width           =   852
      End
      Begin VB.OptionButton Option1 
         Caption         =   "High"
         Height          =   252
         Index           =   1
         Left            =   1080
         TabIndex        =   22
         Top             =   240
         Width           =   732
      End
      Begin VB.OptionButton Option1 
         Caption         =   "Low"
         Height          =   252
         Index           =   0
         Left            =   120
         TabIndex        =   21
         Top             =   240
         Width           =   732
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   " Please Open Firstly "
      ForeColor       =   &H00FF0000&
      Height          =   3252
      Left            =   120
      TabIndex        =   2
      Top             =   0
      Width           =   3252
      Begin VB.ComboBox cmbFreqGateTime 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         ItemData        =   "Form1.frx":0000
         Left            =   1680
         List            =   "Form1.frx":000A
         TabIndex        =   19
         Text            =   "0.1 sec."
         Top             =   2160
         Width           =   1452
      End
      Begin VB.CommandButton cmdOpen 
         BackColor       =   &H000000FF&
         Caption         =   "Open Firstly"
         Default         =   -1  'True
         Height          =   492
         Left            =   960
         TabIndex        =   17
         Top             =   2640
         Width           =   1452
      End
      Begin VB.TextBox txtAddress 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         Left            =   1680
         TabIndex        =   12
         Text            =   "1"
         Top             =   720
         Width           =   1452
      End
      Begin VB.ComboBox cmbChecksum 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         ItemData        =   "Form1.frx":0022
         Left            =   1680
         List            =   "Form1.frx":002C
         TabIndex        =   10
         Text            =   "Disable"
         Top             =   1440
         Width           =   1452
      End
      Begin VB.ComboBox cmbModeSelect 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         ItemData        =   "Form1.frx":0041
         Left            =   1680
         List            =   "Form1.frx":004B
         TabIndex        =   8
         Text            =   "Counter Mode"
         Top             =   1800
         Width           =   1452
      End
      Begin VB.ComboBox cmbCOMPort 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         ItemData        =   "Form1.frx":0069
         Left            =   1680
         List            =   "Form1.frx":0079
         TabIndex        =   4
         Text            =   "COM1"
         Top             =   360
         Width           =   1452
      End
      Begin VB.ComboBox cmbBaudrate 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   288
         ItemData        =   "Form1.frx":0095
         Left            =   1680
         List            =   "Form1.frx":00B1
         TabIndex        =   3
         Text            =   "9600"
         Top             =   1080
         Width           =   1452
      End
      Begin VB.Label Label7 
         Caption         =   "Freq. Gate Time:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   18
         Top             =   2160
         Width           =   1452
      End
      Begin VB.Label Label9 
         Caption         =   "Address[dec]:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   11
         Top             =   720
         Width           =   1332
      End
      Begin VB.Label Label8 
         Caption         =   "Checksum:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   9
         Top             =   1440
         Width           =   972
      End
      Begin VB.Label Label5 
         Caption         =   "Mode Select:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   7
         Top             =   1800
         Width           =   1212
      End
      Begin VB.Label Label3 
         Caption         =   "COM Port:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   6
         Top             =   360
         Width           =   972
      End
      Begin VB.Label Label4 
         Caption         =   "Baud Rate:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   252
         Left            =   120
         TabIndex        =   5
         Top             =   1080
         Width           =   972
      End
   End
   Begin VB.CommandButton cmdReading 
      Caption         =   "&Reading"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   612
      Left            =   3600
      TabIndex        =   1
      Top             =   2880
      Width           =   1572
   End
   Begin VB.CommandButton exitCmd 
      Caption         =   "E&xit"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   13.5
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   612
      Left            =   5400
      TabIndex        =   0
      Top             =   2880
      Width           =   1572
   End
   Begin VB.Label lblCounter 
      BackColor       =   &H0000FFFF&
      BorderStyle     =   1  'Fixed Single
      Height          =   372
      Index           =   1
      Left            =   4800
      TabIndex        =   16
      Top             =   600
      Width           =   1932
   End
   Begin VB.Label lblCounter 
      BackColor       =   &H0000FFFF&
      BorderStyle     =   1  'Fixed Single
      Height          =   372
      Index           =   0
      Left            =   4800
      TabIndex        =   15
      Top             =   120
      Width           =   1932
   End
   Begin VB.Label Label2 
      Caption         =   "Counter 1:"
      Height          =   252
      Left            =   3600
      TabIndex        =   14
      Top             =   600
      Width           =   972
   End
   Begin VB.Label Label1 
      Caption         =   "Counter 0:"
      Height          =   252
      Left            =   3600
      TabIndex        =   13
      Top             =   120
      Width           =   1092
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim mPort        As Integer
Dim mOldAddress, mNewAddress  As Integer
Dim mOldBaudrate, mNewBaudrate As Long
Dim mOldCheckSum, mNewCHecksum As Integer
Dim mRun7080 As Integer
Dim mDigitalOut(0 To 1) As Integer

Private Sub cmbChecksum_Change()
    mOldCheckSum = cmbChecksum.ListIndex
    Debug.Print "cmbChecksum_Change()"
End Sub

Private Sub cmbChecksum_Click()
    mOldCheckSum = cmbChecksum.ListIndex
    Debug.Print "cmbChecksum_Click()"
End Sub

'Private Sub cmbModeSelect_Change()
'    If cmbmodeselect.ListIndex = 1 Then
'        cmbfreqgatetime.enable = True
'        Debug.Print "Change Freq enable"
'    Else
'        cmbfreqgatetime.enable = False
'        Debug.Print "Change Freq Disable"
'    End If
'End Sub

Private Sub cmbModeSelect_Click()
    If cmbModeSelect.ListIndex = 1 Then
        cmbFreqGateTime.Enabled = True
        Debug.Print "Freq enable"
    Else
        cmbFreqGateTime.Enabled = False
        Debug.Print "Freq Disable"
    End If

End Sub

Private Sub cmdOpen_Click()
    Dim AA$, NN$, TT$, CC$, FF$, msg$
    Dim NewAddress%, i%
    Dim DataFormat%, ConfigChange%
    Dim RetVal%, wT%, FreqGateTime%
    Dim frm7080Cmd    As String
    Dim frm7080RecBuf As String
    
    '-------- open the COM Port and store some configuration ----
    If COMOpen = 0 Then
        Debug.Print "COM Open ..."
         OpenCom
    End If
    
    
    '************************************************************
    '----- Now are setting ------
    frm7080Cmd = Space(80)
    frm7080RecBuf = Space(80)
    
    If Val(txtAddress.Text) > 255 Then
        MsgBox "The Module Address > 255, Enter again !", vbCritical, "7017"
    End If
    
    If mOldAddress < 16 Then
        AA$ = "0" + Hex(mOldAddress)
    Else
        AA$ = Hex(mOldAddress)
    End If
    
    NN$ = AA$
    
    Select Case cmbModeSelect.ListIndex
        Case 0
            TT = "50"
        Case 1
            TT = "51"
    End Select
    
    Select Case cmbBaudrate.ListIndex
      Case 0  ' 115200
            CC$ = "0A"
      Case 1  ' 57600
            CC$ = "09"
      Case 2  ' 38400
            CC$ = "08"
      Case 3  ' 19200
            CC$ = "07"
      Case 4  ' 9600
            CC$ = "06"
      Case 5  ' 4800
            CC$ = "05"
      Case 6  ' 2400
            CC$ = "04"
      Case 7  ' 1200
            CC$ = "03"
    End Select
    
    
    If cmbModeSelect.ListIndex = 0 Then  ' Counter Mode
        FreqGateTime = 0
    Else     ' Freq Mode
        FreqGateTime = cmbFreqGateTime.ListIndex
    End If
    DataFormat = 64 * mOldCheckSum + FreqGateTime * 4
    
    If DataFormat < 15 Then
        FF$ = "0" + Hex(DataFormat)
    Else
        FF$ = Hex(DataFormat)
    End If
    
    Debug.Print "AA="; AA$
    Debug.Print "NN="; NN$
    Debug.Print "TT="; TT$
    Debug.Print "CC="; CC$
    Debug.Print "FF="; FF$
    frm7080Cmd = "%" + AA$ + NN$ + TT$ + CC$ + FF$
    Debug.Print "mPort=" + Str(mPort)
    Debug.Print "mOldCheckSum=" + Str(mOldCheckSum)
    Debug.Print "frm7080Cmd="; frm7080Cmd
    
            ConfigChange = 0
            wT = 100
            RetVal = Send_Receive_Cmd(mPort, frm7080Cmd, frm7080RecBuf, 100, mOldCheckSum, wT)
            Sleep (100)
            Debug.Print "frm7080RecBuf->"; frm7080RecBuf
            If RetVal = 0 Then
               If Mid(frm7080RecBuf, 1, 1) = "!" Then
                   If ConfigChange Then
                       msg$ = "Setting Configguration OK !!!" + vbCrLf + _
                            "Please Turn off the power of I-7000 module," + vbCrLf + _
                            "then Turn On again to take effect the setting !!!"
                   Else
                      msg$ = "Setting Configuration OK !!!" + vbCrLf
                   End If
                   Beep
                   '--- Turn off the Digital output firstly ---
                   w7000(0) = mPort ' COM Port
                   w7000(1) = mOldAddress ' Module Address
                   w7000(2) = &H7080
                   w7000(3) = mOldCheckSum ' Checksum
                   w7000(4) = 100 ' TimeOut = 0.1 second
                   w7000(5) = mDigitalOut(1) * 2 + mDigitalOut(0)
                   w7000(6) = 0   ' string debug
                   RetVal = DigitalOut(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
                   If RetVal > 0 Then
                      MsgBox "DigitalOut_7080() Error !", vbCritical, "Error Code:" + Str(RetVal)
                   End If
                 
                   MsgBox msg$, vbInformation, "7080"
               End If
            Else
              MsgBox "Error code:" & Str(RetVal), vbCritical, "Send_receive_Cmd()"
            End If
    
    
End Sub

'Private Sub Command1_Click()
'
'    If COMOpen = 0 Then
'        MsgBox "Please Open COM firstly !!!", vbCritical
'    End If
'
'    w7000(0) = mPort        ' COM Port
'    w7000(1) = mOldAddress ' Module Address
'    w7000(2) = &H7080   ' module ID
'    w7000(4) = 100     ' TimeOut = 0.1 second
'
'    w7000(5) = 0       ' channel no
'    w7000(6) = 1       ' string debug
'
'    f7000(0) = Val(analogoutText.Text)
'    Ret = AnalogOut_7080(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
'
'    If Ret <> 0 Then
'           Beep
'           A$ = "The Error Code:" + Str$(Ret)
'           MsgBox A$, 0, "AnalogOut_7080() error !!!"
'           Close_Com (Port)
'           End
'    End If
'
'    Counter = Counter + 1
'    A$ = "Counter=" + Str$(Counter)
'    A$ = A$ + "  The Analog Output value:" + Format$(f7000(0), "0.###")
'    Label2.Caption = A$
'    Beep
'End Sub

Private Sub exitCmd_Click()
  mRun7080 = 0
  Sleep (100)
  '------- CLOSE_COM -------------------------
  Close_Com (mPort)
    
  Unload Me
End Sub

Private Sub Form_Load()
    cmbCOMPort.ListIndex = 1
    cmbChecksum.ListIndex = 0
    cmbModeSelect.ListIndex = 0
    cmbBaudrate.ListIndex = 4
    cmbFreqGateTime.ListIndex = 0
    DataBit = 8    ' 8 data bit
    Parity = 0     ' Non Parity
    StopBit = 0    ' One Stop Bit
    COMOpen = 0
    Index = 0
    Counter = 0
    ConfigChange = 0
    
    cmbFreqGateTime.Enabled = False
    
    SendTo7000 = Space(80)
    ReceiveFrom7000 = Space(80)
    
    imgDigitalOut(0).Picture = LoadPicture(App.Path + "\off2.ico")
    imgDigitalOut(1).Picture = LoadPicture(App.Path + "\off2.ico")
    
   End Sub

Private Sub Form_Unload(Cancel As Integer)
    If COMOpen > 0 Then
        Close_Com (Port)
    End If
End Sub



Private Sub cmdReading_Click()
    Dim length As Integer
    Dim A$
    Dim CounterValue0 As Long
    Dim CounterValue1 As Long
    Dim RetVal%
  
    If COMOpen = 0 Then
        Beep
        MsgBox "Please Open COM Port Firstly !!!", vbCritical
        Exit Sub
    End If
    
    mRun7080 = 1
    
    While mRun7080
       '************************************************************
       '---------  read 7080 Counter/Freq 0  -----------------------
       w7000(0) = mPort ' COM Port
       w7000(1) = mOldAddress ' Module Address
       w7000(2) = &H7080
       w7000(3) = mOldCheckSum ' Checksum
       w7000(4) = 100 ' TimeOut = 0.1 second
       w7000(5) = 0   ' Counter 0
       w7000(6) = 0   ' string debug
           
       RetVal = CounterIn_7080(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
       If RetVal > 0 Then
          'DescribeErrorCode ModuleConfig(gSetupModuleIndex).ModuleType, RetVal
       Else
          CounterValue0 = MakeDWord(w7000(7), w7000(8))
          lblCounter(0).Caption = CounterValue0
       End If
       
       '--------- read 7080 Counter/Freq 1 -------
       w7000(5) = 1
       RetVal = CounterIn_7080(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
       If RetVal > 0 Then
          'DescribeErrorCode ModuleConfig(gSetupModuleIndex).ModuleType, RetVal
       Else
          CounterValue1 = MakeDWord(w7000(7), w7000(8))
          lblCounter(1).Caption = CounterValue1
       End If
       
       DoEvents
    Wend
End Sub







Public Function OpenCom()
    Dim A$, B$
    Dim i, Response, RetValue As Integer
    Dim Cmd As String
    
    Cmd = Space(80)
    
    A$ = Space(80)
    B$ = Space(80)
    mPort = cmbCOMPort.ListIndex + 1
        
    ' decide the baud rate
    Select Case cmbBaudrate.ListIndex
        Case 0
             mBaudrate = 115200
        Case 1
             mBaudrate = 57600
        Case 2
             mBaudrate = 38400
        Case 3
             mBaudrate = 19200
        Case 4
             mBaudrate = 9600
        Case 5
             mBaudrate = 4800
        Case 6
             mBaudrate = 2400
        Case 7
             mBaudrate = 1200
    End Select
        
      
    RetValue = Open_Com(mPort, mBaudrate, DataBit, Parity, StopBit)
    If RetValue > 0 Then
        Beep
        Response = MsgBox("Quit this demo?", vbYesNo, "OPEN_COM Error Code:" + Str(RetValue))
        If Response = vbYes Then
            Close_Com (mPort)
            End
        End If
    End If
  
    mOldAddress = Val(txtAddress.Text)
    mOldCheckSum = cmbChecksum.ListIndex
  
    COMOpen = 1
    
End Function

Private Sub imgDigitalOut_Click(Index As Integer)
    If COMOpen = 0 Then
        Beep
        MsgBox "Please Open COM Port Firstly !!!", vbCritical
        Exit Sub
    End If
    
    mDigitalOut(Index) = 1 - mDigitalOut(Index)
    If mDigitalOut(Index) Then
        imgDigitalOut(Index).Picture = LoadPicture(App.Path + "\on2.ico")
    Else
        imgDigitalOut(Index).Picture = LoadPicture(App.Path + "\off2.ico")
    End If
       
       w7000(0) = mPort ' COM Port
       w7000(1) = mOldAddress ' Module Address
       w7000(2) = &H7080
       w7000(3) = mOldCheckSum ' Checksum
       w7000(4) = 100 ' TimeOut = 0.1 second
       w7000(5) = mDigitalOut(1) * 2 + mDigitalOut(0)
       w7000(6) = 0   ' string debug
           
       RetVal = DigitalOut(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
       If RetVal > 0 Then
          'DescribeErrorCode ModuleConfig(gSetupModuleIndex).ModuleType, RetVal
          MsgBox "DigitalOut() Error !", vbCritical, "Error Code:" + Str(RetVal)
       End If
    
    
End Sub

Private Sub Option1_Click(Index As Integer)

    If COMOpen = 0 Then
        Beep
        MsgBox "Please Open COM Port Firstly !!!", vbCritical
        Exit Sub
    End If
    
       '************************************************************
       '---------  setting the gate mode  -----------------------
       w7000(0) = mPort ' COM Port
       w7000(1) = mOldAddress ' Module Address
       w7000(2) = &H7080
       w7000(3) = mOldCheckSum ' Checksum
       w7000(4) = 100 ' TimeOut = 0.1 second
       w7000(5) = Index  '0:Low  1:High  2:None
       w7000(6) = 0   ' string debug
           
       RetVal = SetGateMode_7080(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
       If RetVal > 0 Then
          'DescribeErrorCode ModuleConfig(gSetupModuleIndex).ModuleType, RetVal
       End If

End Sub
