Yuji Hosogaya / Mbed 2 deprecated ModbusTCP_Modified

Dependencies:   EthernetNetIf 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 #if MB_TCP_ENABLED == 1
00086   pc.baud(115200);
00087   EtherPinMonitor.attach(&EtherPinMonitorFunc, 0.01);
00088   printf("Setting up...\n");
00089   EthernetErr ethErr = eth.setup();
00090   if(ethErr)
00091   {
00092     printf("Error %d in setup.\n", ethErr);
00093     return -1;
00094   }
00095   printf("Setup OK\n");
00096   IpAddr ip = eth.getIp();
00097   printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
00098 #endif
00099    
00100   Timer tm;
00101   tm.start();
00102 
00103 #if MB_RTU_ENABLED == 1
00104     eStatus = eMBInit( MB_RTU , SLAVE_ID, 0, 9600, MB_PAR_NONE  );
00105 #endif
00106 #if MB_ASCII_ENABLED == 1
00107     eStatus = eMBInit( MB_ASCII , SLAVE_ID, 0, 9600, MB_PAR_NONE  );
00108 #endif
00109 #if MB_TCP_ENABLED == 1
00110     eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT );
00111 #endif
00112     if (eStatus != MB_ENOERR  )
00113       printf( "can't initialize modbus stack!\r\n" );
00114 
00115     /* Enable the Modbus Protocol Stack. */
00116     eStatus = eMBEnable(  );
00117     if (eStatus != MB_ENOERR  )
00118       fprintf( stderr, "can't enable modbus stack!\r\n" );
00119 
00120   // Initialise some registers
00121   usRegInputBuf[1] = 0x1234;
00122   usRegInputBuf[2] = 0x5678;
00123   usRegInputBuf[3] = 0x9abc;        
00124 
00125   while(true)
00126   {
00127 #if MB_TCP_ENABLED == 1
00128     Net::poll();
00129 #endif
00130 
00131     if(tm.read()>.5)
00132     {
00133       led1=!led1; //Show that we are alive
00134       tm.start();
00135     }
00136 
00137     eStatus = eMBPoll(  );
00138             
00139     /* Here we simply count the number of poll cycles. */
00140     usRegInputBuf[0]++;
00141   }
00142   //return 0;
00143 }
00144 
00145 eMBErrorCode
00146 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
00147 {
00148     eMBErrorCode    eStatus = MB_ENOERR ;
00149     int             iRegIndex;
00150 
00151     if( ( usAddress >= REG_INPUT_START )
00152         && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
00153     {
00154         iRegIndex = ( int )( usAddress - usRegInputStart );
00155         while( usNRegs > 0 )
00156         {
00157             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
00158             *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
00159             iRegIndex++;
00160             usNRegs--;
00161         }
00162     }
00163     else
00164     {
00165         eStatus = MB_ENOREG ;
00166     }
00167     return eStatus;
00168 }
00169 
00170 eMBErrorCode
00171 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
00172 {
00173     eMBErrorCode    eStatus = MB_ENOERR ;
00174     int             iRegIndex;
00175 
00176     if( ( usAddress >= REG_HOLDING_START ) &&
00177         ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
00178     {
00179         iRegIndex = ( int )( usAddress - usRegHoldingStart );
00180         switch ( eMode )
00181         {
00182             /* Pass current register values to the protocol stack. */
00183         case MB_REG_READ :
00184             while( usNRegs > 0 )
00185             {
00186                 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
00187                 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
00188                 iRegIndex++;
00189                 usNRegs--;
00190             }
00191             break;
00192 
00193             /* Update current register values with new values from the
00194              * protocol stack. */
00195         case MB_REG_WRITE :
00196             while( usNRegs > 0 )
00197             {
00198                 usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
00199                 usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
00200                 iRegIndex++;
00201                 usNRegs--;
00202             }
00203         }
00204     }
00205     else
00206     {
00207         eStatus = MB_ENOREG ;
00208     }
00209     return eStatus;
00210 }
00211 
00212 /* 
00213  * Following implementation is not actually checked.
00214  */
00215 
00216 eMBErrorCode
00217 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
00218 {
00219     eMBErrorCode    eStatus = MB_ENOERR ;
00220     int             iIntRegIndex;
00221     int                iIntBufNum;
00222     int                iIntBitNum;
00223     int                iExtRegIndex=0;
00224     int                iExtBufNum;
00225     int                iExtBitNum;
00226     UCHAR            ucTemp;
00227     if( ( usAddress >= REG_COIL_START )
00228         && ( usAddress + usNCoils <= REG_COIL_START + REG_COIL_NREGS ) )
00229     {
00230         iIntRegIndex = ( int )( usAddress - usRegCoilStart );
00231 
00232         while( usNCoils > 0 )
00233         {
00234             iIntBufNum=iIntRegIndex/8;
00235             iIntBitNum=iIntRegIndex%8;
00236             iExtBufNum=iExtRegIndex/8;
00237             iExtBitNum=iExtRegIndex%8;
00238 
00239             switch ( eMode )
00240             {
00241             case MB_REG_READ :
00242                 // Read coils
00243                 if(iExtBitNum==0){
00244                     pucRegBuffer[iExtBufNum]=0;
00245                 }
00246                 ucTemp=(usRegCoilBuf[iIntBufNum]>>iIntBitNum) & 1;
00247                 pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00248                 break;
00249 
00250             case MB_REG_WRITE :
00251                 // Write coils
00252                 ucTemp=usRegCoilBuf[iIntBufNum]&(~(1<<iIntBitNum));
00253                 ucTemp|=((pucRegBuffer[iExtBufNum]>>iExtBitNum) & 1)<<iIntBitNum;
00254                 usRegCoilBuf[iIntBufNum]=ucTemp;
00255                 break;
00256             }
00257             iIntRegIndex++;
00258             iExtRegIndex++;
00259             usNCoils--;
00260 
00261         }
00262     }
00263     else
00264     {
00265         eStatus = MB_ENOREG ;
00266     }
00267 
00268     return eStatus;
00269 }
00270 
00271 eMBErrorCode
00272 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
00273 {
00274     eMBErrorCode    eStatus = MB_ENOERR ;
00275     int             iIntRegIndex;
00276     int                iIntBufNum;
00277     int                iIntBitNum;
00278     int                iExtRegIndex=0;
00279     int                iExtBufNum;
00280     int                iExtBitNum;
00281     UCHAR            ucTemp;
00282     if( ( usAddress >= REG_DISC_START )
00283         && ( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_NREGS ) )
00284     {
00285         iIntRegIndex = ( int )( usAddress - usRegDiscStart );
00286 
00287         while( usNDiscrete > 0 )
00288         {
00289             iIntBufNum=iIntRegIndex/8;
00290             iIntBitNum=iIntRegIndex%8;
00291             iExtBufNum=iExtRegIndex/8;
00292             iExtBitNum=iExtRegIndex%8;
00293 
00294             // Read discrete inputs
00295             if(iExtBitNum==0){
00296                 pucRegBuffer[iExtBufNum]=0;
00297             }
00298             ucTemp=(usRegDiscBuf[iIntBufNum]>>iIntBitNum) & 1;
00299             pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum;
00300 
00301             iIntRegIndex++;
00302             iExtRegIndex++;
00303             usNDiscrete--;
00304 
00305         }
00306     }
00307     else
00308     {
00309         eStatus = MB_ENOREG ;
00310     }
00311 
00312     return eStatus;
00313 }