[Tutor] More confusion on conversion
Reggie Dugard
reggie at merfinllc.com
Thu Oct 30 16:09:46 EST 2003
In my last mail I had a bit of a brain freeze :-(. '0' is not 0, but is
in fact 48 or 0x30 or 00110000. This means that
port.write('\x30')
port.write('\x41')
should be equivalent to
port.write('0A')
which is equivalent to
length = 10
port.write('%02X' % length) # one of my previous suggestions
so I don't see why that isn't working for you. Hopefully someone else
can see the error of my ways and point you in the right direction.
Reggie
On Thu, 2003-10-30 at 12:53, Stanfield, Vicki {D167~Indianapolis} wrote:
> Probably I am the one being dense, but I know that the device on the other end expects what it calls a "hex value represented as ASCII." This means that if I want to send a decimal 10, I first have to convert decimal 10 to hex 'A' and then I have to take the hex of the letter 'A' as if it were ASCII and send that. In this example, I need to send 0x30 0x41 (as two bytes of data). In this example, doing this:
>
> port.write('\x30')
> port.write('\x41')
>
> is the equivalent of what I want. Of course, I have to make it handle lengths other than 10 too.
>
> Is that any clearer?
>
> --vicki
>
> -----Original Message-----
> From: Reggie Dugard [mailto:reggie at merfinllc.com]
> Sent: Thursday, October 30, 2003 3:47 PM
> To: Stanfield, Vicki {D167~Indianapolis}
> Cc: tutor at python.org
> Subject: RE: [Tutor] More confusion on conversion
>
>
> Vicki,
>
> Forgive me if I'm being dense, but how many bytes do you want to write
> out on the port for each length? If the answer is 2, then writing '0'
> and 'A' is equivalent to writing 0 and 65 which is equivalent to writing
> 0x00 and 0x41 - they all have the same bit pattern: 00000000 01000001.
>
> Sorry if I'm misunderstanding you.
>
> Reggie
>
> On Thu, 2003-10-30 at 12:24, Stanfield, Vicki {D167~Indianapolis} wrote:
> > Nope, this write '0A' out one the port. I need to write the hex value of the 0 and then the hex value of the 'A'. WHen I tried this:
> >
> > for each in [ hex(ord(x)) for x in '%02X' % length ]:
> > port.write('%02X' %each)
> > time.sleep(.01)
> > print '%02X' %each
> >
> > It was close, but it wants an integer instead of the hex values.
> >
> > port.write('%02X' %each)
> > TypeError: an integer is required
> >
> > --vicki
> >
> > -----Original Message-----
> > From: Reggie Dugard [mailto:reggie at merfinllc.com]
> > Sent: Thursday, October 30, 2003 3:09 PM
> > To: Stanfield, Vicki {D167~Indianapolis}
> > Cc: tutor at python.org
> > Subject: RE: [Tutor] More confusion on conversion
> >
> >
> > Vicki,
> >
> > If I understand you correctly you simply want something like:
> >
> > port.write('%02X' % length)
> >
> > This should write 2 bytes on the port, one for each hex digit. If I'm
> > misunderstanding what you want, maybe you can clarify it a bit more.
> >
> > Also, I'm sending this back to the list so that people more
> > knowledgeable than me can help you out as well.
> >
> > Good luck with your problem.
> >
> > Reggie
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Reggie
More information about the Tutor
mailing list