On Thu, 9 Mar 2017 04:34:43 -0800 (PST),
davidvarandacaniceiro@gmail.com declaimed the
following:
I want to know this because, PySerial only communicate with "string", and i
It sends bytes (at least, in Python 2.x -- not sure if Python 3.x uses
unicode or byte as the native component for PySerial), packaged in a Python
string data type.
There is no constraint on what those bytes contain, beyond that imposed
by the console (console output -- print theString -- may try to convert
non-ASCII values to something unusable).
Saying this, i want to send data in int/hex or binary format instead of
string from the BeagleBone side, other solution can be change the PySerial
module to do this.
"hex" is, to most people, a character string representation of a binary
value using a radix of 16 -- which means each byte of the raw data is
represented by two characters, one for each four bits.
Any help will be aprecciated.
Read the library reference manual section describing the struct module.
import struct
it = 202
bt = "6"
ft = 3.14159
pck = struct.pack("!hiqBBifd",
... it, it, it, it, ord(bt), ord(bt), ft, ft)
repr(pck)
"'\\x00\\xca\\x00\\x00\\x00\\xca\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xca\\xca6\\x00\\x00\\x006@I\\x0f\\xd0@\\t!\\xf9\\xf0\\x1b\\x86n'"
The format string is (repr() shows each byte, and if printable, does
not show the xNN format):
! network byte order
h (signed) short 202 => x00ca
i (signed) integer 202 => x000000ca
q (signed) long-long 202 => x00000000000000ca
B unsigned char (integer data type -- "c" is 1-character string) 202 =>
xca
B unsigned char (need ord() to get integer) ord("6") => 54 => '6'
i (signed) integer ord("6") => 54 => x00 and '6'
f float 3.14159 => '@I' and x04d0
d double 3.14159 =>
'@' and <tab> and '!' and xf9f01b86 and 'n'