Eli Miller Eli Miller - 3 months ago 12
Python Question

Baudline : $\tt stdin$ is very slow

I built a radar system with two radars each with I/Q channels. I wrote a nice python script to read in the data from the serial port. The data is interleaved like so:

2.4,2.5,2.3,2.7,
2.2,2.1,2.9,3.1,
...


The python code is:

import serial
import binascii
import struct
#import sys

ser = serial.Serial('/dev/tty.usbserial-FTT86COK', 9600, bytesize=8, timeout=None)
data_in=ser.read(1)
for x in range(0, 50000):
while (data_in!=b'\x2A'):
data_in = ser.read(1)
data_in = ser.read(80);
d = struct.unpack('<HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH', data_in)
i=0
for i in range(0,10):
j=i*4
print str((d[j]/4096.0*5.0)) + ','
print str((d[j+1]-4096)/4096.0*5.0) + ','
print str(d[j+2]/4096.0*5.0) + ','
print str((d[j+3]-4096)/4096.0*5.0) + ','


I then pipe this data to a tool called baudline.

python sensor_in.py | /Applications/baudline.app/Contents/Resources/baudline -reset -samplerate 96000 -channels 4 -quadrature -stdin


For some reason the baudline tool updates very slowly and I can't see the signal in "real time". It might be my sample rate but I have played around with this number and I get the same result. I don't know how to get my sample rate, but I don't think this is the problem or main problem.

I also tried writing to a file and reading in from baud line but same result.

Any help would be great.

Answer

Add sys.stdout.flush() as the last line in your for loop, like so:

 import sys

 ...

 for i in range(0,10):
    j=i*4
    print str((d[j]/4096.0*5.0)) + ',' 
    print str((d[j+1]-4096)/4096.0*5.0) + ','
    print str(d[j+2]/4096.0*5.0) + ','
    print str((d[j+3]-4096)/4096.0*5.0) + ','
    sys.stdout.flush()