Solved

How to access lpt from visual basic

Posted on 2001-06-06
2
1,267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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