Solved

How to access lpt from visual basic

Posted on 2001-06-06
2
1,258 Views
Last Modified: 2013-11-25
I want to write value to parallel port (lpt)
How can I do it in win NT an 95

thanks in advance

0
Comment
Question by:rdima
2 Comments
 

Accepted Solution

by:
byte_me earned 100 total points
ID: 6159120
Hi there Rdima!

are you using win 9x or NT/2000? well if your running Win9x then you need a dll called inpout32.dll,
download
it from this site... http://www.pcgadgets.com/download.html make sure you put the dll in the system32 dir.

You will need two .bas modules, the first should contain this code.....

'Declare Inp and Out for port I/O
'Two versions, for 16-bit and 32-bit Visual Basic 4 programs.

#If Win32 Then
'DLL procedure names are case-sensitive in VB4.
'Use Aliases so Inp and Out don't have to have matching case in VB.
Public Declare Function Inp Lib "inpout32.dll" _
Alias "Inp32" (ByVal PortAddress As Integer) As Integer
Public Declare Sub Out Lib "inpout32.dll" _
Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

#Else
Public Declare Function Inp Lib "inpout16.Dll" _
Alias "Inp16" (ByVal PortAddress As Integer) As Integer
Public Declare Sub Out Lib "inpout16.Dll" _
Alias "Out16" (ByVal PortAddress As Integer, ByVal Value As Integer)
#End If


and the second should contain this....

'Lptutil.bas
'General purpose routines for use with the PC's parallel port.
'Port accesses require a DLL with Inp and Out.
Option Explicit

Function DataPortRead%(BaseAddress%)
'Reads a parallel port's data port.
DataPortRead = Inp(BaseAddress)
End Function

Sub DataPortWrite(BaseAddress%, Value%)
'Writes a byte to a parallel port's data port.
Out BaseAddress, Value
End Sub

Function StatusPortRead%(BaseAddress%)
'Reads a parallel port's status port.
'Calculates the status-port address from the port's
'base address, and inverts bit 7 of the byte read.
'The status-port hardware reinverts these bits,
'so the value read matches the value at the connector.
StatusPortRead = (Inp(BaseAddress + 1) Xor &H80)
End Function

Function BitRead%(Variable%, BitNumber%)
'Returns the value (0 or 1) of the requested bit in a Variable.
Dim BitValue%
BitValue = 2 ^ BitNumber 'the value of the requested bit
BitRead = (Variable And BitValue) \ BitValue
End Function

Sub BitReset(Variable%, BitNumber%)
'Resets (clears) the requested bit in a Variable.
Dim BitValue, CurrentValue%
'The value of the requested bit.
BitValue = 2 ^ BitNumber
Variable = Variable And (&HFFFF - BitValue)
End Sub

Sub BitSet(Variable%, BitNumber%)
'Sets the requested bit in a Variable.
Dim BitValue, CurrentValue%
'The value of the requested bit.
BitValue = 2 ^ BitNumber
Variable = Variable Or BitValue
End Sub

Sub BitToggle(Variable%, BitNumber%)
'Toggles the requested bit in a Variable.
Dim BitValue, CurrentValue%
'The value of the requested bit.
BitValue = 2 ^ BitNumber
'Is the current value 0 or 1?
CurrentValue = Variable And BitValue
Select Case CurrentValue
  Case 0
      'If current value = 0, set it
      Variable = Variable Or BitValue
  Case Else
      'If current value = 1, reset it
      Variable = Variable And (&HFFFF - BitValue)
End Select
End Sub

Function ControlPortRead%(BaseAddress%)
'Reads a parallel port's control port.
'Calculates the control-port address from the port's
'base address, and inverts bits 0, 1, & 3 of the byte read.
'The control-port hardware reinverts these bits,
'so the value read matches the value at the connector.
ControlPortRead = (Inp(BaseAddress + 2) Xor &HB)
End Function

Sub ControlPortWrite(BaseAddress%, Value%)
'Writes a Value to a parallel port's control port.
'Calculates the control-port address from the port's
'base address, and inverts bits 0, 1, & 3.
'The control-port hardware reinverts these bits,
'so Value is written to the port connector.
Out BaseAddress + 2, Value Xor &HB
End Sub


the secode .bas file contains the functions you need to use to communicate with the port, set the base

address to 888 for the 378h address and pass the byte you want. to use the 8 data pins as inputs and

outputs make sure you set the port to 'bi-directional' mode first.

these functions give you easy access to the Control, status and data port. i have used then in a win

9x os without any errors.  If you are using win NT then download this control... directio from http://www.advantech.com/support/down_model.htm. it comes

with example code and documentation.

When sending the information to the lpt port make sure you use some form of Analogue-to-digital converter
for the signal coming into the lpt port.... it can only accept digital data.



Byte me.
0
 
LVL 1

Expert Comment

by:dalexis
ID: 6159734
If you need to just send text to the printer port, you can either use the Printer object that's native to VB, or you can use the old file I/O method of simply Opening the "lpt1:" (or whatever port number) for output, then using the Print #<file number> statement to send data.

Binary data might be a different story, though. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question