Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vb code to determine whether lpt1 is connected to a printer

21 views
Skip to first unread message

Avi

unread,
Mar 29, 2004, 10:33:40 AM3/29/04
to
hi,

would anyone know how to tell through vb whether LPT1 is connected to
a printer. I am really after some code that would check the port's
connection rather than use the printers object -- the printers object
can include a shared printer that is not connected to LPT1 and that is
why it's not a good indicationo whether LPT1 is in use.


Any assistance willbe appreciated

Thank you in adavance

Avi

MikeD

unread,
Mar 29, 2004, 7:38:39 PM3/29/04
to

"Avi" <avis...@optonline.net> wrote in message
news:ab052923.04032...@posting.google.com...

> hi,
>
> would anyone know how to tell through vb whether LPT1 is connected to
> a printer. I am really after some code that would check the port's
> connection rather than use the printers object -- the printers object
> can include a shared printer that is not connected to LPT1 and that is
> why it's not a good indicationo whether LPT1 is in use.


Just a guess, but perhaps just attempting to open the LPT1 port via normal
VB I/O statements (namely, the Open statement) would work. You might have
to send a printer code or something and possibly (probably) trap for an
error. There might be an API way to detect whether a printer is actually
connected to a specific port (I'd imagine the printer would have to be
online; otherwise, it probably wouldn't be considered connected), but I
don't know what it is.

You might try searching www.google.com. I'd be willing to bet this question
has been asked a number of times. I just don't happen to remember it being
asked, or else I'd point you to a definitive answer.

Mike


Randy Birch

unread,
Mar 29, 2004, 11:16:13 PM3/29/04
to
Get this code and construct the demo as shown ...
http://vbnet.mvps.org/code/enums/enumprinters.htm

Then, make these modifications:

ADD:
'new printer level UDT and const:

Public Const PRINTER_LEVEL5 = &H5

Public Type PRINTER_INFO_5
pPrinterName As Long
pPortName As Long
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type


CHANGE:

- Dim ptr() as PRINTER_INFO_4 to PRINTER_INFO_5
- Two occurances of const PRINTER_LEVEL4 to PRINTER_LEVEL5 in the API calls
- One occurance of GetStrFromPtrA(.pServerName) to
GetStrFromPtrA(.pPortName)


Your output will now look like:

PDFCreator PDFCreator: local
HP LaserJet 5Si MX LPT1: local
CutePDF Printer CUTEPT1: local
Canon i950 USB001 bidirectional local offline
--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Avi" <avis...@optonline.net> wrote in message
news:ab052923.04032...@posting.google.com...

: hi,

Avi

unread,
Apr 4, 2004, 4:36:55 PM4/4/04
to
Thanks for both of you, Mike and Randy. Mike's idea is sort of what I
had in mind to use a 'open lpt1' sort of statement and do the
checking. In fact, as I will explain later in this post, I had
already tried this without success. Randy, I was impressed by the
reservoir of important utilities/functions in the website you referred
me to, especially the code you provided me with. Moreover, I did what
you have suggested. With your help I'm almolst there. I used the
code you gave me plus the modifications ( without really understanding
about printer related API calls), and it was partially successful. To
be more specific, I tried to create a control environment, so to
speak, where I turned off and on connected printers, and the report
your code displayed was partially accurate whenever I selectively
unplugged printers. For example, when I run the vb code from my
machine, making sure that a printer connected to another computer on
the network is OFFLINE(i.e., unplugging line going into printer), the
report displayed (on the listbox) indicates that my computer is
connected to that printer as "local shared" through USB001 and there
is no mention of the printer being offline. I would expect in this
situation that the report will say that the printer is offline. Also,
should not the report indicate that the printer is a 'net
shared'(private network) printer rather than local shared. Since I'm
new to 'the printers world' it might be that my expectations are
wrong, let alone I am not so familiar with some of the terminology
(e.g., bidirectional,net). Another example: that same printer
mentioned above was actually added to my computer as local printer.
Having said that, once transferred from a status of shared
printer(through WORKGROUP setup) to local printer, I can still see
that printer on the report, since I have not removed it ( through
Start-->Control Settings-->Printers ). Indeed the report showed that
printer as local (why both local?) shared printer twice under two
different names. THe strange thing is that when I disconnect the line
from my computer to that printer (the local printer), the report
indicates that the printer is offline and at the same time it also
indicates that this same printer -- the one used to be connected
through another computer as a shared one -- is offline. So at one
point the report is wrong, if my understanding is good, by saying that
a disconected printer is NOT offline, and at another point, when I
disconnect that same printer ( in local connection) is connected, the
report is accurate by maintaining that both printers are offline, the
local one I disconected and the one that is always disconected. I'm
sorry if I explain myself in a way that just makes things more
complicated, and I hope you did not give up one me. Another thing to
mention is that when I disconnect a totally different but local
printer from my computer (either by unpluggimh or just by switching it
off), the report does not indicate of that printer being offline.

Now I think I should provide more informatuion about the context of
the need to be able to determine what printers are connected to what
port and whether ther are switched off : I'm very near to the end of a
VB application that is actually a point of sale application. One
printer, the receipt printer, is assumed to be connected to LPT1;
another printer can be connected to the USB or it can be connected to
another computer and uused as shared computer by all computers in the
WORKGROUP setup. One thing that happnes is that whenever a receipt is
printed, a cash drawer, that is connected through a line (similar to
telephone line) to the receipt printer, is opened. There are some
cases when the printer is off (e.g, application user did not make sure
the printer is connected) In such cases the application will freeze,
since the code responsible for opening the cash drawer -- and I'm
not sure about my terminology -- by printing 'something' to the
receipt printer, which, in turn recognizes that 'something' as a sign
to cause the cash drawer to open, is assuming the cash drawer is
there. Whereas I can use error handling technics (i.e., on error
goto...) to handle the case of printing a receipt with a printer that
is switched off or is unplugged, I can not apply this error handling
technique with respect to the drawer. I NEED to know if it's there
before I try to open it. Since the drawer is connected to the receipt
printer, the only way I can tell, as far as know, wheteher it's
connected is to check the connection with the printer. Idealy, I
would be able to determine if the drawer is connected, but that, I
believe is hard becuase there is no direct connection between the
drawer and the register. Here is the code to open the drawer that is
connected to a printer on LPT1:

Open "LPT1" For Output As #1


Print #1, Chr(27) & Chr(112) & Chr(0) & Chr(25) & Chr(250)


Close #1

This snippet of code bring about 'application freezing' if the drawer
is not connected.

if you are still with me, then I hope something comes into your mind
with respect to the report and th problem in general, and i would
definitely like to hear what you have to say, Mike and Randy.

once again thanks you all for the time and effort. I really
appreciate that.

regards

Avi

0 new messages