Programme Nucleo RS485 - Modbus TCP/IP (ACP 40)

Fork of Modbus by Cam Marshall

Committer:
TomTom83
Date:
Fri Jul 06 11:45:57 2018 +0000
Revision:
2:b6ae32d99b4a
Parent:
1:390d9808cdde
Programme RS485 - Modbus TCP/IP (ACP40)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TomTom83 1:390d9808cdde 1 /*
TomTom83 1:390d9808cdde 2 * FreeModbus Libary: lwIP Port
TomTom83 1:390d9808cdde 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
TomTom83 1:390d9808cdde 4 *
TomTom83 1:390d9808cdde 5 * This library is free software; you can redistribute it and/or
TomTom83 1:390d9808cdde 6 * modify it under the terms of the GNU Lesser General Public
TomTom83 1:390d9808cdde 7 * License as published by the Free Software Foundation; either
TomTom83 1:390d9808cdde 8 * version 2.1 of the License, or (at your option) any later version.
TomTom83 1:390d9808cdde 9 *
TomTom83 1:390d9808cdde 10 * This library is distributed in the hope that it will be useful,
TomTom83 1:390d9808cdde 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
TomTom83 1:390d9808cdde 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
TomTom83 1:390d9808cdde 13 * Lesser General Public License for more details.
TomTom83 1:390d9808cdde 14 *
TomTom83 1:390d9808cdde 15 * You should have received a copy of the GNU Lesser General Public
TomTom83 1:390d9808cdde 16 * License along with this library; if not, write to the Free Software
TomTom83 1:390d9808cdde 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
TomTom83 1:390d9808cdde 18 *
TomTom83 1:390d9808cdde 19 * File: $Id: portother.c,v 1.2 2006/09/04 14:39:20 wolti Exp $
TomTom83 1:390d9808cdde 20 */
TomTom83 1:390d9808cdde 21
TomTom83 1:390d9808cdde 22 /* ----------------------- System includes ----------------------------------*/
TomTom83 1:390d9808cdde 23 #include <stdio.h>
TomTom83 1:390d9808cdde 24 #include <stdarg.h>
TomTom83 1:390d9808cdde 25 #include <string.h>
TomTom83 1:390d9808cdde 26
TomTom83 1:390d9808cdde 27 #include "port.h"
TomTom83 1:390d9808cdde 28
TomTom83 1:390d9808cdde 29 /* ----------------------- Defines ------------------------------------------*/
TomTom83 1:390d9808cdde 30 #define MB_FRAME_LOG_BUFSIZE 512
TomTom83 1:390d9808cdde 31
TomTom83 1:390d9808cdde 32 /* ----------------------- Start implementation -----------------------------*/
TomTom83 1:390d9808cdde 33
TomTom83 1:390d9808cdde 34 #ifdef MB_TCP_DEBUG
TomTom83 1:390d9808cdde 35 void
TomTom83 1:390d9808cdde 36 prvvMBTCPLogFrame( UCHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen )
TomTom83 1:390d9808cdde 37 {
TomTom83 1:390d9808cdde 38 int i;
TomTom83 1:390d9808cdde 39 int res = 0;
TomTom83 1:390d9808cdde 40 int iBufPos = 0;
TomTom83 1:390d9808cdde 41 size_t iBufLeft = MB_FRAME_LOG_BUFSIZE;
TomTom83 1:390d9808cdde 42 static CHAR arcBuffer[MB_FRAME_LOG_BUFSIZE];
TomTom83 1:390d9808cdde 43
TomTom83 1:390d9808cdde 44 assert( pucFrame != NULL );
TomTom83 1:390d9808cdde 45
TomTom83 1:390d9808cdde 46 for( i = 0; i < usFrameLen; i++ )
TomTom83 1:390d9808cdde 47 {
TomTom83 1:390d9808cdde 48 /* Print some additional frame information. */
TomTom83 1:390d9808cdde 49 switch ( i )
TomTom83 1:390d9808cdde 50 {
TomTom83 1:390d9808cdde 51 case 0:
TomTom83 1:390d9808cdde 52 /* TID = Transaction Identifier. */
TomTom83 1:390d9808cdde 53 res = snprintf( &arcBuffer[iBufPos], iBufLeft, "| TID = " );
TomTom83 1:390d9808cdde 54 break;
TomTom83 1:390d9808cdde 55 case 2:
TomTom83 1:390d9808cdde 56 /* PID = Protocol Identifier. */
TomTom83 1:390d9808cdde 57 res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | PID = " );
TomTom83 1:390d9808cdde 58 break;
TomTom83 1:390d9808cdde 59 case 4:
TomTom83 1:390d9808cdde 60 /* Length */
TomTom83 1:390d9808cdde 61 res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | LEN = " );
TomTom83 1:390d9808cdde 62 break;
TomTom83 1:390d9808cdde 63 case 6:
TomTom83 1:390d9808cdde 64 /* UID = Unit Identifier. */
TomTom83 1:390d9808cdde 65 res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | UID = " );
TomTom83 1:390d9808cdde 66 break;
TomTom83 1:390d9808cdde 67 case 7:
TomTom83 1:390d9808cdde 68 /* MB Function Code. */
TomTom83 1:390d9808cdde 69 res = snprintf( &arcBuffer[iBufPos], iBufLeft, "|| FUNC = " );
TomTom83 1:390d9808cdde 70 break;
TomTom83 1:390d9808cdde 71 case 8:
TomTom83 1:390d9808cdde 72 /* MB PDU rest. */
TomTom83 1:390d9808cdde 73 res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | DATA = " );
TomTom83 1:390d9808cdde 74 break;
TomTom83 1:390d9808cdde 75 default:
TomTom83 1:390d9808cdde 76 res = 0;
TomTom83 1:390d9808cdde 77 break;
TomTom83 1:390d9808cdde 78 }
TomTom83 1:390d9808cdde 79 if( res == -1 )
TomTom83 1:390d9808cdde 80 {
TomTom83 1:390d9808cdde 81 break;
TomTom83 1:390d9808cdde 82 }
TomTom83 1:390d9808cdde 83 else
TomTom83 1:390d9808cdde 84 {
TomTom83 1:390d9808cdde 85 iBufPos += res;
TomTom83 1:390d9808cdde 86 iBufLeft -= res;
TomTom83 1:390d9808cdde 87 }
TomTom83 1:390d9808cdde 88
TomTom83 1:390d9808cdde 89 /* Print the data. */
TomTom83 1:390d9808cdde 90 res = snprintf( &arcBuffer[iBufPos], iBufLeft, "%02X", pucFrame[i] );
TomTom83 1:390d9808cdde 91 if( res == -1 )
TomTom83 1:390d9808cdde 92 {
TomTom83 1:390d9808cdde 93 break;
TomTom83 1:390d9808cdde 94 }
TomTom83 1:390d9808cdde 95 else
TomTom83 1:390d9808cdde 96 {
TomTom83 1:390d9808cdde 97 iBufPos += res;
TomTom83 1:390d9808cdde 98 iBufLeft -= res;
TomTom83 1:390d9808cdde 99 }
TomTom83 1:390d9808cdde 100 }
TomTom83 1:390d9808cdde 101
TomTom83 1:390d9808cdde 102 if( res != -1 )
TomTom83 1:390d9808cdde 103 {
TomTom83 1:390d9808cdde 104 /* Append an end of frame string. */
TomTom83 1:390d9808cdde 105 res = snprintf( &arcBuffer[iBufPos], iBufLeft, " |\r\n" );
TomTom83 1:390d9808cdde 106 if( res != -1 )
TomTom83 1:390d9808cdde 107 {
TomTom83 1:390d9808cdde 108 vMBPortLog( MB_LOG_DEBUG, (const char*)pucMsg, "%s", arcBuffer );
TomTom83 1:390d9808cdde 109 }
TomTom83 1:390d9808cdde 110 }
TomTom83 1:390d9808cdde 111 }
TomTom83 1:390d9808cdde 112 #endif
TomTom83 1:390d9808cdde 113
TomTom83 1:390d9808cdde 114 void
TomTom83 1:390d9808cdde 115 vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... )
TomTom83 1:390d9808cdde 116 {
TomTom83 1:390d9808cdde 117 va_list args;
TomTom83 1:390d9808cdde 118 static const char *arszLevel2Str[] = { "DEBUG", "INFO", "WARN", "ERROR" };
TomTom83 1:390d9808cdde 119
TomTom83 1:390d9808cdde 120 ( void )printf( "%s: %s: ", arszLevel2Str[eLevel], szModule );
TomTom83 1:390d9808cdde 121 va_start( args, szFmt );
TomTom83 1:390d9808cdde 122 vprintf( szFmt, args );
TomTom83 1:390d9808cdde 123 va_end( args );
TomTom83 1:390d9808cdde 124 }
TomTom83 1:390d9808cdde 125