-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_uart_echo.py
75 lines (57 loc) · 2.02 KB
/
test_uart_echo.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
#
# Copyright (C) 2023-2024, HENSOLDT Cyber GmbH
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# For commercial licensing, contact: [email protected]
#
import pytest
import time
import board_automation.tools
import random
import string
import time
def rand_letters(amount):
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=amount))
#-------------------------------------------------------------------------------
def test_uart_echo(boot):
"""
Test UART echo send data and receive back
"""
test_runner = boot()
# synchronize with test application, timeout is 10 secs based on empirical
# evidence. System load can likely impact this timing.
(ret, idx) = test_runner.system_log_match_sequence(
[
'initialize UART ok',
],
10)
if not ret:
pytest.fail('could not detect test start')
serial_socket = test_runner.get_serial_socket()
serial_socket.settimeout(30)
block_size = 512
loops = 32
recv_data = ""
if isinstance(test_runner.board_runner, \
board_automation.automation_HW_CI.CIBoardRunner):
print("Testing on hardware CI")
serial_socket.recv(1) #flush buffer
for i in range(loops):
data = rand_letters(block_size)
t1 = time.time()
serial_socket.sendall(data.encode("utf-8"))
while len(recv_data) < block_size:
try:
recv_data += serial_socket.recv(block_size).decode()
except Exception as e:
print(f"Exception: {e}")
break
if data != recv_data[:block_size]:
print(f"Data: {data}\nRata: {recv_data}")
pytest.fail('Received data does not match with sent data')
print(f"Sent and received Packet {i+1} of {loops}, Duration: {'%.2f' % ((time.time() - t1)*1000)}ms")
recv_data = recv_data[block_size:]
time.sleep(0.5)
serial_socket.close()
print(f"Success: {block_size*loops} Bytes sent and received")