Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1247

Python • Serial communication between arduino and python pausing when Guizero button pressed

$
0
0
Hi everyone,
Excuse me if a similar topic has been answered before, as I am a fairly inexperienced programmer, but I have looked through the forums and been unable to understand/apply any previous posts' solutions.

I have been coding a GUI using guizero for communication between my Arduino Mega and my RPi 4. The arduino is responsible for controlling some motors and takes simple serial inputs to set a target tension. The taken inputs are "on/off", and numbers (interpreted as double()) for the tension values. It then outputs a series of calculated values for monitoring, diagnostic etc.

Here is the code for my RPi4:

Code:

from guizero import App, Text, PushButton, TextBox, Boximport serialimport threadingimport time# Initialize global variablespower_state = False# Initializing serial communication with ClearCore via USB cableser = serial.Serial('COM9', 9600, timeout=1)#Define functionsdef change_power():    global power_state # pulls global power variable    if (power_state == False):        power_state = True # updates to on        message.value = "Power: on" # displays power state on GUI                #command = 'on'         ser.write("on".encode('ascii')) # sends 'On' via serial to ClearCore        print("Sent: On")  # Debug print statement            else:        power_state = False        message.value = "Power: Off"        ser.write("Off".encode('ascii')) # sends 'Off' via serial to ClearCore        #ser.flush()  # Ensure the serial buffer is flushed??        print("Sent: Off")  # Debug print statementdef send_serial():    input_text = input_box.value # takes whatever is in input box    print(f"Input text: {input_text}") # check        ser.write(input_text.encode('ascii')) # prints to serial    #ser.flush()  # Ensure the serial buffer is flushed ??    print(f"Sent: {input_text}")  # Debug print statementdef update_text():    data=ser.readline() # read serial     print(data.decode('utf-8')) # check        decoded = data.decode('utf') # decode        text.value = decoded # update text in box #Creates app app = App(title="Serial Communication", height=500, width=500)#Title for power button - updated to display power_state with pressmessage = Text(app, text="Welcome")#Power button - changes power state with each presspower_button = PushButton(app, text="Power", command=change_power)#Input box for serial communicationinput_box = TextBox(app)#Send button for serial communication - send_button = PushButton(app, text="Send", command=send_serial)#Label for Received Serial dataserial_output = Text(app, text="Serial Output:\n")#Box for displaying received serial databox = Box(app, border=1, height = 200, width = 400)box.set_border(1,'black') # sets border thickness and colourbox.show() # Just in case hiddentext = Text(box,text=1) # Text widget to show read serial valuestext.repeat(1, update_text) # Updates read serial values# App main loopapp.display()
This code is currently able to read the serial data coming from the Arduino and update the text in the box, albeit not formatted (not an issue for now). There are no issues with sending a number to the arduino with the input box and send button, the arduino receives this and updates its values accordingly. All the print checks work correctly as well.

However, when I press the power button the serial readings stop updating the text in the box and in the shell itself. Instead i get blank new lines at a slow rate.

I believe it may be to do with the global power_state or with 'if' loop of the change_power() function but I am unsure. I am pretty confident that the arduino code works as long as the correct inputs are used as I have done extensive testing using the Arduino IDE serial monitor already.

Please let me know if I am missing anything simple like threading, or serial buffers/flushs. Admittedly I don't quite understand these latter topics so any help would be greatly appreciated!

Thank you very much

Statistics: Posted by BGCTS — Wed Jan 15, 2025 5:33 pm



Viewing all articles
Browse latest Browse all 1247

Trending Articles