01 Py Serial
01 Py Serial
Dr Frazer K. Noble
Department of Mechanical and Electrical Engineering
Massey University
E: [email protected]
O: AV 1.18, Albany Village
1
Introduction
In this presentation, I will describe:
2
Getting started
Create a new directory named "01_py_serial" in C:\Users\%USER%\Documents .
3
Open Visual Studio Code in C:\Users\%USER%\Documents\01_py_serial : either:
1. Open Visual Studio Code and select "File > Open Folder..." and navigate to
C:\Users\%USER%\Documents\01_py_serial ;
or,
2. Right click in C:\Users\%USER%\Documents\01_py_serial and select "Open with
Code".
1. press Ctrl + ~ ;
or
2. select "View > Terminal".
4
Install pyserial .
Type the following command into the terminal and then press Enter :
This will install the latest version of pyserial into the current environment.
5
py_serial.py
Create a new file named "py_serial.py" in C:\Users\%USER%\Documents\01_py_serial .
Open py_serial.py and type the following Python code into the file:
import os
import sys
def main():
return 0
if __name__ == "__main__":
sys.exit(main())
6
In py_serial.py , type the following Python code:
import serial
from serial.tools.list_ports import comports
This will import the serial module and and the module's comports() function.
7
In py_serial.py 's main() function, type the following Python code:
ser = serial.Serial()
8
In py_serial.py 's main() function, type the following Python code:
print("BAUDRATES:\n{}".format(ser.BAUDRATES))
print("BYTESIZES:\n{}".format(ser.BYTESIZES))
print("PARITIES:\n{}".format(ser.PARITIES))
print("STOPBITS:\n{}".format(ser.STOPBITS))
print("COMPORTS:\n{}".format([str(c) for c in comports()]))
This will display lists of all the baud rates, the data packet sizes, the parities, the number
of stop bits, and the name of the devices that can be used.
9
Type the following command into the terminal and then press Enter :
python py_serial.py
10
Something similar to the following will be displayed in the terminal:
BAUDRATES:
(50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200)
BYTESIZES:
(5, 6, 7, 8)
PARITIES:
('N', 'E', 'O', 'M', 'S')
STOPBITS:
(1, 1.5, 2)
COMPORTS:
['COM3 - Arduino Uno (COM3)']
Here, we can see the baud rates, the data packet sizes, the parities, the number of stop
bits, and the name of the devices that can be used.
A baud rate of 9600, data packet size of 8 bits, no parity, and 1 stop bit, i.e. 9600 8N1 ,
is a commonly used combination of values.
11
In py_serial.py 's main() function, type the following Python code:
ser.baudrate = 9600
ser.bytesize = 8
ser.parity = 'N'
ser.stopbits = 1
ser.port = 'COM3'
ser.timeout = 0.5
This will set ser 's baud rate, data packet size, parity, number of stop bits, device name,
and timeout to 9600 , 8 , None , 1 , COM3 , and 0.5 respectively.
12
In py_serial.py 's main() function, type the following Python code:
try:
ser.open()
except Exception as e:
print(e)
return 1
This will try to open the ser 's port using the assigned baud rate, data packet size,
parity, number of stop bits, and device name. Otherwise, an exception is displayed and
the program ends.
13
Unplug the Arduino from the computer.
Type the following command into the terminal and then press Enter :
python py_serial.py
14
Something similar will be displayed in the terminal:
BAUDRATES:
(50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200)
BYTESIZES:
(5, 6, 7, 8)
PARITIES:
('N', 'E', 'O', 'M', 'S')
STOPBITS:
(1, 1.5, 2)
COMPORTS:
[]
could not open port 'COM3': FileNotFoundError(2, 'The
system cannot find the file specified.', None, 2)
15
In py_serial.py 's main() function, type the following Python code:
while True:
pin = input("Enter a pin to toggle (00 - 13), Q/q to break: ")
data = '{}\n'.format(pin)
ser.write(data.encode("utf-8"))
data = ser.readline()
print('{}'.format(data.decode("utf-8")))
ser.close()
This will continuously prompt the user to type in a two-digit number, e.g. 13 , which
will be encoded into an array of bytes, then written to ser 's port. If the user types "Q"
or "q", the loop will break, and ser 's port will be closed.
16
Testing
Type the following command into the terminal and then press Enter :
python py_serial.py
17
Something similar to the following will be displayed in the terminal:
BAUDRATES:
(50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200)
BYTESIZES:
(5, 6, 7, 8)
PARITIES:
('N', 'E', 'O', 'M', 'S')
STOPBITS:
(1, 1.5, 2)
COMPORTS:
['COM3 - Arduino Uno (COM3)']
Enter a pin to toggle (00 - 13), Q/q to break: 13
0
Press Enter .
20
References
1. https://www.arduino.cc/
2. https://pythonhosted.org/pyserial/index.html
21