Direct Printing to port 
Author Message
 Direct Printing to port

Open "COM1:" for output as #1
Print #, "This is a test"
Close 1

This will print to a printer attached to COM1

I have 2 questions...

What if the printer is attached to a USB port?

The other question is... is there anyway to use this method print a picture
jpg

Something like
Open "COM1:" for output as #1
Print #, Print a jpg file
Close 1

I will appreciate any help... Thanks in advance



Thu, 30 Jun 2011 01:27:36 GMT  
 Direct Printing to port

Quote:
> Open "COM1:" for output as #1
> Print #, "This is a test"
> Close 1

> This will print to a printer attached to COM1

> I have 2 questions...

> What if the printer is attached to a USB port?

You could enable "Print directly to the printer" and use the Printer object.
See this article:

http://en.wikipedia.org/wiki/Printer_Command_Language

Quote:
> The other question is... is there anyway to use this method print a
> picture jpg

Yes, but it's complicated. It's basically for printing text only. If you
want graphics, you need to talk to the printer to activate printing graphics
line by line by using escape character(ASCII 27) to send commands, or use
postscript if the printer understand it, etc. However, why not use the
Printer object which does this for you automatically? It's what Windows use
by default, which basically treat everything, including text as pictures, so
the printer doesn't have to support a particular font. Here is an example
using VB6:

Option Explicit

Private Sub Form_Load()
    Dim pic As StdPicture

    Set pic = LoadPicture("C:\test.jpg")
    Printer.PaintPicture pic, 0, 0
    Printer.EndDoc

End Sub

If you want to see the picture in a PictureBox first, then replace "pic"
above with "Picture1.Picture".



Thu, 30 Jun 2011 02:11:30 GMT  
 Direct Printing to port


Quote:


>> Open "COM1:" for output as #1
>> Print #, "This is a test"
>> Close 1

>> This will print to a printer attached to COM1

>> I have 2 questions...

>> What if the printer is attached to a USB port?

> You could enable "Print directly to the printer" and use the Printer
> object. See this article:

> http://en.wikipedia.org/wiki/Printer_Command_Language

>> The other question is... is there anyway to use this method print a
>> picture jpg

> Yes, but it's complicated. It's basically for printing text only. If you
> want graphics, you need to talk to the printer to activate printing
> graphics line by line by using escape character(ASCII 27) to send
> commands, or use PostScript if the printer understand it, etc. However,
> why not use the Printer object which does this for you automatically? It's
> what Windows use by default, which basically treat everything, including
> text as pictures, so the printer doesn't have to support a particular
> font. Here is an example using VB6:

> Option Explicit

> Private Sub Form_Load()
>    Dim pic As StdPicture

>    Set pic = LoadPicture("C:\test.jpg")
>    Printer.PaintPicture pic, 0, 0
>    Printer.EndDoc

> End Sub

> If you want to see the picture in a PictureBox first, then replace "pic"
> above with "Picture1.Picture".

Thanks for your help... The problem with printer object (in the case of
transaction/receipt printer is they print awfully slow if connected to Com:
port. Using specific printer command (to print a picture) tends to narrow
down enduser receipt printer choice to only those supported by the
application. Anyway; I appreciate your help.


Thu, 30 Jun 2011 05:04:48 GMT  
 Direct Printing to port

Quote:
> Open "COM1:" for output as #1
> Print #, "This is a test"
> Close 1
> This will print to a printer attached to COM1
> I have 2 questions... What if the printer is attached to a USB port?

You can send data directly to the port of any installed printer using the
OpenPrinter function in winspool.drv and associated functions (see example
at the end of this post which sends data to whatever happens to be the
current VB printer object).

Quote:
> The other question is... is there anyway to use this
> method print a picture jpg

Yes, but it is complicated and you need to know the precise details of the
printer you are using and the various Esc functions it accepts and of its
various graphic modes and how the data needs to be arranged for each mode,
information which is not nomally supplied any more these days when you
purchase a printer and which you will need to search for. I remember many
years ago when virtually all printers came with a full hundred or more page
manual containing all these details, but I'm afraid those days are long
gone. You would essentially putting yourself back to the old DOS days when
eacg DOS application would need to individually know the full details of
every printer you might wish to print to. It is possible, but complicated.
Better to use the installed drivers for each printer by using standard
Windows (or VB) page printing methods.

Anyway, here is an example of printing straight forward text, which is easy.
You will still need to know details of the various Esc sequences your
printer accepts, and the various emulations it can perform and how it can be
switched to them, if you wish to perform other tasks, and the various text
control sequences if you wish to control the printer font names or sizes.

As Expvb has already said, why cannot you just use the VB printer object.
What problems are you having that require you to talk to the printer
hardware directly? Anyway, here's an example of printing simple text:

Mike

Option Explicit
Private Declare Function OpenPrinter Lib "winspool.drv" _
  Alias "OpenPrinterA" (ByVal pPrinterName As String, _
  phPrinter As Long, ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" _
  Alias "StartDocPrinterA" (ByVal hPrinter As Long, _
  ByVal Level As Long, pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long, pBuf As Any, _
  ByVal cdBuf As Long, pcWritten As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" _
  (ByVal hPrinter As Long) As Long
Private Type DOCINFO
 pDocName As String
 pOutputFile As String
 pDatatype As String
End Type

Private Sub Command1_Click()
Dim printerHandle As Long, retVal As Long
Dim bytesWritten As Long, lDoc As Long
Dim s1 As String, MyDocInfo As DOCINFO
retVal = OpenPrinter(Printer.DeviceName, printerHandle, 0)
If retVal = 0 Then
  MsgBox "Printer Not found"
Exit Sub
End If
MyDocInfo.pDocName = "Any Name"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(printerHandle, 1, MyDocInfo)
Call StartPagePrinter(printerHandle)
s1 = "This is the first line of text" & vbCrLf _
  & "And this is the second line" & vbFormFeed
retVal = WritePrinter(printerHandle, ByVal s1, _
    Len(s1), bytesWritten)
retVal = EndPagePrinter(printerHandle)
retVal = EndDocPrinter(printerHandle)
retVal = ClosePrinter(printerHandle)
End Sub



Thu, 30 Jun 2011 05:12:29 GMT  
 Direct Printing to port
Thanks Mike, Appreciate it


Quote:


>> Open "COM1:" for output as #1
>> Print #, "This is a test"
>> Close 1
>> This will print to a printer attached to COM1
>> I have 2 questions... What if the printer is attached to a USB port?

> You can send data directly to the port of any installed printer using the
> OpenPrinter function in winspool.drv and associated functions (see example
> at the end of this post which sends data to whatever happens to be the
> current VB printer object).

>> The other question is... is there anyway to use this
>> method print a picture jpg

> Yes, but it is complicated and you need to know the precise details of the
> printer you are using and the various Esc functions it accepts and of its
> various graphic modes and how the data needs to be arranged for each mode,
> information which is not nomally supplied any more these days when you
> purchase a printer and which you will need to search for. I remember many
> years ago when virtually all printers came with a full hundred or more
> page manual containing all these details, but I'm afraid those days are
> long gone. You would essentially putting yourself back to the old DOS days
> when eacg DOS application would need to individually know the full details
> of every printer you might wish to print to. It is possible, but
> complicated. Better to use the installed drivers for each printer by using
> standard Windows (or VB) page printing methods.

> Anyway, here is an example of printing straight forward text, which is
> easy. You will still need to know details of the various Esc sequences
> your printer accepts, and the various emulations it can perform and how it
> can be switched to them, if you wish to perform other tasks, and the
> various text control sequences if you wish to control the printer font
> names or sizes.

> As Expvb has already said, why cannot you just use the VB printer object.
> What problems are you having that require you to talk to the printer
> hardware directly? Anyway, here's an example of printing simple text:

> Mike

> Option Explicit
> Private Declare Function OpenPrinter Lib "winspool.drv" _
>  Alias "OpenPrinterA" (ByVal pPrinterName As String, _
>  phPrinter As Long, ByVal pDefault As Long) As Long
> Private Declare Function StartDocPrinter Lib "winspool.drv" _
>  Alias "StartDocPrinterA" (ByVal hPrinter As Long, _
>  ByVal Level As Long, pDocInfo As DOCINFO) As Long
> Private Declare Function StartPagePrinter Lib "winspool.drv" _
>  (ByVal hPrinter As Long) As Long
> Private Declare Function WritePrinter Lib "winspool.drv" _
>  (ByVal hPrinter As Long, pBuf As Any, _
>  ByVal cdBuf As Long, pcWritten As Long) As Long
> Private Declare Function ClosePrinter Lib "winspool.drv" _
>  (ByVal hPrinter As Long) As Long
> Private Declare Function EndDocPrinter Lib "winspool.drv" _
>  (ByVal hPrinter As Long) As Long
> Private Declare Function EndPagePrinter Lib "winspool.drv" _
>  (ByVal hPrinter As Long) As Long
> Private Type DOCINFO
> pDocName As String
> pOutputFile As String
> pDatatype As String
> End Type

> Private Sub Command1_Click()
> Dim printerHandle As Long, retVal As Long
> Dim bytesWritten As Long, lDoc As Long
> Dim s1 As String, MyDocInfo As DOCINFO
> retVal = OpenPrinter(Printer.DeviceName, printerHandle, 0)
> If retVal = 0 Then
>  MsgBox "Printer Not found"
> Exit Sub
> End If
> MyDocInfo.pDocName = "Any Name"
> MyDocInfo.pOutputFile = vbNullString
> MyDocInfo.pDatatype = vbNullString
> lDoc = StartDocPrinter(printerHandle, 1, MyDocInfo)
> Call StartPagePrinter(printerHandle)
> s1 = "This is the first line of text" & vbCrLf _
>  & "And this is the second line" & vbFormFeed
> retVal = WritePrinter(printerHandle, ByVal s1, _
>    Len(s1), bytesWritten)
> retVal = EndPagePrinter(printerHandle)
> retVal = EndDocPrinter(printerHandle)
> retVal = ClosePrinter(printerHandle)
> End Sub



Thu, 30 Jun 2011 05:43:59 GMT  
 Direct Printing to port

Quote:
> Open "COM1:" for output as #1
> Print #, "This is a test"
> Close 1

> This will print to a printer attached to COM1

> I have 2 questions...

> What if the printer is attached to a USB port?

> The other question is... is there anyway to use this method print a picture
> jpg

> Something like
> Open "COM1:" for output as #1
> Print #, Print a jpg file
> Close 1

> I will appreciate any help... Thanks in advance

JP , you say that if you try to print to the receipt printer via the
VB printer object, the printing is slow, I would like to just address
that problem (because it is not a problem of the Receipt Printer and
it is not a problem of the VB object either, it is something else,
pretty easy to fix)...

The VB printer object has a wacky - difficult for print font selected
as a default.
Believe it or not, sometimes different printers have difficulties
printing different fonts and they have a group of their own preferred
fonts, that they print very fast with.
So, most of the time, this will be the case and the certain printer,
that you have, will simply have a hard time printing fast, with the
font that is selected in the VB printer object. To remedy this, all
you need to do, is find your printer's preferred fonts, from the
printer manual and try some of those fonts, by setting them as the
preffered printing font in the VB printer object.

You can even go further and not even use Windows fonts, by using "text
only" drivers for the printer, if none of the Windows fonts improve
your printing speed up to your satisfaction.
For example, here is what a certain "printer manufacturer" - tech
support person told us, as a good tip in this situation (this pertains
to certain Epson and Epson compatible brand printers):
- "I wanted to let you know that, for the fastest print, your
customers can use the font name "FONTA1x1". This is for the Vendor
Thermal Driver. This Font will act as a Printer font, rather than
Windows Font, so because it is printing in Text mode, it prints much
faster than other fonts. For larger font sizes, they can also try
FONTA2x1 (which is double high, single wide). When using these
"FONTxxx" fonts, the "Font Size" input will not work, as there is only
one font size available for each of these font names. The best way to
find the font name is to open up Wordpad, and look at the drop-down
font list. Anything with an icon next to it will print in image mode;
all the other ones (13cpi, FontA1x1, etc.) will print in text mode."

Support,
www.AlexandriaComputers.com



Thu, 07 Jul 2011 09:49:51 GMT  
 Direct Printing to port
Thanks for the tip... appreciate it. I am trying out the suggestions. Thank
you



Quote:
> Open "COM1:" for output as #1
> Print #, "This is a test"
> Close 1

> This will print to a printer attached to COM1

> I have 2 questions...

> What if the printer is attached to a USB port?

> The other question is... is there anyway to use this method print a
> picture
> jpg

> Something like
> Open "COM1:" for output as #1
> Print #, Print a jpg file
> Close 1

> I will appreciate any help... Thanks in advance

JP , you say that if you try to print to the receipt printer via the
VB printer object, the printing is slow, I would like to just address
that problem (because it is not a problem of the Receipt Printer and
it is not a problem of the VB object either, it is something else,
pretty easy to fix)...

The VB printer object has a wacky - difficult for print font selected
as a default.
Believe it or not, sometimes different printers have difficulties
printing different fonts and they have a group of their own preferred
fonts, that they print very fast with.
So, most of the time, this will be the case and the certain printer,
that you have, will simply have a hard time printing fast, with the
font that is selected in the VB printer object. To remedy this, all
you need to do, is find your printer's preferred fonts, from the
printer manual and try some of those fonts, by setting them as the
preffered printing font in the VB printer object.

You can even go further and not even use Windows fonts, by using "text
only" drivers for the printer, if none of the Windows fonts improve
your printing speed up to your satisfaction.
For example, here is what a certain "printer manufacturer" - tech
support person told us, as a good tip in this situation (this pertains
to certain Epson and Epson compatible brand printers):
- "I wanted to let you know that, for the fastest print, your
customers can use the font name "FONTA1x1". This is for the Vendor
Thermal Driver. This Font will act as a Printer font, rather than
Windows Font, so because it is printing in Text mode, it prints much
faster than other fonts. For larger font sizes, they can also try
FONTA2x1 (which is double high, single wide). When using these
"FONTxxx" fonts, the "Font Size" input will not work, as there is only
one font size available for each of these font names. The best way to
find the font name is to open up Wordpad, and look at the drop-down
font list. Anything with an icon next to it will print in image mode;
all the other ones (13cpi, FontA1x1, etc.) will print in text mode."

Support,
www.AlexandriaComputers.com



Fri, 08 Jul 2011 04:50:35 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Printing Direct To LPT Port

2. Printing RAW byte direct to port in WIN95

3. printing escape sequences for direct printer control to print micr on checks

4. How to send byte direct to parallel port ?

5. Direct Access to Parallel Port or Via Driver

6. COM port - direct access?

7. Direct Access to the parallel port ?

8. Direct access to i/o ports?

9. Direct parallel port access...?!?

10. Direct acces of Serial Port

11. Direct access to comm port

12. Parallel or serial port control (direct)

 

 
Powered by phpBB® Forum Software