Change PySerial or other module similar

81 views
Skip to first unread message

davidvaran...@gmail.com

unread,
Mar 9, 2017, 7:34:44 AM3/9/17
to BeagleBoard
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

Mark Barton

unread,
Mar 9, 2017, 9:51:00 AM3/9/17
to beagl...@googlegroups.com
--
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 beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/d5ed90c0-4e92-4aaa-8fa5-51ebc4f6329b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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

Dennis Lee Bieber

unread,
Mar 9, 2017, 9:58:02 AM3/9/17
to beagl...@googlegroups.com
On Thu, 9 Mar 2017 04:34:43 -0800 (PST),
davidvaran...@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'


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

David Caniceiro

unread,
Mar 9, 2017, 1:09:12 PM3/9/17
to beagl...@googlegroups.com
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 <wlf...@ix.netcom.com>:
On Thu, 9 Mar 2017 04:34:43 -0800 (PST),
--
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 beagleboard+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/b8p2cc16t0ei7qklbtfba89oerst3drm6e%404ax.com.

daniel.di...@gmail.com

unread,
Sep 6, 2017, 8:52:19 AM9/6/17
to BeagleBoard
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 <wlf...@ix.netcom.com>:
On Thu, 9 Mar 2017 04:34:43 -0800 (PST),
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages