From 012cb58cd5503fe563fd3fe1a38f3c8851c8bde6 Mon Sep 17 00:00:00 2001 From: Max Brueggemann Date: Sat, 3 Dec 2016 17:14:21 +0100 Subject: [PATCH] Cleanup and additional comments --- yaMBSiavr.c | 189 +++++++++++++++++++++++++++++++--------------------- yaMBSiavr.h | 2 +- 2 files changed, 115 insertions(+), 76 deletions(-) diff --git a/yaMBSiavr.c b/yaMBSiavr.c index 517bb67..adc74f2 100644 --- a/yaMBSiavr.c +++ b/yaMBSiavr.c @@ -36,36 +36,43 @@ volatile uint16_t DataPos = 0; volatile unsigned char PacketTopIndex = 7; volatile unsigned char modBusStaMaStates = 0; -uint8_t modbusGetBusState(void) { +uint8_t modbusGetBusState(void) +{ return BusState; } #if ADDRESS_MODE == SINGLE_ADR volatile unsigned char Address = 0x00; -uint8_t modbusGetAddress(void) { +uint8_t modbusGetAddress(void) +{ return Address; } -void modbusSetAddress(unsigned char newadr) { +void modbusSetAddress(unsigned char newadr) +{ Address = newadr; } #endif #if PHYSICAL_TYPE == 485 - void transceiver_txen(void) { +void transceiver_txen(void) +{ TRANSCEIVER_ENABLE_PORT|=(1< perform crc check - //BusState=(1<MaxFrameIndex) modbusReset(); - else { + else + { rxbuffer[DataPos]=data; DataPos++; //TODO: maybe prevent this from exceeding 255? } - } else if (!(BusState & (1<> 8); - *(outreg+1+c*2) = (uint8_t)(*(inreg+c)); - } +void intToModbusRegister(volatile uint16_t *inreg, volatile uint8_t *outreg, uint8_t amount) +{ + for (uint8_t c=0; c> 8); + *(outreg+1+c*2) = (uint8_t)(*(inreg+c)); + } } -/* @brief: copies a single or multiple 16-bit-words from one array of integers to an array of bytes -* -* +/* @brief: copies a single or multiple 16-bit-words from one array of integers to an array of bytes +* */ -void modbusRegisterToInt(volatile uint8_t *inreg, volatile uint16_t *outreg, uint8_t amount) { - for (uint8_t c=0; c=startAddress) && ((startAddress+size)>=(requestedAmount+requestedAdr))) { + if ((requestedAdr>=startAddress) && ((startAddress+size)>=(requestedAmount+requestedAdr))) + { if ((rxbuffer[1]==fcReadInputStatus) || (rxbuffer[1]==fcReadCoilStatus)) { if (requestedAmount<=((MaxFrameIndex-4)*8)) //message buffer big enough? @@ -340,7 +378,8 @@ uint8_t modbusExchangeBits(volatile uint8_t *ptrToInArray, uint16_t startAddress } //modbusSendException(ecSlaveDeviceFailure); //inanpropriate call of modbusExchangeBits return 0; - } else { + } else + { modbusSendException(ecIllegalDataValue); return 0; } diff --git a/yaMBSiavr.h b/yaMBSiavr.h index e1963cd..32622c7 100644 --- a/yaMBSiavr.h +++ b/yaMBSiavr.h @@ -286,7 +286,7 @@ extern uint16_t modbusRequestedAddress(void); * Returns 1 if the crc check is positive, returns 0 if it fails. * Appends two crc bytes to the array. */ -extern uint16_t crc16(volatile uint8_t *ptrToArray,uint8_t inputSize); +extern uint8_t crc16(volatile uint8_t *ptrToArray,uint8_t inputSize); /* @brief: Handles single/multiple input/coil reading and single/multiple coil writing. *