Forking https://os.mbed.com/users/cam/code/Modbus/ to work for NUCLEO 64 boards
Fork of Cam's original FreeModbus port (https://os.mbed.com/users/cam/code/Modbus/)
Change: - Serial implementation to work for NUCLEO 64 boards and receive interrupts instead of timer. (see `portserial.cpp`)
Added: - Custom RTU mode. Allows for external implementation of packet receiving and sending. Sends and receives packets as whole frames (address + PDU) (i.e. this was added for a custom LoRa implementation). implement `xMBRTUCustGetPDU` and `xMBRTUCustSendResponse` (see `mbport.h`) and call `eMBRTUCustomInit( address )`. implementations need to be fully initialised as `eMBRTUCustomInit` only sets the address and nothing else.
mbfuncother.cpp@4:7621103c5a40, 2020-08-04 (annotated)
- Committer:
- danielmckinnell
- Date:
- Tue Aug 04 04:42:52 2020 +0000
- Revision:
- 4:7621103c5a40
- Parent:
- 0:0453a0a7e500
RTU Serial updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cam | 0:0453a0a7e500 | 1 | /* |
cam | 0:0453a0a7e500 | 2 | * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
cam | 0:0453a0a7e500 | 3 | * Copyright (c) 2006 Christian Walter <wolti@sil.at> |
cam | 0:0453a0a7e500 | 4 | * All rights reserved. |
cam | 0:0453a0a7e500 | 5 | * |
cam | 0:0453a0a7e500 | 6 | * Redistribution and use in source and binary forms, with or without |
cam | 0:0453a0a7e500 | 7 | * modification, are permitted provided that the following conditions |
cam | 0:0453a0a7e500 | 8 | * are met: |
cam | 0:0453a0a7e500 | 9 | * 1. Redistributions of source code must retain the above copyright |
cam | 0:0453a0a7e500 | 10 | * notice, this list of conditions and the following disclaimer. |
cam | 0:0453a0a7e500 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
cam | 0:0453a0a7e500 | 12 | * notice, this list of conditions and the following disclaimer in the |
cam | 0:0453a0a7e500 | 13 | * documentation and/or other materials provided with the distribution. |
cam | 0:0453a0a7e500 | 14 | * 3. The name of the author may not be used to endorse or promote products |
cam | 0:0453a0a7e500 | 15 | * derived from this software without specific prior written permission. |
cam | 0:0453a0a7e500 | 16 | * |
cam | 0:0453a0a7e500 | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
cam | 0:0453a0a7e500 | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
cam | 0:0453a0a7e500 | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
cam | 0:0453a0a7e500 | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
cam | 0:0453a0a7e500 | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
cam | 0:0453a0a7e500 | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
cam | 0:0453a0a7e500 | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
cam | 0:0453a0a7e500 | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
cam | 0:0453a0a7e500 | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
cam | 0:0453a0a7e500 | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cam | 0:0453a0a7e500 | 27 | * |
cam | 0:0453a0a7e500 | 28 | * File: $Id: mbfuncother.c,v 1.8 2006/12/07 22:10:34 wolti Exp $ |
cam | 0:0453a0a7e500 | 29 | */ |
cam | 0:0453a0a7e500 | 30 | |
cam | 0:0453a0a7e500 | 31 | /* ----------------------- System includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 32 | #include "stdlib.h" |
cam | 0:0453a0a7e500 | 33 | #include "string.h" |
cam | 0:0453a0a7e500 | 34 | |
cam | 0:0453a0a7e500 | 35 | /* ----------------------- Platform includes --------------------------------*/ |
cam | 0:0453a0a7e500 | 36 | #include "port.h" |
cam | 0:0453a0a7e500 | 37 | |
cam | 0:0453a0a7e500 | 38 | /* ----------------------- Modbus includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 39 | #include "mb.h" |
cam | 0:0453a0a7e500 | 40 | #include "mbframe.h" |
cam | 0:0453a0a7e500 | 41 | #include "mbproto.h" |
cam | 0:0453a0a7e500 | 42 | #include "mbconfig.h" |
cam | 0:0453a0a7e500 | 43 | |
cam | 0:0453a0a7e500 | 44 | #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0 |
cam | 0:0453a0a7e500 | 45 | |
cam | 0:0453a0a7e500 | 46 | /* ----------------------- Static variables ---------------------------------*/ |
cam | 0:0453a0a7e500 | 47 | static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF]; |
cam | 0:0453a0a7e500 | 48 | static USHORT usMBSlaveIDLen; |
cam | 0:0453a0a7e500 | 49 | |
cam | 0:0453a0a7e500 | 50 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 51 | |
cam | 0:0453a0a7e500 | 52 | eMBErrorCode |
cam | 0:0453a0a7e500 | 53 | eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning, |
cam | 0:0453a0a7e500 | 54 | UCHAR const *pucAdditional, USHORT usAdditionalLen ) |
cam | 0:0453a0a7e500 | 55 | { |
cam | 0:0453a0a7e500 | 56 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 57 | |
cam | 0:0453a0a7e500 | 58 | /* the first byte and second byte in the buffer is reserved for |
cam | 0:0453a0a7e500 | 59 | * the parameter ucSlaveID and the running flag. The rest of |
cam | 0:0453a0a7e500 | 60 | * the buffer is available for additional data. */ |
cam | 0:0453a0a7e500 | 61 | if( usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF ) |
cam | 0:0453a0a7e500 | 62 | { |
cam | 0:0453a0a7e500 | 63 | usMBSlaveIDLen = 0; |
cam | 0:0453a0a7e500 | 64 | ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID; |
cam | 0:0453a0a7e500 | 65 | ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 ); |
cam | 0:0453a0a7e500 | 66 | if( usAdditionalLen > 0 ) |
cam | 0:0453a0a7e500 | 67 | { |
cam | 0:0453a0a7e500 | 68 | memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional, |
cam | 0:0453a0a7e500 | 69 | ( size_t )usAdditionalLen ); |
cam | 0:0453a0a7e500 | 70 | usMBSlaveIDLen += usAdditionalLen; |
cam | 0:0453a0a7e500 | 71 | } |
cam | 0:0453a0a7e500 | 72 | } |
cam | 0:0453a0a7e500 | 73 | else |
cam | 0:0453a0a7e500 | 74 | { |
cam | 0:0453a0a7e500 | 75 | eStatus = MB_ENORES; |
cam | 0:0453a0a7e500 | 76 | } |
cam | 0:0453a0a7e500 | 77 | return eStatus; |
cam | 0:0453a0a7e500 | 78 | } |
cam | 0:0453a0a7e500 | 79 | |
cam | 0:0453a0a7e500 | 80 | eMBException |
cam | 0:0453a0a7e500 | 81 | eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen ) |
cam | 0:0453a0a7e500 | 82 | { |
cam | 0:0453a0a7e500 | 83 | memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen ); |
cam | 0:0453a0a7e500 | 84 | *usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen ); |
cam | 0:0453a0a7e500 | 85 | return MB_EX_NONE; |
cam | 0:0453a0a7e500 | 86 | } |
cam | 0:0453a0a7e500 | 87 | |
cam | 0:0453a0a7e500 | 88 | #endif |