【发布时间】:2017-04-26 10:23:19
【问题描述】:
我知道有人问过这个问题,但是每次示例都与我想要做的不同或更复杂时。
First.py
value = 10 #This variable should go to the Second script
第二个.py
newdata = value #This variable should come from the First script, 10
所以我想做的就是将一个 SINGLE 变量传递给另一个独立运行的 python。请我不想将第一个脚本中的所有变量传递给下一个脚本或调用整个第二个脚本,因为我已经看到了一些示例。我在 Raspberry Pi 上运行这两个脚本,第一个脚本读取一些传感器数据,而第二个脚本获取该数据并进一步计算它。第一个脚本中使用了很多变量,所以我真的不想传递所有内容,只传递我想要的。
如果有什么不同,我也想要 v2.7。
谢谢
这是我的代码:{我要传递给 Second.py 的变量是 temp}
First.py
# External module imports
import time
import os
import datetime
import MySQLdb
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')
# Connect to mysql
db=MySQLdb.connect("localhost","zikmir","gforce","temp_database")
cursor=db.cursor()
while True:
# Initialization
sensor= "/sys/bus/w1/devices/28-011620ee98ee/w1_slave"
# Open the file for sensor
file = open(sensor)
# Read all of the text in the file.
text = file.read()
# Close the file now that the text has been read.
file.close()
# Split the text with new lines (\n) and select the second line.
second_line = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temp_data = second_line.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temp = float(temp_data[2:])
# Put the decimal point in the right place and display it.
temp = temp / 1000
# Display time
t= datetime.datetime.now()
print t,temp
# Push data into mySQL
sql = "INSERT INTO time_temp VALUES(now(),%s)"
cursor.execute (sql,(temp,))
db.commit()
# Wait 5 seconds
import seven_segment
seven_segment.getdata(temp)
time.sleep(5)
第二个.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# GPIO ports for the 7seg pins
segments = (11,2,23,8,7,10,18,25)
# 7seg_segment_pins (11,7,4,2,1,10,5,3) + 100R inline
for segment in segments:
GPIO.setup(segment, GPIO.OUT)
GPIO.output(segment, 0)
# GPIO ports for the digit 0-3 pins
digits = (22,27,17)
# 7seg_digit_pins (12,9,8) digits 0-3 respectively
for digit in digits:
GPIO.setup(digit, GPIO.OUT)
GPIO.output(digit, 1)
num = {' ':(0,0,0,0,0,0,0),
'0':(1,1,1,1,1,1,0),
'1':(0,1,1,0,0,0,0),
'2':(1,1,0,1,1,0,1),
'3':(1,1,1,1,0,0,1),
'4':(0,1,1,0,0,1,1),
'5':(1,0,1,1,0,1,1),
'6':(1,0,1,1,1,1,1),
'7':(1,1,1,0,0,0,0),
'8':(1,1,1,1,1,1,1),
'9':(1,1,1,1,0,1,1)}
try:
while True:
def getdata(temp):
n = temp
s = str(n).rjust(3)
for digit in range(3):
for loop in range(0,7):
GPIO.output(segments[loop], num[s[digit]][loop])
if (int(time.ctime()[18:19])%2 == 0) and (digit == 1):
GPIO.output(25, 1)
else:
GPIO.output(25, 0)
GPIO.output(digits[digit], 0)
time.sleep(0.001)
GPIO.output(digits[digit], 1)
finally:
GPIO.cleanup()
我让它在测试代码上工作,但由于某种原因不在我的主代码中,这是我尝试过的,我成功地传递了数据:
firsttest.py
value = 1000
def main():
print("abc")
if __name__ == "__main__":
main()
secondtest.py
from firsttest import value
recieved = value
print recieved
输出确实是 1000,我只需要运行 secondtest.py 并且 firsttest.py 会自动执行。但是当我运行我的原始代码时,first.py 确实执行了,但我没有从 second.py 得到任何输出 我不知道这是否与树莓派有关,因为我正在使用多个 GPIO,或者这是一个编程错误。
任何帮助都会令人惊叹。我更糟糕的解决方案是将 first.py 输出到一个文本文件并让 second.py 从那里读取它,希望这也不会矛盾,因为两个脚本会同时尝试使用它?
【问题讨论】:
-
导入 Second.py 调用其函数之一并传递值
-
@nishantkumar 也许您能用示例代码详细说明一下吗?谢谢。
-
请正确缩进您的代码。
-
另外,我怀疑此更新完全评估了我的回答。 @nishant-kumar 做到了,除非有一点遗漏。
-
仍在等待您修复代码中的缩进,以便我可以看到您的 while 循环在哪里结束,等等...
标签: python python-2.7 raspberry-pi gpio