Solved

How to access lpt from visual basic

Posted on 2001-06-06
2
1,240 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now