Download - Electronics- Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
ElectronicsSerial Communication-UART
Terry Sturtevant
Wilfrid Laurier University
November 7, 2018
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver TransmitterSimplest form of serial communicationBetween 2 devicesUses 2 signals (and Ground), Rx and TxAsynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver Transmitter
Simplest form of serial communicationBetween 2 devicesUses 2 signals (and Ground), Rx and TxAsynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver TransmitterSimplest form of serial communication
Between 2 devicesUses 2 signals (and Ground), Rx and TxAsynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver TransmitterSimplest form of serial communicationBetween 2 devices
Uses 2 signals (and Ground), Rx and TxAsynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver TransmitterSimplest form of serial communicationBetween 2 devicesUses 2 signals (and Ground), Rx and Tx
Asynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Serial Communication -UART
Universal Asynchronous Receiver TransmitterSimplest form of serial communicationBetween 2 devicesUses 2 signals (and Ground), Rx and TxAsynchronous, so both must agree on baud rate
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” levelLSB transmitted firstCan have odd, even, or no parity bit1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” level
LSB transmitted firstCan have odd, even, or no parity bit1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” levelLSB transmitted first
Can have odd, even, or no parity bit1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” levelLSB transmitted firstCan have odd, even, or no parity bit
1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” levelLSB transmitted firstCan have odd, even, or no parity bit1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Communication parameters
1 Start bit at “0” levelLSB transmitted firstCan have odd, even, or no parity bit1 or 2 Stop bits at “1” level
Since start and stop bits are opposite, new characters can alwaysbe detected.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1
1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2
1 1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2 3
1 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2 3 4
1 1 1 0 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2 3 4 5
1 1 1 0 0 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2 3 4 5 6
1 1 1 0 0 1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
LSB
1 2 3 4 5 6 7
MSB
1 1 1 0 0 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
0
LSB
1 2 3 4 5 6 7
MSB
1 1 1 0 0 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
0
LSB
1 2 3 4 5 6 7
MSB
1 1 1 0 0 1 1 0
UART no parity - 01100111
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
Parity0 1 2 3 4 5 6 7
UART even parity
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
Parity0 1 2 3 4 5 6 7
UART even parity
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
Parity0 1 2 3 4 5 6 7
UART odd parity
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Baud rate calculation
Baud rate is the number of bits possible in a seconde.g. 9600 baud → 1 bit takes 1
9600 secondAfter start bit is detected, wait time for 11
2 bit to testfor first data bit and then after every 1 bit intervalResetting at the start bit allows some clock variation
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Baud rate calculation
Baud rate is the number of bits possible in a second
e.g. 9600 baud → 1 bit takes 19600 second
After start bit is detected, wait time for 112 bit to test
for first data bit and then after every 1 bit intervalResetting at the start bit allows some clock variation
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Baud rate calculation
Baud rate is the number of bits possible in a seconde.g. 9600 baud → 1 bit takes 1
9600 second
After start bit is detected, wait time for 112 bit to test
for first data bit and then after every 1 bit intervalResetting at the start bit allows some clock variation
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Baud rate calculation
Baud rate is the number of bits possible in a seconde.g. 9600 baud → 1 bit takes 1
9600 secondAfter start bit is detected, wait time for 11
2 bit to testfor first data bit and then after every 1 bit interval
Resetting at the start bit allows some clock variation
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Baud rate calculation
Baud rate is the number of bits possible in a seconde.g. 9600 baud → 1 bit takes 1
9600 secondAfter start bit is detected, wait time for 11
2 bit to testfor first data bit and then after every 1 bit intervalResetting at the start bit allows some clock variation
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0
1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1
1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2
1 1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2 3
1 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2 3 4
1 1 1 0 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2 3 4 5
1 1 1 0 0 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2 3 4 5 6
1 1 1 0 0 1 1
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
0 1 2 3 4 5 6 7
1 1 1 0 0 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
0 1 2 3 4 5 6 7
1 1 1 0 0 1 1 0
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
0 1 2 3 4 5 6 7
1 1 1 0 0 1 1 0
Bit timing
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted±3→ ±12Zero is not a valid voltageMark level (inactive/1) is a negative voltageSpace level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted
±3→ ±12Zero is not a valid voltageMark level (inactive/1) is a negative voltageSpace level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted±3→ ±12
Zero is not a valid voltageMark level (inactive/1) is a negative voltageSpace level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted±3→ ±12Zero is not a valid voltage
Mark level (inactive/1) is a negative voltageSpace level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted±3→ ±12Zero is not a valid voltageMark level (inactive/1) is a negative voltage
Space level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
RS232 communication
Voltages are inverted±3→ ±12Zero is not a valid voltageMark level (inactive/1) is a negative voltageSpace level (active/0) is a positive voltage
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
ParityMark 0 1 2 3 4 5 6 7
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start
ParityMark
Space
0 1 2 3 4 5 6 7
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
ParityMark
Space
0 1 2 3 4 5 6 7
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial LibraryBaud rate calculationRS232 communication
Start Stop
ParityMark
Space
0 1 2 3 4 5 6 7
RS232 levels
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
UART or Serial Console
On the Raspberry Pi, the serial console uses the UART→ the UART isn’t available for other devicesThe serial console can be disabledThis means you need to connect to monitor, keyboard, etc.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
UART or Serial Console
On the Raspberry Pi, the serial console uses the UART
→ the UART isn’t available for other devicesThe serial console can be disabledThis means you need to connect to monitor, keyboard, etc.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
UART or Serial Console
On the Raspberry Pi, the serial console uses the UART→ the UART isn’t available for other devices
The serial console can be disabledThis means you need to connect to monitor, keyboard, etc.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
UART or Serial Console
On the Raspberry Pi, the serial console uses the UART→ the UART isn’t available for other devicesThe serial console can be disabled
This means you need to connect to monitor, keyboard, etc.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
UART or Serial Console
On the Raspberry Pi, the serial console uses the UART→ the UART isn’t available for other devicesThe serial console can be disabledThis means you need to connect to monitor, keyboard, etc.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
There is a script to enable and disable the serial console
sudo rpi-serial-console disablewill disable the consolesudo rpi-serial-console enablewill enable the console
Remember you probably don’t want to be using thiscommand from within the serial console.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
There is a script to enable and disable the serial consolesudo rpi-serial-console disablewill disable the console
sudo rpi-serial-console enablewill enable the console
Remember you probably don’t want to be using thiscommand from within the serial console.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
There is a script to enable and disable the serial consolesudo rpi-serial-console disablewill disable the consolesudo rpi-serial-console enablewill enable the console
Remember you probably don’t want to be using thiscommand from within the serial console.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
There is a script to enable and disable the serial consolesudo rpi-serial-console disablewill disable the consolesudo rpi-serial-console enablewill enable the console
Remember you probably don’t want to be using thiscommand from within the serial console.
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial
ser = serial.Serial()open portser.baudrate = 19200set baudrateser.port =’//dev//ttyAMA0’set portser.is openreturns True if open, False if not
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial
ser = serial.Serial()open port
ser.baudrate = 19200set baudrateser.port =’//dev//ttyAMA0’set portser.is openreturns True if open, False if not
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial
ser = serial.Serial()open portser.baudrate = 19200set baudrate
ser.port =’//dev//ttyAMA0’set portser.is openreturns True if open, False if not
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial
ser = serial.Serial()open portser.baudrate = 19200set baudrateser.port =’//dev//ttyAMA0’set port
ser.is openreturns True if open, False if not
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial
ser = serial.Serial()open portser.baudrate = 19200set baudrateser.port =’//dev//ttyAMA0’set portser.is openreturns True if open, False if not
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial (continued)
ser.write(b’string’)write stringser.read(10)read 10 bytes (or until timeout)ser.readline()read until ’\n’ receivedser.close()close port
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial (continued)
ser.write(b’string’)write string
ser.read(10)read 10 bytes (or until timeout)ser.readline()read until ’\n’ receivedser.close()close port
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial (continued)
ser.write(b’string’)write stringser.read(10)read 10 bytes (or until timeout)
ser.readline()read until ’\n’ receivedser.close()close port
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial (continued)
ser.write(b’string’)write stringser.read(10)read 10 bytes (or until timeout)ser.readline()read until ’\n’ received
ser.close()close port
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial (continued)
ser.write(b’string’)write stringser.read(10)read 10 bytes (or until timeout)ser.readline()read until ’\n’ receivedser.close()close port
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial sample code
i m p o r t s e r i a ls e r = s e r i a l . S e r i a l (”/ dev /ttyAMA0 ”)s e r . b a u d r a t e = 9600data = s e r . r e a d ( 1 0 )#data = s e r . r e a d l i n e ( )s e r . w r i t e ( data )s e r . c l o s e ( )
Terry Sturtevant Electronics Serial Communication-UART
Serial Communication -UARTUART or Serial Console
Raspberry Pi Python Serial Library
PySerial sample code
i m p o r t s e r i a ls e r = s e r i a l . S e r i a l (”/ dev /ttyAMA0 ”)s e r . b a u d r a t e = 9600data = s e r . r e a d ( 1 0 )#data = s e r . r e a d l i n e ( )s e r . w r i t e ( data )s e r . c l o s e ( )
Terry Sturtevant Electronics Serial Communication-UART