This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathuart.py
90 lines (77 loc) · 2.17 KB
/
uart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
P9 and P23 must be connected together for this test to pass.
'''
from machine import UART
from machine import Pin
import os
import time
uart_id_range = [1, 2]
for uart_id in uart_id_range:
uart = UART(uart_id, 115200)
print(uart)
uart.init(57600, 8, None, 1, pins=('P23', 'P9'))
uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=('P23', 'P9'))
uart.init(baudrate=115200, parity=UART.ODD, stop=1, pins=('P23', 'P9'))
uart.read()
print (uart.readall())
print (uart.readline())
buff=bytearray(1)
print (uart.readinto(buff,1))
print (uart.read())
print (uart.any())
print (uart.write('a'))
uart.deinit()
# now it's time for some loopback tests between pins
for uart_id in uart_id_range:
uart = UART(uart_id, 1000000, pins=('P9', 'P23'))
print(uart)
uart.read()
print(uart.write(b'123456') == 6)
print(uart.read() == b'123456')
uart.deinit()
uart = UART(uart_id, 1000000, pins=('P23', 'P9'))
print(uart)
uart.read()
print(uart.write(b'123456') == 6)
print(uart.read() == b'123456')
uart.deinit()
for uart_id in uart_id_range:
uart = UART(uart_id, 1000000, pins=('P23', 'P9'))
print(uart.write(b'123') == 3)
print(uart.read(1) == b'1')
print(uart.read(2) == b'23')
print(uart.read() == None)
uart.write(b'123')
buf = bytearray(3)
print(uart.readinto(buf, 1) == 1)
print(buf)
print(uart.readinto(buf) == 2)
print(buf)
uart.deinit()
# check for memory leaks...
for i in range (0, 1000):
uart = UART(1, 1000000)
uart.deinit()
uart = UART(2, 1000000)
uart.deinit()
# next ones must raise
try:
UART(1, 9600, parity=None, pins=('GP12', 'GP13', 'GP7'))
except Exception:
print('Exception')
try:
UART(1, 9600, parity=UART.ODD, pins=('GP12', 'GP7'))
except Exception:
print('Exception')
# buffer overflow
for uart_id in uart_id_range:
uart = UART(uart_id, 1000000, pins=('P9', 'P23'))
buf = bytearray([0x55AA]*567)
for i in range(200):
r = uart.write(buf)
r = uart.readall()
r = uart.readall()
print(r)
print(uart.write(b'123456') == 6)
print(uart.read() == b'123456')
uart.deinit()