mainform.frm

来自「16 relay output channels and 16 isolated」· FRM 代码 · 共 417 行

FRM
417
字号
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form MainForm 
   Caption         =   "Digital input with Interrupt for PCM-3780"
   ClientHeight    =   4830
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7590
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   4830
   ScaleWidth      =   7590
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdExit 
      Caption         =   "Exit"
      Height          =   495
      Left            =   5760
      TabIndex        =   11
      Top             =   480
      Width           =   1455
   End
   Begin VB.Frame Frame4 
      Caption         =   "Scan Digital Input"
      Height          =   3255
      Left            =   5400
      TabIndex        =   5
      Top             =   1440
      Width           =   2055
      Begin VB.Timer ScanTimer 
         Enabled         =   0   'False
         Interval        =   200
         Left            =   960
         Top             =   2040
      End
      Begin VB.CommandButton cmdStop 
         Caption         =   "Stop"
         Enabled         =   0   'False
         Height          =   375
         Left            =   1200
         TabIndex        =   9
         Top             =   2640
         Width           =   735
      End
      Begin VB.CommandButton cmdStart 
         Caption         =   "Start"
         Height          =   375
         Left            =   120
         TabIndex        =   8
         Top             =   2640
         Width           =   855
      End
      Begin VB.ComboBox cmbPort 
         Height          =   315
         Left            =   360
         Style           =   2  'Dropdown List
         TabIndex        =   6
         Top             =   360
         Width           =   1335
      End
      Begin VB.Label Label6 
         Caption         =   "Hex"
         Height          =   255
         Left            =   840
         TabIndex        =   13
         Top             =   1560
         Width           =   375
      End
      Begin VB.Label txtPortValue 
         BorderStyle     =   1  'Fixed Single
         Height          =   375
         Left            =   360
         TabIndex        =   7
         Top             =   1080
         Width           =   1335
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "Interrupt"
      Height          =   3255
      Left            =   120
      TabIndex        =   3
      Top             =   1440
      Width           =   5055
      Begin VB.Timer IntTimer 
         Enabled         =   0   'False
         Interval        =   1001
         Left            =   360
         Top             =   2640
      End
      Begin VB.CommandButton cmdStopAll 
         Caption         =   "Stop All"
         Height          =   375
         Left            =   1920
         TabIndex        =   10
         Top             =   2640
         Width           =   975
      End
      Begin MSComctlLib.ListView InterruptList 
         Height          =   2175
         Left            =   120
         TabIndex        =   4
         Top             =   360
         Width           =   4815
         _ExtentX        =   8493
         _ExtentY        =   3836
         View            =   3
         LabelEdit       =   1
         LabelWrap       =   -1  'True
         HideSelection   =   -1  'True
         Checkboxes      =   -1  'True
         FullRowSelect   =   -1  'True
         GridLines       =   -1  'True
         _Version        =   393217
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BorderStyle     =   1
         Appearance      =   1
         NumItems        =   2
         BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Text            =   "Interrupt Channel"
            Object.Width           =   2117
         EndProperty
         BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   1
            Text            =   "I/s"
            Object.Width           =   2540
         EndProperty
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "Select Device"
      Height          =   1215
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   5295
      Begin VB.CommandButton cmdSelect 
         Caption         =   "Select Device"
         Height          =   375
         Left            =   3720
         TabIndex        =   2
         Top             =   240
         Width           =   1455
      End
      Begin VB.Label Label4 
         Caption         =   "Device Name"
         Height          =   255
         Left            =   240
         TabIndex        =   12
         Top             =   360
         Width           =   1335
      End
      Begin VB.Label txtDeviceName 
         BorderStyle     =   1  'Fixed Single
         Height          =   375
         Left            =   240
         TabIndex        =   1
         Top             =   720
         Width           =   4935
      End
   End
End
Attribute VB_Name = "MainForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
 Option Explicit
    Dim DIInterruptEnable(0 To 15) As Long
    Dim dwStartTime, dwCurrentTime, dwTime As Long
    Dim dwEventCount(0 To 128) As Long
    Dim m_bThreadFlag As Boolean
    Dim i As Integer
    Dim ExitCode As Long
    Dim m_ulDevNum As Long
    Dim m_ErrCde As Long
    Dim m_szErrMsg As String * 80
    Dim ThreadID As Long
    Dim usPortProg As Integer
    
Private Sub cmdExit_Click()
    If (ThreadHandle <> 0) Then
       GetExitCodeThread ThreadHandle, ExitCode
       
       If (ExitCode = STILL_ACTIVE) Then
          TerminateThread ThreadHandle, ExitCode
       End If
    End If

Unload Me
End Sub

Private Sub cmdSelect_Click()
    Dim DeviceName As String * 50
    Dim ptGetFeatures As PT_DeviceGetFeatures
    Dim lpDevFeatures As DEVFEATURES
    Dim i As Integer
    Dim strTemp As String
    
    'close timer
    cmdStop_Click
    
   'close thread
    If (ThreadHandle <> 0) Then
        cmdStopAll_Click
    End If
    
    'close device
    If (m_DriverHandle <> 0) Then
        DRV_DeviceClose (m_DriverHandle)
        m_DriverHandle = 0
    End If
    

    m_bThreadFlag = False
    
    cmbPort.Clear
    InterruptList.ListItems.Clear
    
      'Select the Device
    DRV_SelectDevice Me.hWnd, 0, m_ulDevNum, DeviceName
    txtDeviceName.Caption = DeviceName
      'Open the Device
    m_ErrCde = DRV_DeviceOpen(m_ulDevNum, m_DriverHandle)
    If m_ErrCde <> 0 Then
      MsgBox ("The Device error")
    Exit Sub
    End If
       'Get the feature of the device
    ptGetFeatures.buffer = DRV_GetAddress(lpDevFeatures)
    m_ErrCde = DRV_DeviceGetFeatures(m_DriverHandle, ptGetFeatures)
    m_DIChanNum = lpDevFeatures.usMaxDIChl
    usPortProg = lpDevFeatures.usDIOPort
    If (m_DIChanNum <= 0) Then
        MsgBox ("Function not supported")
    End If
    
    Dim SubItem As ListItem
    Dim j As Integer
    j = -1
    For i = 0 To m_DIChanNum - 1
     If (i Mod 8 = 0) Then
     j = j + 1
     End If
     
     Set SubItem = InterruptList.ListItems.Add(, , "Port" & Hex(j) & "_" & "Chan" & Str(i Mod 8))
    Next i
    
    For i = 0 To m_DIChanNum \ 8 - 1
     cmbPort.AddItem ("Port" & Hex(i))
    Next i
    
    cmbPort.ListIndex = 0

End Sub



Private Sub cmdStart_Click()
 
 Dim ptDioSetPortMode As PT_DioSetPortMode
 
 If (usPortProg = 1) Then
 
    For i = 1 To (m_DIChanNum / 8 - 1)
    
        ptDioSetPortMode.Port = i
        ptDioSetPortMode.dir = 0
        m_ErrCde = DRV_DioSetPortMode(m_DriverHandle, ptDioSetPortMode)
        If (m_ErrCde <> 0) Then
            DRV_GetErrorMessage m_ErrCde, m_szErrMsg
            MsgBox m_szErrMsg, vbOKOnly, "Driver Message"
        End If
    Next i
    
 End If
 
 ScanTimer.Enabled = True
 cmdStart.Enabled = False
 cmdStop.Enabled = True
 
 
End Sub

Private Sub cmdStop_Click()
 ScanTimer.Enabled = False
 cmdStart.Enabled = True
 cmdStop.Enabled = False

End Sub

