This is my first time on a forum so bare with me. I am putting together a project using a raspberry pi and python to collect ASCII data from a GPS and echo sounder, to collect geolocated depths. Now i have hit a serious brick wall, i have managed to write the following script which (altering the baud rates) works for both devices, BUT heres the problem, how do I run both devices at the same time and output a single file? Or how do I automatically flip/flop between the two devices and collect one round of data at a time? (this is what I got so far)
Code: Select all
from __future__ import print_function
import serial, io
addr = '/dev/ttyUSB0' # serial port to read data from
baud = 9600 # baud rate for serial port
fname = 'log.dat' # log file to save data in
fmode = 'a' # log file mode = append
with serial.Serial(addr,baud) as pt, open(fname,fmode) as outf:
spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
spb.readline() # throw away first line; likely to start mid-sentence (incomplete)
while (1):
x = spb.readline() # read one line of text from serial port
print (x,end='') # echo line of text on-screen
outf.write(x) # write line of text to file
outf.flush() # make sure it actually gets written outThanks for any help,
Paul