다음은 소프트웨어 직렬의 안정성을 테스트하는 데 사용한 Python입니다. 입력 측은 매우 사소합니다. 파이썬이나 C에서 적절한 비트 뱅 직렬 오픈 호출을하면됩니다. 비트 스트림을 구성하기 위해 파형을 사용해야하기 때문에 출력 측이 더 복잡합니다.
이 코드는 8 비트가 아닌 7 비트를 사용합니다. 테스트는 바이트 당 다른 비트에 대한 지원을 추가하는 것과 거의 동시에 작성되었습니다.
이 코드는 이진 데이터 블록을 랩톱에 연결된 gpio (직렬 동글을 통해)에 씁니다. 랩탑은 수신 직렬 데이터를 출력 직렬 라인에 에코합니다. Pi는 다른 GPIO에서 직렬 데이터를 읽습니다.
이 코드는 전송 된 데이터와 수신 된 데이터 사이의 불일치를 확인합니다. 랩톱에는 오류가없는 것으로 가정되므로 모든 오류는 비트 뱅킹에 있다고 가정합니다.
로그를 보면 19.2kbps보다 작은 것은 견고했습니다. 최대 115.2kbps는 합리적이지만 (체크섬 필요) 230.4kbps는 13 % 바이트 오류율을 나타냅니다.
#!/usr/bin/env python
# bb_serial.py
# 2014-12-23
# Public Domain
# bit bang transmit and receive of serial data
#
# tested by connecting the arbitrary RX/TX gpios to a USB
# serial dongle plugged in to a Linux box.
#
# on the Linux box set the baud and data bits (cs5-cs8)
#
# stty -F /dev/ttyUSB0 19200 cs8
# cat </dev/ttyUSB0 >/dev/ttyUSB0
#
# so the Linux box echoes back data received from the Pi.
#
# laptop timings deviations
#
# baud exp us act us
# 50 20000 13310 * 75
# 75 13333 13310
# 110 9091 13310 * 75
# 134 7462 6792 * 150
# 150 6667 6792
# 200 5000 6792 * 150
# 300 3333 3362
#
import sys
import time
import difflib
import pigpio
RX=19
TX=26
MSGLEN=256
if len(sys.argv) > 1:
baud = int(sys.argv[1])
else:
baud = 115200
if len(sys.argv) > 2:
bits = int(sys.argv[2])
else:
bits = 8
if len(sys.argv) > 3:
runtime = int(sys.argv[3])
else:
runtime = 300
ten_char_time = 100.0 / float(baud)
if ten_char_time < 0.1:
ten_char_time = 0.1
MASK=(1<<bits)-1
# initialise test data
msg = [0] * (MSGLEN+256)
for i in range(len(msg)):
msg[i] = i & MASK
first = 0
pi = pigpio.pi()
pi.set_mode(TX, pigpio.OUTPUT)
# fatal exceptions off (so that closing an unopened gpio doesn't error)
pigpio.exceptions = False
pi.bb_serial_read_close(RX)
# fatal exceptions on
pigpio.exceptions = True
# create a waveform representing the serial data
pi.wave_clear()
TEXT=msg[first:first+MSGLEN]
pi.wave_add_serial(TX, baud, TEXT)
wid=pi.wave_create()
# open a gpio to bit bang read the echoed data
pi.bb_serial_read_open(RX, baud, bits)
# zero error counts
bad_char = 0
total_char = 0
# run for fixed time
start=time.time()
while (time.time()-start) < runtime:
pi.wave_send_once(wid) # transmit serial data
pi.wave_delete(wid)
TXTEXT = TEXT
first += 1
if first >= MSGLEN:
first = 0
TEXT=msg[first:first+MSGLEN]
pi.wave_add_serial(TX, baud, TEXT,bb_bits=7)
while pi.wave_tx_busy(): # wait until all data sent
pass
wid=pi.wave_create()
count = 1
text=""
lt = 0
total_char += MSGLEN
while count: # read echoed serial data
(count, data) = pi.bb_serial_read(RX)
if count:
text += data
lt += count
time.sleep(ten_char_time) # enough time to ensure more data
if text != TXTEXT: # Do sent and received match?
if lt == MSGLEN: # No, is message correct length?
for i in range(MSGLEN): # If so compare byte by byte.
if text[i] != TXTEXT[i]:
# print("{:2X} {:2X}".format(text[i], TXTEXT[i]))
bad_char += 1
else: # Wrong message length, find matching blocks.
ok = 0
s=difflib.SequenceMatcher(None, TXTEXT, text)
for frag in s.get_matching_blocks():
ok += frag[2] # More matching bytes found.
# print(frag)
# print(text, MSGLEN, ok)
if ok < MSGLEN: # Sanity check.
bad_char += (MSGLEN - ok)
else:
print("*** ERRONEOUS good={} LEN={} ***".format(ok, MSGLEN))
print("secs={} baud={} bits={} bad={:.3f}%".
format(runtime, baud, bits, float(bad_char)*100.0/float(total_char)))
print("total={} badchar={}".format(total_char, bad_char))
# free resources
pi.wave_delete(wid)
pi.bb_serial_read_close(RX)
pi.stop()
로그
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 230400; done
secs=300 baud=230400 bad=12.610%
total=249344 badchar=31443
secs=300 baud=230400 bad=12.580%
total=247296 badchar=31111
secs=300 baud=230400 bad=12.669%
total=246528 badchar=31232
secs=300 baud=230400 bad=12.274%
total=249600 badchar=30635
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 115200; done
secs=300 baud=115200 bad=0.378%
total=246784 badchar=934
secs=300 baud=115200 bad=0.152%
total=241408 badchar=368
secs=300 baud=115200 bad=0.189%
total=249088 badchar=472
secs=300 baud=115200 bad=0.347%
total=242688 badchar=843
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 57600; done
secs=300 baud=57600 bad=0.080%
total=220416 badchar=177
secs=300 baud=57600 bad=0.066%
total=219392 badchar=145
secs=300 baud=57600 bad=0.099%
total=219904 badchar=218
secs=300 baud=57600 bad=0.084%
total=219136 badchar=184
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 38400; done
secs=300 baud=38400 bad=0.019%
total=206336 badchar=39
secs=300 baud=38400 bad=0.021%
total=206848 badchar=43
secs=300 baud=38400 bad=0.015%
total=206592 badchar=30
secs=300 baud=38400 bad=0.030%
total=206592 badchar=61
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0
secs=300 baud=19200 bad=0.000%
total=175360 badchar=0
secs=300 baud=19200 bad=0.000%
total=175360 badchar=0
secs=300 baud=19200 bad=0.000%
total=174336 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 75; done
secs=300 baud=75 bad=0.000%
total=2048 badchar=0
secs=300 baud=75 bad=0.000%
total=2048 badchar=0
secs=300 baud=75 bad=0.000%
total=2048 badchar=0
secs=300 baud=75 bad=0.000%
total=2048 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=174592 badchar=0
secs=300 baud=19200 bad=0.000%
total=174592 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=175360 badchar=0
secs=300 baud=19200 bad=0.000%
total=174592 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=174592 badchar=0
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0
secs=300 baud=19200 bad=0.000%
total=175360 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0
secs=300 baud=19200 bad=0.000%
total=174592 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0
harry /ram $ for ((i=0;i<4;i++)); do /code/bb_serial.py 19200; done
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=174848 badchar=0
secs=300 baud=19200 bad=0.000%
total=175104 badchar=0