VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "COMCTL32.OCX"
Begin VB.Form Form1 
   Caption         =   "Demo Kit: 7012, 7021, 7044, 7013"
   ClientHeight    =   7200
   ClientLeft      =   1515
   ClientTop       =   1875
   ClientWidth     =   9795
   BeginProperty Font 
      Name            =   "MS Sans Serif"
      Size            =   9.75
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   ForeColor       =   &H000040C0&
   LinkMode        =   1  'Source
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   7200
   ScaleWidth      =   9795
   Begin VB.TextBox Text2 
      Alignment       =   1  'Right Justify
      Height          =   375
      Left            =   8400
      TabIndex        =   45
      Text            =   "400"
      Top             =   4800
      Width           =   1215
   End
   Begin VB.Timer Timer3 
      Enabled         =   0   'False
      Left            =   8520
      Top             =   3360
   End
   Begin VB.TextBox Text1 
      Alignment       =   1  'Right Justify
      Height          =   375
      Left            =   8400
      TabIndex        =   42
      Text            =   "400"
      Top             =   2160
      Width           =   1215
   End
   Begin VB.TextBox MotorSteps 
      Alignment       =   1  'Right Justify
      Height          =   360
      Left            =   8400
      TabIndex        =   40
      Text            =   "0"
      Top             =   1080
      Width           =   1215
   End
   Begin VB.Timer Timer2 
      Enabled         =   0   'False
      Left            =   8760
      Top             =   0
   End
   Begin VB.PictureBox Picture2 
      BackColor       =   &H00C0FFFF&
      FillColor       =   &H80000012&
      ForeColor       =   &H000000FF&
      Height          =   1575
      Left            =   1920
      ScaleHeight     =   1515
      ScaleWidth      =   2955
      TabIndex        =   10
      Top             =   4440
      Width           =   3015
   End
   Begin VB.PictureBox Picture1 
      BackColor       =   &H00C0FFFF&
      FillColor       =   &H80000012&
      ForeColor       =   &H000000FF&
      Height          =   1575
      Left            =   1920
      ScaleHeight     =   1515
      ScaleWidth      =   2955
      TabIndex        =   9
      Top             =   2280
      Width           =   3015
   End
   Begin ComctlLib.Slider Slider1 
      Height          =   5175
      Left            =   360
      TabIndex        =   2
      Top             =   240
      Width           =   630
      _ExtentX        =   1111
      _ExtentY        =   9128
      _Version        =   327682
      BorderStyle     =   1
      Orientation     =   1
      Max             =   200
      SelStart        =   5
      Value           =   5
   End
   Begin VB.ComboBox comCombo 
      ForeColor       =   &H000000FF&
      Height          =   360
      ItemData        =   "Form1.frx":0000
      Left            =   120
      List            =   "Form1.frx":0010
      TabIndex        =   4
      Text            =   "COM1"
      Top             =   6600
      Width           =   1332
   End
   Begin VB.Timer Timer1 
      Interval        =   200
      Left            =   1080
      Top             =   1560
   End
   Begin VB.CommandButton ActiveCmd 
      Caption         =   "Active"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   13.5
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   1680
      TabIndex        =   1
      Top             =   6480
      Width           =   1095
   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          =   495
      Left            =   2880
      TabIndex        =   0
      Top             =   6480
      Width           =   735
   End
   Begin VB.Label Label25 
      Caption         =   "(mSeconds)"
      Height          =   255
      Left            =   8400
      TabIndex        =   46
      Top             =   5280
      Width           =   1215
   End
   Begin VB.Label Label24 
      Caption         =   "Interval of 7013D analog input"
      Height          =   735
      Left            =   8400
      TabIndex        =   44
      Top             =   3960
      Width           =   1215
   End
   Begin VB.Label Label23 
      Caption         =   "(mSeconds)"
      Height          =   255
      Left            =   8400
      TabIndex        =   43
      Top             =   2640
      Width           =   1215
   End
   Begin VB.Label Label22 
      Caption         =   "Interval of steps"
      Height          =   255
      Left            =   8280
      TabIndex        =   41
      Top             =   1800
      Width           =   1455
   End
   Begin VB.Label Label21 
      Caption         =   "0: repeat step"
      Height          =   255
      Left            =   8400
      TabIndex        =   39
      Top             =   720
      Width           =   1215
   End
   Begin VB.Label Label20 
      Caption         =   "Motor steps"
      Height          =   255
      Left            =   8400
      TabIndex        =   38
      Top             =   480
      Width           =   1215
   End
   Begin VB.Image Image1 
      Height          =   3780
      Left            =   5040
      Picture         =   "Form1.frx":002C
      Stretch         =   -1  'True
      Top             =   2280
      Width           =   3120
   End
   Begin VB.Label lCounter 
      Caption         =   "0"
      ForeColor       =   &H000000FF&
      Height          =   375
      Left            =   7080
      TabIndex        =   37
      Top             =   1920
      Width           =   1215
   End
   Begin VB.Label Label19 
      Caption         =   "Counter:"
      ForeColor       =   &H00FF0000&
      Height          =   255
      Left            =   6240
      TabIndex        =   36
      Top             =   1920
      Width           =   735
   End
   Begin VB.Label Label18 
      Caption         =   "Module Status"
      Height          =   495
      Left            =   4200
      TabIndex        =   35
      Top             =   6480
      Width           =   735
   End
   Begin VB.Label lTMid 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1320
      TabIndex        =   34
      Top             =   5040
      Width           =   495
   End
   Begin VB.Label lAMid 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1320
      TabIndex        =   33
      Top             =   2880
      Width           =   495
   End
   Begin VB.Label l7012 
      BackColor       =   &H00FFFF00&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "7012   #1"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   735
      Left            =   7440
      TabIndex        =   32
      Top             =   6360
      Width           =   735
   End
   Begin VB.Label l7013 
      BackColor       =   &H00FFFF00&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "7013   #4"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   735
      Left            =   6600
      TabIndex        =   31
      Top             =   6360
      Width           =   735
   End
   Begin VB.Label l7044 
      BackColor       =   &H00FFFF00&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "7044   #3"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   735
      Left            =   5760
      TabIndex        =   30
      Top             =   6360
      Width           =   735
   End
   Begin VB.Label l7021 
      BackColor       =   &H00FFFF00&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "7021   #2"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   735
      Left            =   4920
      TabIndex        =   29
      Top             =   6360
      Width           =   735
   End
   Begin VB.Label Label17 
      Caption         =   "Rotate Direction"
      Height          =   495
      Left            =   7320
      TabIndex        =   28
      Top             =   1200
      Width           =   975
   End
   Begin VB.Label Label16 
      Caption         =   "Analog Output"
      Height          =   495
      Left            =   5880
      TabIndex        =   27
      Top             =   1200
      Width           =   855
   End
   Begin VB.Label Label15 
      Caption         =   "Step Motor"
      Height          =   495
      Left            =   4560
      TabIndex        =   26
      Top             =   1200
      Width           =   615
   End
   Begin VB.Label Label14 
      Caption         =   "Lamp Action"
      Height          =   495
      Left            =   3240
      TabIndex        =   25
      Top             =   1200
      Width           =   735
   End
   Begin VB.Label Label13 
      Caption         =   "Switchs"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1680
      TabIndex        =   24
      Top             =   1200
      Width           =   855
   End
   Begin VB.Label Label12 
      Caption         =   "Lamps"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1680
      TabIndex        =   23
      Top             =   360
      Width           =   855
   End
   Begin VB.Shape sSwitch2 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   4125
      Top             =   1155
      Width           =   375
   End
   Begin VB.Shape sSwitch3 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   5445
      Top             =   1155
      Width           =   375
   End
   Begin VB.Shape sSwitch4 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   6885
      Top             =   1155
      Width           =   375
   End
   Begin VB.Shape sSwitch1 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   2805
      Top             =   1155
      Width           =   375
   End
   Begin VB.Shape Shape12 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   2760
      Top             =   1200
      Width           =   405
   End
   Begin VB.Shape Shape10 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   4080
      Top             =   1200
      Width           =   405
   End
   Begin VB.Shape Shape8 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   5400
      Top             =   1200
      Width           =   405
   End
   Begin VB.Shape Shape4 
      BackColor       =   &H00808080&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   6840
      Top             =   1200
      Width           =   405
   End
   Begin VB.Label Label11 
      Caption         =   "L4"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   6840
      TabIndex        =   22
      Top             =   0
      Width           =   375
   End
   Begin VB.Label Label10 
      Caption         =   "L3"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   5400
      TabIndex        =   21
      Top             =   0
      Width           =   375
   End
   Begin VB.Label Label9 
      Caption         =   "L2"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   4080
      TabIndex        =   20
      Top             =   0
      Width           =   375
   End
   Begin VB.Label Label8 
      Caption         =   "L1"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   2760
      TabIndex        =   19
      Top             =   0
      Width           =   375
   End
   Begin VB.Shape sLamp4 
      BackColor       =   &H00008000&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   6840
      Shape           =   3  'Circle
      Top             =   360
      Width           =   495
   End
   Begin VB.Shape Shape7 
      BackColor       =   &H00000000&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   6840
      Shape           =   3  'Circle
      Top             =   360
      Width           =   405
   End
   Begin VB.Shape sLamp3 
      BackColor       =   &H00008000&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   5400
      Shape           =   3  'Circle
      Top             =   360
      Width           =   495
   End
   Begin VB.Shape Shape5 
      BackColor       =   &H00000000&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   5400
      Shape           =   3  'Circle
      Top             =   360
      Width           =   405
   End
   Begin VB.Shape sLamp2 
      BackColor       =   &H00008000&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   4080
      Shape           =   3  'Circle
      Top             =   360
      Width           =   495
   End
   Begin VB.Shape Shape3 
      BackColor       =   &H00000000&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   4080
      Shape           =   3  'Circle
      Top             =   360
      Width           =   405
   End
   Begin VB.Shape sLamp1 
      BackColor       =   &H00008000&
      BackStyle       =   1  'Opaque
      Height          =   375
      Left            =   2760
      Shape           =   3  'Circle
      Top             =   360
      Width           =   495
   End
   Begin VB.Shape Shape1 
      BackColor       =   &H00000000&
      BackStyle       =   1  'Opaque
      Height          =   405
      Left            =   2760
      Shape           =   3  'Circle
      Top             =   360
      Width           =   405
   End
   Begin VB.Label Label7 
      Caption         =   "10"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1080
      TabIndex        =   18
      Top             =   2640
      Width           =   375
   End
   Begin VB.Label Label6 
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   255
      Left            =   1080
      TabIndex        =   17
      Top             =   5160
      Width           =   255
   End
   Begin VB.Label Label5 
      Caption         =   "20"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1080
      TabIndex        =   16
      Top             =   360
      Width           =   375
   End
   Begin VB.Label Label2 
      Caption         =   "7021 Analog Output"
      ForeColor       =   &H00FF0000&
      Height          =   615
      Left            =   120
      TabIndex        =   15
      Top             =   5520
      Width           =   1215
   End
   Begin VB.Label lTMin 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1200
      TabIndex        =   14
      Top             =   5880
      Width           =   615
   End
   Begin VB.Label lTMax 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1080
      TabIndex        =   13
      Top             =   4320
      Width           =   735
   End
   Begin VB.Label lAMin 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1200
      TabIndex        =   12
      Top             =   3720
      Width           =   615
   End
   Begin VB.Label lAMax 
      Alignment       =   1  'Right Justify
      Caption         =   "0"
      ForeColor       =   &H00FF00FF&
      Height          =   375
      Left            =   1200
      TabIndex        =   11
      Top             =   2160
      Width           =   615
   End
   Begin VB.Label lInput7013 
      Caption         =   "0"
      ForeColor       =   &H000000FF&
      Height          =   375
      Left            =   4200
      TabIndex        =   8
      Top             =   4080
      Width           =   1095
   End
   Begin VB.Label Label1 
      Caption         =   "7013D Analog Input"
      ForeColor       =   &H00FF0000&
      Height          =   375
      Left            =   2160
      TabIndex        =   7
      Top             =   4080
      Width           =   1935
   End
   Begin VB.Label lInput7012 
      Caption         =   "0"
      ForeColor       =   &H000000FF&
      Height          =   375
      Left            =   4320
      TabIndex        =   6
      Top             =   1920
      Width           =   975
   End
   Begin VB.Label Label4 
      Caption         =   "7012D Analog Input"
      ForeColor       =   &H00FF0000&
      Height          =   375
      Left            =   2280
      TabIndex        =   5
      Top             =   1920
      Width           =   1935
   End
   Begin VB.Label Label3 
      Caption         =   "COM Port:"
      ForeColor       =   &H00FF0000&
      Height          =   375
      Left            =   120
      TabIndex        =   3
      Top             =   6240
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Const DataNo = 50                      'Max Data number

Dim ComOpened As Integer
Dim fABuf(0 To DataNo), fTBuf(0 To DataNo) As Single 'Buffer for Analog and temperature
Dim fAMax, fAMin, fTMax, fTMin As Single             'Max/Min value for Analog and temperature
Dim fDeg As Single                      'Degrees
Dim wDirection As Integer               'Direction of stepping motor
Dim LampStatus As Integer               'On/Off for 4 Lamps
Dim MotorStatus As Integer              'On/Off of Stepping Motor
Dim MotorStatus2 As Integer             '0 , 1, 2, 4, 8
Dim iMotorSteps As Integer              'Down count
Dim SinStatus As Integer                '1:Stpping  0:Fixed
Dim LampIdx1, LampIdx2 As Integer
Dim bProcessing, bProcessing2, bProcessing3 As Boolean


Private Sub ActiveCmd_Click()
    Dim i As Integer
    
    If ActiveCmd.Caption = "Stop" Then   'User click the STOP button
        ActiveCmd.Caption = "Active"
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer3.Enabled = False
        Out7044 (0)                     'Put overall lamps to off
        exitCmd.Enabled = True
        comCombo.Enabled = True
        Exit Sub
    End If
    
    'Else, user click the ACTIVE button
    exitCmd.Enabled = False
    comCombo.Enabled = False
    ClearBuf                            'Clear the Buf for fABuf , fTBuf
    fDeg = 0                            'Set default value of degree to zero
    ActiveCmd.Caption = "Stop"
    Close_Com (port)                    'Close the COM port
    port = comCombo.ListIndex + 1
    ComOpened = 0                       'COM port not opened
    OpenCom                             'Open the COM port
    Out7021                             'Analog output(sin-wave)
    If ComOpened = 1 Then
        Timer1.Enabled = True
        Timer2.Enabled = True
        Timer3.Enabled = True
    Else                                'COM port open fail
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer3.Enabled = False
        ActiveCmd_Click                  'Stop the action
        Exit Sub
    End If
      
    Check7044
    For i = 1 To 4
        MotorStepping
    Next
            
End Sub

Private Sub exitCmd_Click()
    End                                 'end the program
End Sub

Private Sub Form_Load()
    comCombo.ListIndex = 0
    DataBit = 8                         '8 data bit
    Parity = 0                          'Non Parity
    StopBit = 0                         'One Stop Bit
    ComOpened = 0                       'COM port not opened
    Timer1.Enabled = False
    Timer2.Enabled = False
    Timer3.Enabled = False
    Timer1.Interval = 200
    Timer2.Interval = 400
    Timer3.Interval = 400
    wDirection = 1                      'Set the direction to 1 or -1
    LampStatus = 0                      'Clear the Lamp
    MotorStatus = 0                     'Stepping Motor is OFF
    MotorStatus2 = 0                    'Stepping Motor is OFF
    SinStatus = 0                       'SinWave is Stepping
    LampIdx1 = 0: LampIdx2 = 0
    bProcessing = False
    bProcessing2 = False
    bProcessing3 = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If COMOpen > 0 Then                 'Close COM port before end program
        Close_Com (port)                'Close COM port
        ComOpened = 0                   'COM port not opened
    End If
End Sub

Public Function OpenCom()
    Dim i, Response, RetValue As Integer
    
    port = comCombo.ListIndex + 1       'Get the COM port that user selected
    BaudRate = 9600                     'Set the communication Baud-Rate to 9600
      
    ComOpened = 0                       'COM port not opened
    RetValue = Open_Com(port, BaudRate, DataBit, Parity, StopBit)  'Open the COM port
    If RetValue > 0 Then                'COM port open fail
        Beep
        ActiveCmd_Click                  'Stop the Action
        Response = MsgBox("Quit this demo?", vbYesNo, "OPEN_COM Error Code:" + Str(RetValue))
        If Response = vbYes Then
            End                         'End the program
        End If
    End If
  
    ComOpened = 1                       'COM port have been successfully opened
