Skip to content

Instantly share code, notes, and snippets.

@sash13
Created February 21, 2019 01:08
Show Gist options
  • Save sash13/89a2fdf62a4ad2a50ab77e96c97446bc to your computer and use it in GitHub Desktop.
Save sash13/89a2fdf62a4ad2a50ab77e96c97446bc to your computer and use it in GitHub Desktop.
draw fft
from serial import *
from Tkinter import *
serialPort = "/dev/ttyUSB0"
baudRate = 115200
ser = Serial(serialPort , baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
#make a TkInter Window
root = Tk()
root.wm_title("Reading Serial")
# make a scrollbar
#scrollbar = Scrollbar(root)
#scrollbar.pack(side=RIGHT, fill=Y)
# make a text box to put the serial output
#log = Text ( root, width=30, height=30, takefocus=0)
#log.pack()
canvas_width = 129
canvas_height = 255
w = Canvas(root,
width=canvas_width,
height=canvas_height)
w.pack()
# attach text box to scrollbar
#log.config(yscrollcommand=scrollbar.set)
#scrollbar.config(command=log.yview)
#make our own buffer
#useful for parsing commands
#Serial.readline seems unreliable at times too
serBuffer = []
def readSerial():
startChar = False
while True:
c = ser.read(1) # attempt to read a character from Serial
#was anything read?
if len(c) == 0:
break
c = ord(c)
# get the buffer from outside of this function
global serBuffer
# check if character is a start
if startChar:
serBuffer.append(c)
if c == 255:
startChar = True
if len(serBuffer) >= 127:
startChar = False
#log.insert('0.0', ''.join(map(str, serBuffer)))
for x in xrange(len(serBuffer)):
#w.delete("all")
w.create_line(x, 255, x, 255-serBuffer[x], fill="#ff0000")
w.create_line(x, 0, x, 255-serBuffer[x], fill="#ffffff")
serBuffer = []
root.after(10, readSerial) # check serial again soon
# after initializing serial, an arduino may need a bit of time to reset
root.after(100, readSerial)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment