我有一个python代码可以按照我的预期运行,我希望将函数send_cmd()
移动到另一个commands.py
文件。
import add_pinout
from lib import *
pinout = {}
avr_board = AVRClient('COM3', 250000)
i2c_master = I2CMaster(avr_board)
i2c_slave_adress = 0x1E
i2c_device = I2CDevice(avr_board, i2c_slave_adress)
def send_cmd(cmd):
try:
if cmd[0][:4] == 'LCL_':
if cmd[0] == 'LCL_ALL':
for pin in pinout:
gpio_pin = GPIO(avr_board, pinout[pin], GPIO.Mode.OUTPUT)
if cmd[1] is '1': gpio_pin.write(True)
elif cmd[1] is '0': gpio_pin.write(False)
else:
gpio_pin = GPIO(avr_board, pinout[cmd[1]], GPIO.Mode.OUTPUT)
if cmd[1] is '1': gpio_pin.write(True)
elif cmd[1] is '0': gpio_pin.write(False)
elif cmd[0] == 'WRITE':
i2c_device.data_transfer(cmd[1], len(cmd[1]))
elif cmd[0] == 'READ':
print i2c_device.data_transfer([], len('1010'))
elif cmd[0] == 'WRITE+READ':
print i2c_device.data_transfer(cmd[1], len('1010'))
else:
print 'Something went wrong...'
except KeyError:
print 'Such pin does not exist.'
except IndexError:
print 'Not enough arguments.'
if __name__ == "__main__":
pinout.update(add_pinout.read_pins())
print(pinout)
while(True):
send_cmd(raw_input().split())
我是通过添加from commands.py import send_cmd
并为函数添加参数来实现的。电话看起来像这样:send_cmd(avr_board, pinout, i2c_device, raw_input().split())
,而commands.py
中的声明是def send_cmd(avr_board, pinout, i2c_device, cmd):
。当我运行代码时,它接受用户输入,但是当它从start_cmd
调用commands.py
时,它只会转到else
子句,就像它看不到{{1}所做的列表一样}。
我发现了错误 - 在raw_input().split()
我已在commands.py
语句中将==
更改为is
。我已经扭转了这种变化,现在一切都很好。