Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #
  4. # Measure the bit error rate if a serial port in loopback.
  5. #
  6.  
  7. import serial
  8. import sys
  9. import traceback
  10.  
  11. PORT = 'COM6'
  12. BAUDRATE = 2000000
  13. TIMEOUT = 0.1
  14. TRIALS = 10
  15. N = 512
  16. A = 'ABCDEFGHIJKLMNOP'*N
  17.  
  18. total_errors = 0
  19. total_sent = 0
  20.  
  21. def send_and_receive():
  22. global total_sent
  23. global total_errors
  24. N0 = len(A)
  25. N1 = s.write(A)
  26. if N0 != N1: raise Exception('sent %d of %d octets' % (N1, N0))
  27. B = s.read(N1)
  28. N2 = len(B)
  29. if N1 != N2: raise Exception('received %d of %d octets' % (N2, N1))
  30. for a, b in zip(A, B):
  31. if a != b: total_errors += 1
  32. total_sent += N1
  33. # Run the experiment.
  34. try:
  35. s = serial.Serial(port=PORT,baudrate=BAUDRATE,timeout=TIMEOUT)
  36. s.flushInput()
  37. s.flushOutput()
  38. for i in xrange(TRIALS):
  39. send_and_receive()
  40. except:
  41. traceback.print_exc(file=sys.stdout)
  42. print('failed on test %d of %d with N=%d' % (i+1, TRIALS, N))
  43. finally:
  44. s.close()
  45. # Compute BER.
  46. ber = float(total_errors)/float(total_sent)
  47. print('%d/%d = %f BER' % (total_errors,total_sent,ber))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement