Change PySerial or other module similar

Hi,

I would like to know if exist any other module to communicate via serial in python, instead of PySerial.

I want to know this because, PySerial only communicate with “string”, and i have a microcontroller that will receive data from a BeagleBone Black, and i shouldnt do an algoritm on this microcontroller to change strings to int or bytes, because will spend a lot of processing, and every character on a string uses one byte, and that will occupate a lot on this micro.

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.

Any help will be aprecciated.

Best Regards,
David

Hi David, PySerial is the way to go. Python has several ways to do the conversion for you. Google around a bit and you will find several examples. Use the array module if you want to send several bytes using PySerial. Here is an example of sending a list of data. msgList is a list of integer data to send. For example msgList = [1,2,3,4,5] and serialObj is the opened serial stream. import array binMessage = array.array(‘B’) ← creates an empty binary array binMessage.extend(msgList) ← converts and copies the list to the array serialObj.write(binMessage.tostring()) ← Send it out. Notice the tostring function serialObj.flush() Mark

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'

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

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: