Old advantech IO card VB6 sample
from https://advdownload.advantech.com/productfile/Downloadfile2/1-6N6T6/Manual.pdf
3.3 DLL Driver Tutorial for Visual Basic Application
Step 1: Add Demo Board With DEVINST.EXE 1. The same as step 1 in the Win32 Console example.
Step 2: Write Your Application 1. Go into the Start menu and click on the Visual Basic 5.0 icon in the Microsoft Visual Basic folder. Figure 3-12: Starting Microsoft Visual Basic 5.0 58 Advantech DLL Drivers User's Manual 2. The Visual Basic 5.0 development environment will load as follows: Figure 3-13: Select Standard EXE from the New Project Dialog Box 3. Select the Standard EXE icon and press the Open button. A new project is created. Then click on the Project Explorer in the View menu. Add the declaration file, Driver.bas module by clicking on Add Module in the Project menu. The Driver.bas file is located in the \Advantech\Adsapi\Include directory. Name the adsoft.frm form in the project. Chapter 3 Tutorial 59 Figure 3-14: Adding the Declaration File driver.bas
4. Design your form: place a Label control on Form1 and enter “Analog Input” as its Caption field. Then place a TextBox control on Form1. Switch to the Property Window and enter txtAIValue as its Name property. At last, place a CommandButton control on the form. Enter cmdRead as its Name property, and enter Read as the Caption property. Your form should look similar to the one shown below: Figure 3-15: Form Design of Form1
5. Write your code for the cmdRead button as below: 60 Advantech DLL Drivers User's Manual
Private Sub cmdRead_Click()
Dim ErrCde As Long ‘ Error code Dim szErrMsg As String * 80 ‘ Error string Dim DeviceHandle As Long Dim AiConfig As PT_AIConfig Dim AiVoltageIn As PT_AIVoltageIn Dim voltage As Single ‘ Step 1: open device ErrCde = DRV_DeviceOpen(0, DeviceHandle) ‘ Make sure device number = 0 If (ErrCde <> 0) Then DRV_GetErrorMessage ErrCde, szErrMsg Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”) Exit Sub End If ‘ Step 2: configure input range AiConfig.DasChan = 0 ‘ channel: 0 AiConfig.DasGain = 0 ‘ gain code: 0 ErrCde = DRV_AIConfig(DeviceHandle, AiConfig) If (ErrCde <> 0) Then DRV_GetErrorMessage ErrCde, szErrMsg Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”) Exit Sub End If ‘ Step 3: read value AiVoltageIn.chan = AiConfig.DasChan AiVoltageIn.gain = AiConfig.DasGain AiVoltageIn.TrigMode = 0 AiVoltageIn.voltage = DRV_GetAddress(voltage) ErrCde = DRV_AIVoltageIn(DeviceHandle, AiVoltageIn) If (ErrCde <> 0) Then DRV_GetErrorMessage ErrCde, szErrMsg Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”) Exit Sub End If ‘ Step 4: display value txtAIValue.Text = Format(voltage, “###0.00”) ‘ Step 5: close device ErrCde = DRV_DeviceClose(DeviceHandle) If (ErrCde <> 0) Then DRV_GetErrorMessage ErrCde, szErrMsg Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”)
End If
End Sub
Chapter 3 Tutorial 61 Notice: The DRV_GetAddress function is for Visual Basic only. In VC++ or Delphi, users can get a pointer or address of a variable. However, in Visual Basic, there is no standard function to get the memory address of a variable. The DLL driver requires an address as a parameter when calling most functions. Step 3: Test Your Program 1. Press F5 to run the program. Then press the Read button in your sample application. The data will appear as below. Figure 3-16: Testing the Sample Program
留言
張貼留言