Private Sub cmdStopAll_Click()
    Dim i As Integer
    Dim EnableEvent As PT_EnableEvent

    'update the UI
    For i = 0 To m_DIChanNum - 1
    
       If (InterruptList.ListItems(i + 1).Checked) Then
            InterruptList.ListItems(i + 1).Checked = False
        End If
    Next i
    
    'Close Thread
    If (ThreadHandle <> 0) Then
       GetExitCodeThread ThreadHandle, ExitCode
       
       If (ExitCode = STILL_ACTIVE) Then
          TerminateThread ThreadHandle, ExitCode
       End If
    End If
    
    'Disable all the events
    EnableEvent.Enabled = 0
    EnableEvent.Count = 1
    
    For i = 0 To 23
        EnableEvent.EventType = ADS_EVT_INTERRUPT_DI0 + i
        m_ErrCde = DRV_EnableEvent(m_DriverHandle, EnableEvent)
        If (m_ErrCde <> 0) Then
             DRV_GetErrorMessage m_ErrCde, m_szErrMsg
             MsgBox m_szErrMsg, vbOKOnly, "Driver Message"
        End If
    Next i
    
    m_bThreadFlag = False
    IntTimer.Enabled = False

End Sub

Private Sub Form_Load()
    cmdSelect_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If (m_DriverHandle <> 0) Then
        DRV_DeviceClose (m_DriverHandle)
    End If
  Call TerminateProcess(GetCurrentProcess, ByVal 0&)
End Sub

Private Sub InterruptList_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    Dim Response As Long
    Dim EventSetting As PT_EnableEvent

   If Item.Checked Then
      EventSetting.Enabled = 1
   Else
      EventSetting.Enabled = 0
   End If
   'enable/disable event
    EventSetting.Count = 1
    EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + Item.Index - 1
    
    m_ErrCde = DRV_EnableEvent(m_DriverHandle, EventSetting)
    If (m_ErrCde <> 0) Then
        InterruptList.ListItems(Item.Index).Checked = False
        DRV_GetErrorMessage m_ErrCde, m_szErrMsg
        MsgBox m_szErrMsg, vbOKOnly, "Driver Message"
        Exit Sub
    End If



 If (m_bThreadFlag = False) Then
        'Create Interrupt Event handler thread and suspend it
        ThreadHandle = CreateThread(0, 0, AddressOf EventThread, vbNullString, CREATE_SUSPENDED, ThreadID)
        If (ThreadHandle = 0) Then
             Response = MsgBox("Create Thread Failed!", vbOKOnly, "Error!!")
             InterruptList.ListItems(Item.Index).Checked = False
             Response = DRV_DeviceClose(m_DriverHandle)
            Exit Sub
        End If
    
      'Set thread priority
       ' Using THREAD_PRIORITY_TIME_CRITICAL for interrupt event handling routine,
       ' you can get about 20 KHz response ratio by using PENTIUM 200 MMX PC.
        Response = SetThreadPriority(ThreadHandle, THREAD_PRIORITY_NORMAL)
    
       'Awake thread
       Response = ResumeThread(ThreadHandle)
       IntTimer.Enabled = True
       m_bThreadFlag = True
End If
End Sub

Private Sub IntTimer_Timer()
Dim Ratio As Single
Dim strRatio(0 To 23) As String

    dwCurrentTime = GetTickCount()
        dwTime = dwCurrentTime - dwStartTime
        If (dwTime >= 1000) Then
            dwStartTime = dwCurrentTime
            For i = 0 To m_DIChanNum - 1
                strRatio(i) = Format(CLng(lEventCount(i)) / CSng(dwTime) * 1000#, "0")
                lEventCount(i) = 0
            Next i
            For i = 0 To m_DIChanNum - 1
            InterruptList.ListItems(i + 1).SubItems(1) = strRatio(i)
                
            Next i

            
        End If
End Sub

Private Sub ScanTimer_Timer()
    Dim DIValue As Integer
    Dim ptDioReadPortByte As PT_DioReadPortByte
    ptDioReadPortByte.Port = cmbPort.ListIndex
    ptDioReadPortByte.value = DRV_GetAddress(DIValue)
    m_ErrCde = DRV_DioReadPortByte(m_DriverHandle, ptDioReadPortByte)
    txtPortValue.Caption = Hex(DIValue)
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?