TP3 RBT 2GII Ex1

Dependencies:   mbed Modbus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mb.h"
00003 
00004 #define BITS_OUT_START  0x1001  //Bits de sorties         (fonctions 1 et 5)
00005 #define BITS_OUT_NBITS  16
00006 #define BITS_OUT_BYTES  BITS_OUT_NBITS/8  //16/8 = 2 octets
00007 
00008 #define BITS_IN_START   0x2001  //Bits d'entées           (fonction 2)
00009 #define BITS_IN_NBITS   16
00010 #define BITS_IN_BYTES   BITS_IN_NBITS/8  //16/8 = 2 octets
00011 
00012 #define REG_OUT_START  0x3001  //Registres de sorties     (fonctions 3 et 6)
00013 #define REG_OUT_NREGS  4
00014 
00015 #define REG_IN_START   0x4001  //Registres d'entées       (fonction 4)
00016 #define REG_IN_NREGS   4
00017 
00018 #define SLAVE_ID       0x07
00019 
00020 static USHORT   BitsOutStart = BITS_OUT_START;          // (fonctions 1 et 5)
00021 static UCHAR    BitsOut[BITS_OUT_BYTES] = {0xA5,0x5A};
00022 static USHORT   BitsInStart = BITS_IN_START;            // (fonction 2)
00023 static UCHAR    BitsIn[BITS_IN_BYTES] = {0x3C,0xC3};
00024 static USHORT   RegOutStart = REG_OUT_START;            // (fonctions 3 et 6)
00025 static USHORT   RegOut[REG_OUT_NREGS] = {0x0123,0x4567,0x89AB,0xCDEF};
00026 static USHORT   RegInStart = REG_IN_START;              // (fonction 4)
00027 static USHORT   RegIn[REG_IN_NREGS] = {0x1122,0x3344,0x5566,0x7788};
00028 
00029 int main() {
00030    eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE );
00031    eMBEnable();
00032    
00033    while(1)  { 
00034       eMBPoll();
00035    }
00036 }
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 // Procedure résevée pour les bits de sorties (Foction 1 et 5)
00072 // pucRegBuffer => coils buffer         usAddress => coils address
00073 // usNCoils => coils number             eMode => read or write 
00074 eMBErrorCode
00075 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
00076 {
00077     eMBErrorCode    eStatus = MB_ENOERR;
00078     int             iIntRegIndex;
00079     int                iIntBufNum;
00080     int                iIntBitNum;
00081     int                iExtRegIndex=0;
00082     int                iExtBufNum;
00083     int                iExtBitNum;
00084     UCHAR            ucTemp;
00085     if( ( usAddress >= BITS_OUT_START )
00086         && ( usAddress + usNCoils <= BITS_OUT_START + BITS_OUT_NBITS ) )
00087     {
00088         iIntRegIndex = ( int )( usAddress - BitsOutStart );
00089 
00090         while( usNCoils > 0 )
00091         {
00092             iIntBufNum=iIntRegIndex/8;
00093             iIntBitNum=iIntRegIndex%8;
00094             iExtBufNum=iExtRegIndex/8;
00095             iExtBitNum=iExtRegIndex%8;
00096 
00097             switch ( eMode )
00098             {
00099             case MB_REG_READ:
00100                 // Read coils
00101                 if(iExtBitNum==0){
00102                     pucRegBuffer[iExtBufNum]=0;
00103                 }
00104                 ucTemp=(BitsOut[iIntBufNum]>>iIntBitNum) & 1;
00105                 pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00106                 break;
00107 
00108             case MB_REG_WRITE:
00109                 // Write coils
00110                 ucTemp=BitsOut[iIntBufNum]&(~(1<<iIntBitNum));
00111                 ucTemp|=((pucRegBuffer[iExtBufNum]>>iExtBitNum) & 1)<<iIntBitNum;
00112                 BitsOut[iIntBufNum]=ucTemp;
00113                 break;
00114             }
00115             iIntRegIndex++;
00116             iExtRegIndex++;
00117             usNCoils--;
00118 
00119         }
00120     }
00121     else
00122     {
00123         eStatus = MB_ENOREG;
00124     }
00125     return eStatus;
00126 }
00127 
00128 // Procedure résevée pour les bits d'entrées (Foction 2)
00129 // pucRegBuffer => discrete buffer      usAddress => discrete address
00130 // usNDiscrete => discrete number  
00131 eMBErrorCode
00132 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
00133 {
00134     eMBErrorCode    eStatus = MB_ENOERR;
00135     int             iIntRegIndex;
00136     int                iIntBufNum;
00137     int                iIntBitNum;
00138     int                iExtRegIndex=0;
00139     int                iExtBufNum;
00140     int                iExtBitNum;
00141     UCHAR            ucTemp;
00142     if( ( usAddress >= BITS_IN_START )
00143         && ( usAddress + usNDiscrete <= BITS_IN_START + BITS_IN_NBITS ) )
00144     {
00145         iIntRegIndex = ( int )( usAddress - BitsInStart );
00146 
00147         while( usNDiscrete > 0 )
00148         {
00149             iIntBufNum=iIntRegIndex/8;
00150             iIntBitNum=iIntRegIndex%8;
00151             iExtBufNum=iExtRegIndex/8;
00152             iExtBitNum=iExtRegIndex%8;
00153 
00154             // Read discrete inputs
00155             if(iExtBitNum==0){
00156                 pucRegBuffer[iExtBufNum]=0;
00157             }
00158             ucTemp=(BitsIn[iIntBufNum]>>iIntBitNum) & 1;
00159             pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00160 
00161             iIntRegIndex++;
00162             iExtRegIndex++;
00163             usNDiscrete--;
00164         }
00165     }
00166     else
00167     {
00168         eStatus = MB_ENOREG;
00169     }
00170     return eStatus;
00171 }
00172 
00173 // Procedure résevée pour les registres de sorties (Foction 3 et 6)
00174 // pucRegBuffer => holding register buffer   usAddress holding => register address
00175 // usNRegs holding register number           eMode => read or write 
00176 eMBErrorCode
00177 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
00178 {
00179     eMBErrorCode    eStatus = MB_ENOERR;
00180     int             iRegIndex;
00181 
00182     if( ( usAddress >= REG_OUT_START ) &&
00183         ( usAddress + usNRegs <= REG_OUT_START + REG_OUT_NREGS ) )
00184     {
00185         iRegIndex = ( int )( usAddress - RegOutStart );
00186         switch ( eMode )
00187         {
00188         //*** Pass current register values to the protocol stack. ***
00189         case MB_REG_READ:
00190             while( usNRegs > 0 )
00191             {
00192                 *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] >> 8 );
00193                 *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] & 0xFF );
00194                 iRegIndex++;
00195                 usNRegs--;
00196             }
00197             break;
00198 
00199         //***Update current register values with new values from the protocol stack.***
00200         case MB_REG_WRITE:
00201             while( usNRegs > 0 )
00202             {
00203                 RegOut[iRegIndex] = *pucRegBuffer++ << 8;
00204                 RegOut[iRegIndex] |= *pucRegBuffer++;
00205                 iRegIndex++;
00206                 usNRegs--;
00207             }
00208         }
00209     }
00210     else
00211     {
00212         eStatus = MB_ENOREG;
00213     }
00214     return eStatus;
00215 }
00216 // Procedure résevée pour les registres de sorties (Foction 4)
00217 // pucRegBuffer => input register buffer        usAddress => input register address
00218 // usNRegs input register number  
00219 eMBErrorCode
00220 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
00221 {
00222     eMBErrorCode    eStatus = MB_ENOERR;
00223     int             iRegIndex;
00224 
00225     if( ( usAddress >= REG_IN_START )
00226         && ( usAddress + usNRegs <= REG_IN_START + REG_IN_NREGS ) )
00227     {
00228         iRegIndex = ( int )( usAddress - RegInStart );
00229         while( usNRegs > 0 )
00230         {
00231             *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] >> 8 );
00232             *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] & 0xFF );
00233             iRegIndex++;
00234             usNRegs--;
00235         }
00236     }
00237     else
00238     {
00239         eStatus = MB_ENOREG;
00240     }
00241     return eStatus;
00242 }