If you just have to send bytes, you can find useful to use a bytearray.
*[Python 3]*
serial.write(bytearray([255]))
serial.write(bytearray([2, 75, 250]))
The struct module is still useful to send C style values, but I suggest you
make explicit whether you are using little or big-endian to avoid confusion
(default will use otherwise the machine running the code default which may
change when you move the code to other machine):
struct.pack('>L', 75655)
El dijous, 9 març de 2017 19:09:12 UTC+1, David Caniceiro va escriure:
>
> Thanks for your answers guys,
>
> testing the Dennis answer, and after read the struct information,i reached
> a solution:
>
> if i use -> struct.pack('B',255)
> now only use one byte on serial communication, instead of 3 bytes like i
> was doing before(sending a string '255')
>
> But after some tests, i confirmed if i use the hex string directly on
> write. its works too.
> Ex:: serial.write('\xFF')
>
> Thanks again for the answers, and sorry for being newbie.
>
> Best Regards,
> David
>
>
>
>
>
>
>
> 2017-03-09 14:57 GMT+00:00 Dennis Lee Bieber <[email protected]
> <javascript:>>:
>
>> On Thu, 9 Mar 2017 04:34:43 -0800 (PST),
>> [email protected] <javascript:> 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'
>>
>>
>> --
>> Wulfraed Dennis Lee Bieber AF6VN
>> [email protected] <javascript:> HTTP://wlfraed.home.netcom.com/
>>
>> --
>> For more options, visit http://beagleboard.org/discuss
>> ---
>> You received this message because you are subscribed to a topic in the
>> Google Groups "BeagleBoard" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/beagleboard/C6m1B9PNxmk/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/beagleboard/b8p2cc16t0ei7qklbtfba89oerst3drm6e%404ax.com
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/beagleboard/f1c0a78e-0c6f-4921-9345-bd27bb6e9fa0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.