Hi there,
I am writing a code in Python (well maybe this is a little presumptuous from my side…) to make a GUI which function is to read a couple of coordinates given by an Arduino in x;y format through Serial, being x and y a couple of natural positive numbers from 1 to 2800.
The 2 numbers are separate by a ; in order to know when one finishes and the second starts, but I could change this for any other method to differentiate the 2 numbers.
The GUI will “draw” a red rectangle where the coordinates of Arduino are pointing.
Here is my problem: I can see the coordinates coming through with a simple sketch that shows them on the monitor, but when I think of putting the 2 received numbers into actual variables in the main code in Python I don’t know how to do it (beginner minus level…). I would like my program to read the Serial data, recognize the first number (before the “;” ) and put it into a variable xpos. Then take the second number (after the “;”) and put it into the second variable, ypos.
I am using Tkinter to draw the background image and all graphic aspects, and plan to use the xpos and ypos variables to draw the rectangle where the coordinates indicate.
This is the code I have written so far:
from serial import *
from tkinter import *
serialPort = "/dev/ttyUSB0"
baudRate = 9600
ser = Serial(serialPort , baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
root = Tk()
root.geometry("1280x800")
root.attributes('-fullscreen',True)
bg = PhotoImage(file = "/home/pi/Pictures/SCREEN2.png")
canvas = Canvas( root, width = 1280, height = 800)
canvas.pack(fill = "both", expand = True)
canvas.create_image( 0, 0, image = bg, anchor = "nw")
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()
# 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():
while True:
c = ser.read() # attempt to read a character from Serial
#was anything read?
if len(c) == 0:
break
# get the buffer from outside of this function
global serBuffer
# check if character is a delimeter
if c == '\r':
c = '' # don't want returns. chuck it
if c == '\n':
serBuffer += "\n" # add the newline to the buffer
#add the line to the TOP of the log
log.insert('0.0', serBuffer)
serBuffer = "" # empty the buffer
else:
serBuffer += c # add to the buffer
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)
#canvas.create_rectangle(200, 200, 300,300, width = 0, fill = 'red')
# Execute tkinter
root.mainloop()
Needless to say that the return of this sketch is TypeError: can only concatenate str (not “bytes”) to str
In fact this is cut-paste from different examples and I didn’t expect to get the 2 coordinates with the code. If anyone would be able to drive me through the way I should get those variables I would appreciate it.
Thanks for watching.
Joan.