2017-03-10 49 views
0

我已經使用MCP203XX在我的樹莓上有更多輸入。我的程序讀取所有輸入,但不是全部同時輸入。我怎樣才能調整我的程序,使它們全部同時讀取? 我知道這是因爲我的If語句,但我不知道正確的方式,因爲我沒有那麼多的編程經驗。立即讀取MCP230XX的所有數據

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

你是什麼意思,它不念同一時間?

在一個永遠的循環,你的兩個端口,讀到這裏輸入一次:

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)