Send and receive binary data via serial communication with python3 (on mac)

things to do

Serial (UART) communication is performed between mac and python3 using a USB serial driver or the like.

How to do

You can do this by installing pyserial with pip and import serial. I found a lot of articles in the search that I couldn't do with python3, but as of February 29, 2020, I could do it with python3.

environment

procedure

Install pyserial

install.sh


$ pip install pyserial

Receive

The code is as follows, which receives 1 byte serially, converts it to decimal notation, and converts it to standard output.

serial_read.py


import serial
import struct

ser = serial.Serial(
      port = "/dev/cu.usbserial-XXXXXXXX",
      baudrate = 115200,
      #parity = serial.PARITY_NONE,
      #bytesize = serial.EIGHTBITS,
      #stopbits = serial.STOPBITS_ONE,
      #timeout = None,
      #xonxoff = 0,
      #rtscts = 0,
      )

while True:
      if ser.in_waiting > 0:
            recv_data = ser.read(1)
            print(type(recv_data))
            a = struct.unpack_from("B",recv_data ,0)
            print( a )

The port string should be ls /dev/cu.*, and select the appropriate device from the devices that appear as ʻusbserial -...`. The commented out part retains the default settings, so comment it out and set the appropriate parameters if necessary.

You can check if there is received data in the serial buffer (the number of bytes contained in the buffer) by referring to ser.in_waiting.

The received data will be treated as a character string even if it is binary data. If the received data is binary, you need to convert it to a number using struct_unpack_from () as in the code above. If it is a utf8 string, you need to decode it. If it is a so-called 7bit character string, it can be treated as a character string as it is without decoding or unpacking.

Send

Send any byte string (buf).

serial_write.py


import serial
import struct

def send( buf ):
      while True:
            if ser.out_waiting == 0:
                  break
      for b in buf:
            a = struct.pack( "B", b )
            ser.write(a)
      ser.flush()

ser = serial.Serial(
      port = "/dev/cu.usbserial-XXXXXXXX",
      baudrate = 115200,
      #parity = serial.PARITY_NONE,
      #bytesize = serial.EIGHTBITS,
      #stopbits = serial.STOPBITS_ONE,
      #timeout = None,
      #xonxoff = 0,
      #rtscts = 0,
      )

x = [ 0x01, 0xFF, 0x02, 0xFE, 0x03]
send( x )

If you do not flush by yourself, it seems that it will not send until some transmission data is buffered. If you do not check ser.out_waiting, it seems that sometimes it cannot be sent normally.

If you want to send a (alphanumeric) string instead of a binary, you can do it below.

serial_str_write.py


x = b'abcdef'
ser.write( x )

If you want to send a utf8 string, you need to encode it.

Summary

I thought it would be easier than writing in C language or swift, but I did a lot of things other than the communication part, so I had a hard time finding out about unpack and encode.

Recommended Posts

Send and receive binary data via serial communication with python3 (on mac)
Send and receive data with MQTT via Watson IoT Platform
Serial communication with Python
Try importing MLB data on Mac and Python
Start communication with UDP and send and receive with TCP
Install selenium on Mac and try it with python
Send data from Python to Processing via socket communication
Send and receive Gmail via the Gmail API using Python
Serial communication control with python and I2C communication (using USBGPIO8 device)
Serial communication control with python and SPI communication (using USBGPIO8 device)
Send email via gmail with Python 3.4.3.
Build a Python environment on your Mac with Anaconda and PyCharm
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Install lp_solve on Mac OS X and call it with python.
Data pipeline construction with Python and Luigi
Receive textual data from mysql with python
Try working with binary data in Python
Check and receive Serial port in Python (Port check)
POST variously with Python and receive with Flask
A memo with Python2.7 and Python3 on CentOS
Follow active applications on Mac with Python
Notes on building Python and pyenv on Mac
Build Python environment with Anaconda on Mac
Send email with SES in Python and short message with SMS on SNS
Put MeCab binding for Python with pip on Windows, mac and Linux
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Receive and display HTML form data in Python
A story stuck with handling Python binary data
python on mac
Try working with Mongo in Python on Mac
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Install MongoDB on Ubuntu 16.04 and operate via python
Install Python3 on Mac and build environment [Definitive Edition]
Notes on HDR and RAW image processing with Python
Automatic follow on Twitter with python and selenium! (RPA)
Installing Python 3 on Mac and checking basic operation Part 1
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Email hipchat with postfix, fluentd and python on Azure
Investigate Java and python data exchange with Apache Arrow
Automate Chrome with Python and Selenium on your Chromebook
Get data from database via ODBC with Python (Access)
Socket communication with Python
Data analysis with python 2
Install Python on Mac
Install Python 3 on Mac
Binary search with python
Binary search with Python3
HTTP communication with Python
Send email with Python
Install Python 3.4 on Mac
Data analysis with Python
Word Count with Apache Spark and python (Mac OS X)
Notes on importing data from MySQL or CSV with Python
Building a Python environment on a Mac and using Jupyter lab
Notes on handling large amounts of data with python + pandas
IP spoof using tor on macOS and check with python
Test Python with Miniconda on OS X and Linux with travis-ci
TWE-Lite serial communication app Byte mode setting (send in Python)
Get rid of dirty data with Python and regular expressions
Solve the spiral book (algorithm and data structure) with python!