Skip to content
Snippets Groups Projects
Commit f86a0ba4 authored by dawehr's avatar dawehr
Browse files

Added test scripts for testing WiFi and UART bridge.

parent 1daf1972
No related branches found
No related tags found
No related merge requests found
import socket
import serial
TCP_IP = "192.168.1.1"
TCP_PORT = 8080
msg_size = 1024
message = bytes(i % 256 for i in range(msg_size))
dropped = True
ser = serial.Serial('COM6', 921600, timeout=0.01)
ser.reset_input_buffer()
if ser.in_waiting:
print("that doesn't work")
while True:
if dropped:
attempts = 0
while attempts < 5:
print("Trying to connect")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
try:
sock.connect((TCP_IP, TCP_PORT))
dropped = False
break
except:
attempts += 1
if dropped:
print("Failed to connect")
break
print("connected")
try:
print("Sending {} bytes".format(len(message)))
ser.write(message)
except Exception as e:
print("Failed to send all data")
continue
received = []
while len(received) < msg_size:
try:
just_received = sock.recv(msg_size - len(received))
except Exception as e:
print("Exception when receiving: ", e)
break
if len(just_received) == 0:
print("Your socket broke")
break
received.extend(just_received)
if len(received) != msg_size:
print("\tError: Received {} bytes".format(len(received)))
elif bytes(received) != message:
print("\tError: Received data does not match")
else:
print("\tYou're a winner!")
debug_msg = ser.read(4096)
if len(debug_msg) != 0:
print(debug_msg.decode())
import socket
import serial
TCP_IP = "192.168.1.1"
TCP_PORT = 8080
msg_size = 1024*100
message = bytes(i % 256 for i in range(msg_size))
dropped = True
ser = serial.Serial('COM6', 921600, timeout=5)
ser.reset_input_buffer()
if ser.in_waiting:
print("that doesn't work")
while True:
if dropped:
attempts = 0
while attempts < 5:
print("Trying to connect")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
try:
sock.connect((TCP_IP, TCP_PORT))
dropped = False
break
except:
attempts += 1
if dropped:
print("Failed to connect")
break
print("connected")
try:
print("Sending {} bytes".format(len(message)))
sock.sendall(message)
except Exception as e:
print("Failed to send all data")
continue
received = ser.read(msg_size)
if len(received) != msg_size:
print("\tError: Received {} bytes".format(len(received)))
elif received != message:
print("\tError: Received data does not match")
else:
print("\tYou're a winner!")
import socket
import time
import csv
TCP_IP = "192.168.1.1"
# UDP_IP = "127.0.0.1"
TCP_PORT = 8080
# sock.bind(('', UDP_PORT))
message = bytes(range(32))
times_full = []
times_network = []
times = [0.0]*100
dropped = True
response = bytes("initial", 'ASCII')
addr = "initial"
recvd_data = []
for i in range(100):
if dropped:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
sock.connect((TCP_IP, TCP_PORT))
dropped = False
send_msg = list(message)
send_msg[0] = i
send_msg = bytes(send_msg)
send_time = time.clock()
times[i] = send_time
# send_time = time.clock()
sock.send(send_msg)
try:
response = sock.recv(1024)
recvd_data.extend(response)
except:
print("timed out")
dropped = True
if len(recvd_data) >= 32:
end_time = time.clock()
response = bytes(recvd_data[0:32])
recvd_data = recvd_data[32:]
msg_id = int(response[0])
latency = end_time - times[msg_id]
# serial_time = int.from_bytes(response[0:4], byteorder='big') / 1000
serial_time = 0
# times_full.append(1000 * (end_time - send_time) - 0)
times_full.append(1000 * latency)
times_network.append(1000 * (end_time - send_time) - serial_time)
print("received " + str(response) + " in " + str(times_full[-1]) + " from " + str(addr))
time.sleep(0.1)
# with open("tcp_dist.csv", 'w', newline='') as f:
# writer = csv.writer(f)
# for time in times_network:
# time = time + 2.6
# writer.writerow([time])
for time in [times_full, times_network]:
print("lowest: " + str(min(time)))
print("highest: " + str(max(time)))
print("median: " + str(sorted(time)[int(len(time) / 2)]))
print("average; " + str(sum(time) / len(time)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment