Esempio n. 1
0
// The modbus_write_register() function shall write the value of value holding registers at the address addr of the remote device.
// The function uses the Modbus function code 0x06 (preset single register).
func (this *RtuConnection) Write_register(addr int, value uint16) error {
	res, errno := C.modbus_write_register(this.ctx, C.int(addr), C.int(value))
	if int(res) != 1 {
		return errors.New(modbus_strerror(errno))
	}
	return nil
}
func (this ModbusRTUConnection) WriteHolding(slave int8, addr int, value uint16) error {
	if C.modbus_set_slave(this.ctx, C.int(slave)) != 0 {
		return errors.New(fmt.Sprintf("Invalid slave id %d", slave))
	}

	this.ProcessHook(RTU_06_LEN)
	if C.modbus_write_register(this.ctx, C.int(addr), C.int(value)) != 1 {
		return errors.New(C.GoString(C.modbus_strerror(C.getErrno())))
	}

	return nil
}