From 744280fab850d0649e59e727de8956be2c78e092 Mon Sep 17 00:00:00 2001 From: jfs atom Date: Sat, 12 Jun 2021 21:34:27 +0200 Subject: [PATCH] arduino connection --- gui/SerialEvent/SerialEvent.ino | 60 +++++++++++++++++++++++++++++++++ gui/ser_one.py | 22 +++++++++--- 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 gui/SerialEvent/SerialEvent.ino diff --git a/gui/SerialEvent/SerialEvent.ino b/gui/SerialEvent/SerialEvent.ino new file mode 100644 index 0000000..29480f7 --- /dev/null +++ b/gui/SerialEvent/SerialEvent.ino @@ -0,0 +1,60 @@ +/* + Serial Event example + + When new serial data arrives, this sketch adds it to a String. + When a newline is received, the loop prints the string and clears it. + + A good test for this is to try it with a GPS receiver that sends out + NMEA 0183 sentences. + + NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or + other ATmega32U4 based boards. + + created 9 May 2011 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/SerialEvent +*/ + +String inputString = ""; // a String to hold incoming data +bool stringComplete = false; // whether the string is complete + +void setup() { + // initialize serial: + Serial.begin(9600); + // reserve 200 bytes for the inputString: + inputString.reserve(200); +} + +void loop() { + // print the string when a newline arrives: + if (stringComplete) { + String ss = " I got :"; + ss = ss + inputString; + Serial.println(ss); + // clear the string: + inputString = ""; + stringComplete = false; + } +} + +/* + SerialEvent occurs whenever a new data comes in the hardware serial RX. This + routine is run between each time loop() runs, so using delay inside loop can + delay response. Multiple bytes of data may be available. +*/ +void serialEvent() { + while (Serial.available()) { + // get the new byte: + char inChar = (char)Serial.read(); + // add it to the inputString: + inputString += inChar; + // if the incoming character is a newline, set a flag so the main loop can + // do something about it: + if (inChar == '\n') { + stringComplete = true; + } + } +} diff --git a/gui/ser_one.py b/gui/ser_one.py index 01e2047..058362b 100644 --- a/gui/ser_one.py +++ b/gui/ser_one.py @@ -16,22 +16,34 @@ def initComPort(index): serialObj.open() for onePort in ports: - comButton = Button(root,text=onePort,font=('Calibri','13'),height=1, width=45,command=functools.partial(initComPort,index=ports.index(onePort))) + comButton = Button(root,text=onePort,font=('Calibri','8'),height=1, width=45,command=functools.partial(initComPort,index=ports.index(onePort))) comButton.grid(row=ports.index(onePort),column=0) dataCanvas = Canvas(root,width=600,height=400, bg='white') dataCanvas.grid(row=0, column=1,rowspan=100) vsb = Scrollbar(root,orient='vertical',command= dataCanvas.yview) -vsb.grid(row=0,column=2,columnspan=100,sticky='ns') +vsb.grid(row=0,column=2,rowspan=100,sticky='ns') dataCanvas.config(yscrollcommand=vsb.set) dataFrame = Frame(dataCanvas,bg='white') dataCanvas.create_window((10,0),window=dataFrame,anchor='nw') -def letsDoIt(): - serialObj.write(b'Ok there\n') +def homeAll(): + serialObj.write(b'\n') +def homeX(): + serialObj.write(b'\n') +def homeY(): + serialObj.write(b'\n') +def homeZ(): + serialObj.write(b'\n') -b1 = Button(root,text='Click me',command=letsDoIt) +b1 = Button(root,text='Home All',command=homeAll) b1.grid(row=0,column=3) +b2 = Button(root,text='Home X',command=homeX) +b2.grid(row=1,column=3) +b3 = Button(root,text='Home Y',command=homeY) +b3.grid(row=2,column=3) +b4 = Button(root,text='Home Z',command=homeZ) +b4.grid(row=3,column=3) def checkSerialPort(): if serialObj.isOpen() and serialObj.in_waiting: