Created
May 12, 2015 11:06
-
-
Save openp2pdesign/adf137826b688f08ab35 to your computer and use it in GitHub Desktop.
A desktop app in wxPython+Matplotlib that draws according data from a serial port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
import wx | |
import serial | |
# Import matplotlib for wxPython | |
import matplotlib | |
matplotlib.use('WXAgg') | |
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas | |
from matplotlib.backends.backend_wx import NavigationToolbar2Wx | |
from matplotlib.figure import Figure | |
class MyFrame(wx.Frame): | |
def __init__(self, parent, title): | |
super(MyFrame, self).__init__(parent, title=title, | |
size=(350, 250)) | |
# Initialize the Matplotlib graph | |
self.figure = Figure() | |
self.axes = self.figure.add_subplot(111) | |
self.canvas = FigureCanvas(self, -1, self.figure) | |
# Create a timer for redrawing the frame every 100 milliseconds | |
self.Timer = wx.Timer(self) | |
self.Timer.Start(100) | |
self.Bind(wx.EVT_TIMER, self.OnPaint) | |
# Show the frame | |
self.Centre() | |
self.Show() | |
def OnPaint(self, event=None): | |
# Get data from serial port | |
value = arduino.readline() | |
# Draw the serial data | |
# Cleare the previos graph | |
self.axes.clear() | |
# Set the Y axis limit in order to provide consistency in the visualization | |
self.axes.set_ylim([0,100]) | |
# Draw the bar | |
self.axes.bar(1,value) | |
# Update the graph | |
self.canvas.draw() | |
# Main program | |
if __name__ == '__main__': | |
# Connect to serial port first | |
try: | |
arduino = serial.Serial('/dev/tty.usbmodem1421', 9600) | |
except: | |
print "Failed to connect" | |
exit() | |
# Create and launch the wx interface | |
app = wx.App() | |
MyFrame(None, 'Serial data test') | |
app.MainLoop() | |
# Close the serial connection | |
arduino.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment