Login
Register
Forum
Kontakt
Bitmania
2011-07-16 19:58
by bitman
Eine kleine Python-Tkinter-GUI für das Net-IO
AVR-NET-IO

Ich hab mal eine kleine GUI in Python und Tkinter gebaut mit der man die Ports An und Aus-schalten kann.
Einmal einzeln oder auch alle gleichzeitig.




Inklusive Knight-Rider [tm] Funktion (ich konnte nicht anders).



Falls also jemand den AVR-NET-IO unter Linux testen möchte, kann er damit direkt loslegen.
Aktuell fehlen noch ADC, IN und LCD

Hier der Code (bitte keine Kritik, das Ding ist in Eille zusammenhackt und ich kann eigentlich gar kein Tkinter)
#!/usr/bin/python
import socket, struct, time
from Tkinter import *
ip = '192.168.0.90' #
port = 50290

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt( socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0) )
s.settimeout(10)


def connect():
try:
s.connect((ip, port))
print(": Socket created : "+str(s))
return(s)
except socket.error, (value,message):
if s:
s.close()
print "Could not open socket: " + message
sys.exit(1)


class App:
def __init__(self, master):
s=connect()
frame = Frame(master)

Label(master, text="all ").grid(row=1, sticky=E)
Label(master, text="Out 1 ").grid(row=2, sticky=E)
Label(master, text="Out 2 ").grid(row=3, sticky=E)
Label(master, text="Out 3 ").grid(row=4, sticky=E)
Label(master, text="Out 4 ").grid(row=5, sticky=E)
Label(master, text="Out 5 ").grid(row=6, sticky=E)
Label(master, text="Out 6 ").grid(row=7, sticky=E)
Label(master, text="Out 7 ").grid(row=8, sticky=E)
Label(master, text="Out 8 ").grid(row=9, sticky=E)
Label(master, text="Knight").grid(row=10, sticky=E)

Label(master, text="ADC-1").grid(row=11, sticky=E)
Label(master, text="ADC-2").grid(row=12, sticky=E)
Label(master, text="ADC-3").grid(row=13, sticky=E)
Label(master, text="ADC-4").grid(row=14, sticky=E)

Label(master, text="In 1").grid(row=15, sticky=E)
Label(master, text="In 2").grid(row=16, sticky=E)
Label(master, text="In 3").grid(row=17, sticky=E)
Label(master, text="In 4").grid(row=18, sticky=E)

Label(master, text="LCD 1").grid(row=19, sticky=E)
Label(master, text="LCD 2").grid(row=20, sticky=E)


ex = Button(master, text="QUIT", fg="red", command=frame.quit, width=20)
out_on_01 = Button(master, text="on", command=self.allon)
out_on_02 = Button(master, text="on", command=self.out1on)
out_on_03 = Button(master, text="on", command=self.out2on)
out_on_04 = Button(master, text="on", command=self.out3on)
out_on_05 = Button(master, text="on", command=self.out4on)
out_on_06 = Button(master, text="on", command=self.out5on)
out_on_07 = Button(master, text="on", command=self.out6on)
out_on_08 = Button(master, text="on", command=self.out7on)
out_on_09 = Button(master, text="on", command=self.out8on)
out_off_01 = Button(master, text="off", command=self.alloff)
out_off_02 = Button(master, text="off", command=self.out1off)
out_off_03 = Button(master, text="off", command=self.out2off)
out_off_04 = Button(master, text="off", command=self.out3off)
out_off_05 = Button(master, text="off", command=self.out4off)
out_off_06 = Button(master, text="off", command=self.out5off)
out_off_07 = Button(master, text="off", command=self.out6off)
out_off_08 = Button(master, text="off", command=self.out7off)
out_off_09 = Button(master, text="off", command=self.out8off)
rid = Button(master, text="ride", command=self.ride)

adc1 = Label(master, text="n.a.")
adc2 = Label(master, text="n.a.")
adc3 = Label(master, text="n.a.")
adc4 = Label(master, text="n.a.")

in1 = Label(master, text="n.a.")
in2 = Label(master, text="n.a.")
in3 = Label(master, text="n.a.")
in4 = Label(master, text="n.a.")

lcd1 = Entry(master, width=8)
lcd2 = Entry(master, width=8)

ex.grid(row=0, columnspan=3)

out_on_01.grid(row=1, column=1, sticky=E)
out_on_02.grid(row=2, column=1, sticky=E)
out_on_03.grid(row=3, column=1, sticky=E)
out_on_04.grid(row=4, column=1, sticky=E)
out_on_05.grid(row=5, column=1, sticky=E)
out_on_06.grid(row=6, column=1, sticky=E)
out_on_07.grid(row=7, column=1, sticky=E)
out_on_08.grid(row=8, column=1, sticky=E)
out_on_09.grid(row=9, column=1, sticky=E)

rid.grid(row=10, column=1, columnspan=2)

out_off_01.grid(row=1, column=2, sticky=W)
out_off_02.grid(row=2, column=2, sticky=W)
out_off_03.grid(row=3, column=2, sticky=W)
out_off_04.grid(row=4, column=2, sticky=W)
out_off_05.grid(row=5, column=2, sticky=W)
out_off_06.grid(row=6, column=2, sticky=W)
out_off_07.grid(row=7, column=2, sticky=W)
out_off_08.grid(row=8, column=2, sticky=W)
out_off_09.grid(row=9, column=2, sticky=W)

adc1.grid(row=11, column=1, columnspan=2)
adc2.grid(row=12, column=1, columnspan=2)
adc3.grid(row=13, column=1, columnspan=2)
adc4.grid(row=14, column=1, columnspan=2)

in1.grid(row=15, column=1, columnspan=2)
in2.grid(row=16, column=1, columnspan=2)
in3.grid(row=17, column=1, columnspan=2)
in4.grid(row=18, column=1, columnspan=2)

lcd1.grid(row=19, column=1, columnspan=2)
lcd2.grid(row=20, column=1, columnspan=2)



def out1on(self):
self.mysend("setport 1.1
")
def out1off(self):
self.mysend("setport 1.0
")
def out2on(self):
self.mysend("setport 2.1
")
def out2off(self):
self.mysend("setport 2.0
")
def out3on(self):
self.mysend("setport 3.1
")
def out3off(self):
self.mysend("setport 3.0
")
def out4on(self):
self.mysend("setport 4.1
")
def out4off(self):
self.mysend("setport 4.0
")
def out5on(self):
self.mysend("setport 5.1
")
def out5off(self):
self.mysend("setport 5.0
")
def out6on(self):
self.mysend("setport 6.1
")
def out6off(self):
self.mysend("setport 6.0
")
def out7on(self):
self.mysend("setport 7.1
")
def out7off(self):
self.mysend("setport 7.0
")
def out8on(self):
self.mysend("setport 8.1
")
def out8off(self):
self.mysend("setport 8.0
")

def allon(self):
self.mysend("setport 1.1
")
self.mysend("setport 2.1
")
self.mysend("setport 3.1
")
self.mysend("setport 4.1
")
self.mysend("setport 5.1
")
self.mysend("setport 6.1
")
self.mysend("setport 7.1
")
self.mysend("setport 8.1
")
def alloff(self):
self.mysend("setport 1.0
")
self.mysend("setport 2.0
")
self.mysend("setport 3.0
")
self.mysend("setport 4.0
")
self.mysend("setport 5.0
")
self.mysend("setport 6.0
")
self.mysend("setport 7.0
")
self.mysend("setport 8.0
")
def ride(self): # ride self? WTF
for x in range(3):
for t in range(8):
self.mysend("setport "+str(t+1)+".1
")
time.sleep(.1)
for t in range(8):
self.mysend("setport "+str(t+1)+".0
")
time.sleep(.1)
for t in range(8):
self.mysend("setport "+str(8-t)+".1
")
time.sleep(.1)
for t in range(8):
self.mysend("setport "+str(8-t)+".0
")
time.sleep(.1)

def mysend(self, tmp):
print "Send: "+tmp[:-1]
bytes_send = s.send(tmp)
#print(": Sent "+str(bytes_send)+" bytes")
try:
tmp2=s.recv(4096)
except socket.error, error:
print(":socket-error: "+str(error))
sys.exit()
#return(tmp2)


root = Tk()

app = App(root)

root.mainloop()