Alex z
Alex z

Reputation: 33

How to share variables between two python scripts issues

I am building a gps guided rc boat using an arduino and an rpi. The arduino sends over serial data which is then interpreted using this file:

import serial

with serial.Serial('/dev/ttyAMA0', 115200, timeout=1) as ser:
 while True:
  line = ser.readline()
  max_char = len(line)
  a = 3
  # read a '\n' terminated line
  if len(line) > 0:
   if chr(line[0]) == '~':
    key = int(chr(line[1]))
    if key == 1:
     lat = line[a:max_char-2]
     print("lat:")
     print(lat.decode('utf-8'))
    if key == 2:
     lng = line[a:max_char-2]
     print("lng:")
     print(lng.decode('utf-8'))
    if key == 3:
     alt = line[a:max_char-2]
     print("alt:")
     print(alt.decode('utf-8'))
    if key == 4:
     sat = line[a:max_char-2]
     print("sat:")
     print(sat.decode('utf-8'))
    if key == 5:
     crs = line[a:max_char-2]
     print("Crs:")
     print(crs.decode('utf-8'))
   else:
    print("oops:")

I then want another python script to be able to access the variables crs, lat,lng etc. However when I tried using the following lines in another python script it ran the whole function.

from serial2rpi import *

print(crs)
print(lat)
# etc.

The reason I can't just combine the scripts is that the gps takes about a half a second to refresh. The main script which will be grabbing the variable is making calculations and updating the positions of servos speed controllers.

Any help would be greatly appreciated.

Upvotes: 1

Views: 192

Answers (1)

aminrd
aminrd

Reputation: 5030

You need to add threading and locks to your code. I hope this method works:

File: serial2rpi.py

import serial
import threading

lat = None
lng = None
alt = None
sat = None
crs = None

info_lock = threading.Lock()

def serialReaderThread():
    with serial.Serial('/dev/ttyAMA0', 115200, timeout=1) as ser:
        while True:
            line = ser.readline()
            max_char = len(line)
            a = 3
            # read a '\n' terminated line
            if len(line) > 0:
                if chr(line[0]) == '~':
                    key = int(chr(line[1]))

                    if key == 1:
                        info_lock.acquire()
                        lat = line[a:max_char - 2]
                        print("lat:")
                        print(lat.decode('utf-8'))
                        info_lock.release()

                    if key == 2:
                        info_lock.acquire()
                        lng = line[a:max_char - 2]
                        print("lng:")
                        print(lng.decode('utf-8'))
                        info_lock.release()

                    if key == 3:
                        info_lock.acquire()
                        alt = line[a:max_char - 2]
                        print("alt:")
                        print(alt.decode('utf-8'))
                        info_lock.release()

                    if key == 4:
                        info_lock.acquire()
                        sat = line[a:max_char - 2]
                        print("sat:")
                        print(sat.decode('utf-8'))
                        info_lock.release()

                    if key == 5:
                        info_lock.acquire()
                        crs = line[a:max_char - 2]
                        print("Crs:")
                        print(crs.decode('utf-8'))
                        info_lock.release()                        
                else:
                    print("oops:")

File: Other.py

from serial2rpi import *

# Run this line whenever you want to start reading the serial's input
threading.Thread(target=serialReaderThread).start()

# Acquire the lock whenever you want to read or write in your code
info_lock.acquire()
print(crs)
print(lat)
info_lock.release()
# etc.

Upvotes: 2

Related Questions