For serial communication, it is convenient to use pyserial.
install
First, install pyserial using pip.
$ pip install pyserial
only this.
Import, configure, output, and finally close. Very easy.
>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial-******', 9600, timeout=1)
>>> ser.write("hello")
>>> ser.close()
In the setting part, specify the device name as the first argument, then the baud rate, and then the timeout. If you want to look up the device name on your mac, go to the terminal
$ ls /dev/tty.*
If you enter,'/dev/tty.usbserial-*****' will be displayed, so you can check it.
In the argument of read (),
--If nothing is specified, one character is read --If you specify an integer (such as 10), read the specified number of characters. --Read to the end of the line with readline ()
>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial-*****', 9600, timeout=1)
>>> c = ser.read()
>>> string = ser.read(10)
>>> line = ser.readline()
>>> ser.close
Recommended Posts