/***************************************************************************//**
 *   @file   MODBUSTCPServer.h
 *   @brief  MODBUS TCP Serveur Application Header
 *   @author Kévin MARCHAND(Kevin.marchand@intelligence-electronique.com)
********************************************************************************
 * Copyright 2014(c) PLCD, Inc.
 *
 * Implémente un serveur MODBUS TCP sur le port 502 (<b>MODBUS_PORT</b>
 *
 * - Cette exemple peut ętre adapté pour effectuer d'autres opérations.
 *
********************************************************************************
 *   SVN Revision: 1
*******************************************************************************/

/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
#ifndef __MODBUSTCP_H
#define __MODBUSTCP_H

#include "TCPIPStack/TCPIPConfig.h"

#include "Include/TCPIPStack/TCPIP.h"

// Defines which port the server will listen on
#define MODBUS_PORT            502

//Define for buffer size
#define HOLDING_REG_SIZE        25
#define INPUT_REG_SIZE          25
#define MODBUS_RX_BUFFER_SIZE  512
#define MODBUS_TX_BUFFER_SIZE  512
#define COIL_SIZE                8


//Modbus Functions
#define	ReadCoil                 1
#define	ReadDiscreteInputs       2
#define	ReadHoldingRegister      3
#define	ReadInputRegister        4
#define	WriteSingleCoil          5
#define	WriteSingleRegister      6
#define	WriteMultipleCoils      15
#define	WriteMultipleRegister   16

//Modbus Errors
#define Illegal_Function_Code   0x01u
#define Illegal_Data_Address    0x02u

//Position of the data in the frame
#define MODBUS_UnitID           6
#define MODBUS_FunctionCode     7
#define MODBUS_BYTE_COUNT       8
#define MODBUS_DataStart       13
#define MODBUS_ExceptionCode    8

//number of words available for READ HOLDING REGISTER
#define MAX_REG_SIZE 50u //is it same as HOLDING REG SIZE

//Modbus command structure
typedef struct
{
    WORD_VAL TransactionID;       //Transaction ID
    WORD_VAL ProtocolID;          //Protocol ID
    WORD Length;                  //Length
    BYTE UnitID;                  //Unit ID
    BYTE FunctionCode;            //Function code
    WORD_VAL StartAddress;        //Starting register address
    WORD_VAL NumberOfRegister;    //Number of registers
} _MODBUS_COMMAND;                //MODBUS_COMMAND

//_MODBUS_COMMAND MODBUS_COMMAND;

typedef struct
{
    BYTE Val;
    BYTE Addr;

} WORD_VAL1, WORD_BITS1;

extern BYTE MODBUS_RX[MODBUS_RX_BUFFER_SIZE];                                          //MODBUS UART buffer
extern BYTE MODBUS_TX[MODBUS_TX_BUFFER_SIZE];                                          //Buffer to transfer MODBUS response
extern _MODBUS_COMMAND MODBUS_COMMAND;

//Function Prototypes
int  MODBUSTCPServer(void);
void ProcessReceivedMessage(void);
void readHoldingRegister(void);
void writeHoldingRegister(void);
void readInputRegister(void);
void writeMultipleCoils(void);
void writeSingleCoil(void);
void ModbusError(unsigned char ErrNum);

WORD ModbusUartServer(int numPort);
long LectureLong(long valeur, int adrModbus, int poids);
float LectureReel(float valeur, int adrModbus, int poids, float multi);
void ProcessMessageRecu(void);
void LectureMots(int numPort, int inversionPFPf);
void EcritureMots(int numPort);

#endif
