Exemple #1
0
    if (msg[0] == Slave_add):
        ret = msg[1]
        if (ret > 5):
            ret = -1
        elif (ret < 1):
            ret = -1
        print("Modbus Master Msg parse successful!")
        return ret
    else:
        ret = 1
        #print("#>:Modbus slave address not match!")
        return ret


'''
def modbus_s_write_cb(x):
  global fun,REG_addr,tmp_data,var
  fun, REG_addr, tmp_data = x.read()
  var = tmp_data[1]
  if fun == 4 and REG_addr == 1:
    modbus_s.update_function(fun, REG_addr, 90)
    modbus_s.send(1, 3, 1, 90)
    if var == 9:
      rgb.setColorAll(0x6600cc)
      print('modbus received msg!')

  pass
modbus_s.set_write_cb(modbus_s_write_cb)
'''
def modbus_s_write_cb(x):
    global fun, REG_addr, tmp_data, var
    fun, REG_addr, tmp_data = x.read()
    var = tmp_data[1]
    if fun == 4 and REG_addr == 1:
        modbus_s.update_function(fun, REG_addr, 90)
        modbus_s.send(1, 3, 1, 90)
        if var == 9:
            rgb.setColorAll(0x6600cc)
            print('modbus received msg!')

    pass


modbus_s.set_write_cb(modbus_s_write_cb)


def buttonA_wasPressed():
    global fun, REG_addr, tmp_data, var
    print('modbus slave send msg!')
    #modbus_s.update_function(4, 1, 99)
    modbus_s.send(1, 3, 1, 99)  #slave address, 功能码、寄存器地址、数据
    rgb.setColorAll(0xff6600)
    pass


btnA.wasPressed(buttonA_wasPressed)

rgb.setColorAll(0x666633)
while 1: