Jan-29-2018, 08:21 PM
After several weeks of reading web pages and books, I have the Python / Arduino serial interface working. It was all easy until I tried to get actual (live) status of Arduino digital outputs. It was finally solved using the “.set()” function, which was suggested by “Don Rozenberg the author of PAGE - Python Automatic GUI Generator". Thanks to all for the feedback I received. I have attached the Python and Arduino scripts.
Python ver 37-32, tkinter. TkVersion 8.6 #! /usr/bin/env python # Arduino_Driver.py import serial import time from tkinter import * ArduinoSerial = serial.Serial('COM4', 9600, timeout=.1) # open serial port time.sleep(2) def led_on(): ArduinoSerial.write(b'1') # set Arduino output pin 13 high var1.set(ArduinoSerial.readline().decode('utf-8').strip())# get Arduino output pin 13 status if var1.get() == '1': var1.set('Pin 13 is ON') def led_off(): ArduinoSerial.write(b'0') # set Arduino output pin 13 low var1.set(ArduinoSerial.readline().decode('utf-8').strip()) # get Arduino output pin 13 status if var1.get() == '0': var1.set('Pin 13 is OFF') def led_Exit(): ArduinoSerial.write(b'0') # set Arduino output pin 13 low and quit ArduinoSerial.close() # close serial port quit() root = Tk() font1 = "-family {Courier New} -size 10 -weight bold -slant roman -underline 0 -overstrike 0" font2 = "-family {Segoe UI} -size 12 -weight bold -slant roman -underline 0 -overstrike 0" font3 = "-family {@Arial Unicode MS} -size 12 -weight bold -slant roman -underline 1 -overstrike 0" font4 = "-family {Segoe UI} -size 10 -weight bold -slant roman -underline 0 -overstrike 0" var1 = StringVar() var1.set('OFF') root.geometry("430x200+600+200") root.title(" Arduino Output Control") root.configure(background="#d9d9d9") btn1 = Button(root, text='Press for Led ON', font = font1, bg = 'light green', highlightbackground= 'black', \ borderwidth = 3, activebackground = 'light gray', relief=RAISED, command=led_on) # activate Arduino pin 13 btn1.place(relx=0.34, rely=0.10, height=30, width=150) btn2 = Button(root, text='Press for Led OFF', font = font1, bg = 'red2', fg = 'white', highlightbackground= 'black', \ borderwidth = 3, activebackground = 'light gray', relief=RAISED, command=led_off) # deactivate Arduino pin 13 btn2.place(relx=0.34, rely=0.30, height=30, width=150) btn3 = Button(root, text='Press to Exit', font = font3, bg = 'maroon1', highlightbackground= 'black', \ borderwidth = 3, activebackground = 'gray', relief=RAISED,command=led_Exit) # close serial port and quit program btn3.place(relx=0.34, rely=0.50, height=30, width=150) lbl1 = Label (root, textvariable = var1, font = font2, bg = 'light blue', fg = 'black', highlightbackground= 'black', \ borderwidth = 3, relief=RAISED) # message label for status of arduino pin 13 lbl1.place(relx=0.34, rely=0.70, height=30, width=150) root.mainloop()
Arduino ver 1.8.5, Arduino UNO int data; void setup() { Serial.begin(9600); //initialize serial COM at 9600 baudrate pinMode(13, OUTPUT); //make the LED pin (13) as output digitalWrite (13, LOW); } void loop() { if (Serial.available()> 0) { data = Serial.read(); } if (data == '1') { digitalWrite (13, HIGH); Serial.println(digitalRead(13)); data = '2'; } else if (data == '0') { digitalWrite (13, LOW); Serial.println(digitalRead(13)); data = '2'; } }