Using tkinter and python to control arduino -


i have posted question before, , user enough answer it. used suggestions , found code not working @ all, after researching , tinkering it, came impasse. here want do:

i want control 2 arduinos using serial connection between arduinos , raspberry pi , using python control them. using gui interface , tkinter library create buttons.

the original problem information go through arduino when user stopped pressing button , needed information sent button pressed , continuously until button released. member suggested using lambda.

although did not work proposed, feel might have had right idea, here code came after suggestion.

import serial running = true ser = serial.serial('/dev/ttyusb0') def send_data(self):     if self.char not none:         ser.write(self.char)     #run again in 100ms. here control how fast     #to send data.  first parameter after number     #of milliseconds wait befor calling function     self.job=self.after(100,self.send_data)  class application(frame):     """defining remote control buttons"""     def __init__(self,master):         self.char = []         self.job = []         self.send_data = []         """initialize frame"""         frame.__init__(self,master)         self.grid() # how many times has user clicked button         self.create_widgets()       def set_char(self,char):         self.char=char     def create_widgets(self):         """creates 4 buttons move servos"""         #create 'up' button         self.button1=button(self,text="up")         self.button1.bind("<buttonpress-1>",lambda x:self.set_char('1'))         self.button1.bind("<buttonrelease-1>",lambda x:self.set_char(none))         self.button1.grid()         send_data(self)          #create 'down' button         self.button2=button(self,text="down")         self.button2.bind("<buttonpress>",lambda x:self.set_char('2'))         self.button2.bind("<buttonrelease>",lambda x:self.set_char(none))         self.button2.grid()          #create 'left' button         self.button3=button(self,text="left")         self.button3.bind("<buttonpress-3>",lambda x:self.set_char('3'))         self.button3.bind("<buttonrelease-3>",lambda x:self.set_char(none))         self.button3.grid()          #creeate 'right' button         self.button4=button(self,text="right")         self.button4.bind("<buttonpress-4>",lambda x:self.set_char('4'))         self.button4.bind("<buttonrelease-4>",lambda x:self.set_char(none))         self.button4.grid()  #main root = tk() root.title("remote control") root.geometry("250x250") app = application(root) root.mainloop()  

your theory workable, has implementation issues. hacked smaller test original code , fixed several mistakes. 1 writes screen few of don't have arduino attached our computers , has 1 button, shows how should work.

import sys tkinter import * tkinter.ttk import *  # mock serial class class mockser(object):      def __init__(self):         self.colcount = 0      def write(self, data):         sys.stdout.write(data)         self.colcount += 1         if self.colcount > 78:             sys.stdout.write('\n')             sys.colcount = 0         sys.stdout.flush()   class application(frame):     """defining remote control buttons"""     def __init__(self, master, writer):         frame.__init__(self, master)         self.writer = writer         self.char = none         self.grid() # how many times has user clicked button         self.create_widgets()         def send_data(self):         if self.char not none:             self.writer.write(self.char)         #run again in 100ms. here control how fast         #to send data.  first parameter after number         #of milliseconds wait befor calling function         self.after(100,self.send_data)      def set_char(self,char):         self.char=char      def create_widgets(self):         """creates 4 buttons move servos"""         #create 'up' button         self.button1=button(self,text="up")         self.button1.bind("<buttonpress-1>",lambda x:self.set_char('1'))         self.button1.bind("<buttonrelease-1>",lambda x:self.set_char(none))         self.button1.grid()         # start send_data poll         self.send_data()  root = tk() root.title("remote control") root.geometry("250x250") writer = mockser() app = application(root, writer) root.mainloop()  

the code doesn't consider real world problems such serial port closing or queue filling, start.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -