forked from cws4204/e-picycle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_serial.py
More file actions
21 lines (17 loc) · 896 Bytes
/
python_serial.py
File metadata and controls
21 lines (17 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# get lines of text from serial port, save them to a file
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 = 'received.txt' # log file to save data in
fmode = 'a' # log file mode = append
with serial.Serial(addr,9600) 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 out