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

2024-04-23 21:01:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用arduino和rpi建造一艘gps导航的rc船。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需要大约半秒钟的刷新时间。获取变量的主脚本是计算和更新伺服速度控制器的位置

任何帮助都将不胜感激


Tags: key脚本iflinealtsatmaxarduino
1条回答
网友
1楼 · 发布于 2024-04-23 21:01:35

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

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

相关问题 更多 >