// The modbus_write_registers() function shall write the content of the nb holding registers from the array src at address addr of the remote device. // The function uses the Modbus function code 0x10 (preset multiple registers). func (this *RtuConnection) Write_registers(addr int, values []uint16) error { ioval := make([]C.uint16_t, len(values)) for i := 0; i < len(values); i++ { ioval[i] = C.uint16_t(values[i]) } res, errno := C.modbus_write_registers(this.ctx, C.int(addr), C.int(len(ioval)), &ioval[0]) if int(res) != len(ioval) { return errors.New(modbus_strerror(errno)) } return nil }
func (this ModbusRTUConnection) WriteHoldings(slave int8, startAddr int, values []uint16) error { if C.modbus_set_slave(this.ctx, C.int(slave)) != 0 { return errors.New(fmt.Sprintf("Invalid slave id %d", slave)) } vals := make([]C.uint16_t, len(values)) for i, v := range values { vals[i] = C.uint16_t(v) } this.ProcessHook(RTU_10_LEN + len(values)*int(unsafe.Sizeof(vals[0]))) if C.modbus_write_registers(this.ctx, C.int(startAddr), C.int(len(values)), &vals[0]) < 0 { return errors.New(C.GoString(C.modbus_strerror(C.getErrno()))) } return nil }