Esempio n. 1
0
// The modbus_write_bits() function shall write the status of the nb bits (coils) from src at the address addr of the remote device. The src array must contains bytes set to TRUE or FALSE.
// The function uses the Modbus function code 0x0F (force multiple coils).
func (this *RtuConnection) Write_bits(addr int, values []bool) error {
	ioval := make([]C.uint8_t, len(values))
	for i := 0; i < len(values); i++ {
		ioval[i] = C.FALSE
		if values[i] {
			ioval[i] = C.TRUE
		}
	}

	res, errno := C.modbus_write_bits(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) 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
}