stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.33k stars 1.71k forks source link

Can we write to individual bits of registers? #178

Closed riceman0 closed 10 years ago

riceman0 commented 10 years ago

Hello, we are writing some software that uses the very useful libmodbus library, and have a requirement to interact with a device that serves out modbus registers of which the individual bits control various settings. e.g., the fourth bit controls active/inactive: b0001000000000000.

Is there a way to use the modbus_write_bit() function to manipulate that specific bit, without modifying the adjacent bits?

Obviously the long way to do this is to read the entire register using read_modbus_register, flip the bit, and write the entire register back. But this is a little more complex, time consuming, and vulnerable to conflict (eg if something else tries to write in the middle of this operation, those changes will be lost).

I specifically need to do the following: write 1 to BINARY register 401024/0 (single bit # 0) read 1 from BINARY register 400022/9 (single bit # 9) write 0 to BINARY register 401024/8 (single bit # 8)  write 1 to BINARY register 400004/4 (single bit # 4) 

So we were wondering if libmodbus provides some mechanism to do this in a single operation; e.g., modbus_write_bit()?

Thanks for any help.

stephane commented 10 years ago

Has been discussed on mailing list... (write mask register)