471,354 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

sending commands to parallel printer through python

Hi all,
I need to automate printer command testing, prinetr supports
parallel/
serial/USB.How can i send the commands from python to printer.

I have got pyparallel, as am new to python, no idea how to work on
it.
Please give some tips,The comamnd to be sent to the printer is hex
data "1B 40".please give a example,it will be grateful.
Thank you. postedthe same in it.comp.lang.python
Regards
-Hari
Dec 5 '07 #1
2 4062
hari wrote:
Hi all,
I need to automate printer command testing, prinetr supports
parallel/
serial/USB.How can i send the commands from python to printer.

I have got pyparallel, as am new to python, no idea how to work on
it.
Please give some tips,The comamnd to be sent to the printer is hex
data "1B 40".please give a example,it will be grateful.
Thank you. postedthe same in it.comp.lang.python
Regards
-Hari

If the printer is connected to the computer running the program you can open the
printer in binary mode and write to it directly. If it is a networked printer
you will need to write to the spooler.

printer=open("LPT1", "wb")
printer.write(int('01b', 16), int('40', 16)

printer.close()

-Larry
Dec 5 '07 #2
En Wed, 05 Dec 2007 10:19:51 -0300, hari <ha********@gmail.comescribi�:
Hi all,
I need to automate printer command testing, prinetr supports
parallel/
serial/USB.How can i send the commands from python to printer.

I have got pyparallel, as am new to python, no idea how to work on
it.
Please give some tips,The comamnd to be sent to the printer is hex
data "1B 40".please give a example,it will be grateful.
a) how to control the printer port:
You should look for some info on the protocol and timings. I vaguely
remember that you should write the desired data onto the eight data lines,
then set the STROBE control signal for some time, then reset the signal.
Something like this:

import parallel

port = parallel.Parallel()

def send_to_printer(bytes):
for byte in bytes:
port.setData(ord(byte))
port.setDataStrobe(1)
sleep(...)
port.setDataStrobe(0)
sleep(...)

send_to_printer("Hello world\r\n")

You'll have to look for the right values to sleep in each case. Those 1/0
may be reversed too.

b) how to convert hex data:

The easiest way would be: send_to_printer("\x1B\x40")

If you have the string "1B 40" already built:

def hex_to_raw(hex):
return ''.join([chr(int(num,16)) for num in hex.split()])

data = "1B 40"
send_to_printer(hex_to_raw(data))

--
Gabriel Genellina

Dec 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David | last post: by
3 posts views Thread by Vlki | last post: by
2 posts views Thread by Carlos Pezzotta | last post: by
2 posts views Thread by notregister | last post: by
reply views Thread by notregister | last post: by
12 posts views Thread by david.brown.0 | last post: by
4 posts views Thread by Hector M Banda | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.