Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. from serial import *
  2. from Tkinter import *
  3.  
  4. serialPort = "/dev/ttyUSB0"
  5. baudRate = 115200
  6. ser = Serial(serialPort , baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
  7.  
  8. #make a TkInter Window
  9. root = Tk()
  10. root.wm_title("Reading Serial")
  11.  
  12. # make a scrollbar
  13. #scrollbar = Scrollbar(root)
  14. #scrollbar.pack(side=RIGHT, fill=Y)
  15.  
  16. # make a text box to put the serial output
  17. #log = Text ( root, width=30, height=30, takefocus=0)
  18. #log.pack()
  19.  
  20. canvas_width = 129
  21. canvas_height = 255
  22. w = Canvas(root,
  23. width=canvas_width,
  24. height=canvas_height)
  25. w.pack()
  26.  
  27. # attach text box to scrollbar
  28. #log.config(yscrollcommand=scrollbar.set)
  29. #scrollbar.config(command=log.yview)
  30.  
  31. #make our own buffer
  32. #useful for parsing commands
  33. #Serial.readline seems unreliable at times too
  34. serBuffer = []
  35.  
  36. def readSerial():
  37. startChar = False
  38. while True:
  39. c = ser.read(1) # attempt to read a character from Serial
  40.  
  41. #was anything read?
  42. if len(c) == 0:
  43. break
  44. c = ord(c)
  45.  
  46. # get the buffer from outside of this function
  47. global serBuffer
  48.  
  49. # check if character is a start
  50.  
  51. if startChar:
  52. serBuffer.append(c)
  53.  
  54. if c == 255:
  55. startChar = True
  56.  
  57. if len(serBuffer) >= 127:
  58. startChar = False
  59. #log.insert('0.0', ''.join(map(str, serBuffer)))
  60. for x in xrange(len(serBuffer)):
  61. #w.delete("all")
  62. w.create_line(x, 255, x, 255-serBuffer[x], fill="#ff0000")
  63. w.create_line(x, 0, x, 255-serBuffer[x], fill="#ffffff")
  64. serBuffer = []
  65.  
  66. root.after(10, readSerial) # check serial again soon
  67.  
  68.  
  69. # after initializing serial, an arduino may need a bit of time to reset
  70. root.after(100, readSerial)
  71.  
  72. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement