sending commands to parallel printer through python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 5 09:59:51 EST 2007


En Wed, 05 Dec 2007 10:19:51 -0300, hari <haricibi83 at gmail.com> escribi�:

> 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




More information about the Python-list mailing list