A bit of reformatting and set the ADUlen in the default case

This commit is contained in:
Douglas Clowes
2014-09-04 13:40:02 +10:00
parent 378170280f
commit 830ed5a45c

View File

@@ -17,7 +17,7 @@
* data * data
* ETX * ETX
* BCC * BCC
*/ */
#define PROTOCOL_NAME "MODBUS_AP" #define PROTOCOL_NAME "MODBUS_AP"
#define PROTOCOL_INIT MODBUSInitProtocol #define PROTOCOL_INIT MODBUSInitProtocol
#define ADUSIZE 17 #define ADUSIZE 17
@@ -44,7 +44,17 @@ struct cmdPar {
double MBdata[2000]; double MBdata[2000];
}; };
enum MBFCODES { RdCoil=1, RdInp=2, RdHldReg=3, RdInpReg=4, WrCoil=5, WrReg=6, WrMCoils=15, WrMRegs=16 }; enum MBFCODES {
RdCoil=1, /* Read Coil - single output bit */
RdInp=2, /* Read Input - single input bit */
RdHldReg=3, /* Read Holding Register */
RdInpReg=4, /* Read Input Register */
WrCoil=5, /* Write Coil - force output bit */
WrReg=6, /* Write Register */
WrLoop=8, /* Write Loopback */
WrMCoils=15, /* Write Multiple Coils */
WrMRegs=16 /* Write Multiple Registers */
};
typedef struct modbus_private_t Private, *pPrivate; typedef struct modbus_private_t Private, *pPrivate;
struct modbus_private_t { struct modbus_private_t {
@@ -143,8 +153,8 @@ static double ieee2double(unsigned char ieee[4]) {
int exponent, bias = 127; int exponent, bias = 127;
mantissa = ((ieee[1] << 16) & 0x7F0000) mantissa = ((ieee[1] << 16) & 0x7F0000)
| ((ieee[2] << 8) & 0xFF00) | ((ieee[2] << 8) & 0xFF00)
| ((ieee[3] ) & 0xFF); | ((ieee[3] ) & 0xFF);
exponent = (ieee[0] & 0x7F) * 2 + ((ieee[1] >> 7) & 1); /* raw exponent */ exponent = (ieee[0] & 0x7F) * 2 + ((ieee[1] >> 7) & 1); /* raw exponent */
if (exponent == 255) { if (exponent == 255) {
@@ -171,8 +181,8 @@ static double ieee2double(unsigned char ieee[4]) {
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/** @brief Parse a command string of the form "MBAddr:Fcode:SAddr:Dtype:MBdata" /** @brief Parse a command string of the form "MBAddr:Fcode:SAddr:Dtype:MBdata"
Dtype U16, U32, F32 with optional byte order array eg U32[3,2,1,0] Dtype U16, U32, F32 with optional byte order array eg U32[3,2,1,0]
*/ */
static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) { static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) {
int len; int len;
unsigned int i; unsigned int i;
@@ -213,11 +223,11 @@ static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) {
} }
switch (MBC->Fcode) { switch (MBC->Fcode) {
case RdCoil: case RdCoil:
/* Coils are numbered from 1 but addressed from 0 */ /* Coils are numbered from 1 but addressed from 0 */
MBC->SAddr--; MBC->SAddr--;
MBC->NumBytes = MBC->NumVal; MBC->NumBytes = MBC->NumVal;
MBC->NumRd = MBC->NumVal; MBC->NumRd = MBC->NumVal;
break; break;
case WrCoil: case WrCoil:
MBC->SAddr--; MBC->SAddr--;
dVal = strtod(cmdLine, &nextPar); dVal = strtod(cmdLine, &nextPar);
@@ -228,7 +238,7 @@ static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) {
} else { } else {
/* TODO ERROR */ /* TODO ERROR */
} }
break; break;
case RdHldReg: case RdHldReg:
if (MBC->Dtype == F32 || MBC->Dtype == U32) { if (MBC->Dtype == F32 || MBC->Dtype == U32) {
MBC->NumRd = MBC->NumVal * 2; MBC->NumRd = MBC->NumVal * 2;
@@ -237,7 +247,7 @@ static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) {
} else { } else {
/* TODO ERROR */ /* TODO ERROR */
} }
break; break;
case WrMRegs: case WrMRegs:
for (i=0; i < MBC->NumVal; i++) { for (i=0; i < MBC->NumVal; i++) {
MBC->MBdata[i] = strtod(cmdLine, &nextPar); MBC->MBdata[i] = strtod(cmdLine, &nextPar);
@@ -250,7 +260,7 @@ static void parseCmd(char *cmdLine, struct cmdPar *MBC, int *BO) {
MBC->NumWr = MBC->NumVal; MBC->NumWr = MBC->NumVal;
MBC->NumBytes = MBC->NumVal * 2; MBC->NumBytes = MBC->NumVal * 2;
} }
break; break;
} }
} }
@@ -274,20 +284,20 @@ static int ModbusOutput(pPrivate myPriv, pDynString wrBuffer, pDynString send_bu
switch (myPriv->MBcmd.Fcode) { switch (myPriv->MBcmd.Fcode) {
case RdCoil: case RdCoil:
case RdHldReg: case RdHldReg:
/* TODO Max NumRd = 125 */ /* TODO Max NumRd = 125 */
ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8; ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8;
ADU[9] = myPriv->MBcmd.SAddr & 0xFF; ADU[9] = myPriv->MBcmd.SAddr & 0xFF;
ADU[10] = (myPriv->MBcmd.NumRd & 0xFF00) >> 8; ADU[10] = (myPriv->MBcmd.NumRd & 0xFF00) >> 8;
ADU[11] = myPriv->MBcmd.NumRd & 0xFF; ADU[11] = myPriv->MBcmd.NumRd & 0xFF;
ADUlen = 12; ADUlen = 12;
break; break;
case WrCoil: case WrCoil:
ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8; ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8;
ADU[9] = myPriv->MBcmd.SAddr & 0xFF; ADU[9] = myPriv->MBcmd.SAddr & 0xFF;
ADU[10] = myPriv->MBcmd.MBdata[0]; ADU[10] = myPriv->MBcmd.MBdata[0];
ADU[11] = 0; ADU[11] = 0;
ADUlen = 12; ADUlen = 12;
break; break;
case WrMRegs: case WrMRegs:
ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8; ADU[8] = (myPriv->MBcmd.SAddr & 0xFF00) >> 8;
ADU[9] = myPriv->MBcmd.SAddr & 0xFF; ADU[9] = myPriv->MBcmd.SAddr & 0xFF;
@@ -300,7 +310,7 @@ static int ModbusOutput(pPrivate myPriv, pDynString wrBuffer, pDynString send_bu
ADU[j + myPriv->BO[0]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF00) >> 8; ADU[j + myPriv->BO[0]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF00) >> 8;
ADU[j + myPriv->BO[1]] = (unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF; ADU[j + myPriv->BO[1]] = (unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF;
} }
break; break;
case U32: case U32:
for (i=0, j=13; i < myPriv->MBcmd.NumVal; i++, j+=4) { for (i=0, j=13; i < myPriv->MBcmd.NumVal; i++, j+=4) {
ADU[j + myPriv->BO[0]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF000000) >> 24; ADU[j + myPriv->BO[0]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF000000) >> 24;
@@ -308,7 +318,7 @@ static int ModbusOutput(pPrivate myPriv, pDynString wrBuffer, pDynString send_bu
ADU[j + myPriv->BO[2]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF00) >> 8; ADU[j + myPriv->BO[2]] = ((unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF00) >> 8;
ADU[j + myPriv->BO[3]] = (unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF; ADU[j + myPriv->BO[3]] = (unsigned int)myPriv->MBcmd.MBdata[i] & 0xFF;
} }
break; break;
case F32: case F32:
for (i=0, j=13; i < myPriv->MBcmd.NumVal; i++, j+=4) { for (i=0, j=13; i < myPriv->MBcmd.NumVal; i++, j+=4) {
double2ieee(myPriv->MBcmd.MBdata[i], ieee); double2ieee(myPriv->MBcmd.MBdata[i], ieee);
@@ -317,13 +327,14 @@ static int ModbusOutput(pPrivate myPriv, pDynString wrBuffer, pDynString send_bu
ADU[j + myPriv->BO[2]] = ieee[2]; ADU[j + myPriv->BO[2]] = ieee[2];
ADU[j + myPriv->BO[3]] = ieee[3]; ADU[j + myPriv->BO[3]] = ieee[3];
} }
break; break;
} }
ADUlen = 13 + myPriv->MBcmd.NumBytes; ADUlen = 13 + myPriv->MBcmd.NumBytes;
break; break;
default: default:
// Not Implemented // Not Implemented
break; ADUlen = 7;
break;
} }
/* Length field in MBAP includes byte 7 of the MBAP, so Length = ADU length - 6. */ /* Length field in MBAP includes byte 7 of the MBAP, so Length = ADU length - 6. */
PDUlenPlusUID = ADUlen - 6; PDUlenPlusUID = ADUlen - 6;