我使用MCP203XX在我的Raspberry上有更多输入。我的程序读取所有输入,但不是所有输入同时读取。如何调整我的程序以便同时读取它们? 我知道这是因为我的If声明,但我不知道正确的方法,因为我没有那么多的编程经验。
import smbus
import time
import snap7
import struct
bus = smbus.SMBus(1)
DEVICE = 0x20 #Device adress (A0-A2)
IODIRA = 0x00 #Pin direction register
GPIOA = 0x12 #Register for inputs
IODIRB = 0x01
GPIOB = 0x13
#Set GPA pins as inputs
bus.write_byte_data(DEVICE,IODIRA,0xFF)
bus.write_byte_data(DEVICE,IODIRB,0xFF)
# connecting PLC
client = snap7.client.Client()
client.connect('192.168.5.2', 0, 1)
db_number = 109
start1 = 64
start2 = 65
#Loop until user presses CTRL-C
while True:
#Read state of GPIOB register
MySwitchB = bus.read_byte_data(DEVICE,GPIOB)
if MySwitchB & 0b00000001 == 0b00000001:
in1 = 1
print('Switch1 was pressed!')
else:
in1 = 0
print('no1')
if MySwitchB & 0b00000010 == 0b00000010:
in2 = 1
print('Switch2 was pressed!')
else:
in2 = 0
print('no2')
if MySwitchB & 0b00000100 == 0b00000100:
in3 = 1
print('Switch3 was pressed!')
else:
in3 = 0
print('no3')
if MySwitchB & 0b00001000 == 0b00001000:
in4 = 1
print('Switch4 was pressed!')
else:
in4 = 0
print('no4')
if MySwitchB & 0b00010000 == 0b00010000:
in5 = 1
print('Switch5 was pressed!')
else:
in5 = 0
print('no5')
if MySwitchB & 0b00100000 == 0b00100000:
in6 = 1
print('Switch6 was pressed!')
else:
in6 = 0
print('no6')
if MySwitchB & 0b01000000 == 0b01000000:
in7 = 1
print('Switch7 was pressed!')
else:
in7 = 0
print('no7')
if MySwitchB & 0b10000000 == 0b10000000:
in8 = 1
print('Switch8 was pressed!')
else:
in8 = 0
print('no8')
#Read state of GPIOA register
MySwitchA = bus.read_byte_data(DEVICE,GPIOA)
if MySwitchA & 0b00000001 == 0b00000001:
in9 = 1
print('Switch9 was pressed!')
else:
in9 = 0
print('no9')
if MySwitchA & 0b00000010 == 0b00000010:
in10 = 1
print('Switch10 was pressed!')
else:
in10 = 0
print('no10')
if MySwitchA & 0b00000100 == 0b00000100:
in11 = 1
print('Switch11 was pressed!')
else:
in11 = 0
print('no11')
if MySwitchA & 0b00001000 == 0b00001000:
in12 = 1
print('Switch12 was pressed!')
else:
in12 = 0
print('no12')
if MySwitchA & 0b00010000 == 0b00010000:
in13 = 1
print('Switch13 was pressed!')
else:
in13 = 0
print('no13')
if MySwitchA & 0b00100000 == 0b00100000:
in14 = 1
print('Switch14 was pressed!')
else:
in14 = 0
print('no14')
if MySwitchA & 0b01000000 == 0b01000000:
in15 = 1
print('Switch15 was pressed!')
else:
in15 = 0
print('no15')
if MySwitchA & 0b10000000 == 0b10000000:
in16 = 1
print('Switch16 was pressed!')
else:
in16 = 0
print('no16')
# decimal value of 8 binairy bits
som1 = in1 +in2 * 2 ** 1 +in3 * 2 ** 2 +in4 * 2 ** 3 +in5 * 2 ** 4 +in6 * 2 ** 5 +in7 * 2 ** 6 +in8 * 2 ** 7
som2 = in9 +in10 * 2 ** 1 +in11 * 2 ** 2 +in12 * 2 ** 3 +in13 * 2 ** 4 +in14 * 2 ** 5 +in15 * 2 ** 6 +in16 * 2 ** 7
test_byte = 1
test_data1 = bytearray(test_byte)
test_data2 = bytearray(test_byte)
test_data1[0] = som1
test_data2[0] = som2
client.db_write(db_number, start1, test_data1)
client.db_write(db_number, start2, test_data2)
time.sleep(0.2)
答案 0 :(得分:0)
你是什么意思,它不同时阅读?
在永久循环中,您为两个端口读取输入一次,此处:
MySwitchB = bus.read_byte_data(DEVICE,GPIOB)
MySwitchA = bus.read_byte_data(DEVICE,GPIOA)
将ifs数组更改为
更好byte1 = 0
byte2 = 0
for i in range(0, 7):
byte1 += MySwitchA & (1 << i)
for i in range(0, 7):
byte2 += MySwitchB & (1 << i)
然后,我不知道你接下来要用test_byte做什么。您是否尝试在som1和som2值中获取数字的字节表示?
您可以使用chr(som1)
或我的示例执行此操作:
number_of_bytes = 2
test_data = bytearray(number_of_bytes)
test_data[0] = chr(byte1)
test_data[1] = chr(byte2)