Skip to content

Commit f31966d

Browse files
Add Python test scripts and rename MicroPython scripts (#246)
1 parent b1dae7f commit f31966d

File tree

4 files changed

+199
-91
lines changed

4 files changed

+199
-91
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import network
2+
import utime as time
3+
import usocket as socket
4+
5+
# Set your wifi ssid and password here
6+
WIFI_SSID = const('')
7+
WIFI_PASSWORD = const('')
8+
9+
# Set the server address here like 1.2.3.4
10+
SERVER_ADDR = const('')
11+
12+
# These constants should match the server
13+
BUF_SIZE = const(2048)
14+
SERVER_PORT = const(4242)
15+
TEST_ITERATIONS = const(10)
16+
17+
# Check if wifi details have been set
18+
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
19+
raise RuntimeError('set wifi ssid and password in this script')
20+
21+
# Check server ip address set
22+
if len(SERVER_ADDR) == 0:
23+
raise RuntimeError('set the IP address of the server')
24+
25+
# Start connection
26+
wlan = network.WLAN(network.STA_IF)
27+
wlan.active(True)
28+
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
29+
30+
# Wait for connect success or failure
31+
max_wait = 20
32+
while max_wait > 0:
33+
if wlan.status() < 0 or wlan.status() >= 3:
34+
break
35+
max_wait -= 1
36+
print('waiting for connection...')
37+
time.sleep(1)
38+
39+
# Handle connection error
40+
if wlan.status() != 3:
41+
raise RuntimeError('wifi connection failed %d' % wlan.status())
42+
else:
43+
print('connected')
44+
status = wlan.ifconfig()
45+
print( 'ip = ' + status[0] )
46+
47+
# Open socket to the server
48+
sock = socket.socket()
49+
addr = (SERVER_ADDR, SERVER_PORT)
50+
sock.connect(addr)
51+
52+
# repeat test for a number of iterations
53+
for test_iteration in range(TEST_ITERATIONS):
54+
55+
# Read BUF_SIZE bytes from the server
56+
read_buf = sock.read(BUF_SIZE)
57+
print('read %d bytes from server' % len(read_buf))
58+
59+
# Check size of data received
60+
if len(read_buf) != BUF_SIZE:
61+
raise RuntimeError('wrong amount of data read')
62+
63+
# Send the data back to the server
64+
write_len = sock.write(read_buf)
65+
print('written %d bytes to server' % write_len)
66+
if write_len != BUF_SIZE:
67+
raise RuntimeError('wrong amount of data written')
68+
69+
# All done
70+
sock.close()
71+
print("test completed")
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import network
2+
import utime as time
3+
import usocket as socket
4+
import random
5+
6+
# Set your wifi ssid and password here
7+
WIFI_SSID = const('')
8+
WIFI_PASSWORD = const('')
9+
10+
# These constants should match the client
11+
BUF_SIZE = const(2048)
12+
SERVER_PORT = const(4242)
13+
TEST_ITERATIONS = const(10)
14+
15+
# Check if wifi details have been set
16+
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
17+
raise RuntimeError('Please set wifi ssid and password in this script')
18+
19+
# Start connection
20+
wlan = network.WLAN(network.STA_IF)
21+
wlan.active(True)
22+
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
23+
24+
# Wait for connect success or failure
25+
max_wait = 20
26+
while max_wait > 0:
27+
if wlan.status() < 0 or wlan.status() >= 3:
28+
break
29+
max_wait -= 1
30+
print('waiting for connection...')
31+
time.sleep(1)
32+
33+
# Handle connection error
34+
if wlan.status() != 3:
35+
raise RuntimeError('wifi connection failed %d' % wlan.status())
36+
else:
37+
print('connected')
38+
status = wlan.ifconfig()
39+
print( 'ip = ' + status[0] )
40+
41+
# Open socket to the server
42+
sock = socket.socket()
43+
addr = ('0.0.0.0', SERVER_PORT)
44+
sock.bind(addr)
45+
sock.listen(1)
46+
print('server listening on', addr)
47+
48+
# Wait for the client
49+
con = None
50+
con, addr = sock.accept()
51+
print('client connected from', addr)
52+
53+
# repeat test for a number of iterations
54+
for test_iteration in range(TEST_ITERATIONS):
55+
56+
# Generate a buffer of random data
57+
random_list = []
58+
for n in range(BUF_SIZE):
59+
random_list.append(random.randint(0, 255))
60+
write_buf = bytearray(random_list)
61+
62+
# write BUF_SIZE bytes to the client
63+
write_len = con.send(bytearray(write_buf))
64+
print('Written %d bytes to client' % write_len)
65+
66+
# Check size of data written
67+
if write_len != BUF_SIZE:
68+
raise RuntimeError('wrong amount of data written')
69+
70+
# Read the data back from the client
71+
read_buf = con.read(BUF_SIZE)
72+
print('read %d bytes from client' % len(read_buf))
73+
74+
# Check size of data received
75+
if len(read_buf) != BUF_SIZE:
76+
raise RuntimeError('wrong amount of data read')
77+
78+
# Check the data sent and received
79+
if read_buf != write_buf:
80+
raise RuntimeError('buffer mismatch')
81+
82+
# All done
83+
con.close()
84+
sock.close()
85+
print("test completed")

pico_w/python_test_tcp/python_test_tcp_client.py

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,43 @@
1-
import network
2-
import utime as time
3-
import usocket as socket
1+
#!/usr/bin/python
42

5-
# Set your wifi ssid and password here
6-
WIFI_SSID = const('')
7-
WIFI_PASSWORD = const('')
8-
9-
# Set the server address here like 1.2.3.4
10-
SERVER_ADDR = const('')
11-
12-
# These constants should match the server
13-
BUF_SIZE = const(2048)
14-
SERVER_PORT = const(4242)
15-
TEST_ITERATIONS = const(10)
16-
17-
# Check if wifi details have been set
18-
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
19-
raise RuntimeError('set wifi ssid and password in this script')
3+
import socket
4+
import sys
205

216
# Check server ip address set
22-
if len(SERVER_ADDR) == 0:
23-
raise RuntimeError('set the IP address of the server')
7+
if len(sys.argv) < 2:
8+
raise RuntimeError('pass IP address of the server')
249

25-
# Start connection
26-
wlan = network.WLAN(network.STA_IF)
27-
wlan.active(True)
28-
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
29-
30-
# Wait for connect success or failure
31-
max_wait = 20
32-
while max_wait > 0:
33-
if wlan.status() < 0 or wlan.status() >= 3:
34-
break
35-
max_wait -= 1
36-
print('waiting for connection...')
37-
time.sleep(1)
10+
# Set the server address here like 1.2.3.4
11+
SERVER_ADDR = sys.argv[1]
3812

39-
# Handle connection error
40-
if wlan.status() != 3:
41-
raise RuntimeError('wifi connection failed %d' % wlan.status())
42-
else:
43-
print('connected')
44-
status = wlan.ifconfig()
45-
print( 'ip = ' + status[0] )
13+
# These constants should match the server
14+
BUF_SIZE = 2048
15+
SERVER_PORT = 4242
16+
TEST_ITERATIONS = 10
4617

4718
# Open socket to the server
4819
sock = socket.socket()
4920
addr = (SERVER_ADDR, SERVER_PORT)
5021
sock.connect(addr)
5122

52-
# repeat test for a number of iterations
23+
# Repeat test for a number of iterations
5324
for test_iteration in range(TEST_ITERATIONS):
54-
25+
5526
# Read BUF_SIZE bytes from the server
56-
read_buf = sock.read(BUF_SIZE)
57-
print('read %d bytes from server' % len(read_buf))
27+
total_size = BUF_SIZE
28+
read_buf = b''
29+
while total_size > 0:
30+
buf = sock.recv(BUF_SIZE)
31+
print('read %d bytes from server' % len(buf))
32+
total_size -= len(buf)
33+
read_buf += buf
5834

5935
# Check size of data received
6036
if len(read_buf) != BUF_SIZE:
61-
raise RuntimeError('wrong amount of data read')
37+
raise RuntimeError('wrong amount of data read %d', len(read_buf))
6238

6339
# Send the data back to the server
64-
write_len = sock.write(read_buf)
40+
write_len = sock.send(read_buf)
6541
print('written %d bytes to server' % write_len)
6642
if write_len != BUF_SIZE:
6743
raise RuntimeError('wrong amount of data written')

pico_w/python_test_tcp/python_test_tcp_server.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,51 @@
1-
import network
2-
import utime as time
3-
import usocket as socket
1+
#!/usr/bin/python
2+
43
import random
4+
import socket
55

6-
# Set your wifi ssid and password here
7-
WIFI_SSID = const('')
8-
WIFI_PASSWORD = const('')
6+
# Set server adress to machines IP
7+
SERVER_ADDR = "0.0.0.0"
98

109
# These constants should match the client
11-
BUF_SIZE = const(2048)
12-
SERVER_PORT = const(4242)
13-
TEST_ITERATIONS = const(10)
14-
15-
# Check if wifi details have been set
16-
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
17-
raise RuntimeError('Please set wifi ssid and password in this script')
18-
19-
# Start connection
20-
wlan = network.WLAN(network.STA_IF)
21-
wlan.active(True)
22-
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
23-
24-
# Wait for connect success or failure
25-
max_wait = 20
26-
while max_wait > 0:
27-
if wlan.status() < 0 or wlan.status() >= 3:
28-
break
29-
max_wait -= 1
30-
print('waiting for connection...')
31-
time.sleep(1)
32-
33-
# Handle connection error
34-
if wlan.status() != 3:
35-
raise RuntimeError('wifi connection failed %d' % wlan.status())
36-
else:
37-
print('connected')
38-
status = wlan.ifconfig()
39-
print( 'ip = ' + status[0] )
10+
BUF_SIZE = 2048
11+
TEST_ITERATIONS = 10
12+
SERVER_PORT = 4242
4013

4114
# Open socket to the server
4215
sock = socket.socket()
43-
addr = ('0.0.0.0', SERVER_PORT)
44-
sock.bind(addr)
16+
sock.bind((SERVER_ADDR, SERVER_PORT))
4517
sock.listen(1)
46-
print('server listening on', addr)
18+
print("server listening on", SERVER_ADDR, SERVER_PORT)
4719

4820
# Wait for the client
4921
con = None
5022
con, addr = sock.accept()
51-
print('client connected from', addr)
23+
print("client connected from", addr)
5224

53-
# repeat test for a number of iterations
25+
# Repeat test for a number of iterations
5426
for test_iteration in range(TEST_ITERATIONS):
55-
5627
# Generate a buffer of random data
5728
random_list = []
5829
for n in range(BUF_SIZE):
5930
random_list.append(random.randint(0, 255))
6031
write_buf = bytearray(random_list)
6132

62-
# write BUF_SIZE bytes to the client
33+
# Write BUF_SIZE bytes to the client
6334
write_len = con.send(bytearray(write_buf))
6435
print('Written %d bytes to client' % write_len)
6536

6637
# Check size of data written
6738
if write_len != BUF_SIZE:
68-
raise RuntimeError('wrong amount of data written')
39+
raise RuntimeError('wrong amount of data written %d' % write_len)
6940

7041
# Read the data back from the client
71-
read_buf = con.read(BUF_SIZE)
72-
print('read %d bytes from client' % len(read_buf))
42+
total_size = BUF_SIZE
43+
read_buf = b''
44+
while total_size > 0:
45+
buf = con.recv(BUF_SIZE)
46+
print('read %d bytes from client' % len(buf))
47+
total_size -= len(buf)
48+
read_buf += buf
7349

7450
# Check size of data received
7551
if len(read_buf) != BUF_SIZE:

0 commit comments

Comments
 (0)