我有一个程序可以读取Arduino UNO的Digital PINS。默认情况下,我已使用“ PULLUP”关键字打开了所有的Digital PINS。现在,如果我将“跳线”插入任何数字PIN,则为PULLDOWN(关键字)。我在间隔0.5毫秒后连续读取数字PINS的状态(此时间间隔在Arduino IDE中进行了编码),它会给我“ 1”和“ 0”,如果将跳线插入任何数字PIN中,它将给出“ 0” ”,否则它将为“ 1”。

然后将“ 0”和“ 1”存储在数字PINS的两个数组中,即“用于保持先前状态的预数据,用于保持当前状态的新数据”,并在GUI屏幕上显示状态。

首次运行Arduino UNO时,“ newdata”和“ predata”相同。然后,我有一个无限While循环,该循环连续读取Digital PINS并将其存储在“ curdata”数组中,并将其与“ predata”数组进行比较。如果“ predata”和“ curdata”数组不匹配,它将存储“ curdata”数组的所有值写入“ predata”并在GUI屏幕上显示。

除此if语句外,所有代码均正常运行,未在执行。由于这个原因,程序卡住了:

if (curdata[a7] != predata[a7]):
     predata[a7]=curdata[a7]
     oldstatus()
     newstatus()


*完整代码:

from serial import Serial
import time
from tkinter import *
import tkinter as tk

#Making Connection with Arduino
arduinodata = Serial("COM6",9600)

#Reading Data from Arduino
data1 = arduinodata.readline()

#Configuring GUI Screen
win = Tk()
win.title("Arduino")
win.geometry("800x600+50+50")
win.config(bg='white')

#Heading Label
label1=Label(win, text="Digital PIN Status", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid", padx=20, pady=20) #"flat", "raised", "sunken", "ridge", "solid", and "groove"
label1.pack(pady=(15,60))

#Converting Digital PINS data into string and storing them into firstdata array
firstdata=str(data1)

#Inintailizating Arrays
predata=[]
newdata=[]
curdata=[]

#Storing First state of first ten Digital PIINS into the "predata" and "curdata" array.
i=2
for a0 in range(10):
    predata.append(firstdata[i])
    newdata.append(firstdata[i])
    i=i+2

#Displaying Digital PIN Number on GUI Screen
lblframe = tk.Frame(win)
for a1 in range(10):
    pre1=Label(lblframe, text=("PIN",(a1+2)), font=("Calibri",12, "bold"), bg="white", borderwidth=1, relief="solid", padx=5, pady=2)
    pre1.grid(row=0, column=a1)

#Displaying data of "predata" array on GUI Screen
for a2 in range(10):
    binary1 = predata[a2]
    if ( binary1 == "1" ):
        pre2=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
        pre2.grid(row=1, column=a2, sticky="nw")
    else:
        pre2=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
        pre2.grid(row=1, column=a2, sticky="nw")

#Displaying data of "curdata" on GUI Screen.
for a3 in range(10):
    binary2 = newdata[a3]
    if (binary2 == "1"):
        pre3=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
        pre3.grid(row=2, column=a3, sticky="nw")
    else:
        pre3=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
        pre3.grid(row=2, column=a3, sticky="nw")

lblframe.pack()

#This Function is to change value of predata on GUI Screen
def oldstatus():
    for a4 in range(10):
        binary=predata[a4]
        if(binary=="1"):
            pre2.config(text="OFF")
        else:
            pre2.config(text="ON")

#This Function is to change value of curdata on GUI Screen
def newstatus():
    for a5 in range(10):
        binary=curdata[a5]
        if(binary=="1"):
            pre3.config(text="OFF")
        else:
            pre3.config(text="ON")

#This Function is to read Digital PINS continuously and if found Change then update predata and
#curdata array and it run only when button is pressed.
def allstatus():
    while True:
        data2 = arduinodata.readline()
        seconddata=str(data2)
        j=2
        for a6 in range(10):
            curdata.append(seconddata[j])
            j=j+2
        for a7 in range(10):
            if (curdata[a7] != "1"):
                predata[a7]=curdata[a7]
                oldstatus()
                newstatus()

#It is to run allstatus function.
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=allstatus)
button1.pack(pady=(30,0))

win.mainloop()


输出:

第一输出:

python - 如果语句未执行?-LMLPHP

在按下开始按钮时:

python - 如果语句未执行?-LMLPHP

最佳答案

试试这个:

from serial import Serial
import time
from tkinter import *
import tkinter as tk

#Reading Arduino
arduinodata = Serial("COM6",9600)

#Storing Arduino Data in data1 Array
data1 = arduinodata.readline()

#Creating GUI Screen
win = Tk()
win.title("Arduino")
win.geometry("800x600+50+50")
win.config(bg='white')

#Displaying Heading on GUI Screen
label1=Label(win, text="Digital PIN Status", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid", padx=20, pady=20) #"flat", "raised", "sunken", "ridge", "solid", and "groove"
label1.pack(pady=(15,60))

#Converting Datatype of data1 array from binary to string.
firstdata=str(data1)

#Initiliazating Array to store Values of Current and Previous state of
#Arduino Digital Pins.
predata=[]
newdata=[]

#Storing Arduino data into arrays.
i=2
for a0 in range(10):
    predata.append(firstdata[i])
    newdata.append(firstdata[i])
    i=i+2

lblframe = tk.Frame(win)
#Displaying Digital PIN heading on GUI Screen.
for a1 in range(10):
    pre1=Label(lblframe, text=("PIN",(a1+2)), font=("Calibri",12, "bold"), bg="white", borderwidth=1, relief="solid", padx=5, pady=2)
    pre1.grid(row=0, column=a1)

#mylabels function will display values of predata and newdata array on GUI
#Screen
def mylabels():
    for a2 in range(10):
        #Displaying Data of predata array on GUI Screen
        if ( int(predata[a2]) == 1 ):
            pre2=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
            pre2.grid(row=1, column=a2, sticky="nw")
        else:
            pre2=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
            pre2.grid(row=1, column=a2, sticky="nw")

    #Displaying Data of newdata array on GUI Screen
    for a3 in range(10):
        if (int(newdata[a3]) == 1):
            pre3=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
            pre3.grid(row=2, column=a3, sticky="nw")
        else:
            pre3=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
            pre3.grid(row=2, column=a3, sticky="nw")

lblframe.pack()

#Calling mylabel() function automatically when the GUI Screen is created.
mylabels()

#Statuschanger function to change the status of predata and newdata arrays on
#GUI and in array.
def statuschanger():
    #Clearing predata and newdata array to store new values in it.
    predata.clear()
    newdata.clear()
    #Again Reading the Digital PINS of Arduino Board.
    data2 = arduinodata.readline()
    #Converting data of arduino Digital PINS from binary to string.
    seconddata=str(data2)
    #Storing Arduino data into arrays.
    j=2
    for a6 in range(10):
        predata.append(seconddata[j])
        newdata.append(seconddata[j])
        j=j+2
    #Calling mylabel function to Updata Values on GUI Screen.
    mylabels()
    #Calling statuschanger function again and again after every 100msec.
    win.after(100, statuschanger)

#Button1 to run the statuschanger function.
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=statuschanger)
button1.pack(pady=(30,0))

win.mainloop()

07-24 14:10