Add Python test scripts and rename MicroPython scripts (#246)
This commit is contained in:
71
pico_w/python_test_tcp/micropython_test_tcp_client.py
Normal file
71
pico_w/python_test_tcp/micropython_test_tcp_client.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import network
|
||||||
|
import utime as time
|
||||||
|
import usocket as socket
|
||||||
|
|
||||||
|
# Set your wifi ssid and password here
|
||||||
|
WIFI_SSID = const('')
|
||||||
|
WIFI_PASSWORD = const('')
|
||||||
|
|
||||||
|
# Set the server address here like 1.2.3.4
|
||||||
|
SERVER_ADDR = const('')
|
||||||
|
|
||||||
|
# These constants should match the server
|
||||||
|
BUF_SIZE = const(2048)
|
||||||
|
SERVER_PORT = const(4242)
|
||||||
|
TEST_ITERATIONS = const(10)
|
||||||
|
|
||||||
|
# Check if wifi details have been set
|
||||||
|
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
|
||||||
|
raise RuntimeError('set wifi ssid and password in this script')
|
||||||
|
|
||||||
|
# Check server ip address set
|
||||||
|
if len(SERVER_ADDR) == 0:
|
||||||
|
raise RuntimeError('set the IP address of the server')
|
||||||
|
|
||||||
|
# Start connection
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(True)
|
||||||
|
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
|
||||||
|
|
||||||
|
# Wait for connect success or failure
|
||||||
|
max_wait = 20
|
||||||
|
while max_wait > 0:
|
||||||
|
if wlan.status() < 0 or wlan.status() >= 3:
|
||||||
|
break
|
||||||
|
max_wait -= 1
|
||||||
|
print('waiting for connection...')
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# Handle connection error
|
||||||
|
if wlan.status() != 3:
|
||||||
|
raise RuntimeError('wifi connection failed %d' % wlan.status())
|
||||||
|
else:
|
||||||
|
print('connected')
|
||||||
|
status = wlan.ifconfig()
|
||||||
|
print( 'ip = ' + status[0] )
|
||||||
|
|
||||||
|
# Open socket to the server
|
||||||
|
sock = socket.socket()
|
||||||
|
addr = (SERVER_ADDR, SERVER_PORT)
|
||||||
|
sock.connect(addr)
|
||||||
|
|
||||||
|
# repeat test for a number of iterations
|
||||||
|
for test_iteration in range(TEST_ITERATIONS):
|
||||||
|
|
||||||
|
# Read BUF_SIZE bytes from the server
|
||||||
|
read_buf = sock.read(BUF_SIZE)
|
||||||
|
print('read %d bytes from server' % len(read_buf))
|
||||||
|
|
||||||
|
# Check size of data received
|
||||||
|
if len(read_buf) != BUF_SIZE:
|
||||||
|
raise RuntimeError('wrong amount of data read')
|
||||||
|
|
||||||
|
# Send the data back to the server
|
||||||
|
write_len = sock.write(read_buf)
|
||||||
|
print('written %d bytes to server' % write_len)
|
||||||
|
if write_len != BUF_SIZE:
|
||||||
|
raise RuntimeError('wrong amount of data written')
|
||||||
|
|
||||||
|
# All done
|
||||||
|
sock.close()
|
||||||
|
print("test completed")
|
||||||
85
pico_w/python_test_tcp/micropython_test_tcp_server.py
Normal file
85
pico_w/python_test_tcp/micropython_test_tcp_server.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import network
|
||||||
|
import utime as time
|
||||||
|
import usocket as socket
|
||||||
|
import random
|
||||||
|
|
||||||
|
# Set your wifi ssid and password here
|
||||||
|
WIFI_SSID = const('')
|
||||||
|
WIFI_PASSWORD = const('')
|
||||||
|
|
||||||
|
# These constants should match the client
|
||||||
|
BUF_SIZE = const(2048)
|
||||||
|
SERVER_PORT = const(4242)
|
||||||
|
TEST_ITERATIONS = const(10)
|
||||||
|
|
||||||
|
# Check if wifi details have been set
|
||||||
|
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
|
||||||
|
raise RuntimeError('Please set wifi ssid and password in this script')
|
||||||
|
|
||||||
|
# Start connection
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(True)
|
||||||
|
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
|
||||||
|
|
||||||
|
# Wait for connect success or failure
|
||||||
|
max_wait = 20
|
||||||
|
while max_wait > 0:
|
||||||
|
if wlan.status() < 0 or wlan.status() >= 3:
|
||||||
|
break
|
||||||
|
max_wait -= 1
|
||||||
|
print('waiting for connection...')
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# Handle connection error
|
||||||
|
if wlan.status() != 3:
|
||||||
|
raise RuntimeError('wifi connection failed %d' % wlan.status())
|
||||||
|
else:
|
||||||
|
print('connected')
|
||||||
|
status = wlan.ifconfig()
|
||||||
|
print( 'ip = ' + status[0] )
|
||||||
|
|
||||||
|
# Open socket to the server
|
||||||
|
sock = socket.socket()
|
||||||
|
addr = ('0.0.0.0', SERVER_PORT)
|
||||||
|
sock.bind(addr)
|
||||||
|
sock.listen(1)
|
||||||
|
print('server listening on', addr)
|
||||||
|
|
||||||
|
# Wait for the client
|
||||||
|
con = None
|
||||||
|
con, addr = sock.accept()
|
||||||
|
print('client connected from', addr)
|
||||||
|
|
||||||
|
# repeat test for a number of iterations
|
||||||
|
for test_iteration in range(TEST_ITERATIONS):
|
||||||
|
|
||||||
|
# Generate a buffer of random data
|
||||||
|
random_list = []
|
||||||
|
for n in range(BUF_SIZE):
|
||||||
|
random_list.append(random.randint(0, 255))
|
||||||
|
write_buf = bytearray(random_list)
|
||||||
|
|
||||||
|
# write BUF_SIZE bytes to the client
|
||||||
|
write_len = con.send(bytearray(write_buf))
|
||||||
|
print('Written %d bytes to client' % write_len)
|
||||||
|
|
||||||
|
# Check size of data written
|
||||||
|
if write_len != BUF_SIZE:
|
||||||
|
raise RuntimeError('wrong amount of data written')
|
||||||
|
|
||||||
|
# Read the data back from the client
|
||||||
|
read_buf = con.read(BUF_SIZE)
|
||||||
|
print('read %d bytes from client' % len(read_buf))
|
||||||
|
|
||||||
|
# Check size of data received
|
||||||
|
if len(read_buf) != BUF_SIZE:
|
||||||
|
raise RuntimeError('wrong amount of data read')
|
||||||
|
|
||||||
|
# Check the data sent and received
|
||||||
|
if read_buf != write_buf:
|
||||||
|
raise RuntimeError('buffer mismatch')
|
||||||
|
|
||||||
|
# All done
|
||||||
|
con.close()
|
||||||
|
sock.close()
|
||||||
|
print("test completed")
|
||||||
@@ -1,67 +1,43 @@
|
|||||||
import network
|
#!/usr/bin/python
|
||||||
import utime as time
|
|
||||||
import usocket as socket
|
|
||||||
|
|
||||||
# Set your wifi ssid and password here
|
import socket
|
||||||
WIFI_SSID = const('')
|
import sys
|
||||||
WIFI_PASSWORD = const('')
|
|
||||||
|
|
||||||
# Set the server address here like 1.2.3.4
|
|
||||||
SERVER_ADDR = const('')
|
|
||||||
|
|
||||||
# These constants should match the server
|
|
||||||
BUF_SIZE = const(2048)
|
|
||||||
SERVER_PORT = const(4242)
|
|
||||||
TEST_ITERATIONS = const(10)
|
|
||||||
|
|
||||||
# Check if wifi details have been set
|
|
||||||
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
|
|
||||||
raise RuntimeError('set wifi ssid and password in this script')
|
|
||||||
|
|
||||||
# Check server ip address set
|
# Check server ip address set
|
||||||
if len(SERVER_ADDR) == 0:
|
if len(sys.argv) < 2:
|
||||||
raise RuntimeError('set the IP address of the server')
|
raise RuntimeError('pass IP address of the server')
|
||||||
|
|
||||||
# Start connection
|
# Set the server address here like 1.2.3.4
|
||||||
wlan = network.WLAN(network.STA_IF)
|
SERVER_ADDR = sys.argv[1]
|
||||||
wlan.active(True)
|
|
||||||
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
|
|
||||||
|
|
||||||
# Wait for connect success or failure
|
# These constants should match the server
|
||||||
max_wait = 20
|
BUF_SIZE = 2048
|
||||||
while max_wait > 0:
|
SERVER_PORT = 4242
|
||||||
if wlan.status() < 0 or wlan.status() >= 3:
|
TEST_ITERATIONS = 10
|
||||||
break
|
|
||||||
max_wait -= 1
|
|
||||||
print('waiting for connection...')
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# Handle connection error
|
|
||||||
if wlan.status() != 3:
|
|
||||||
raise RuntimeError('wifi connection failed %d' % wlan.status())
|
|
||||||
else:
|
|
||||||
print('connected')
|
|
||||||
status = wlan.ifconfig()
|
|
||||||
print( 'ip = ' + status[0] )
|
|
||||||
|
|
||||||
# Open socket to the server
|
# Open socket to the server
|
||||||
sock = socket.socket()
|
sock = socket.socket()
|
||||||
addr = (SERVER_ADDR, SERVER_PORT)
|
addr = (SERVER_ADDR, SERVER_PORT)
|
||||||
sock.connect(addr)
|
sock.connect(addr)
|
||||||
|
|
||||||
# repeat test for a number of iterations
|
# Repeat test for a number of iterations
|
||||||
for test_iteration in range(TEST_ITERATIONS):
|
for test_iteration in range(TEST_ITERATIONS):
|
||||||
|
|
||||||
# Read BUF_SIZE bytes from the server
|
# Read BUF_SIZE bytes from the server
|
||||||
read_buf = sock.read(BUF_SIZE)
|
total_size = BUF_SIZE
|
||||||
print('read %d bytes from server' % len(read_buf))
|
read_buf = b''
|
||||||
|
while total_size > 0:
|
||||||
|
buf = sock.recv(BUF_SIZE)
|
||||||
|
print('read %d bytes from server' % len(buf))
|
||||||
|
total_size -= len(buf)
|
||||||
|
read_buf += buf
|
||||||
|
|
||||||
# Check size of data received
|
# Check size of data received
|
||||||
if len(read_buf) != BUF_SIZE:
|
if len(read_buf) != BUF_SIZE:
|
||||||
raise RuntimeError('wrong amount of data read')
|
raise RuntimeError('wrong amount of data read %d', len(read_buf))
|
||||||
|
|
||||||
# Send the data back to the server
|
# Send the data back to the server
|
||||||
write_len = sock.write(read_buf)
|
write_len = sock.send(read_buf)
|
||||||
print('written %d bytes to server' % write_len)
|
print('written %d bytes to server' % write_len)
|
||||||
if write_len != BUF_SIZE:
|
if write_len != BUF_SIZE:
|
||||||
raise RuntimeError('wrong amount of data written')
|
raise RuntimeError('wrong amount of data written')
|
||||||
|
|||||||
@@ -1,75 +1,51 @@
|
|||||||
import network
|
#!/usr/bin/python
|
||||||
import utime as time
|
|
||||||
import usocket as socket
|
|
||||||
import random
|
|
||||||
|
|
||||||
# Set your wifi ssid and password here
|
import random
|
||||||
WIFI_SSID = const('')
|
import socket
|
||||||
WIFI_PASSWORD = const('')
|
|
||||||
|
# Set server adress to machines IP
|
||||||
|
SERVER_ADDR = "0.0.0.0"
|
||||||
|
|
||||||
# These constants should match the client
|
# These constants should match the client
|
||||||
BUF_SIZE = const(2048)
|
BUF_SIZE = 2048
|
||||||
SERVER_PORT = const(4242)
|
TEST_ITERATIONS = 10
|
||||||
TEST_ITERATIONS = const(10)
|
SERVER_PORT = 4242
|
||||||
|
|
||||||
# Check if wifi details have been set
|
|
||||||
if len(WIFI_SSID) == 0 or len(WIFI_PASSWORD) == 0:
|
|
||||||
raise RuntimeError('Please set wifi ssid and password in this script')
|
|
||||||
|
|
||||||
# Start connection
|
|
||||||
wlan = network.WLAN(network.STA_IF)
|
|
||||||
wlan.active(True)
|
|
||||||
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
|
|
||||||
|
|
||||||
# Wait for connect success or failure
|
|
||||||
max_wait = 20
|
|
||||||
while max_wait > 0:
|
|
||||||
if wlan.status() < 0 or wlan.status() >= 3:
|
|
||||||
break
|
|
||||||
max_wait -= 1
|
|
||||||
print('waiting for connection...')
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# Handle connection error
|
|
||||||
if wlan.status() != 3:
|
|
||||||
raise RuntimeError('wifi connection failed %d' % wlan.status())
|
|
||||||
else:
|
|
||||||
print('connected')
|
|
||||||
status = wlan.ifconfig()
|
|
||||||
print( 'ip = ' + status[0] )
|
|
||||||
|
|
||||||
# Open socket to the server
|
# Open socket to the server
|
||||||
sock = socket.socket()
|
sock = socket.socket()
|
||||||
addr = ('0.0.0.0', SERVER_PORT)
|
sock.bind((SERVER_ADDR, SERVER_PORT))
|
||||||
sock.bind(addr)
|
|
||||||
sock.listen(1)
|
sock.listen(1)
|
||||||
print('server listening on', addr)
|
print("server listening on", SERVER_ADDR, SERVER_PORT)
|
||||||
|
|
||||||
# Wait for the client
|
# Wait for the client
|
||||||
con = None
|
con = None
|
||||||
con, addr = sock.accept()
|
con, addr = sock.accept()
|
||||||
print('client connected from', addr)
|
print("client connected from", addr)
|
||||||
|
|
||||||
# repeat test for a number of iterations
|
# Repeat test for a number of iterations
|
||||||
for test_iteration in range(TEST_ITERATIONS):
|
for test_iteration in range(TEST_ITERATIONS):
|
||||||
|
|
||||||
# Generate a buffer of random data
|
# Generate a buffer of random data
|
||||||
random_list = []
|
random_list = []
|
||||||
for n in range(BUF_SIZE):
|
for n in range(BUF_SIZE):
|
||||||
random_list.append(random.randint(0, 255))
|
random_list.append(random.randint(0, 255))
|
||||||
write_buf = bytearray(random_list)
|
write_buf = bytearray(random_list)
|
||||||
|
|
||||||
# write BUF_SIZE bytes to the client
|
# Write BUF_SIZE bytes to the client
|
||||||
write_len = con.send(bytearray(write_buf))
|
write_len = con.send(bytearray(write_buf))
|
||||||
print('Written %d bytes to client' % write_len)
|
print('Written %d bytes to client' % write_len)
|
||||||
|
|
||||||
# Check size of data written
|
# Check size of data written
|
||||||
if write_len != BUF_SIZE:
|
if write_len != BUF_SIZE:
|
||||||
raise RuntimeError('wrong amount of data written')
|
raise RuntimeError('wrong amount of data written %d' % write_len)
|
||||||
|
|
||||||
# Read the data back from the client
|
# Read the data back from the client
|
||||||
read_buf = con.read(BUF_SIZE)
|
total_size = BUF_SIZE
|
||||||
print('read %d bytes from client' % len(read_buf))
|
read_buf = b''
|
||||||
|
while total_size > 0:
|
||||||
|
buf = con.recv(BUF_SIZE)
|
||||||
|
print('read %d bytes from client' % len(buf))
|
||||||
|
total_size -= len(buf)
|
||||||
|
read_buf += buf
|
||||||
|
|
||||||
# Check size of data received
|
# Check size of data received
|
||||||
if len(read_buf) != BUF_SIZE:
|
if len(read_buf) != BUF_SIZE:
|
||||||
|
|||||||
Reference in New Issue
Block a user