test public

Fork of Probleme_implementation_lwip by Tom Martins

Committer:
TomTom83
Date:
Mon Jul 02 14:36:46 2018 +0000
Revision:
1:a3ee8cb24540
Parent:
0:0453a0a7e500
Child:
2:9e76d51d9fb6
adaptation de bafian modbus

Who changed what in which revision?

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