示例#1
0
// The modbus_write_bit() function shall write the status of status at the address addr of the remote device. The value must be set to TRUE or FALSE.
// The function uses the Modbus function code 0x05 (force single coil).
func (this *RtuConnection) Write_bit(addr int, status bool) error {
	var val C.int = C.FALSE
	if status {
		val = C.TRUE
	}

	res, errno := C.modbus_write_bit(this.ctx, C.int(addr), val)
	if int(res) != 1 {
		return errors.New(modbus_strerror(errno))
	}
	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
}