Modbus RTU/ASCII/TCP with lwip TCP working partial, but with errors (retransmitions)

Dependencies:   EthernetNetIf mbed

Committer:
tmav123
Date:
Mon Dec 05 22:49:02 2011 +0000
Revision:
0:f54e9507171b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tmav123 0:f54e9507171b 1 /*
tmav123 0:f54e9507171b 2 * FreeModbus Libary: BARE Demo Application
tmav123 0:f54e9507171b 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
tmav123 0:f54e9507171b 4 *
tmav123 0:f54e9507171b 5 * This program is free software; you can redistribute it and/or modify
tmav123 0:f54e9507171b 6 * it under the terms of the GNU General Public License as published by
tmav123 0:f54e9507171b 7 * the Free Software Foundation; either version 2 of the License, or
tmav123 0:f54e9507171b 8 * (at your option) any later version.
tmav123 0:f54e9507171b 9 *
tmav123 0:f54e9507171b 10 * This program is distributed in the hope that it will be useful,
tmav123 0:f54e9507171b 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tmav123 0:f54e9507171b 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tmav123 0:f54e9507171b 13 * GNU General Public License for more details.
tmav123 0:f54e9507171b 14 *
tmav123 0:f54e9507171b 15 * You should have received a copy of the GNU General Public License
tmav123 0:f54e9507171b 16 * along with this program; if not, write to the Free Software
tmav123 0:f54e9507171b 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
tmav123 0:f54e9507171b 18 *
tmav123 0:f54e9507171b 19 * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
tmav123 0:f54e9507171b 20
tmav123 0:f54e9507171b 21 * modified from: Thanassis Mavrogeorgiadis 5-12-2011
tmav123 0:f54e9507171b 22 */
tmav123 0:f54e9507171b 23
tmav123 0:f54e9507171b 24 #include "mbed.h"
tmav123 0:f54e9507171b 25
tmav123 0:f54e9507171b 26 /* ----------------------- Modbus includes ----------------------------------*/
tmav123 0:f54e9507171b 27 #include "mb.h"
tmav123 0:f54e9507171b 28 #include "mbport.h"
tmav123 0:f54e9507171b 29 #include "mbconfig.h"
tmav123 0:f54e9507171b 30
tmav123 0:f54e9507171b 31
tmav123 0:f54e9507171b 32 #if MB_TCP_ENABLED == 1
tmav123 0:f54e9507171b 33 #include "EthernetNetIf.h"
tmav123 0:f54e9507171b 34 EthernetNetIf eth;
tmav123 0:f54e9507171b 35 #endif
tmav123 0:f54e9507171b 36
tmav123 0:f54e9507171b 37 extern Serial pc;
tmav123 0:f54e9507171b 38
tmav123 0:f54e9507171b 39 DigitalOut led1(LED1);
tmav123 0:f54e9507171b 40
tmav123 0:f54e9507171b 41 DigitalIn ENET_LINK(P1_25);
tmav123 0:f54e9507171b 42 DigitalOut LedLINK(LED4);
tmav123 0:f54e9507171b 43 Ticker EtherPinMonitor;
tmav123 0:f54e9507171b 44
tmav123 0:f54e9507171b 45 #if MB_TCP_ENABLED == 1
tmav123 0:f54e9507171b 46 void EtherPinMonitorFunc(void)
tmav123 0:f54e9507171b 47 {
tmav123 0:f54e9507171b 48 LedLINK = !ENET_LINK;
tmav123 0:f54e9507171b 49 }
tmav123 0:f54e9507171b 50 #endif
tmav123 0:f54e9507171b 51
tmav123 0:f54e9507171b 52 /* ----------------------- Defines ------------------------------------------*/
tmav123 0:f54e9507171b 53 #define REG_INPUT_START 1000
tmav123 0:f54e9507171b 54 #define REG_INPUT_NREGS 4
tmav123 0:f54e9507171b 55 #define REG_HOLDING_START 2000
tmav123 0:f54e9507171b 56 #define REG_HOLDING_NREGS 130
tmav123 0:f54e9507171b 57
tmav123 0:f54e9507171b 58 #define SLAVE_ID 0x0A
tmav123 0:f54e9507171b 59
tmav123 0:f54e9507171b 60 /* ----------------------- Static variables ---------------------------------*/
tmav123 0:f54e9507171b 61 static USHORT usRegInputStart = REG_INPUT_START;
tmav123 0:f54e9507171b 62 static USHORT usRegInputBuf[REG_INPUT_NREGS];
tmav123 0:f54e9507171b 63 static USHORT usRegHoldingStart = REG_HOLDING_START;
tmav123 0:f54e9507171b 64 static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
tmav123 0:f54e9507171b 65
tmav123 0:f54e9507171b 66 /* ----------------------- Start implementation -----------------------------*/
tmav123 0:f54e9507171b 67
tmav123 0:f54e9507171b 68
tmav123 0:f54e9507171b 69 int main() {
tmav123 0:f54e9507171b 70 eMBErrorCode eStatus;
tmav123 0:f54e9507171b 71
tmav123 0:f54e9507171b 72 #if MB_TCP_ENABLED == 1
tmav123 0:f54e9507171b 73 pc.baud(115200);
tmav123 0:f54e9507171b 74 EtherPinMonitor.attach(&EtherPinMonitorFunc, 0.01);
tmav123 0:f54e9507171b 75 printf("Setting up...\n");
tmav123 0:f54e9507171b 76 EthernetErr ethErr = eth.setup();
tmav123 0:f54e9507171b 77 if(ethErr)
tmav123 0:f54e9507171b 78 {
tmav123 0:f54e9507171b 79 printf("Error %d in setup.\n", ethErr);
tmav123 0:f54e9507171b 80 return -1;
tmav123 0:f54e9507171b 81 }
tmav123 0:f54e9507171b 82 printf("Setup OK\n");
tmav123 0:f54e9507171b 83 #endif
tmav123 0:f54e9507171b 84
tmav123 0:f54e9507171b 85 Timer tm;
tmav123 0:f54e9507171b 86 tm.start();
tmav123 0:f54e9507171b 87
tmav123 0:f54e9507171b 88 #if MB_RTU_ENABLED == 1
tmav123 0:f54e9507171b 89 eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE );
tmav123 0:f54e9507171b 90 #endif
tmav123 0:f54e9507171b 91 #if MB_ASCII_ENABLED == 1
tmav123 0:f54e9507171b 92 eStatus = eMBInit( MB_ASCII, SLAVE_ID, 0, 9600, MB_PAR_NONE );
tmav123 0:f54e9507171b 93 #endif
tmav123 0:f54e9507171b 94 #if MB_TCP_ENABLED == 1
tmav123 0:f54e9507171b 95 eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT );
tmav123 0:f54e9507171b 96 #endif
tmav123 0:f54e9507171b 97 if (eStatus != MB_ENOERR )
tmav123 0:f54e9507171b 98 printf( "can't initialize modbus stack!\r\n" );
tmav123 0:f54e9507171b 99
tmav123 0:f54e9507171b 100 /* Enable the Modbus Protocol Stack. */
tmav123 0:f54e9507171b 101 eStatus = eMBEnable( );
tmav123 0:f54e9507171b 102 if (eStatus != MB_ENOERR )
tmav123 0:f54e9507171b 103 fprintf( stderr, "can't enable modbus stack!\r\n" );
tmav123 0:f54e9507171b 104
tmav123 0:f54e9507171b 105 // Initialise some registers
tmav123 0:f54e9507171b 106 usRegInputBuf[1] = 0x1234;
tmav123 0:f54e9507171b 107 usRegInputBuf[2] = 0x5678;
tmav123 0:f54e9507171b 108 usRegInputBuf[3] = 0x9abc;
tmav123 0:f54e9507171b 109
tmav123 0:f54e9507171b 110 while(true)
tmav123 0:f54e9507171b 111 {
tmav123 0:f54e9507171b 112 #if MB_TCP_ENABLED == 1
tmav123 0:f54e9507171b 113 Net::poll();
tmav123 0:f54e9507171b 114 #endif
tmav123 0:f54e9507171b 115 if(tm.read()>.5)
tmav123 0:f54e9507171b 116 {
tmav123 0:f54e9507171b 117 led1=!led1; //Show that we are alive
tmav123 0:f54e9507171b 118 tm.start();
tmav123 0:f54e9507171b 119 }
tmav123 0:f54e9507171b 120
tmav123 0:f54e9507171b 121 eStatus = eMBPoll( );
tmav123 0:f54e9507171b 122
tmav123 0:f54e9507171b 123 /* Here we simply count the number of poll cycles. */
tmav123 0:f54e9507171b 124 usRegInputBuf[0]++;
tmav123 0:f54e9507171b 125 }
tmav123 0:f54e9507171b 126 return 0;
tmav123 0:f54e9507171b 127 }
tmav123 0:f54e9507171b 128
tmav123 0:f54e9507171b 129 eMBErrorCode
tmav123 0:f54e9507171b 130 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
tmav123 0:f54e9507171b 131 {
tmav123 0:f54e9507171b 132 eMBErrorCode eStatus = MB_ENOERR;
tmav123 0:f54e9507171b 133 int iRegIndex;
tmav123 0:f54e9507171b 134
tmav123 0:f54e9507171b 135 if( ( usAddress >= REG_INPUT_START )
tmav123 0:f54e9507171b 136 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
tmav123 0:f54e9507171b 137 {
tmav123 0:f54e9507171b 138 iRegIndex = ( int )( usAddress - usRegInputStart );
tmav123 0:f54e9507171b 139 while( usNRegs > 0 )
tmav123 0:f54e9507171b 140 {
tmav123 0:f54e9507171b 141 *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
tmav123 0:f54e9507171b 142 *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
tmav123 0:f54e9507171b 143 iRegIndex++;
tmav123 0:f54e9507171b 144 usNRegs--;
tmav123 0:f54e9507171b 145 }
tmav123 0:f54e9507171b 146 }
tmav123 0:f54e9507171b 147 else
tmav123 0:f54e9507171b 148 {
tmav123 0:f54e9507171b 149 eStatus = MB_ENOREG;
tmav123 0:f54e9507171b 150 }
tmav123 0:f54e9507171b 151 return eStatus;
tmav123 0:f54e9507171b 152 }
tmav123 0:f54e9507171b 153
tmav123 0:f54e9507171b 154 eMBErrorCode
tmav123 0:f54e9507171b 155 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
tmav123 0:f54e9507171b 156 {
tmav123 0:f54e9507171b 157 eMBErrorCode eStatus = MB_ENOERR;
tmav123 0:f54e9507171b 158 int iRegIndex;
tmav123 0:f54e9507171b 159
tmav123 0:f54e9507171b 160 if( ( usAddress >= REG_HOLDING_START ) &&
tmav123 0:f54e9507171b 161 ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
tmav123 0:f54e9507171b 162 {
tmav123 0:f54e9507171b 163 iRegIndex = ( int )( usAddress - usRegHoldingStart );
tmav123 0:f54e9507171b 164 switch ( eMode )
tmav123 0:f54e9507171b 165 {
tmav123 0:f54e9507171b 166 /* Pass current register values to the protocol stack. */
tmav123 0:f54e9507171b 167 case MB_REG_READ:
tmav123 0:f54e9507171b 168 while( usNRegs > 0 )
tmav123 0:f54e9507171b 169 {
tmav123 0:f54e9507171b 170 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
tmav123 0:f54e9507171b 171 *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
tmav123 0:f54e9507171b 172 iRegIndex++;
tmav123 0:f54e9507171b 173 usNRegs--;
tmav123 0:f54e9507171b 174 }
tmav123 0:f54e9507171b 175 break;
tmav123 0:f54e9507171b 176
tmav123 0:f54e9507171b 177 /* Update current register values with new values from the
tmav123 0:f54e9507171b 178 * protocol stack. */
tmav123 0:f54e9507171b 179 case MB_REG_WRITE:
tmav123 0:f54e9507171b 180 while( usNRegs > 0 )
tmav123 0:f54e9507171b 181 {
tmav123 0:f54e9507171b 182 usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
tmav123 0:f54e9507171b 183 usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
tmav123 0:f54e9507171b 184 iRegIndex++;
tmav123 0:f54e9507171b 185 usNRegs--;
tmav123 0:f54e9507171b 186 }
tmav123 0:f54e9507171b 187 }
tmav123 0:f54e9507171b 188 }
tmav123 0:f54e9507171b 189 else
tmav123 0:f54e9507171b 190 {
tmav123 0:f54e9507171b 191 eStatus = MB_ENOREG;
tmav123 0:f54e9507171b 192 }
tmav123 0:f54e9507171b 193 return eStatus;
tmav123 0:f54e9507171b 194 }
tmav123 0:f54e9507171b 195
tmav123 0:f54e9507171b 196 eMBErrorCode
tmav123 0:f54e9507171b 197 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
tmav123 0:f54e9507171b 198 {
tmav123 0:f54e9507171b 199 return MB_ENOREG;
tmav123 0:f54e9507171b 200 }
tmav123 0:f54e9507171b 201
tmav123 0:f54e9507171b 202 eMBErrorCode
tmav123 0:f54e9507171b 203 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
tmav123 0:f54e9507171b 204 {
tmav123 0:f54e9507171b 205 return MB_ENOREG;
tmav123 0:f54e9507171b 206 }
tmav123 0:f54e9507171b 207