如何在两个python脚本问题之间共享变量

时间:2019-11-28 21:56:30

标签: python arduino raspberry-pi serial-port

我正在使用arduino和rpi建造一艘GPS遥控船。 arduino发送串行数据,然后使用以下文件对其进行解释:

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:")

然后我希望另一个python脚本能够访问crs,lat,lng等变量。但是,当我尝试在另一个python脚本中使用以下行时,它将运行整个函数。

from serial2rpi import *

print(crs)
print(lat)
# etc.

我不能仅合并脚本的原因是gps大约需要半秒钟才能刷新。抓取变量的主要脚本是进行计算并更新伺服速度控制器的位置。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您需要在代码中添加线程和锁。我希望这种方法有效:

文件: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:")

文件: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.