Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

1

//Definiciones referentes al modo de Modbus #include<avr/io.h> #include<avr/interrupt.h> #include<util/delay.h> //------------------- Definiciones ----------------------------------------#define TRUE #define FALSE #define BITS_UCHAR 1 0 8U ( 1) ( 1) ( 0) ( 1)

#define MB_ASCII_ENABLED #define MB_RTU_ENABLED #define MB_TCP_ENABLED #define MB_ASCII_TIMEOUT_SEC

#ifndef MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS #define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS #endif #define MB_FUNC_HANDLERS_MAX #define MB_FUNC_OTHER_REP_SLAVEID_BUF #define MB_FUNC_OTHER_REP_SLAVEID_ENABLED //Definiciones referentes a la trama /* * <------------------------ MODBUS SERIAL LINE PDU (1) -------------------> * <----------- MODBUS PDU (1') ----------------> ( 16 ) ( 32 ) ( 1) (0)

* +-----------+---------------+----------------------------+-------------+ * | Address | Function Code | Data | CRC/LRC |

* +-----------+---------------+----------------------------+-------------+ * | * (2) * * (1) ... MB_SER_PDU_SIZE_MAX = 256 * (2) ... MB_SER_PDU_ADDR_OFF = 0 * (3) ... MB_SER_PDU_PDU_OFF = 1 * (4) ... MB_SER_PDU_SIZE_CRC = 2 | (3/2') | (3') | (4)

2
* * (1') ... MB_PDU_SIZE_MAX * (2') ... MB_PDU_FUNC_OFF * (3') ... MB_PDU_DATA_OFF * </code> */ #define MB_PDU_SIZE_MAX #define MB_PDU_SIZE_MIN #define MB_PDU_FUNC_OFF #define MB_PDU_DATA_OFF 253 /*!< Maximum size of a PDU. */ 1 /*!< Function Code */ 0 /*!< Offset of function code in PDU. */ 1 /*!< Offset for response data in PDU. */ = 253 =0 =1

/* ------------- Definiciones Puertos --------------------------------------------------------------*/ define INLINE inline extern "C" { }

#define PR_BEGIN_EXTERN_C #define PR_END_EXTERN_C

#define ENTER_CRITICAL_SECTION( ) cli() #define EXIT_CRITICAL_SECTION( ) #define assert( x ) /* ----------------------- Definiciones Locacin Bobinas -------------------------------------*/ #define REG_COILS_START #define REG_COILS_SIZE 1000 30 sei()

/* ----------------------- Definiciones Locacin Entradas discretas -------------------------------------*/ #define REG_DISCRETE_START #define REG_DISCRETE_SIZE 2000 50

/* ----------------------- Definiciones Locacin Holding Registers --------------------------------------*/ #define REG_HOLDREG_START 4000 #define REG_HOLDREG_NREGS 20 /* ----------------------- Definiciones Locacin Registros de Entradas -------------------------------------*/ #define REG_INPUT_START 5000 #define REG_INPUT_NREGS 10 /* ----------------------- Prototipos 0-------------------------------------*/ typedef char BOOL; //Variable TRUE o FALSE

typedef unsigned char UCHAR;

3
typedef char CHAR;

typedef unsigned short USHORT; typedef short SHORT; typedef unsigned long ULONG; typedef long volatile UCHAR typedef enum { MB_RTU, MB_ASCII, MB_TCP } eMBMode; // Modo de transmisin RTU. // Modo de transmisin ASCII.(No implementado) // Modo TCP.(No implementado) LONG; ucHoldRegBuf[REG_HOLDREG_NREGS];

typedef enum { MB_REG_READ, MB_REG_WRITE } eMBRegisterMode; // Lee valores de registros. // Escribe valores de registros.

typedef enum { MB_ENOERR, MB_ENOREG, MB_EINVAL, MB_EPORTERR, MB_ENORES, MB_EIO, MB_EILLSTATE, MB_ETIMEDOUT } eMBErrorCode; // Sin error. // Direccin de registro invlida. // Argumento invlido. // Error en capa de puerto serie. // Recursos insuficientes. // Error de E/S. // Pila de protocolo en estado invlido. // Expir el timeout.

4
typedef void typedef void ( *pvMBFrameStart ) ( void ); ( *pvMBFrameStop ) ( void );

typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength ); typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength ); typedef void( *pvMBFrameClose ) ( void ); /* ----------------------- Definiciones de tipo ---------------------------------*/ typedef enum { STATE_RX_INIT, STATE_RX_IDLE, STATE_RX_RCV, STATE_RX_ERROR } eMBRcvState; // Receptor est en su estado inicial. // Receptor est en estado idle. // Trama siendo recibida. // Trama invlida.

