⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mbusmasterfunctions.pas

📁 ModBus Communications Libraries for Delphi
💻 PAS
📖 第 1 页 / 共 4 页
字号:
 * Read Holding Registers/Read Multiple Registers as modulo-10000 long int
 * data.
 *
 * Reads the contents of pairs of consecutive output registers (holding
 * registers, 4:00000 table) representing a modulo-10000 long int value
 * into 32-bit int values and performs number format conversion.
 *
 * @remark Modbus does not know about any other data type than discretes
 * and 16-bit registers. Because a modulo-10000 value is of
 * 32-bit length, it will be transferred as two consecutive
 * 16-bit registers.
 * This means that the amount of registers transferred with this function
 * is twice the amount of int values passed to this function.
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param int32Arr Buffer which will be filled with the data read. The length
 * of the array determines how many values are read (Range: 1-62).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readMultipleMod10000(
   slaveAddr: integer; startRef: integer; out int32Arr: array of integer);
var
   retVal: integer;
begin
   retVal := mbusMaster_readMultipleMod10000(mbusHdl, slaveAddr, startRef,
                addr(int32Arr), high(int32Arr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 3 (03 hex) for 32-bit float data types,
 * Read Holding Registers/Read Multiple Registers as float data.
 *
 * Reads the contents of pairs of consecutive output registers (holding
 * registers, 4:00000 table) into float values.
 *
 * @remark Modbus does not know about any other data type than discretes
 * and 16-bit registers. Because a float value is of
 * 32-bit length, it will be transferred as two consecutive 16-bit registers.
 * This means that the amount of registers transferred with this function
 * is twice the amount of float values passed to this function.
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param float32Arr Buffer which will be filled with the data read. The length
 * of the array determines how many values are read (Range: 1-62).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readMultipleFloats(
   slaveAddr: integer; startRef: integer; out float32Arr: array of single);
var
   retVal: integer;
begin
   retVal := mbusMaster_readMultipleFloats(mbusHdl, slaveAddr, startRef,
                addr(float32Arr), high(float32Arr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(*****************************************************************************
 * Class 1 Modbus functions
 *****************************************************************************)

(**
 * Modbus function 1 (01 hex),
 * Read Coil Status/Read Coils.
 *
 * Reads the contents of the discrete outputs (coils, 0:00000 table).
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param bitArr Buffer which will contain the data read. The length
 * of the array determines how many coils are read (Range: 1-2000).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readCoils(
   slaveAddr: integer; startRef: integer; out bitArr: array of boolean);
var
   intArr: array[0..2000-1] of integer;
   i : integer;
   retVal: integer;
begin
   if high(bitArr) > high(intArr) then
      raise exceptionFactory(1);
   { Note that open array index range is always zero-based }
   retVal := mbusMaster_readCoils(mbusHdl, slaveAddr, startRef,
                addr(intArr), high(bitArr) + 1);
   for i := 0 to high(bitArr) do
      bitArr[i] := (intArr[i] = 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 2 (02 hex),
 * Read Inputs Status/Read Input Discretes.
 *
 * Reads the contents of the discrete inputs (input status, 1:00000 table).
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param bitArr Buffer which will contain the data read. The length
 * of the array determines how many inputs are read (Range: 1-2000).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readInputDiscretes(
   slaveAddr: integer; startRef: integer; out bitArr: array of boolean);
var
   intArr: array[0..2000-1] of integer;
   i : integer;
   retVal: integer;
begin
   if high(bitArr) > high(intArr) then
      raise exceptionFactory(1);
   { Note that open array index range is always zero-based }
   retVal := mbusMaster_readInputDiscretes(mbusHdl, slaveAddr, startRef,
                addr(intArr), high(bitArr) + 1);
   for i := 0 to high(bitArr) do
      bitArr[i] := (intArr[i] = 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 4 (04 hex),
 * Read Input Registers.
 *
 * Read the contents of the input registers
 * (3:00000 table).
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start register (Range: 1 - $10000)
 * @param regArr  Buffer which will be filled with the data read. The length
 * of the array determines how many registers are read (Range: 1-125).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readInputRegisters(
   slaveAddr: integer; startRef: integer; out regArr: array of word);
var
   retVal: integer;
begin
   retVal := mbusMaster_readInputRegisters(
                mbusHdl, slaveAddr, startRef, addr(regArr), high(regArr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 4 (04 hex) for 32-bit long int data types,
 * Read Input Registers as long int data
 *
 * Reads the contents of pairs of consecutive input registers (3:00000
 * table) into 32-bit long int values.
 *
 * @remark Modbus does not know about any other data type than discretes
 * and 16-bit registers. Because a long int value is of
 * 32-bit length, it will be transferred as two consecutive
 * 16-bit registers.
 * This means that the amount of registers transferred with this function
 * is twice the amount of int values passed to this function.
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param int32Arr Buffer which will be filled with the data read. The length
 * of the array determines how many values are read (Range: 1-62).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readInputLongInts(
   slaveAddr: integer; startRef: integer; out int32Arr: array of integer);
var
   retVal: integer;
begin
   retVal := mbusMaster_readInputLongInts(mbusHdl, slaveAddr, startRef,
                addr(int32Arr), high(int32Arr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 4 (04 hex) for 32-bit modulo-10000 long int data types,
 * Read Input Registers as modulo-10000 long int data.
 *
 * Reads the contents of pairs of consecutive input registers (3:00000
 * table) representing a modulo-10000 long int value into 32-bit long int
 * values and performs number format conversion.
 *
 * @remark Modbus does not know about any other data type than discretes
 * and 16-bit registers. Because an modulo-10000 value is of
 * 32-bit length, it will be transferred as two consecutive
 * 16-bit registers.
 * This means that the amount of registers transferred with this function
 * is twice the amount of int values passed to this function.
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param int32Arr Buffer which will be filled with the data read. The length
 * of the array determines how many values are read (Range: 1-62).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readInputMod10000(
   slaveAddr: integer; startRef: integer; out int32Arr: array of integer);
var
   retVal: integer;
begin
   retVal := mbusMaster_readInputMod10000(mbusHdl, slaveAddr,
                startRef, addr(int32Arr), high(int32Arr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 4 (04 hex) for 32-bit float data types,
 * Read Input Registers as float data.
 *
 * Reads the contents of pairs of consecutive input registers (3:00000
 * table) into float values.
 *
 * @remark Modbus does not know about any other data type than discretes
 * and 16-bit registers. Because a float value is of
 * 32-bit length, it will be transferred as two consecutive
 * 16-bit registers.
 * This means that the amount of registers transferred with this function
 * is twice the amount of float values passed to this function.
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param startRef Start reference (Range: 1 - $10000)
 * @param float32Arr Buffer which will be filled with the data read. The length
 * of the array determines how many values are read (Range: 1-62).
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readInputFloats(
   slaveAddr: integer; startRef: integer; out float32Arr: array of single);
var
   retVal: integer;
begin
   retVal := mbusMaster_readInputFloats(mbusHdl, slaveAddr, startRef,
                addr(float32Arr), high(float32Arr) + 1);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 5 (05 hex),
 * Force Single Coil/Write Coil.
 *
 * Sets a single discrete output variable (coil, 0:00000 table)
 * to either ON or OFF.
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 0 - 255)
 * @param bitAddr Coil address (Range: 1 - $10000)
 * @param bitVal true sets, false clears discrete output variable
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note Broadcast supported for serial protocols
 *)
procedure TMbusMasterFunctions.writeCoil(
   slaveAddr: integer; bitAddr: integer; bitVal: boolean);
var
   intVal : integer;
   retVal: integer;
begin
   if (bitVal)then
      intVal := 1
   else
      intVal := 0;
   retVal := mbusMaster_writeCoil(mbusHdl, slaveAddr, bitAddr, intVal);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 6 (06 hex),
 * Preset Single Register/Write Single Register.
 *
 * Writes a value into a single output register
 * (holding register, 4:00000 reference).
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 0 - 255)
 * @param regAddr Register address (Range: 1 - $10000)
 * @param regVal  Data to be sent
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note Broadcast supported for serial protocols
 *)
procedure TMbusMasterFunctions.writeSingleRegister(
   slaveAddr: integer; regAddr: integer; regVal: word);
var
   retVal: integer;
begin
   retVal := mbusMaster_writeSingleRegister(
                mbusHdl, slaveAddr, regAddr, regVal);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(**
 * Modbus function 7 (07 hex),
 * Read Exception Status.
 *
 * Reads the eight exception status coils within the slave device.
 *
 * @param slaveAddr Modbus address of slave device or unit
 * identifier (Range: 1 - 255)
 * @param statusByte Slave status byte. The meaning of this status byte is
 * slave specific and varies from device to device.
 * @exception EIllegalStateError Port or connection is closed
 * @exception EInOutError An I/O error occurred
 * @exception EIllegalArgumentError A parameter is out of range
 * @exception EBusProtocolException A protocol failure occurred. See
 * @note No broadcast supported
 *)
procedure TMbusMasterFunctions.readExceptionStatus(
   slaveAddr: integer; out statusByte: byte);
var
   retVal: integer;
begin
   retVal := mbusMaster_readExceptionStatus(mbusHdl, slaveAddr, statusByte);
   if retVal <> 0 then
      raise exceptionFactory(retVal);
end;


(*****************************************************************************
 * Class 2 Modbus Functions
 *****************************************************************************)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -