Control robot gripper through modbus

Dependencies:   Modbus_For_F446RE mbed

main.cpp

Committer:
stanley1228
Date:
2017-07-06
Revision:
0:20ab14f5bf60

File content as of revision 0:20ab14f5bf60:

/*
 * FreeModbus Libary: BARE Demo Application
 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
 */

/* ----------------------- System includes --------------------------------*/

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "mbed.h" //stanley
DigitalOut myled(LED1); //stanley


Serial pc(USBTX, USBRX);  //stanley

/* ----------------------- Defines ------------------------------------------*/
#define REG_INPUT_START (3001)  //not use now stanley
#define REG_INPUT_NREGS 15      //not use now stanley
#define REG_HOLDING_START (4001)
#define REG_HOLDING_NREGS 10

#define SLAVE_ID 0x01

//==
//Modbus struct
//==
   

enum{
    DEF_INX_R_GRIPPER_HOLD=0,
    DEF_INX_L_GRIPPER_HOLD,
    DEF_INX_TEST1,
    DEF_INX_TEST2,
    DEF_INX_CYCLE_COUNT
};

/* ----------------------- Static variables ---------------------------------*/
static USHORT   usRegInputStart = REG_INPUT_START; //not use now stanley
static USHORT   usRegInputBuf[REG_INPUT_NREGS]; //not use now stanley

static USHORT   usRegHoldingStart = REG_HOLDING_START;
static USHORT   usRegHoldingBuf[REG_HOLDING_NREGS];

/* ----------------------- Start implementation -----------------------------*/

int main( void )
{
    eMBErrorCode    eStatus;

    eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 460800, MB_PAR_NONE );

    /* Enable the Modbus Protocol Stack. */
    eMBEnable(  );
    //eStatus = eMBEnable(  );
    
    // Initialise some registers
    usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]=0;
    usRegHoldingBuf[DEF_INX_L_GRIPPER_HOLD]=0;
    usRegHoldingBuf[DEF_INX_TEST1]=123;
    usRegHoldingBuf[DEF_INX_TEST2]=456;
    usRegHoldingBuf[DEF_INX_CYCLE_COUNT]=0;
    
    
    myled=1;//stanley
    
    while(1)
    {
        //(void)eMBPoll(  ); origianl
        //
        eStatus=eMBPoll();
        
        /* Here we simply count the number of poll cycles. */
        usRegHoldingBuf[DEF_INX_CYCLE_COUNT]++;
         
        wait_ms(5);        //stanley
        
        
        if(usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]==0)
        {
            myled=0;
        }
        else if(usRegHoldingBuf[DEF_INX_R_GRIPPER_HOLD]==1)
        {
            myled=1;        
        }
        
    }
}

eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;

    if( ( usAddress >= REG_INPUT_START )
        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
    {
        iRegIndex = ( int )( usAddress - usRegInputStart );
        while( usNRegs > 0 )
        {
            *pucRegBuffer++ =
                ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
            *pucRegBuffer++ =
                ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
            iRegIndex++;
            usNRegs--;
        }
    }
    else
    {
        eStatus = MB_ENOREG;
    }

    return eStatus;
}

eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )  //change variable to REG_HOLDING ,original is all input register
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;

    if (eMode == MB_REG_READ)
    {
        if( ( usAddress >= REG_HOLDING_START )
            && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
        {
            iRegIndex = ( int )( usAddress - usRegHoldingStart );
            while( usNRegs > 0 )
            {
                *pucRegBuffer++ =
                    ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
                *pucRegBuffer++ =
                    ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF );
                iRegIndex++;
                usNRegs--;
            }
        }
    }

    if (eMode == MB_REG_WRITE)
    {
        if( ( usAddress >= REG_HOLDING_START )
            && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
        {
            iRegIndex = ( int )( usAddress - usRegHoldingStart );
            while( usNRegs > 0 )
            {
                usRegHoldingBuf[iRegIndex] =  ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
                pucRegBuffer+=2;
                iRegIndex++;
                usNRegs--;
            }
        }
    }

    return eStatus;
}


eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
               eMBRegisterMode eMode )
{
    return MB_ENOREG;
}

eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
    return MB_ENOREG;
}