Reading the Games Port Analogue to Digital (AtoD) lines in Visual Basic
The example below assumes you have a resistive load (a potentiometer, an LDR, etc) attached to Line D0 of the Games Port. It also displays the value read for the four switched lines at the Port. If you want to read and display the values of the other three AtoD lines you will need to add lines of code with separate counting loops and then "AND" the PortByte values with "2", "4" and "8" to extract the values of lines D1, D2 and D3 respectively.
For more information about the Games Port see here.
STEP 1 Create a Form with two Labels and a Timer control.

STEP 2 Set the Timer Interval to 100 milliseconds.

STEP 3 Go to the Code window and type in the following ...
| Private Sub Timer1_Timer() ' (C) 2001 J Fuller - Based on routines written ' by Mike Leishman of Newman College ' The approach is to send all AtoD lines at the games Port ' HIGH by writing a value to the Port (any value can be used). ' The routine then loops until the Target line goes LOW. ' The value of the counter represents the analogue value read by the Port. ' In this example, Line 1 (DO) is the Target line. Label1.Caption = PortIn(512) ' Display the Value for the four switched lines ' The A to D function: Call PortOut(512, PortByte) ' Write a value to Games Port to activate the HIGH state CountValue = 0 Do PortByte = PortIn(512) ' Read the Value at the Games Port test = (PortByte And 1) = 0 'Test line D0 to see if it is LOW CountValue = CountValue + 1 Loop Until (CountValue > 3200) Or test ' ie until Line D0 goes LOW Label2.Caption = CountValue End Sub |
STEP 4 In the Module Code window type:
Public Declare Sub PortOut Lib
"io.dll" (ByVal Port As Integer, ByVal Value As Byte)
Public Declare Function PortIn Lib "io.dll" (ByVal Port As Integer) As
Byte
Make sure you have io.dll from: http://www.geekhideout.com in the C:\WINDOWS\SYSTEM directory.
STEP 5 Attach your interface devices and run your programme.
