Skip to content

Instantly share code, notes, and snippets.

@turbinenreiter
Created December 10, 2013 20:43
Show Gist options
  • Save turbinenreiter/7898985 to your computer and use it in GitHub Desktop.
Save turbinenreiter/7898985 to your computer and use it in GitHub Desktop.
Code to read data from the serial port and plot it.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
import serial
app = QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()
data = [0]
raw=serial.Serial("/dev/ttyACM0",9600)
raw.open()
def update():
global curve, data
line = raw.readline()
data.append(int(line))
xdata = np.array(data, dtype='float64')
curve.setData(xdata)
app.processEvents()
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
@turbinenreiter
Copy link
Author

@srikarpv

If you are on linux, you can use picocom to read the serial data and pipe it into a file.

i.e.:
picocom /dev/ttyACM0 > log.csv

Hit ctrl-c if you want to stop logging. You can use tee to see and log the stream at the same time.

If you want to do it in python, you just have to take the above code and write the variable line to a file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment