Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement