simonvetter / modbus

Go modbus stack (client and server)
MIT License
262 stars 83 forks source link

How to set slaver-Id? #9

Closed wwhai closed 2 years ago

wwhai commented 2 years ago

How to set modbus slaver-Id?

simonvetter commented 2 years ago

Hi there,

I think what you're after is the SetUnitId() method. Call it on the client whenever necessary to change the target slave id of any future requests e.g.:

  client.SetUnitId(3)
  err = client.WriteRegister(0x1000, 0x20)
  regs, err = client.ReadRegister(0x1000, modbus.HOLDING_REGISTER)
  // both requests above will target slave id # 3

  // now select another slave
  client.SetUnitId(9)
  err = client.WriteRegister(0x1001, 0x30)
  err = client.WriteRegister(0x1010, 0x01)
  // those writes will target slave id # 9

Hope this helps, -Simon

wwhai commented 2 years ago

Hi there,

I think what you're after is the SetUnitId() method. Call it on the client whenever necessary to change the target slave id of any future requests e.g.:

  client.SetUnitId(3)
  err = client.WriteRegister(0x1000, 0x20)
  regs, err = client.ReadRegister(0x1000, modbus.HOLDING_REGISTER)
  // both requests above will target slave id # 3

  // now select another slave
  client.SetUnitId(9)
  err = client.WriteRegister(0x1001, 0x30)
  err = client.WriteRegister(0x1010, 0x01)
  // those writes will target slave id # 9

Hope this helps, -Simon

Yes , It's my need, thanks