hsu han-lin
/
Modbus_Test
from Cam Marshall original modbus
Fork of Modbus by
Revision 6:c3eaa87a6b3e, committed 2017-03-22
- Comitter:
- stanley1228
- Date:
- Wed Mar 22 22:37:25 2017 +0800
- Parent:
- 5:c712a10c9414
- Child:
- 7:44579368875a
- Commit message:
- 1.Add REG_HOLDING_START,REG_HOLDING_NREGS
2.change eMBRegHoldingCB function
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Mar 14 22:23:17 2017 +0800 +++ b/main.cpp Wed Mar 22 22:37:25 2017 +0800 @@ -31,14 +31,44 @@ Serial pc(USBTX, USBRX); //stanley /* ----------------------- Defines ------------------------------------------*/ -#define REG_INPUT_START 1000 -#define REG_INPUT_NREGS 4 +#define REG_INPUT_START (3001) +#define REG_INPUT_NREGS 15 +#define REG_HOLDING_START (4001) +#define REG_HOLDING_NREGS 6 + #define SLAVE_ID 0x0A + //== + //Modbus struct + //== + //Input register 0~14 + //public UInt16[] PosVal = new UInt16[DEF_MAX_AXIS]; 7 + //public Int16[] VelValue = new Int16[DEF_MAX_AXIS]; 7 + //public Int16 Err_State = 0; 1 + + ////Holding register 0~5 + //public Int16 TargetPosX; 1 + //public Int16 TargetPosY; 1 + //public Int16 TargetPosZ; 1 + //public Int16 OPMode; //P2P 1 + //public Int16 SpeedRatio; //0~1 1 + +enum{ + DEF_RHI_TARGET_POSX=0, + DEF_RHI_TARGET_POSY, + DEF_RHI_TARGET_POSZ, + DEF_RHI_OPMODE, + DEF_RHI_SPPED_RATIO_L, + DEF_RHI_SPPED_RATIO_H +}; + /* ----------------------- Static variables ---------------------------------*/ static USHORT usRegInputStart = REG_INPUT_START; static USHORT usRegInputBuf[REG_INPUT_NREGS]; +static USHORT usRegHoldingStart = REG_HOLDING_START; +static USHORT usRegHoldingBuf[REG_HOLDING_NREGS]; + /* ----------------------- Start implementation -----------------------------*/ int main( void ) @@ -48,13 +78,28 @@ eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE ); /* Enable the Modbus Protocol Stack. */ - eStatus = eMBEnable( ); + eMBEnable( ); + //eStatus = eMBEnable( ); // Initialise some registers usRegInputBuf[1] = 0x1234; usRegInputBuf[2] = 0x5678; - usRegInputBuf[3] = 0x9abc; - + usRegInputBuf[3] = 0x9abc; + usRegInputBuf[4] = 0x1000; + usRegInputBuf[5] = 0x1001; + usRegInputBuf[6] = 0x1002; + + usRegHoldingBuf[DEF_RHI_TARGET_POSX]=0x0; + usRegHoldingBuf[DEF_RHI_TARGET_POSY]=0x1122; + usRegHoldingBuf[DEF_RHI_TARGET_POSZ]=0x3344; + usRegHoldingBuf[DEF_RHI_OPMODE]=0x2; + + float fSpeedRatio=0.345; + USHORT* usp=(USHORT*)&fSpeedRatio; + usRegHoldingBuf[DEF_RHI_SPPED_RATIO_L]=*usp; + usRegHoldingBuf[DEF_RHI_SPPED_RATIO_H]=*(usp+1); + + myled=1;//stanley @@ -105,23 +150,23 @@ } eMBErrorCode -eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) +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_INPUT_START ) - && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) + if( ( usAddress >= REG_HOLDING_START ) + && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegInputStart ); + iRegIndex = ( int )( usAddress - usRegHoldingStart ); while( usNRegs > 0 ) { *pucRegBuffer++ = - ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); + ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 ); *pucRegBuffer++ = - ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); + ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF ); iRegIndex++; usNRegs--; } @@ -130,13 +175,13 @@ if (eMode == MB_REG_WRITE) { - if( ( usAddress >= REG_INPUT_START ) - && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) + if( ( usAddress >= REG_HOLDING_START ) + && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegInputStart ); + iRegIndex = ( int )( usAddress - usRegHoldingStart ); while( usNRegs > 0 ) { - usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); + usRegHoldingBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); pucRegBuffer+=2; iRegIndex++; usNRegs--;