Skip to content

Instantly share code, notes, and snippets.

@nfarring
Created November 9, 2011 01:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nfarring/1349971 to your computer and use it in GitHub Desktop.
Save nfarring/1349971 to your computer and use it in GitHub Desktop.
Serial Bit Error Rate Tester in Python
#!/usr/bin/env python
#
# Measure the bit error rate if a serial port in loopback.
#
import serial
import sys
import traceback
PORT = 'COM6'
BAUDRATE = 2000000
TIMEOUT = 0.1
TRIALS = 10
N = 512
A = 'ABCDEFGHIJKLMNOP'*N
total_errors = 0
total_sent = 0
def send_and_receive():
global total_sent
global total_errors
N0 = len(A)
N1 = s.write(A)
if N0 != N1: raise Exception('sent %d of %d octets' % (N1, N0))
B = s.read(N1)
N2 = len(B)
if N1 != N2: raise Exception('received %d of %d octets' % (N2, N1))
for a, b in zip(A, B):
if a != b: total_errors += 1
total_sent += N1
# Run the experiment.
try:
s = serial.Serial(port=PORT,baudrate=BAUDRATE,timeout=TIMEOUT)
s.flushInput()
s.flushOutput()
for i in xrange(TRIALS):
send_and_receive()
except:
traceback.print_exc(file=sys.stdout)
print('failed on test %d of %d with N=%d' % (i+1, TRIALS, N))
finally:
s.close()
# Compute BER.
ber = float(total_errors)/float(total_sent)
print('%d/%d = %f BER' % (total_errors,total_sent,ber))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment