func (this ModbusRTUConnection) ReadInputRegisters(slave int8, startAddr int, nb int) ([]uint16, error) {
	if C.modbus_set_slave(this.ctx, C.int(slave)) != 0 {
		return nil, errors.New(fmt.Sprintf("Invalid slave id %d", slave))
	}

	var space [65535]C.uint16_t

	this.ProcessHook(RTU_03_LEN)
	if ressived := C.modbus_read_input_registers(this.ctx, C.int(startAddr),
		C.int(nb), &(space[0])); int(ressived) != nb {
		if ressived == -1 {
			errno := C.getErrno()
			if errno == C.EMBMDATA {
				return nil, errors.New(C.GoString(C.modbus_strerror(errno)))
			}
			return nil, errors.New(fmt.Sprintf("Unknown modbus error errno=%d", errno))
		} else if ressived == 0 {
			return nil, errors.New("No ansver ressived")
		}
	} else {
		result := make([]uint16, ressived)
		for i := 0; i < int(ressived); i++ {
			result[i] = uint16(space[i])
		}
		return result, nil
	}

	return nil, nil
}
//The modbus_set_slave() function shall set the slave number in the libmodbus context.
//The behavior depends of network and the role of the device:
func (this *RtuConnection) Set_slave(slave uint8) error {
	res, errno := C.modbus_set_slave(this.ctx, C.int(slave))
	if int(res) != 0 {
		this.curentSlave = INVALID_SLAVE
		return errors.New(modbus_strerror(errno))
	} else {
		this.curentSlave = int16(slave)
		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
}
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
}
func (this ModbusRTUConnection) WriteSingleCoil(slave int8, addr int, value bool) error {
	if C.modbus_set_slave(this.ctx, C.int(slave)) != 0 {
		return errors.New(fmt.Sprintf("Invalid slave id %d", slave))
	}

	var v C.int
	if value {
		v = C.TRUE
	}

	this.ProcessHook(RTU_05_LEN)
	if C.modbus_write_bit(this.ctx, C.int(addr), v) != 1 {
		return errors.New(C.GoString(C.modbus_strerror(C.getErrno())))
	}

	return nil
}
func (this ModbusRTUConnection) WriteCoils(slave int8, startAddr int, values []bool) 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.uint8_t, len(values))
	for i, v := range values {
		if v {
			vals[i] = C.TRUE
		}
	}

	this.ProcessHook(RTU_0F_LEN + int(math.Ceil(float64(len(values))/8.0)))
	if C.modbus_write_bits(this.ctx, C.int(startAddr), C.int(len(values)), &vals[0]) < 0 {
		return errors.New(C.GoString(C.modbus_strerror(C.getErrno())))
	}
	return nil
}