1

I'm interfacing a device over the Modbus protocol.

There's commands for reading and writing registers (register = 2 bytes).

However, often a register contains multiple config flags as bits, and I need to write just one of them or so.

I could read it, modify flag and write it back, but is that the right way?

I've seen some commands for writing coils, but I'm not really sure what a coil is - is it a bit? If so, how do get address of a coil?

MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • You might want to learn some electronics, and learn about the actual equipment you're supposed to control. – Some programmer dude Dec 28 '14 at 13:51
  • @JoachimPileborg I have the datasheet, but it's very confusing, translated from chinese I think. Not sure what makes you think I should learn electronics? I doubt "coil" means "inductor" ins this context, it's just a relict from when everything was done using relays. – MightyPork Dec 28 '14 at 13:53

1 Answers1

3

The command for this is called Mask Write Register, function code 0x16. Unfortunately you'll have to verify that your device actually supports this function.

If it doesn't, you'll have to do what you originally proposed - read/modify/write. It's important to be aware that this is never going to be a safe operation; there's no way to guarantee that the value won't change between the time you read it and the time you write the modified value back.

Coils are one of the four data areas in Modbus (Holding Registers, Input Registers, Coils, Discrete Inputs), and yes they are a boolean/bit, but that doesn't really matter if the values you're supposed to read/write are stored as bits in a register.

Kevin Herron
  • 6,500
  • 3
  • 26
  • 35
  • how could be implemented [Modbus Pulse Coils](https://stackoverflow.com/questions/66230042/modbus-pulse-coils-with-spring-boot-project) function? Could you suggest anything in this case? – catch23 Feb 18 '21 at 18:28
  • Use a library that implements the WriteMultipleRegisters function. You should post a new question with more information about your situation. – Kevin Herron Feb 18 '21 at 18:54