typedef enum { STATE_TX_IDLE, STATE_TX_XMIT } eMBSndState; // Transmisor en estado idle. // Transmisor en estado de transferencia.

/* ----------------------- Definiciones de Eventos ---------------------------------*/ typedef enum { EV_READY, // Prembulo finalizado // Trama recibida.

EV_FRAME_RECEIVED, EV_EXECUTE, EV_FRAME_SENT } eMBEventType;

// Funcin en ejecucin. // Trama enviada.

typedef enum {

5
MB_PAR_NONE, MB_PAR_ODD, MB_PAR_EVEN } eMBParity; /* ----------------------- Definiciones de Funciones del Protocolo --------------------------*/ #define MB_ADDRESS_BROADCAST #define MB_ADDRESS_MIN #define MB_ADDRESS_MAX #define MB_FUNC_NONE #define MB_FUNC_READ_COILS #define MB_FUNC_READ_DISCRETE_INPUTS #define MB_FUNC_READ_HOLDING_REGISTER #define MB_FUNC_READ_INPUT_REGISTER #define MB_FUNC_WRITE_SINGLE_COIL #define MB_FUNC_WRITE_REGISTER #define MB_FUNC_WRITE_MULTIPLE_COILS #define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 0 ) // Direccin de difucin Modbus. // Sin paridad. // Paridad impar. // Paridad par.

( 1 ) // Direccin ms pequea posible de esclavo. ( 247 ) // Direccin ms alta posible de esclavo. ( 0 ) //No implementada. ( 1) ( 2) ( 3) ( 4) ( 5) ( 6) ( 15 ) ( 16 ) //No implementada.

#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 ) #define MB_FUNC_DIAG_READ_EXCEPTION #define MB_FUNC_DIAG_DIAGNOSTIC

( 7 )//No implementada. ( 8) //No implementada. ( 11 ) ( 12 ) ( 17 ) //No implementada. //No implementada. //No implementada.

#define MB_FUNC_DIAG_GET_COM_EVENT_CNT #define MB_FUNC_DIAG_GET_COM_EVENT_LOG #define MB_FUNC_OTHER_REPORT_SLAVEID #define MB_FUNC_ERROR ( 128 )

//No implementada.

/* ----------------------- Definiciones de Excepciones ---------------------------------*/ typedef enum { MB_EX_NONE = 0x00, MB_EX_ILLEGAL_FUNCTION = 0x01, MB_EX_ILLEGAL_DATA_ADDRESS = 0x02, MB_EX_ILLEGAL_DATA_VALUE = 0x03, MB_EX_SLAVE_DEVICE_FAILURE = 0x04,

6
MB_EX_ACKNOWLEDGE = 0x05, MB_EX_SLAVE_BUSY = 0x06, MB_EX_MEMORY_PARITY_ERROR = 0x08, MB_EX_GATEWAY_PATH_FAILED = 0x0A, MB_EX_GATEWAY_TGT_FAILED = 0x0B } eMBException; typedef typedef struct { UCHAR ucFunctionCode; eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );

