By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

sending commands to parallel printer through python

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.