TVZ2019 / Mbed 2 deprecated MODBUS_TCP-IP

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * FreeModbus Libary: BARE Demo Application
00003  * Copyright (C) 2006 Christian Walter <wolti@sil.at>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
00020 
00021 * modified from: Thanassis Mavrogeorgiadis 5-12-2011
00022  */
00023 
00024 #include "mbed.h"
00025 
00026 /* ----------------------- Modbus includes ----------------------------------*/
00027 #include "mb.h"
00028 #include "mbport.h"
00029 #include "mbconfig.h"
00030 
00031 
00032 #if MB_TCP_ENABLED == 1
00033 #include "EthernetNetIf.h"
00034 EthernetNetIf eth;  
00035 #endif
00036 
00037 extern Serial pc;
00038 
00039 DigitalOut led1(LED1);
00040 
00041 DigitalIn ENET_LINK(P1_25);
00042 DigitalOut LedLINK(LED4);
00043 Ticker EtherPinMonitor;
00044 
00045 #if MB_TCP_ENABLED == 1
00046 void EtherPinMonitorFunc(void)
00047 {
00048   LedLINK = !ENET_LINK; 
00049 }
00050 #endif
00051 
00052 /* ----------------------- Defines ------------------------------------------*/
00053 #define REG_INPUT_START 1001
00054 #define REG_INPUT_NREGS 4
00055 
00056 #define REG_HOLDING_START       2001
00057 #define REG_HOLDING_NREGS       130
00058 
00059 #define REG_COIL_START (3000+1)
00060 #define REG_COIL_NREGS 8
00061 #define REG_COIL_BYTES REG_COIL_NREGS/8
00062 
00063 #define REG_DISC_START (4000+1)
00064 #define REG_DISC_NREGS 8
00065 #define REG_DISC_BYTES REG_DISC_NREGS/8
00066 
00067 #define SLAVE_ID 0x0A
00068 
00069 /* ----------------------- Static variables ---------------------------------*/
00070 static USHORT   usRegInputStart = REG_INPUT_START;
00071 static USHORT   usRegInputBuf[REG_INPUT_NREGS];
00072 static USHORT   usRegHoldingStart = REG_HOLDING_START;
00073 static USHORT   usRegHoldingBuf[REG_HOLDING_NREGS]={0x0123,0x4567,0x89AB,0xCDEF,0xDEAD,0xBEEF,0xDEAD,0xBEEF,0xDEAD,0xBEEF};
00074 static USHORT    usRegCoilStart = REG_COIL_START;
00075 static UCHAR     usRegCoilBuf[REG_COIL_BYTES]={0xA5};
00076 static USHORT    usRegDiscStart = REG_DISC_START;
00077 static UCHAR     usRegDiscBuf[REG_DISC_BYTES]={0x5A};
00078 
00079 /* ----------------------- Start implementation -----------------------------*/
00080 
00081 
00082 int main() {
00083   eMBErrorCode    eStatus;
00084   
00085 printf("Ekipa book :)    ");
00086 #if MB_TCP_ENABLED == 1
00087   pc.baud(115200);
00088   EtherPinMonitor.attach(&EtherPinMonitorFunc, 0.01);
00089   printf("Setting up...\n");
00090   EthernetErr ethErr = eth.setup();
00091   if(ethErr)
00092   {
00093     printf("Error %d in setup.\n", ethErr);
00094     return -1;
00095   }
00096   printf("Setup OK\n");
00097   IpAddr ip = eth.getIp();
00098   printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
00099 #endif
00100     
00101   Timer tm;
00102   tm.start();
00103 
00104 #if MB_RTU_ENABLED == 1
00105     eStatus = eMBInit( MB_RTU , SLAVE_ID, 0, 9600, MB_PAR_NONE  );
00106 #endif
00107 #if MB_ASCII_ENABLED == 1
00108     eStatus = eMBInit( MB_ASCII , SLAVE_ID, 0, 9600, MB_PAR_NONE  );
00109 #endif
00110 #if MB_TCP_ENABLED == 1
00111     eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT );
00112 #endif
00113     if (eStatus != MB_ENOERR  )
00114       printf( "can't initialize modbus stack!\r\n" );
00115 
00116     /* Enable the Modbus Protocol Stack. */
00117     eStatus = eMBEnable(  );
00118     if (eStatus != MB_ENOERR  )
00119       fprintf( stderr, "can't enable modbus stack!\r\n" );
00120 
00121   // Initialise some registers
00122   usRegInputBuf[1] = 0x1234;
00123   usRegInputBuf[2] = 0x5678;
00124   usRegInputBuf[3] = 0x9abc;        
00125 
00126   while(true)
00127   {
00128 #if MB_TCP_ENABLED == 1
00129     Net::poll();
00130 #endif
00131     if(tm.read()>1.5)
00132     {
00133       led1=!led1; //Show that we are alive
00134       tm.start();
00135       //printf ("|R0| %d||",usRegInputBuf[1]);
00136      // printf ("|R1| %d||",usRegHoldingBuf[1]);
00137     }
00138  
00139 
00140     eStatus = eMBPoll(  );
00141             
00142     /* Here we simply count the number of poll cycles. */
00143     usRegInputBuf[0]++;
00144   }
00145   //return 0;
00146 }
00147 
00148 eMBErrorCode
00149 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
00150 {
00151     eMBErrorCode    eStatus = MB_ENOERR ;
00152     int             iRegIndex;
00153 
00154     if( ( usAddress >= REG_INPUT_START )
00155         && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
00156     {
00157         iRegIndex = ( int )( usAddress - usRegInputStart );
00158         while( usNRegs > 0 )
00159         {
00160             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
00161             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
00162             iRegIndex++;
00163             usNRegs--;
00164         }
00165     }
00166     else
00167     {
00168         eStatus = MB_ENOREG ;
00169     }
00170     return eStatus;
00171 }
00172 
00173 eMBErrorCode
00174 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
00175 {
00176     eMBErrorCode    eStatus = MB_ENOERR ;
00177     int             iRegIndex;
00178 
00179     if( ( usAddress >= REG_HOLDING_START ) &&
00180         ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
00181     {
00182         iRegIndex = ( int )( usAddress - usRegHoldingStart );
00183         switch ( eMode )
00184         {
00185             /* Pass current register values to the protocol stack. */
00186         case MB_REG_READ :
00187             while( usNRegs > 0 )
00188             {
00189                 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
00190                 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
00191                 iRegIndex++;
00192                 usNRegs--;
00193             }
00194             break;
00195 
00196             /* Update current register values with new values from the
00197              * protocol stack. */
00198         case MB_REG_WRITE :
00199             while( usNRegs > 0 )
00200             {
00201                 usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
00202                 usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
00203                 iRegIndex++;
00204                 usNRegs--;
00205             }
00206         }
00207     }
00208     else
00209     {
00210         eStatus = MB_ENOREG ;
00211     }
00212     return eStatus;
00213     
00214     
00215 }
00216 
00217 /* 
00218  * Following implementation is not actually checked.
00219  */
00220 
00221 eMBErrorCode
00222 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
00223 {
00224     eMBErrorCode    eStatus = MB_ENOERR ;
00225     int             iIntRegIndex;
00226     int                iIntBufNum;
00227     int                iIntBitNum;
00228     int                iExtRegIndex=0;
00229     int                iExtBufNum;
00230     int                iExtBitNum;
00231     UCHAR            ucTemp;
00232     if( ( usAddress >= REG_COIL_START )
00233         && ( usAddress + usNCoils <= REG_COIL_START + REG_COIL_NREGS ) )
00234     {
00235         iIntRegIndex = ( int )( usAddress - usRegCoilStart );
00236 
00237         while( usNCoils > 0 )
00238         {
00239             iIntBufNum=iIntRegIndex/8;
00240             iIntBitNum=iIntRegIndex%8;
00241             iExtBufNum=iExtRegIndex/8;
00242             iExtBitNum=iExtRegIndex%8;
00243 
00244             switch ( eMode )
00245             {
00246             case MB_REG_READ :
00247                 // Read coils
00248                 if(iExtBitNum==0){
00249                     pucRegBuffer[iExtBufNum]=0;
00250                 }
00251                 ucTemp=(usRegCoilBuf[iIntBufNum]>>iIntBitNum) & 1;
00252                 pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00253                 break;
00254 
00255             case MB_REG_WRITE :
00256                 // Write coils
00257                 ucTemp=usRegCoilBuf[iIntBufNum]&(~(1<<iIntBitNum));
00258                 ucTemp|=((pucRegBuffer[iExtBufNum]>>iExtBitNum) & 1)<<iIntBitNum;
00259                 usRegCoilBuf[iIntBufNum]=ucTemp;
00260                 break;
00261             }
00262             iIntRegIndex++;
00263             iExtRegIndex++;
00264             usNCoils--;
00265 
00266         }
00267     }
00268     else
00269     {
00270         eStatus = MB_ENOREG ;
00271     }
00272 
00273     return eStatus;
00274 }
00275 
00276 eMBErrorCode
00277 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
00278 {
00279     eMBErrorCode    eStatus = MB_ENOERR ;
00280     int             iIntRegIndex;
00281     int                iIntBufNum;
00282     int                iIntBitNum;
00283     int                iExtRegIndex=0;
00284     int                iExtBufNum;
00285     int                iExtBitNum;
00286     UCHAR            ucTemp;
00287     if( ( usAddress >= REG_DISC_START )
00288         && ( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_NREGS ) )
00289     {
00290         iIntRegIndex = ( int )( usAddress - usRegDiscStart );
00291 
00292         while( usNDiscrete > 0 )
00293         {
00294             iIntBufNum=iIntRegIndex/8;
00295             iIntBitNum=iIntRegIndex%8;
00296             iExtBufNum=iExtRegIndex/8;
00297             iExtBitNum=iExtRegIndex%8;
00298 
00299             // Read discrete inputs
00300             if(iExtBitNum==0){
00301                 pucRegBuffer[iExtBufNum]=0;
00302             }
00303             ucTemp=(usRegDiscBuf[iIntBufNum]>>iIntBitNum) & 1;
00304             pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00305 
00306             iIntRegIndex++;
00307             iExtRegIndex++;
00308             usNDiscrete--;
00309 
00310         }
00311     }
00312     else
00313     {
00314         eStatus = MB_ENOREG ;
00315     }
00316 
00317     return eStatus;
00318 }