for an NMEA-RTK-receiver, I need to forward data from UART to USB (the NMEA sentences 10 time 200 bytes a second) and at the same time data from USB to UART (the NTRIP correction data 200 bytes once per second).
There are no timing requirements to the NMEA data apart from the fact, that the forwarding should have a low jitter in general. Each sentence ends with \r\n.
The NTRIP data (USB to UART) should be send in one block.
There is no timing requirement between the two directions.
In general, the code is working and I can see the bytes on the UART with the help of an oscilloscope.
My problems:
- my code looks awful to me (ok, something I can stand with that sometimes)
- my code has a performance issue
What can I improve?
Init:
Code: Select all
uart = UART(0, 115200, timeout=10, timeout_char=10)
spoll = uselect.poll()
spoll.register(sys.stdin, uselect.POLLIN)
NTRIP = bytearray(501)
NTRIPptr = 0
NTRIPstate = 0 # 0: wait for data; 1: wait for NMEA; 2: do NTRIP
Code: Select all
while (True):
# read NTRIP from USB (once per second)
while spoll.poll(0): # read data from USB and shift to UART (forward SAPOS/NTRIP information)
NTRIP[NTRIPptr] = ord(sys.stdin.read(1))
if NTRIPptr < 500:
NTRIPptr += 1
# send after two NMEA blocks received via UART; NMEA block frequency: 10Hz
if NTRIPstate > 1:
uart.write(NTRIP[0:NTRIPptr])
NTRIPptr, NTRIPstate = 0, 0
NMEAsentence = uart.readline()
if NMEAsentence != None:
sys.stdout.buffer.write(bytes(NMEAsentence))
if "PLSHD" in NMEAsentence: # last sentence contains "$PLSHD...."
if NTRIPptr != 0:
NTRIPstate += 1
else:
led.off()
else:
led.on()
# NMEA sentence example (real position intentionally camouflaged):
# $GNVTG,46.99,T,,M,0.00,N,0.01,K,D*15
# $GNGGA,170810.400,5199.9498700,N,00899.8506100,E,2,49,0.54,203.65,M,47.42,M,,*79
# $GNRMC,170810.400,A,5199.9498700,N,00899.8506100,E,0.00,46.99,010622,,,D,V*39
# $GPHDT,89.086,T*0A
# $PLSHD,1,32,33,0.436,89.086,-0.264*4E