Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'subject:Python': 0.05; 'binary': 0.05; 'preferably': 0.05; '21,': 0.07; 'ascii': 0.07; 'column': 0.07; 'skip:\\ 10': 0.07; 'try:': 0.07; 'python': 0.09; '22,': 0.09; 'integers': 0.09; 'notation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:b 70': 0.09; 'throw': 0.09; '2.7': 0.13; 'aug': 0.13; '"f"': 0.16; '122,': 0.16; '18:': 0.16; '250:': 0.16; '63,': 0.16; '89,': 0.16; 'columns': 0.16; 'hex': 0.16; 'hex:': 0.16; 'literal.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; "skip:' 60": 0.16; 'byte': 0.17; 'bytes': 0.17; 'thu,': 0.17; '>>>': 0.18; 'bit': 0.21; 'import': 0.21; '31,': 0.22; 'oriented': 0.22; '15,': 0.23; 'random': 0.24; 'skip:[ 10': 0.26; 'guess': 0.27; 'converting': 0.27; 'header:X-Complaints- To:1': 0.28; 'represent': 0.28; '255,': 0.29; 'subject:Sending': 0.29; 'probably': 0.29; "i'm": 0.29; 'point': 0.31; 'problem.': 0.32; 'print': 0.32; 'url:home': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; "can't": 0.34; 'along': 0.35; 'something': 0.35; 'received:org': 0.36; 'wanted': 0.36; 'data.': 0.36; 'subject:with': 0.36; 'should': 0.36; 'previous': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'range': 0.60; 'lower': 0.61; 'biggest': 0.71; '250,': 0.84; 'interesting,': 0.84; 'printer': 0.84; 'subject:commands': 0.84; 'treats': 0.84; 'dennis': 0.91; 'time)': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Sending USB commands with Python Date: Thu, 30 Aug 2012 14:24:02 -0400 Organization: > Bestiaria Support Staff < References: <20120829222932.GA18700@cskk.homeip.net> <5d063028-3d3f-42f2-8787-b33d58460c35@googlegroups.com> <20c77648-7b1f-4a45-82de-c8721c834394@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-76-253-102-254.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346351050 news.xs4all.nl 6949 [2001:888:2000:d::a6]:59088 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28139 On Thu, 30 Aug 2012 05:51:56 -0700 (PDT), "Adam W." 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/