End Function


Private Sub MotorSteps_Change()
    iMotorSteps = Val(MotorSteps.Text)
End Sub

Private Sub Text1_Change()
    Timer2.Interval = Text1.Text
End Sub

Private Sub Text2_Change()
    Timer3.Interval = Text2.Text
End Sub

Private Sub Timer1_Timer()
    If bProcessing = True Then          'avoid reenter this function
        Exit Sub
    Else
        bProcessing = True
    End If
    
    Out7021                             'Analog output(sin-wave)
    ShiftBuf 1                          'Shift the data of picture
    In7012                              'Analog input(sin-wave)
    ReadEventCounter7012                'Read Event Counter from 7012
    DrawPic 1                           'draw the picture from 7012/7013 buffer
    Check7044                           'Check the status(input value) of 7044
    
    bProcessing = False
    If ActiveCmd.Caption = "Active" Then
        Close_Com (port)                'If user stop the action, then close the COM port
        ComOpened = 0
    End If
    
End Sub

Private Sub Out7021()
    Dim A$
    SendTo7000 = Space(100)             'allocate some space to string buffer
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                     'COM Port
    w7000(1) = &H2                      'Module Adddress
    w7000(2) = &H7021                   'module id
    w7000(3) = 0                        'CheckSum disable
    w7000(4) = 100                      'TimeOut = 0.1 second
    w7000(5) = 0                        'channel no
    w7000(6) = 1                        'string debug
    
    If SinStatus = 1 Then fDeg = fDeg + 0.2 'SinWave Stepping
    If fDeg >= 3.14159 Then fDeg = -3.14159  'adjust the degress value
    Slider1.Value = Sin(fDeg) * 100 + 100    'compute the sin-wave value
    f7000(0) = Slider1.Value
    
    If f7000(0) > Slider1.Max Then           'adjust the sin-wave value
        f7000(0) = Slider1.Max
        Slider1.Value = f7000(0)
    End If
    If f7000(0) < Slider1.Min Then
        f7000(0) = Slider1.Min
        Slider1.Value = f7000(0)
    End If
    f7000(0) = (Slider1.Max - f7000(0)) / 10# 'adjust the sin-wave value to 0mA to 20 mA
        
    Ret = AnalogOut(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then                        'Some error occur
        l7021.BackColor = &HFF
    Else
        l7021.BackColor = &HFFFF00          'Analog Output OK!
    End If
End Sub

Private Function In7012()
   
    SendTo7000 = Space(100)                 'allocate space for string
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H1                          'Module Adddress
    w7000(2) = &H7012                       'module id
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = 0                            'not used
    w7000(6) = 1                            'string debug
    
    Ret = AnalogIn(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7012.BackColor = &HFF              'Some error occur
    Else
        l7012.BackColor = &HFFFF00          'analog input OK!
    End If
    
    lInput7012.Caption = Str(f7000(0))          'show the value
    fABuf(DataNo - 1) = f7000(0)                'record the value
    If fAMax < f7000(0) Then fAMax = f7000(0)   'record the max-value
    If fAMin > f7000(0) Then fAMin = f7000(0)   'record the min-value
    In7012 = f7000(0)                           'return value
End Function


Private Function In7013()
    SendTo7000 = Space(100)                 'allocate space for string
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H4                          'Module Adddress
    w7000(2) = &H7013                       'module id
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = 0                            'Not used
    w7000(6) = 1                            'string debug
    
    Ret = AnalogIn(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7013.BackColor = &HFF              'Some error occur
    Else
        l7013.BackColor = &HFFFF00          'Analog input OK
    End If
    
    lInput7013.Caption = Str(f7000(0))          'Show value
    fTBuf(DataNo - 1) = f7000(0)                'Record value
    If fTMax < f7000(0) Then fTMax = f7000(0)   'Record max-value
    If fTMin > f7000(0) Then fTMin = f7000(0)   'record min-value
    In7013 = f7000(0)                           'return value
End Function

Private Sub Out7044(ByVal DOVal As Integer)
    SendTo7000 = Space(100)                 'allocate spaces
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H3                          'Module Adddress
    w7000(2) = &H7044                       'Module ID
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = DOVal And &HFF               'Output Value
    w7000(6) = 1                            'string debug
    
    Ret = DigitalOut(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7044.BackColor = &HFF              'Some Error Occur
    Else
        l7044.BackColor = &HFFFF00          'Digital Output OK!
    End If
   
  
End Sub

Private Sub ShowLamp()
    
    If (LampStatus And 128) <> 0 Then       'Lamp-1
        sLamp1.BackColor = &HFF00&
    Else
        sLamp1.BackColor = &H8000&
    End If
    
    If (LampStatus And 64) <> 0 Then        'Lamp-2
        sLamp2.BackColor = &H88FF&
    Else
        sLamp2.BackColor = &H8000&
    End If
    
    If (LampStatus And 32) <> 0 Then        'Lamp-3
        sLamp3.BackColor = &HFF00&
    Else
        sLamp3.BackColor = &H8000&
    End If

    If (LampStatus And 16) <> 0 Then        'Lamp-4
        sLamp4.BackColor = &H88FF&
    Else
        sLamp4.BackColor = &H8000&
    End If
    
End Sub


Private Function In7044() As Integer
    SendTo7000 = Space(100)                 'allocate spaces
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H3                          'Module Adddress
    w7000(2) = &H7044                       'Module ID
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = 0                            'Input value
    w7000(6) = 1                            'string debug
    
    Ret = DigitalIn(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7044.BackColor = &HFF              'Some Error Occur
    Else
        l7044.BackColor = &HFFFF00          'Digital Input OK
    End If
    
    In7044 = w7000(5)                       'return value
End Function

Private Sub Check7044()
    Dim wRtn As Integer
    Dim Rtn As Integer
    
    '***************************************
    wRtn = In7044                           'Read 7044 status
    If (wRtn And &H8) = 0 Then              'step Lamp
        LampIdx1 = LampIdx1 + 1             'compute Lamp status
        If LampIdx1 > 3 Then
            LampIdx2 = LampIdx2 + 1
            LampIdx1 = 0
        End If
        If LampIdx2 > 3 Then LampIdx2 = 0
        LampStatus = 2 ^ (LampIdx2 + 4)
        sSwitch1.BackColor = &HFFFF00       'Switch-1 has been pressed
        Out7044 (LampStatus)                'Set Lamp status
    Else                                    'Off these Lamps
        If LampStatus <> 0 Then
            Out7044 (0)                     'Off these 4 Lamps
            LampStatus = 0                  'Clear Lamp status
        End If
        sSwitch1.BackColor = &H808080
    End If
    ShowLamp
    
    '***************************************
    If (wRtn And &H1) = 0 Then              'Change the Driection of Stepping Motor
        wDirection = wDirection * -1
        ClearEventCounter7012               'Clear event counter
        sSwitch4.BackColor = &HFFFF00       'Switch-4 has been pressed
    Else
        sSwitch4.BackColor = &H808080
    End If

    '***************************************
    If (wRtn And &H4) = 0 Then              'Step the Stepping Motor
        MotorStatus = 1
        sSwitch2.BackColor = &HFFFF00       'Switch-2 has been pressed
    Else
        MotorStatus = 0
        iMotorSteps = MotorSteps.Text
        sSwitch2.BackColor = &H808080
    End If
    
    '***************************************
    If (wRtn And &H2) = 0 Then              'Analog Output
        SinStatus = 1
        sSwitch3.BackColor = &HFFFF00       'Switch-3 has been pressed
    Else
        SinStatus = 0
        sSwitch3.BackColor = &H808080
    End If
End Sub

Private Sub ClearBuf()
    Dim i As Integer
    
    For i = 0 To DataNo                     'Clear overall buffers
        fABuf(i) = -100
        fTBuf(i) = -100
    Next
    fAMax = 0:   fAMin = 0                  'Set to default value
    fTMax = 49:  fTMin = 1
End Sub

Private Sub ShiftBuf(ByVal PicIdx As Integer)
    Dim i As Integer
    
    If PicIdx = 1 Then
        For i = 1 To DataNo - 1                 'Left Shift these buffers
            If fABuf(i + 1) <> -100 Then
                fABuf(i) = fABuf(i + 1)
            End If
        Next
    Else
        For i = 1 To DataNo - 1                 'Left Shift these buffers
            If fTBuf(i + 1) <> -100 Then
                fTBuf(i) = fTBuf(i + 1)
            End If
        Next
    End If
End Sub

Private Sub DrawPic(ByVal PicIdx As Integer)
    Dim i As Integer

    If PicIdx = 1 Then
        'Set the scale of picture1
        Picture1.Scale (0, fAMin - 1)-(DataNo, fAMax + 1)
        'set the Max/Min/Middle value
        lAMax.Caption = fAMax + 1
        lAMin.Caption = fAMin - 1
        lAMid.Caption = (fAMax + fAMin) / 2
        'Clear these picture
        Picture1.Cls
        'Draw the middle line
        Picture1.ForeColor = &HFF00FF
        Picture1.Line (1, lAMid.Caption)-(DataNo - 1, lAMid.Caption)
        'Draw values as lines
        Picture1.ForeColor = &HFF
        For i = 1 To DataNo - 1
            If fABuf(i) <> -100 Then
                If fABuf(i - 1) = -100 Then
                    Picture1.PSet (i, fAMax - fABuf(i))
                Else
                    Picture1.Line -(i, fAMax - fABuf(i))
                End If
            End If
            DoEvents
        Next i
    Else
        'Set the scale of picture2
        Picture2.Scale (0, fTMin - 1)-(DataNo, fTMax + 1)
        'set the Max/Min/Middle value
        lTMax.Caption = fTMax + 1
        lTMin.Caption = fTMin - 1
        lTMid.Caption = (fTMax + fTMin) / 2
        'Clear these picture
        Picture2.Cls
        'Draw the middle line
        Picture2.ForeColor = &HFF00FF
        Picture2.Line (1, lTMid.Caption)-(DataNo - 1, lTMid.Caption)
        'Draw values as lines
        Picture2.ForeColor = &HFF
        For i = 1 To DataNo - 1
            If fTBuf(i) <> -100 Then
                If fTBuf(i - 1) = -100 Then
                    Picture2.PSet (i, fTMax - fTBuf(i))
                Else
                    Picture2.Line -(i, fTMax - fTBuf(i))
                End If
            End If
            DoEvents
        Next i
    End If
    
End Sub

Private Sub MotorStepping()
    If MotorStatus = 0 Then Exit Sub 'stop the Motor
    
    If MotorStatus2 = 0 Then 'modify the MotorStatus2 value
       MotorStatus2 = 1
    Else
        If wDirection = 1 Then
            MotorStatus2 = MotorStatus2 * 2
        Else
            MotorStatus2 = MotorStatus2 / 2
        End If
    End If
    
    If MotorStatus2 < 1 Then MotorStatus2 = 8
    If MotorStatus2 > 8 Then MotorStatus2 = 1
    
    Out7044 (0 + LampStatus)  'Set digital output bit3 to OFF
    Out7044 (MotorStatus2 + LampStatus)  'Set digital output bit0 to ON
End Sub

Private Sub ReadEventCounter7012()
    SendTo7000 = Space(100)                 'allocate spaces
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H1                          'Module Adddress
    w7000(2) = &H7012                       'Module ID
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = 0                            'Not used
    w7000(6) = 1                            'string debug
    
    Ret = ReadEventCounter(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7012.BackColor = &HFF              'Some error occur
    Else
        l7012.BackColor = &HFFFF00          'Read counter value OK
    End If
    
    lCounter.Caption = w7000(7)             'Show value
End Sub

Private Sub ClearEventCounter7012()
    SendTo7000 = Space(100)                 'allocate spaces
    ReceiveFrom7000 = Space(100)
      
    w7000(0) = port                         'COM Port
    w7000(1) = &H1                          'Module Adddress
    w7000(2) = &H7012                       'Module ID
    w7000(3) = 0                            'CheckSum disable
    w7000(4) = 100                          'TimeOut = 0.1 second
    w7000(5) = 0                            'Not used
    w7000(6) = 1                            'string debug
    
    Ret = ClearEventCounter(w7000(0), f7000(0), SendTo7000, ReceiveFrom7000)
    If Ret <> 0 Then
        l7012.BackColor = &HFF              'Some error occur
    Else
        l7012.BackColor = &HFFFF00          'Clear counter OK
    End If
End Sub

Private Sub Timer2_Timer()
    DoEvents
    If bProcessing2 = True Then
        Exit Sub
    Else
        bProcessing2 = True
    End If
    
    If MotorStatus <> 0 Then            'Check if the Stepping Motor to step
       If MotorSteps.Text = 0 Then
          MotorStepping
       Else
          If (MotorSteps.Text <> 0) _
          And (iMotorSteps > 0) Then
             iMotorSteps = iMotorSteps - 1
             MotorStepping
          End If
       End If
    End If

    bProcessing2 = False
End Sub

Private Sub Timer3_Timer()
    If bProcessing3 = True Then
        Exit Sub
    Else
        bProcessing3 = True
    End If
    
    ShiftBuf 2                          'Shift the data of picture
    In7013                              'Analog input( RTD sensor )
    DrawPic 2                            'draw the picture from 7012/7013 buffer
    
    bProcessing3 = False
End Sub
