Home Parallel Port Serial Port Serial Info Parallel Info USB .NET Webboard I/O Today API Project Directory

 Hardware Interfacing and TCP/IP interfacing in VB programming.

Interfacing Parallel Port with Visual Basic Part 1
Thai Page Click Here
     We can program by using Visual Basic in order to control hardware via Parallel Port with both output and input. Visual Basic canít directly interface with hardware because it must be used DLL (Dynamic Link Library) to help in order to interface with port.         


     From researching the relevant websites and books about the ability of Visual Basic in direct interfacing with port, it hasnít been found. Even when Win32 API (Application Program Interface) which is the book about API function, it about create Dll (Dynamic Link Library) file to help, so I consider that function that interface with port from other language to program DII such as VC++,C++,Pascal, etc. In this article I program by using Visual C++, If you want to know about programming DII file, you can learn from this MS VC++ creating DLL file to interface with Computer Port 

Interfacing VB Program with hardware via Parallel Port in the 2 ways

† †† †1. 1. Interfacing via Printer Port
† †† †2. 2. Interfacing via Card I/O IC 8255

       I will present the first one, interfacing via Printer Port in order to make understanding and can do by yourself. First of all, you must download File DLL at Application topic, named I/O Port Dll File Click Here , after that copy fill DllPort.Dll and paste at Directory Windows\System on your computer.

† †† Reference to Address Printer Port
† †† Using function Inp32 and Out32 extremely need to reference to Address Printer Port in order to verify and send value in each bit of Printer Port that will be divided into 3 parts,

1. Data is the part that send data, 8 bit to Printer (also computer) that we will control signal in each bit in order to connect with printer for reference position of Data Port that will equal to base printer port position.
† †† LPT1 Data=H378
† †† LPT2 Data=H278
† †† 2) Status is the part that verifies the status of printer such as the printer is error, out of paper, sending signal to work, etc. We can use Status Port to control equipment as Input (+5 Vdc) for reference of Data Port Data Port will equal to the position of base printer port+1.
† †† LPT1 Status=H379 (H378+1)
† †† LPT2 Status=H279 (H278+1)
† †† 3) Control is the part that control working of printer such as Strobe, Auto linefeed, Select printer, etc. It can receive and sent data, 6 bit for reference position of Data Port will be equal to the position of base printer port+1
† †† LPT1 Status=H37A(H378+2)
† †† LPT2 Status=H27A(H278+2)

    Start programming
†   Firstly, you create New Project that chooses Standard EXE, VB Program will create a blank form. After that click menu Project ---> Add Module to create a module file that has type, .BAS in order to give API Function of Dll in module of visual basic for programming that is used several form in Project and for convenient in use with other program. If you will use just a form, you must not create module file, you can declare in form. The type that is used in module is below.

    Output Port

† †† †Public Declare Sub Out Lib "DllPort.dll" Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

     Input Port

† †† †Public Declare Function Inp Lib "DllPort.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Integer

If you program just a form, you must use module by changing from Public to Private in the following;

    Output Port - Private Declare Function

† †† †Private Declare Sub Out Lib "DllPort.dll" Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

    Input Port - Private Declare Function

† †† †Private Declare Function Inp Lib "DllPort.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Integer

     Programming by using Function Out and Inp
† ††After declaring function, this step, you must not be interested in module, but you go to form that you save as .FRM type. First of all, we will make understanding about value that reference to address of printer port by going to View Codeís Mode, declaring variable in form like this;

† †† † Dim PortData ' variable for data at port
†Const AddressLPT1 =&H378 'Constant for position of Printer Port 1(LPT1) and Data Port
† †† † Const Statusport1 =AddressLPT1+1 ' Constant for position of Status Port(LPT1))
† †† †Const Controlport1 =AddressLPT1+2 ' Constant for position of Control Port (LPT1))
† †† Const AddressLPT2 =&H278 ' Constant for position of Printer Port 2(LPT2)ŠŇ–Data Portt
†† †† †Const Statusport2 =AddressLPT2+1 ' Constant for position of Status Port(LPT2)
†† †† †Controlport2 =AddressLPT2+2 ' Constant for position of Control Port (LPT2))

† †† So now, we will create form of sending and getting from Parallel Port in program Visual Basic, clicking Control from Control Dialog, is a TextBox, a Label and a Command Button to put in form in the following;

† †† †

For sending value via Parallel Port, you can do like this;

Call Out (PortAddress,Data)

For getting value at Parallel Port

Inp (PortAddress)

Sending output signal
† †† †So now, we will write code in control, starting to send value to control Parallel Port. You must put in Textbox and send value out by clicking at Command Button, for getting value at port will show label. All I mention are written in View Code in Control Command Button.

Private Sub Command1_Click()
† †† †PortData = Val("&H" & Text1.Text) 'count value in Textbox to put in variable PortData
† †† Call Out(AddressLPT1, PortData) ''send to Port
† †† Label1.Caption = Hex(Inp(AddressLPT1)) ''read value from I/O Port LPT1 as Hexadenary
End Sub

† †† Sending value of PortAddress at Printer Port =&H378 Data that is value that send to Printer Port by sending by use Hexadenary, but port will transform data as binary numeral system that has 8 bit such as Hexadenary &H0F at Port =000011112 of binary numeral system.

The table of Hexadenary that control bit that is out via port

Bit No.

Hex

Binary

1
2
3
4
5
6
7
8
All On
All Off

&H01
&H02
&H04
&H08
&H10
&H20
&H40
&H80
&HFF
&H00

00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
11111111
00000000

  The first one, will be signal as TTL about 15 volt, it can use as LED, ,Transistor, Relay, carís motor of children, etc. that will have value of LED at 1,2,3,4 in the right side, we may experiment by taking 8 of LED to connect as serie by resistor =1 k ohm, then connect to Printer Port, read Connectors in topic Hardware , menu Parallel Port Detail click here will make you clear about I/O Port Connectors, here, we will use connector of 2-9 as I/O Data and choose one in 18-25 as ground, so we can experiment to transfer with outputs device. Parallel Port Detail Click Here

 

   On the picture, I use 3 of LED8, 3 row and switch that this experiment I use with Card I/O Parallel Port IC 8255 that has 9 port of Port I/O or 72 bit that has TTL 0-5 volt. For switch, it has been tested for input signal that I separate into another web page.

Programming to get input signal from Printer Port

  
Learning outcome of this article

† ††1. To know how to program by using Visual Basic to control working of I/O Parallel Port
2. To know how to apply File DLL to help you for programming due to you canít directly program to control Visual Basic.
3. To know how to program by using API from using DllPort.Dll file.
4. To know how to use Printer Port except connecting with Printer.



Copyright (c) 2000-2008 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com