0

I have a python program named communication.py to receive data from Arduino as below:

    import serial

if __name__ == '__main__':

    ser = serial.Serial('COM5', 9600, timeout=1)

    ser.reset_input_buffer()

    while True:

        if ser.in_waiting > 0:

            line = ser.readline().decode('utf-8').rstrip()

            if (line == "0.0"):

                data = (0,0) 

            print(data)

the program can print variable values correctly but in another python named print.py program, I tried to export the variable named data in this way

from communication import data

print(data)

but that doesn't work because the python.py program doesn't recognize the variable named "data". Does this error have something to do with the while loop?

  • 2
    i perefer define a function and return your data as whatever you want – Ashkan Goleh Pour Oct 01 '22 at 05:36
  • Does this answer your question? [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – buran Oct 01 '22 at 05:36
  • When import a module the whole code inside `if __name__ == 'main':` block is never executed – buran Oct 01 '22 at 05:37

0 Answers0