1

I am trying to detect if a switch is ON or OFF from and Arduino, send the data into Python and display the result in a GUI The following Python code will read the serial data into Python as follows; (Not my code i must add)

import serial
ser = serial.Serial('com3',9600)
ser.flushInput()
ser.flushOutput()
while True:
        ser.flushInput()
        ser.flushOutput()
        passcode = ser.read()

        if passcode == b'1':
            print("Switch is ON")
        if passcode == b'0':
            print("Switch if OFF")

The results appears as the following on the Python IDE [depending on the result] which is the serial output result

Switch if OFF

Switch if OFF

Switch if OFF

Switch is ON

Switch is ON

Switch is ON

Now my Question ?

Is there any way i can just get "one" reading saying "Switch is On" or "Switch is off" [not the continuous serial result] into Python and Ideally display the result into Tkinter

1
  • the simplest way is to move the lines into a function, and call the function inside the while loop, or directly when you need it once Commented Oct 28, 2017 at 6:56

2 Answers 2

1

My understanding of your question is 'how can I make it so it only prints output on state change?'.

To do this you need to store the state locally and then compare the new state to the stored state. The simplest way to do this is with a variable for example: switch_state.

So:

import serial

ser = serial.Serial('com3',9600)
switch_state = None  # 0 for off, 1 for on, None - not yet set

ser.flushInput()
ser.flushOutput()

while True:
        ser.flushInput()
        ser.flushOutput()
        passcode = ser.read()

        if passcode == b'1' and switch_state != 1:
            print("Switch is ON")
            switch_state = 1

        if passcode == b'0' and switch_state != 0:
            print("Switch if OFF")
            switch_state = 0

I haven't tried the code - but this should be a simple enough solution to your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

from the comment i got in my answer, sometimes the state is not defined (not available?) maybe you need to reset the state to None in this case too ?
Perfect !! Thanks
1

With the following pattern, you can import the function in any other file to print the result in a console or in any tkinter text widget.

import serial
ser = serial.Serial('com3',9600)
ser.flushInput()
ser.flushOutput()

def switch_state():
    ser.flushInput()
    ser.flushOutput()
    passcode = ser.read()

    res = "Switch is "
    if passcode == b'1':
        res += "ON"
    elif passcode == b'0':
        res += "OFF"
    else:
        res += "N/A"
    return res

if __name__ == "__main__": # this is to avoid executing the loop when importing the file
    while True:
        print(switch_state())

(If you prefer a blank result when the state is not available, like this seems to happen sometimes, you can defined res = '' first and keep "Switch is ON" or "Switch is OFF" in different cases.

2 Comments

Thanks for the respone - I ran the script and had an error with the following UnboundLocalError: local variable 'res' referenced before assignment
ok looks like the ser.read() can return something else, i edited the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.