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

📄 busprotocolexceptions.pas

📁 ModBus Communications Libraries for Delphi
💻 PAS
字号:
(**
 * @internal
 * @file BusProtocolExceptions.pas
 *
 * @if NOTICE
 *
 * $Id: BusProtocolExceptions.pas,v 1.5 2005/07/25 22:41:01 henrik Exp $
 *
 * Copyright (c) 2003-2005 FOCUS Software Engineering Pty Ltd, Australia.
 * All rights reserved. <www.focus-sw.com>
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS OF A
 * SEPARATE LICENSE STATEMENT AND LIMITED WARRANTY.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD FOCUS SOFTWARE ENGINEERING,
 * ITS RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION
 * OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS
 * SOURCE CODE FILE.
 *
 * @endif
 *)


unit BusProtocolExceptions;


(*****************************************************************************
 * Interface
 *****************************************************************************)

interface

uses
   Classes,
   SysUtils;


(*****************************************************************************
 * Exceptions
 *****************************************************************************)

(**
 * @defgroup buserror Protocol Errors and Exceptions
 *)
//@{
//@}


type

   (**
    * @class EIllegalArgumentError
    * Illegal argument error
    *
    * A parameter passed to the method is invalid
    * or out of range.
    *
    * @ingroup buserror
    * @see ERangeError
    *)
   EIllegalArgumentError = class(ERangeError);


   (**
    * @class EIllegalStateError
    * Illegal state error
    *
    * The object is called in a wrong state. This exception is thrown by
    * all methods if the protocol has not been opened yet succesfully.
    *
    * @ingroup buserror
    * @see EInvalidOperation
    *)
   EIllegalStateError = class(EInvalidOperation);


   (**
    * @class EEvaluationExpired
    * Evaluation expired
    *
    * This version of the library is a function limited evaluation version
    * and has now expired.
    *
    * @ingroup buserror
    * @see EInvalidOperation
    *)
   EEvaluationExpired = class(EInvalidOperation);


   (**
    * @name I/O error class
    *)
   //@{


   (**
    * @class EOpenErr
    * Port or socket open error
    *
    * The TCP/IP socket or the serial port could not be opened. In case of a
    * serial port it indicates that the serial port does not exist on the
    * system.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EOpenErr = class(EInOutError);


   (**
    * @class EPortAlreadyOpen
    * Serial port already open
    *
    * The serial port defined for the open operation is already opened by
    * another application.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EPortAlreadyOpen = class(EInOutError);


   (**
    * @class ETcpipConnectErr
    * TCP/IP connection error
    *
    * Signals that the TCP/IP connection could not be established.
    * Typically this error occurs when a host does not exist on the network or
    * the IP address or host name is wrong. The remote host must also listen
    * on the appropriate port.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   ETcpipConnectErr = class(EInOutError);


   (**
    * @class EConnectionWasClosed
    * Remote peer closed TCP/IP connection
    *
    * Signals that the TCP/IP connection was closed by the remote peer or is
    * broken.
    *
    * @ingroup buserror
    *)
   EConnectionWasClosed = class(EInOutError);


   (**
    * @class ESocketLibError
    * Socket library error
    *
    * The TCP/IP socket library eg WINSOCK; could not be loaded or the DLL
    * is missing or not installed.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   ESocketLibError = class(EInOutError);


   (**
    * @class EPortAlreadyBound
    * TCP port already bound
    *
    * Indicates that the specified TCP port cannot be bound. The port might
    * already be taken by another application or hasn't been released yet by
    * the TCP/IP stack for re-use.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EPortAlreadyBound = class(EInOutError);


   (**
    * @class EListenFailed
    * Listen failed
    *
    * The listen operation on the specified TCP port failed..
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EListenFailed = class(EInOutError);


   (**
    * @class EFiledesExceeded
    * File descriptors exceeded
    *
    * Maximum number of usable file descriptors exceeded.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EFiledesExceeded = class(EInOutError);

   (**
    * @class EPortNoAccess
    * No permission to access serial port or TCP port
    *
    * You don't have permission to access the serial port or TCP port. Run the
    * program as root. If the error is related to a serial port, change the
    * access privilege. If it is related to TCP/IP use TCP port number which
    * is outside the IPPORT_RESERVED range.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EPortNoAccess = class(EInOutError);


   (**
    * @class EPortNotAvail
    * TCP port not available
    *
    * The specified TCP port is not available on this machine.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EPortNotAvail = class(EInOutError);

   //@}


   (**
    * @name Fieldbus protocol error class
    *)
   //@{


   (**
    * @class EBusProtocolException
    * General fieldbus protocol error.
    *
    * Signals that a fieldbus protocol related I/O exception of has
    * occurred. This class is the general class of exceptions produced by
    * failed or interrupted data transfer functions. It is also produced when
    * receiving invalid frames or exception responses.
    *
    * @ingroup buserror
    * @see EInOutError
    *)
   EBusProtocolException = class(EInOutError);


   (**
    * @class EChecksumException
    * Checksum error.
    *
    * Signals that the checksum of a received frame is invalid.
    * A poor data link typically causes this error.
    *
    * @ingroup buserror
    * @see EBusProtocolException
    *)
   EChecksumException = class(EBusProtocolException);


   (**
    * @class EInvalidFrameException
    * Invalid frame error.
    *
    * Signals that a received frame does not correspond either by structure or
    * content to the specification or does not match a previously sent query
    * frame. A poor data link typically causes this error.
    *
    * @ingroup buserror
    * @see EBusProtocolException
    *)
   EInvalidFrameException = class(EBusProtocolException);


   (**
    * @class EInvalidReplyException
    * Invalid reply error.
    *
    * Signals that a received reply does not correspond
    * to the specification.
    *
    * @ingroup buserror
    * @see BusProtocolException
    *)
   EInvalidReplyException = class(EBusProtocolException);


   (**
    * @class EReplyTimeoutException
    * Reply time-out.
    *
    * Signals that a fieldbus data transfer timed out. This can occur if the
    * slave device does not reply in time or does not reply at all. A wrong
    * unit adress will also cause this error. In some occassions this
    * expection is also produced if the characters received don't constitute a
    * complete frame.
    *
    * @ingroup buserror
    * @see EBusProtocolException
    *)
   EReplyTimeoutException = class(EBusProtocolException);


   (**
    * @class EMbusResponseException
    * @brief Modbus\reg exception response.
    *
    * Signals that a Modbus exception response was received. Exception
    * responses are sent by a slave device instead of a normal response
    * message if it received the query message correctly but cannot handle the
    * query. This error usually occurs if a master queried an invalid or
    * non-existing data address or if the master used a Modbus function which
    * is not supported by the slave device.
    *
    * @ingroup buserror
    * @see EBusProtocolException
    *)
   EMbusResponseException = class(EBusProtocolException);

   (**
    * @class EMbusIllegalFunctionException
    * Illegal Function exception response.
    *
    * Signals that an Illegal Function exception response (code 01) was
    * received. This exception response is sent by a slave device instead of a
    * normal response message if a master sent a Modbus function which is not
    * supported by the slave device.
    *
    * @ingroup buserror
    * @see EMbusResponseException
    *)
   EMbusIllegalFunctionException = class(EMbusResponseException);

   (**
    * @class EMbusIllegalAddressException
    * Illegal Data Address exception response.
    *
    * Signals that an Illegal Data Address exception response (code 02)
    * was received. This exception response is sent by a slave device instead
    * of a normal response message if a master queried an invalid or
    * non-existing data address.
    *
    * @ingroup buserror
    * @see EMbusResponseException
    *)
   EMbusIllegalAddressException = class(EMbusResponseException);


   (**
    * @class EMbusIllegalValueException
    * Illegal Data Value exception response.
    *
    * Signals that a Illegal Value exception response was (code 03)
    * received. This exception response is sent by a slave device instead of
    * a normal response message if a master sent a data value which is not an
    * allowable value for the slave device.
    *
    * @ingroup buserror
    * @see EMbusResponseException
    *)
   EMbusIllegalValueException = class(EMbusResponseException);


   (**
    * @class EMbusSlaveFailureException
    * Slave Device Failure exception response.
    *
    * Signals that a Slave Device Failure exception response (code 04)
    * was received. This exception response is sent by a slave device instead
    * of a normal response message if an unrecoverable error occured while
    * processing the requested action. This response is also sent if the
    * request would generate a response whose size exceeds the allowable data
    * size.
    *
    * @ingroup buserror
    * @see EMbusResponseException
    *)
   EMbusSlaveFailureException = class(EMbusResponseException);

   //@}


function exceptionFactory(retCode: integer): Exception;


(*****************************************************************************
 * Implementation
 *****************************************************************************)

implementation

(**
 * @internal
 * Creates an exception object based on an error code
 *
 * @param retCode Error code
 * @return Exception object
 *)
function exceptionFactory(retCode: integer): Exception;
begin
   case retCode of
       0: exceptionFactory := nil; { Operation was successful }
       1: exceptionFactory := ERangeError.Create(
                                 'Illegal argument error');
       2: exceptionFactory := EInvalidOperation.Create(
                                 'Illegal state error');
       3: exceptionFactory := EEvaluationExpired.Create(
                                 'Evaluation expired');
     { FTALK_IO_ERROR_CLASS }
     $40: exceptionFactory := EInOutError.Create(
                                 'I/O error class');
     $41: exceptionFactory := EInOutError.Create(
                                 'I/O error');
     $42: exceptionFactory := EOpenErr.Create(
                                 'Port or socket open error');
     $43: exceptionFactory := EPortAlreadyOpen.Create(
                                 'Serial port already open');
     $44: exceptionFactory := ETcpipConnectErr.Create(
                                 'TCP/IP connection error');
     $45: exceptionFactory := EConnectionWasClosed.Create(
                                 'TCP/IP connection was closed by remote peer');
     $46: exceptionFactory := ESocketLibError.Create(
                                 'Socket library error');
     $47: exceptionFactory := EPortAlreadyBound.Create(
                                 'TCP Port alread bound');
     $48: exceptionFactory := EListenFailed.Create(
                                 'Listen failed');
     $49: exceptionFactory := EFiledesExceeded.Create(
                                 'File descriptors exceeded');
     $4A: exceptionFactory := EPortNoAccess.Create(
                                 'No permission to access serial or TCP port');
     $4B: exceptionFactory := EPortNotAvail.Create(
                                 'TCP Port not available');
     { FTALK_BUS_PROTOCOL_ERROR_CLASS }
     $80: exceptionFactory := EBusProtocolException.Create(
                                 'Fieldbus protocol error class');
     $81: exceptionFactory := EChecksumException.Create(
                                 'Checksum error');
     $82: exceptionFactory := EInvalidFrameException.Create(
                                 'Invalid frame error');
     $83: exceptionFactory := EInvalidReplyException.Create(
                                 'Invalid reply error');
     $84: exceptionFactory := EReplyTimeoutException.Create(
                                 'Reply time-out');
     { FTALK_MBUS_EXCEPTION_RESPONSE }
     $A0: exceptionFactory := EMbusResponseException.Create(
                                  'Modbus exception response');
     $A1: exceptionFactory := EMbusIllegalFunctionException.Create(
                                 'Illegal Function exception response');
     $A2: exceptionFactory := EMbusIllegalAddressException.Create(
                                 'Illegal Data Address exception response');
     $A3: exceptionFactory := EMbusIllegalValueException.Create(
                                 'Illegal Data Value exception response');
     $A4: exceptionFactory := EMbusSlaveFailureException.Create(
                                 'Slave Device Failure exception response');
   else
      exceptionFactory := EExternalException.Create('Unknown error');
   end;
end;


end.

⌨️ 快捷键说明

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