Discussion:
How can i send 8-bit data or binary data with pyserial?
(too old to reply)
ouz as
2004-12-14 10:31:00 UTC
Permalink
i have an electronic module which only understand binary data.
i use python pyserial.
for example the module starts when 001000000 8-bit binary data sent.but
pyserial sent only string data.
Can i send this binary data with pyserial or another way with python.

Thanks,

_________________________________________________________________
Depolama alani sikintisindan kurtulun - hemen Hotmail'e üye olun!
http://odeme.hotmail.com.tr
Craig Ringer
2004-12-14 11:10:04 UTC
Permalink
Post by ouz as
i have an electronic module which only understand binary data.
i use python pyserial.
for example the module starts when 001000000 8-bit binary data sent.but
pyserial sent only string data.
Can i send this binary data with pyserial or another way with python.
Strings can be considered binary data, where each character represents
eight bits. Simply build the binary data however you're most comfortable
(a list of bytes; a list of bits; a string representation of a binary
numer, an 8-bit string used as a buffer; whatever) then compose into a
string and send.
Post by ouz as
bits = '0101101110111101'
bytes = [ mybits[i*8:(i+1)*8] for i in range(len(mybits)/8) ]
bytechars = [ chr(int(x,2)) for x in bytes ]
bytestr = ''.join(bytechars)
bytestr
'[\xbd'

Obviously, you could tidy up that conversion a little. This being
Python, I wouldn't be surprised if there was a built-in function to do
the conversion ;-) .
Post by ouz as
bits = '0101101110111101'
truthlist = [ int(x) and True or False for x in mybits ]
truthlist
[False, True, False, True, True, False, True, True, True, False, True,
True, True, True, False, True]
Post by ouz as
hexstring = "0x%x" % int(mybits,2)
hexstring
'0x5bbd'
Post by ouz as
hexstring2 = "\x5b\xbd"
hexstring2
'[\xbd'

and you can convert them all to strings. Just remember that you can work
with a string as a buffer of 8-bit blocks and you'll be fine. In your
Post by ouz as
byte = '001000000'
byte_chr = chr(int(byte,2))
byte_chr
'@'

--
Craig Ringer
Fredrik Lundh
2004-12-14 11:13:15 UTC
Permalink
Post by ouz as
i have an electronic module which only understand binary data.
i use python pyserial.
for example the module starts when 001000000 8-bit binary data sent.but
that's nine bits...
Post by ouz as
pyserial sent only string data.
in computers, bits are combined into bytes (or longer machine words).
the strings you pass to pyserial are treated as byte sequences.

this might help you figure out how to convert between bit patterns and
byte values:

http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary.html
Post by ouz as
Can i send this binary data with pyserial or another way with python.
convert the bitpattern to a byte, and send it to pyserial.

01000000 = 0x40 (hex) = 64 (dec)

so

ser.write(chr(64))

or

ser.write(chr(0x40))

or even

ser.write("\x40")

to go from a byte to a bitpattern, use ord(byte):

ord(chr(0x40)) = 0x40

hardware-oriented code often represent bitpatterns as hex constants. by
using constants, it's easy to combine different bits:

FLAG1 = 0x40
FLAG2 = 0x20

ser.write(chr(FLAG1)) # set first flag
ser.write(chr(FLAG1|FLAG2)) # set both flags

flags = getstatus()
ser.write(flags ^ FLAG2) # toggle flag 2

etc.

</F>
Scott David Daniels
2004-12-15 22:24:26 UTC
Permalink
Post by ouz as
i have an electronic module which only understand binary data.
i use python pyserial.
for example the module starts when 001000000 8-bit binary data sent.but
... in computers, bits are combined into bytes (or longer machine words).
the strings you pass to pyserial are treated as byte sequences....
Just one other bit (byte?) of data that wasn't mentioned here:
the array module also provides handy conversions.

array.array('B', [1,2,3,4]).tostring() == '\1\2\3\4'

This may prove useful as you create message packets.

-Scott David Daniels
***@Acm.Org

Diez B. Roggisch
2004-12-14 12:31:49 UTC
Permalink
Post by ouz as
i have an electronic module which only understand binary data.
i use python pyserial.
for example the module starts when 001000000 8-bit binary data sent.but
pyserial sent only string data.
Can i send this binary data with pyserial or another way with python.
Strings _are_ binary data. Use the module struct to convert data from more
convenient representations (numbers) to strings, or use chr() to convert
them yourself.
--
Regards,

Diez B. Roggisch
Loading...