Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28139

Re: Sending USB commands with Python

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Sending USB commands with Python
Date 2012-08-30 14:24 -0400
Organization > Bestiaria Support Staff <
References (3 earlier) <ad907349-042b-4ddf-aef7-043d7dfb1f9a@googlegroups.com> <mailman.3952.1346292474.4697.python-list@python.org> <5d063028-3d3f-42f2-8787-b33d58460c35@googlegroups.com> <mailman.3957.1346302513.4697.python-list@python.org> <20c77648-7b1f-4a45-82de-c8721c834394@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3982.1346351050.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 30 Aug 2012 05:51:56 -0700 (PDT), "Adam W."
<AWasilenko@gmail.com> declaimed the following in
gmane.comp.python.general:


> 
> Interesting, so what if I only wanted to send 4bits as a hex value?  Also can I somehow throw in some binary alongside of hex?  At some point in my program I'm going to need to send some commands preferably in hex along with the binary image data.

	You will never be able to send something less than 8-bits. I suspect
the printer treats each byte as one column of dots, so you'd probably be
sending \xF0 or \x0F depending on which bit is "top" or "bottom" -- this
would give a half column of ink and the other half would be blank.

	\x??	is the Python notation to represent a binary value in a
string/byte LITERAL.

	Your previous attempt with the "F" was sending ASCII F characters,
hex value 46 (01000110) [was your striped layout one skinny stripe, and
three widths lower a double-width stripe?].

	As long as your image data is in on/off (B/W) bytes (unsigned, or to
simplify, integers in the range 0..255), it should be no problem. 

>>> import random
>>> data = [random.randint(0, 255) for i in range(32)]
>>> data
[25, 134, 166, 159, 253, 121, 114, 110, 18, 122, 197, 233, 127, 216,
188, 89, 10, 201, 250, 32, 231, 32, 99, 7, 227, 122, 166, 172, 21, 78,
17, 166]
>>> bar = bytearray(data)
>>> bar
bytearray(b'\x19\x86\xa6\x9f\xfdyrn\x12z\xc5\xe9\x7f\xd8\xbcY\n\xc9\xfa
\xe7 c\x07\xe3z\xa6\xac\x15N\x11\xa6')
>>> bytes(bar)
'\x19\x86\xa6\x9f\xfdyrn\x12z\xc5\xe9\x7f\xd8\xbcY\n\xc9\xfa \xe7
c\x07\xe3z\xa6\xac\x15N\x11\xa6'
>>> 
>>> bbar = bytes(bar)
>>> for b in bbar:
... 	print "Decimal: %4.4s:\tHex: \\x%2.2X\t%s" % (ord(b), ord(b), b)
... 	
Decimal:   25:	Hex: \x19	
Decimal:  134:	Hex: \x86	?
Decimal:  166:	Hex: \xA6	?
Decimal:  159:	Hex: \x9F	?
Decimal:  253:	Hex: \xFD	??
Decimal:  121:	Hex: \x79	y
Decimal:  114:	Hex: \x72	r
Decimal:  110:	Hex: \x6E	n
Decimal:   18:	Hex: \x12	
Decimal:  122:	Hex: \x7A	z
Decimal:  197:	Hex: \xC5	?
Decimal:  233:	Hex: \xE9	?
Decimal:  127:	Hex: \x7F	
Decimal:  216:	Hex: \xD8	?
Decimal:  188:	Hex: \xBC	?
Decimal:   89:	Hex: \x59	Y
Decimal:   10:	Hex: \x0A	

Decimal:  201:	Hex: \xC9	?
Decimal:  250:	Hex: \xFA	??
Decimal:   32:	Hex: \x20	 
Decimal:  231:	Hex: \xE7	?
Decimal:   32:	Hex: \x20	 
Decimal:   99:	Hex: \x63	c
Decimal:    7:	Hex: \x07	
Decimal:  227:	Hex: \xE3	?
Decimal:  122:	Hex: \x7A	z
Decimal:  166:	Hex: \xA6	?
Decimal:  172:	Hex: \xAC	°
Decimal:   21:	Hex: \x15	
Decimal:   78:	Hex: \x4E	N
Decimal:   17:	Hex: \x11	
Decimal:  166:	Hex: \xA6	?
>>> 

	You might be able to go directly:

bbar = bytes(data)

based on the documentation for Python 3.2.3. I can't even find bytes()
in the 2.7 documentation.

	I suspect your biggest problem will be in converting your image
format (at the minimum, it may be row oriented and you need to generate
columns of 8-rows at a time)


	Try:

data = [		22,	
			0,	1,	3,	7,	15,	31,	63,	127,	255,
			127,	63,	31,	15,	7,	3,	1,	0	]
bbar = bytes(data)

	Set data length to 17, and send bbar...

	If my guess is right, this should put up an equilateral triangle.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Sending USB commands with Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-29 18:56 -0400
  Re: Sending USB commands with Python "Adam W." <AWasilenko@gmail.com> - 2012-08-29 16:45 -0700
  Re: Sending USB commands with Python MRAB <python@mrabarnett.plus.com> - 2012-08-30 01:53 +0100
  Re: Sending USB commands with Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-29 22:07 -0400
    Re: Sending USB commands with Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-30 00:55 -0400
      Re: Sending USB commands with Python "Adam W." <AWasilenko@gmail.com> - 2012-08-30 05:51 -0700
        Re: Sending USB commands with Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-30 14:24 -0400
        Re: Sending USB commands with Python Cameron Simpson <cs@zip.com.au> - 2012-08-31 08:47 +1000

csiph-web