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

📄 umodbusclient.pas

📁 基于MODBUSTCP协议的客户端通讯程序
💻 PAS
字号:
unit uModBusClient;

interface

uses
  Windows, SysUtils, Classes, SyncObjs;

const
  mbUndefined = $FF;

  mbReadCoils = 1;
  mbReadDiscreteInputs = 2;
  mbReadHoldingRegisters = 3;
  mbReadInputRegisters = 4;

  mbWriteMultipleCoils = 15;
  mbWriteMultipleRegisters = 16;

  mbeIllegalFunction    = 1;
  mbeIllegalDataAddress = 2;
  mbeIllegalDataValue   = 3;
  mbeSlaveDeviceFailure = 4;


type
{
byte 0:            identifier - copied by responder - usually 0
byte 1:            identifier - copied by responder - usually 0
byte 2:            protocol identifier = 0
byte 3:            protocol identifier = 0
byte 4:            length field (upper byte) = 0 (since all messages are smaller than 256)
byte 5:             length field (lower byte) = number of bytes following

byte 6:             unit identifier (previously 'slave address')
byte 7:            MODBUS function code
byte 8 on:            data as needed
}

  Tmb_tcp_header = packed record
    fTransactionID: word;
    fProtocolID: word;
    fRecLength: word;
  end;

  //The mb_req_pdu is defined as:
  //mb_req_pdu = {function_code, request_data}, where
  //function_code = [1 byte] MODBUS function code corresponding to the desired
  //MODBUS function code or requested through the client API,
  //request_data = [n bytes] This field is function code dependent and usually
  //contains information such as variable references,
  //variable counts, data offsets, sub-function codes etc.
  Tmb_req_pdu = packed record
    fUnitID: byte;
    fFunctionCode: byte;
    case byte of
      mbReadCoils, mbReadDiscreteInputs, mbReadHoldingRegisters,
      mbReadInputRegisters: (fReadStartAddr: word;
        fReadQuantity: word);
      mbWriteMultipleCoils: (fCoilsStartAddr: word;
        fCoilQuantity: word;
        fCount1: byte;
        fOutCoilValues: TByteArray);
      mbWriteMultipleRegisters: (fHoldStartAddr: word;
        fRegisterQuantity: word;
        fCount2: byte;
        fOutRegisterValues: TWORDArray);
  end;

  //The mb_rsp_pdu is defined as:
  //mb_rsp_pdu = {function_code, response_data}, where
  //function_code = [1 byte] MODBUS function code
  //response_data = [n bytes] This field is function code dependent and usually
  //contains information such as variable references,
  //variable counts, data offsets, sub-function codes, etc.
  Tmb_rsp_pdu = packed record
    fUnitID: byte;
    fFunctionCode: byte;
    case byte of
      mbReadCoils, mbReadDiscreteInputs, mbReadHoldingRegisters,
      mbReadInputRegisters: (fCount: byte;
        fReadData: TByteArray);
      mbWriteMultipleCoils,
      mbWriteMultipleRegisters: (fStartAddr2: word;
        fQuantity: word);
      mbUndefined: (fExceptionCode: byte);
  end;
  //The mb_excep_rsp_pdu is defined as:
  //mb_excep_rsp_pdu = {function_code, request_data}, where
  //exception-function_code = [1 byte] MODBUS function code + 0x80
  //exception_code = [1 byte] MODBUS Exception Code Defined in table
  //"MODBUS Exception Codes" (see section 7 ).

  Tmb_excep_rsp_pdu = packed record
    fUnitID: byte;
    fFunctionCode: byte;
    fExceptionCode: byte;
  end;


implementation

initialization

finalization
end.

⌨️ 快捷键说明

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