bitte um Entschuldigung irgendwie funkt das Ganze noch nicht ...
from tkinter import *
import serial # kommunikation
import serial.tools.list_ports
import time
import tk_tools
##------------variablen------------##
arduino = ""
# Sendeprotokoll #.#M3#.# bzw #G1 X100 F100#
# Werterückgabe #1# oder #0#
##------------funktionen- Stepper------------##
def comNano(befehl, timeout): # befehl zum arduino schicken und antwort zurückgeben
global arduino
if arduino == "": return ""
bytebefehl = bytes(befehl, 'utf-8')
arduino.write(bytebefehl)
vomarduino = ""
delaymax = time.time() + timeout
while delaymax > time.time():
if arduino.inWaiting() > 0: # es ist was da

inputchar = str(arduino.read(1), 'utf-8')
if inputchar >= " ": # kein steuerzeichen
vomarduino += inputchar
else:
if vomarduino > "": # es hat geklappt
return vomarduino
return ""
def suchNano(kennung, timeout): # den mit kennung definierten arduino über xxx sekunden pro port suchen
global arduino
ports = serial.tools.list_ports.comports(include_links=False)
for port in ports :
arduinoport = port.device
try:
arduino = serial.Serial(arduinoport, baudrate = 9600, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, timeout = 1, xonxoff = 0, rtscts = 0)
arduino.setDTR(False) # arduino reset erzwingen ...
arduino.flushInput()
arduino.setDTR(True)
delaymax = time.time() + timeout
while delaymax > time.time():
if arduino.inWaiting() > 0:
vomarduino = str(arduino.readline())
if vomarduino.find(kennung) > 0: # gefunden

v = vomarduino.find(" v") # v suchen
if v == 0: v = vomarduino.find(" V") # V suchen
if v > 0: # versionsnummer ermitteln
try:
n = int(vomarduino[v + 2: v + 5])
except:
n = 0
else:
n = 0
return ("v%03d" % n) # versionsnummer zurückgeben
except:
dummy = 1
arduino = ""
return ""
def findString(text, text2, suchbeginn): # teilstring in string suchen (0: nicht gefunden, sonst stelle)
if suchbeginn < 1: # ganzen string durchsuchen
suchbeginn = 1
return (text.find(text2, suchbeginn - 1) + 1)
##------------startroutine-----------##
print ()
print (" Code_v010 (Slave " + suchNano("Code", 3) + ")")
##------------Funktionen-GUI-----------##
def nix():
return
def schließen():
tkFenster.destroy()
return
def show_pressed(but):
but.config(activebackground='red')
def show_released(but):
but.config(activebackground='yellow')
#-------------------------------------------------------------------------------------------
import webbrowser
new = 1
url = "
http://www.vatican.va/content/vatican/de.html"
def openweb():
webbrowser.open(url,new=new)
#-----------------------------------------------------------------------------
def wechsleFarbeMB():
lab3["fg"]="#FF0000"
lab3["font"]="Arial 16 italic"
lab3["height"] = 2
lab3["width"] = 20
lab3["anchor"] ="center"
#return
def wechsleFarbeMB2():
lab3["fg"]="#000000"
lab3["font"]="Arial 10"
lab3["height"] = 2
lab3["width"] = 20
lab3["anchor"] ="center"
#return
#--------------Test--------------------------------------------------------------------------
def SendText():
values = textfeld1.get('1.0', 'end')
#print("Values", values)
s = values # das feld in einem string
while findString(s, "\n", 0) > 0:
stelle = findString(s, "\n", 0)
if (stelle - 1) > 0:
befehl = "#.#" + s[: stelle - 1] + "#.#"
s = s[stelle: ]
if comNano(befehl, 100)[:3] == "#1#":
print("1")
else:
print("0")
else:
s = s[stelle: ]
print("Stelle = 0")
#------------ Erzeugung des Fensters------------#
tkFenster = Tk()
tkFenster.title('NC-Drahtbiegeautomat')
tkFenster.geometry('670x650')
# Canvas für den Hintergrund
imageHintergrund = PhotoImage(file="Ausgedinge_02.png")
canvasHintergrund = Canvas(master=tkFenster)
canvasHintergrund.place(x=0, y=0, width=670, height=600)
canvasHintergrund.create_image(0, 0, image=imageHintergrund, anchor='nw')
# Button zum Senden
buttonSenden = Button(master=canvasHintergrund, text="Senden", font= ("Arial", 13, "bold"), bg="yellow",fg="red", command=SendText)
show_released(buttonSenden)
buttonSenden.bind("<ButtonPress-1>", lambda ev: [show_pressed(buttonSenden), led.to_green(True)])
buttonSenden.bind('<ButtonRelease-1>', lambda ev:[show_released(buttonSenden), led.to_green(on=False)])
buttonSenden.place(x=90, y=500, width=200, height=50)
# LED
led = tk_tools.Led(tkFenster, size=50)
led.grid(row=0, column=0)
#-----------------------#
"""
#label = Label(tkFenster)
#label.place (x=530, y=360, width=140, height=30)
"""
#--------------manueller Befehl an Arduino--------------------------------------------------------------------------
def SendZ1():
text = "#.#M3#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
time.sleep(1)
text = "#.#M5#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
return
def SendZ2():
text = "#.#M8#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
time.sleep(1)
text = "#.#M9#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
return
def SendZ3():
text = "#.#M1#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
time.sleep(1)
text = "#.#M2#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
return
def SendCut():
text = "#.#M6#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
time.sleep(1)
text = "#.#M7#.#"
print(text)
if comNano(text, 3)[:3] == "#1#":
print("1")
else:
print("0")
return
def SendStop(): #Reset
text = "#.#RESET#.#" #Reset
print(text)
if comNano(text, 3)[:3] == "#1#": #Reset
print("1")
else:
print("0")
return
time.sleep(5)