Jason Engelman
/
Modbus
main.cpp@0:7b5d37a81b6b, 2010-10-05 (annotated)
- Committer:
- tecnosys
- Date:
- Tue Oct 05 11:51:06 2010 +0000
- Revision:
- 0:7b5d37a81b6b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tecnosys | 0:7b5d37a81b6b | 1 | /* |
tecnosys | 0:7b5d37a81b6b | 2 | * FreeModbus Libary: BARE Demo Application |
tecnosys | 0:7b5d37a81b6b | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
tecnosys | 0:7b5d37a81b6b | 4 | * |
tecnosys | 0:7b5d37a81b6b | 5 | * This program is free software; you can redistribute it and/or modify |
tecnosys | 0:7b5d37a81b6b | 6 | * it under the terms of the GNU General Public License as published by |
tecnosys | 0:7b5d37a81b6b | 7 | * the Free Software Foundation; either version 2 of the License, or |
tecnosys | 0:7b5d37a81b6b | 8 | * (at your option) any later version. |
tecnosys | 0:7b5d37a81b6b | 9 | * |
tecnosys | 0:7b5d37a81b6b | 10 | * This program is distributed in the hope that it will be useful, |
tecnosys | 0:7b5d37a81b6b | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
tecnosys | 0:7b5d37a81b6b | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
tecnosys | 0:7b5d37a81b6b | 13 | * GNU General Public License for more details. |
tecnosys | 0:7b5d37a81b6b | 14 | * |
tecnosys | 0:7b5d37a81b6b | 15 | * You should have received a copy of the GNU General Public License |
tecnosys | 0:7b5d37a81b6b | 16 | * along with this program; if not, write to the Free Software |
tecnosys | 0:7b5d37a81b6b | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
tecnosys | 0:7b5d37a81b6b | 18 | * |
tecnosys | 0:7b5d37a81b6b | 19 | * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ |
tecnosys | 0:7b5d37a81b6b | 20 | */ |
tecnosys | 0:7b5d37a81b6b | 21 | |
tecnosys | 0:7b5d37a81b6b | 22 | /* ----------------------- System includes --------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 23 | |
tecnosys | 0:7b5d37a81b6b | 24 | /* ----------------------- Modbus includes ----------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 25 | #include "mb.h" |
tecnosys | 0:7b5d37a81b6b | 26 | #include "mbport.h" |
tecnosys | 0:7b5d37a81b6b | 27 | |
tecnosys | 0:7b5d37a81b6b | 28 | /* ----------------------- Defines ------------------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 29 | #define REG_INPUT_START 1000 |
tecnosys | 0:7b5d37a81b6b | 30 | #define REG_INPUT_NREGS 4 |
tecnosys | 0:7b5d37a81b6b | 31 | #define SLAVE_ID 0x0A |
tecnosys | 0:7b5d37a81b6b | 32 | |
tecnosys | 0:7b5d37a81b6b | 33 | /* ----------------------- Static variables ---------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 34 | static USHORT usRegInputStart = REG_INPUT_START; |
tecnosys | 0:7b5d37a81b6b | 35 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
tecnosys | 0:7b5d37a81b6b | 36 | |
tecnosys | 0:7b5d37a81b6b | 37 | /* ----------------------- Start implementation -----------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 38 | int |
tecnosys | 0:7b5d37a81b6b | 39 | main( void ) |
tecnosys | 0:7b5d37a81b6b | 40 | { |
tecnosys | 0:7b5d37a81b6b | 41 | eMBErrorCode eStatus; |
tecnosys | 0:7b5d37a81b6b | 42 | |
tecnosys | 0:7b5d37a81b6b | 43 | eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); |
tecnosys | 0:7b5d37a81b6b | 44 | |
tecnosys | 0:7b5d37a81b6b | 45 | /* Enable the Modbus Protocol Stack. */ |
tecnosys | 0:7b5d37a81b6b | 46 | eStatus = eMBEnable( ); |
tecnosys | 0:7b5d37a81b6b | 47 | |
tecnosys | 0:7b5d37a81b6b | 48 | // Initialise some registers |
tecnosys | 0:7b5d37a81b6b | 49 | usRegInputBuf[1] = 0x1234; |
tecnosys | 0:7b5d37a81b6b | 50 | usRegInputBuf[2] = 0x5678; |
tecnosys | 0:7b5d37a81b6b | 51 | usRegInputBuf[3] = 0x9abc; |
tecnosys | 0:7b5d37a81b6b | 52 | |
tecnosys | 0:7b5d37a81b6b | 53 | for( ;; ) |
tecnosys | 0:7b5d37a81b6b | 54 | { |
tecnosys | 0:7b5d37a81b6b | 55 | ( void )eMBPoll( ); |
tecnosys | 0:7b5d37a81b6b | 56 | |
tecnosys | 0:7b5d37a81b6b | 57 | /* Here we simply count the number of poll cycles. */ |
tecnosys | 0:7b5d37a81b6b | 58 | usRegInputBuf[0]++; |
tecnosys | 0:7b5d37a81b6b | 59 | } |
tecnosys | 0:7b5d37a81b6b | 60 | } |
tecnosys | 0:7b5d37a81b6b | 61 | |
tecnosys | 0:7b5d37a81b6b | 62 | eMBErrorCode |
tecnosys | 0:7b5d37a81b6b | 63 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
tecnosys | 0:7b5d37a81b6b | 64 | { |
tecnosys | 0:7b5d37a81b6b | 65 | eMBErrorCode eStatus = MB_ENOERR; |
tecnosys | 0:7b5d37a81b6b | 66 | int iRegIndex; |
tecnosys | 0:7b5d37a81b6b | 67 | |
tecnosys | 0:7b5d37a81b6b | 68 | if( ( usAddress >= REG_INPUT_START ) |
tecnosys | 0:7b5d37a81b6b | 69 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
tecnosys | 0:7b5d37a81b6b | 70 | { |
tecnosys | 0:7b5d37a81b6b | 71 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
tecnosys | 0:7b5d37a81b6b | 72 | while( usNRegs > 0 ) |
tecnosys | 0:7b5d37a81b6b | 73 | { |
tecnosys | 0:7b5d37a81b6b | 74 | *pucRegBuffer++ = |
tecnosys | 0:7b5d37a81b6b | 75 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
tecnosys | 0:7b5d37a81b6b | 76 | *pucRegBuffer++ = |
tecnosys | 0:7b5d37a81b6b | 77 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
tecnosys | 0:7b5d37a81b6b | 78 | iRegIndex++; |
tecnosys | 0:7b5d37a81b6b | 79 | usNRegs--; |
tecnosys | 0:7b5d37a81b6b | 80 | } |
tecnosys | 0:7b5d37a81b6b | 81 | } |
tecnosys | 0:7b5d37a81b6b | 82 | else |
tecnosys | 0:7b5d37a81b6b | 83 | { |
tecnosys | 0:7b5d37a81b6b | 84 | eStatus = MB_ENOREG; |
tecnosys | 0:7b5d37a81b6b | 85 | } |
tecnosys | 0:7b5d37a81b6b | 86 | |
tecnosys | 0:7b5d37a81b6b | 87 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 88 | } |
tecnosys | 0:7b5d37a81b6b | 89 | |
tecnosys | 0:7b5d37a81b6b | 90 | eMBErrorCode |
tecnosys | 0:7b5d37a81b6b | 91 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
tecnosys | 0:7b5d37a81b6b | 92 | { |
tecnosys | 0:7b5d37a81b6b | 93 | eMBErrorCode eStatus = MB_ENOERR; |
tecnosys | 0:7b5d37a81b6b | 94 | int iRegIndex; |
tecnosys | 0:7b5d37a81b6b | 95 | |
tecnosys | 0:7b5d37a81b6b | 96 | if (eMode == MB_REG_READ) |
tecnosys | 0:7b5d37a81b6b | 97 | { |
tecnosys | 0:7b5d37a81b6b | 98 | if( ( usAddress >= REG_INPUT_START ) |
tecnosys | 0:7b5d37a81b6b | 99 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
tecnosys | 0:7b5d37a81b6b | 100 | { |
tecnosys | 0:7b5d37a81b6b | 101 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
tecnosys | 0:7b5d37a81b6b | 102 | while( usNRegs > 0 ) |
tecnosys | 0:7b5d37a81b6b | 103 | { |
tecnosys | 0:7b5d37a81b6b | 104 | *pucRegBuffer++ = |
tecnosys | 0:7b5d37a81b6b | 105 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
tecnosys | 0:7b5d37a81b6b | 106 | *pucRegBuffer++ = |
tecnosys | 0:7b5d37a81b6b | 107 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
tecnosys | 0:7b5d37a81b6b | 108 | iRegIndex++; |
tecnosys | 0:7b5d37a81b6b | 109 | usNRegs--; |
tecnosys | 0:7b5d37a81b6b | 110 | } |
tecnosys | 0:7b5d37a81b6b | 111 | } |
tecnosys | 0:7b5d37a81b6b | 112 | } |
tecnosys | 0:7b5d37a81b6b | 113 | |
tecnosys | 0:7b5d37a81b6b | 114 | if (eMode == MB_REG_WRITE) |
tecnosys | 0:7b5d37a81b6b | 115 | { |
tecnosys | 0:7b5d37a81b6b | 116 | if( ( usAddress >= REG_INPUT_START ) |
tecnosys | 0:7b5d37a81b6b | 117 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
tecnosys | 0:7b5d37a81b6b | 118 | { |
tecnosys | 0:7b5d37a81b6b | 119 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
tecnosys | 0:7b5d37a81b6b | 120 | while( usNRegs > 0 ) |
tecnosys | 0:7b5d37a81b6b | 121 | { |
tecnosys | 0:7b5d37a81b6b | 122 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
tecnosys | 0:7b5d37a81b6b | 123 | pucRegBuffer+=2; |
tecnosys | 0:7b5d37a81b6b | 124 | iRegIndex++; |
tecnosys | 0:7b5d37a81b6b | 125 | usNRegs--; |
tecnosys | 0:7b5d37a81b6b | 126 | } |
tecnosys | 0:7b5d37a81b6b | 127 | } |
tecnosys | 0:7b5d37a81b6b | 128 | } |
tecnosys | 0:7b5d37a81b6b | 129 | |
tecnosys | 0:7b5d37a81b6b | 130 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 131 | } |
tecnosys | 0:7b5d37a81b6b | 132 | |
tecnosys | 0:7b5d37a81b6b | 133 | |
tecnosys | 0:7b5d37a81b6b | 134 | eMBErrorCode |
tecnosys | 0:7b5d37a81b6b | 135 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
tecnosys | 0:7b5d37a81b6b | 136 | eMBRegisterMode eMode ) |
tecnosys | 0:7b5d37a81b6b | 137 | { |
tecnosys | 0:7b5d37a81b6b | 138 | return MB_ENOREG; |
tecnosys | 0:7b5d37a81b6b | 139 | } |
tecnosys | 0:7b5d37a81b6b | 140 | |
tecnosys | 0:7b5d37a81b6b | 141 | eMBErrorCode |
tecnosys | 0:7b5d37a81b6b | 142 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
tecnosys | 0:7b5d37a81b6b | 143 | { |
tecnosys | 0:7b5d37a81b6b | 144 | return MB_ENOREG; |
tecnosys | 0:7b5d37a81b6b | 145 | } |
tecnosys | 0:7b5d37a81b6b | 146 |