pxMBFunctionHandler pxHandler; } xMBFunctionHandler; /* -------------------Prototipo de Funciones ------------------------------*/ /* Inicia el protocolo Modbus RTU o ASCII. * El protocolo permanece deshabilitado hasta que no se llame a eMBEnable( ). * eMode: ASCII RTU. * ucSlaveAddress: Direccin del esclavo. Solo las tramas enviadas a este esclavo procesa. * ucPort: El puerto usado, por ejemplo, 1 para COM1 en Windows. Este valor es dependiente * de la plataforma y algunos puertos eligen ignorarla. (No implementado). * ulBaudRate: velocidad en baudios. Por ejemplo 19200. * eParity: Paridad usada en transmisin serial. * * Si no ocurre error la funcin retorna eMBErrorCode::MB_ENOERR. */ eMBErrorCode eMBInic( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ); // Esta funcin deshabilita el protocolo Modbus y libera todo recurso de hardware usado. eMBErrorCode eMBErrorCode eMBClose( void ); eMBEnable( void );

// eMBErrorCode::MB_ENOERR. Si no estaba en estado habilitado retorna // eMBErrorCode::MB_EILLSTATE. eMBErrorCode eMBErrorCode eMBDisable( void ); eMBPoll( void );

7
eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning, UCHAR const *pucAdditional, USHORT usAdditionalLen ); //-------------- Funciones de implementacin ---------------------------eMBErrorCode eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler ); eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs );

eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ); eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ); eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete );

/* ---------------------- Funciones Modbus ---------------------------------------*/ eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen ); eMBException eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ); eMBException eMBException eMBException eMBException eMBException eMBException eMBException eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ); eMBFuncWriteHoldingRegister( volatile UCHAR * pucFrame, USHORT * usLen ); eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen ); eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ); eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen ); eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen ); eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );

/* ----------------------- Funciones de soporte -----------------------------*/ BOOL BOOL BOOL xMBPortEventInic( void ); xMBPortEventPost( eMBEventType eEvent ); xMBPortEventGet( eMBEventType * eEvent );

/* ----------------------- Funciones del puerto serie ----------------------------*/ BOOL void void void BOOL BOOL xMBPortSerialInic( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ); vMBPortClose( void ); xMBPortSerialClose( void ); vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ); xMBPortSerialGetByte( CHAR * pucByte ); xMBPortSerialPutByte( CHAR ucByte );

/* ----------------------- Funciones de Temporizado ---------------------------------*/ BOOL void xMBPortTimersInic( USHORT usTimeOut50us ); xMBPortTimersClose( void );

8
void void void vMBPortTimersEnable( void ); vMBPortTimersDisable( void ); vMBPortTimersDelay( USHORT usTimeOutMS );

/* ----------------------- Llamadas genricas a protocolo ---------------------*/ extern extern extern BOOL( *pxMBFrameCBByteReceived ) ( void ); BOOL( *pxMBFrameCBTransmitterEmpty ) ( void ); BOOL( *pxMBPortCBTimerExpired ) ( void );

/*- Funciones tiles para trabajar con bits en las funciones referentes a coils ---*/ void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits, UCHAR ucValues );

UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits ); /* ---- Funciones de transmisin protocolo Modbus RTU ----------------------------------*/ eMBErrorCode eMBRTUInic( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ); void void eMBRTUStart( void ); eMBRTUStop( void ); eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength ); eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );

eMBErrorCode eMBErrorCode BOOL BOOL BOOL BOOL

xMBRTUReceiveFSM( void ); xMBRTUTransmitFSM( void ); xMBRTUTimerT15Expired( void ); xMBRTUTimerT35Expired( void );

eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ); eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ); eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ); eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs );

UCHAR temporario; static volatile eMBRcvState eRcvState; static volatile eMBSndState eSndState;

static volatile FrameError;

9
static volatile ParityError; static volatile OverRunError;

You might also like