PROGRAM PLC_PRG VAR CONSTANT END_VAR VAR MasterMB: Modbus; bAddr: BYTE := 1; pwH, pwL: POINTER TO WORD; xLoggerInit: BOOL := TRUE; FbDatalogger1: FbDatalogger; xActivate: BOOL; aValues: ARRAY [1..MAX_CHANNELS] OF REAL; xEvent, xPad: BOOL; typConfigParameters: typConfigDataLogger; Timeout: WORD; n: WORD; FFT_Segm: BYTE := 0; END_VAR VAR_INPUT sSerNoIn: STRING; sCalValIn: STRING := '10000'; sTempIn: STRING := '20'; sDevNameIn: STRING; wAddrIn: WORD; END_VAR CASE GVL.bStep OF //read serial nos. of all M14 units 5: GVL.Init := TRUE; //set init. flag MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0030; //register MasterMB.utQuery.uiReadQuantity := 16#0002; MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error GVL.adwSerNo[bAddr] := (SHL(WORD_TO_DWORD(MasterMB.utResponse.awData[0]), 16) OR MasterMB.utResponse.awData[1]) AND 16#000FFFFF; ELSE //at error GVL.adwSerNo[bAddr] := 0; FOR n := 0 TO 499 BY 1 DO GVL.arFFT[n, bAddr] := 0; //delete FFT buffer END_FOR END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //read device names of all M14 units 6: MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0080; //register MasterMB.utQuery.uiReadQuantity := 16#000A; //20 characters MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error FOR n := 0 TO 9 BY 1 DO GVL.asDevName[bAddr][2 * n] := WORD_TO_BYTE(SHR(MasterMB.utResponse.awData[n], 8)); GVL.asDevName[bAddr][2 * n + 1] := WORD_TO_BYTE(MasterMB.utResponse.awData[n]); END_FOR ELSE //at error GVL.asDevName[bAddr] := ''; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //read modes of all M14 units 7: MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0023; //register MasterMB.utQuery.uiReadQuantity := 16#0001; MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error GVL.awMode[bAddr] := MasterMB.utResponse.awData[0]; ELSE //at error GVL.awMode[bAddr] := 2; //no mode displayed END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //read filters/integrators of all M14 units 8: MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0022; //register MasterMB.utQuery.uiReadQuantity := 16#0001; //1 word MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE AND GVL.awMode[bAddr] = 0 THEN //if no error and RMS/peak mode active GVL.awHP[bAddr] := SHR(MasterMB.utResponse.awData[0], 8); GVL.awLP[bAddr] := MasterMB.utResponse.awData[0] AND 16#00FF; GVL.asFilter[bAddr] := GVL.asHP[GVL.awHP[bAddr]]; //high pass GVL.asFilter[bAddr] := CONCAT(GVL.asFilter[bAddr], ' Hz - '); IF GVL.awHP[bAddr] > 8 THEN //for velocity GVL.asFilter[bAddr] := CONCAT(GVL.asFilter[bAddr], GVL.asLP[3]); //low pass fixed 1000 Hz GVL.awIntegr[bAddr] := 1; //unit mm/s ELSE //for acceleration GVL.asFilter[bAddr] := CONCAT(GVL.asFilter[bAddr], GVL.asLP[GVL.awLP[bAddr]]); //low pass GVL.awIntegr[bAddr] := 0; //unit m/s² END_IF GVL.asFilter[bAddr] := CONCAT(GVL.asFilter[bAddr], ' Hz'); ELSE //at error and in FFT mode GVL.asFilter[bAddr] := ''; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //read gain of all M14 units 9: MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0025; //gain MasterMB.utQuery.uiReadQuantity := 16#0001; MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error GVL.awGain[bAddr] := MasterMB.utResponse.awData[0]; ELSE //at error GVL.awGain[bAddr] := 4; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //read cal. dates of all M14 units 10: MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0041; //register MasterMB.utQuery.uiReadQuantity := 16#0002; MasterMB(); IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error GVL.asCalDate[bAddr] := WORD_TO_STRING(MasterMB.utResponse.awData[0]); //month (1 .. 12) GVL.asCalDate[bAddr] := CONCAT(GVL.asCalDate[bAddr], ' / '); GVL.asCalDate[bAddr] := CONCAT(GVL.asCalDate[bAddr], WORD_TO_STRING(MasterMB.utResponse.awData[1])); //year - 2000 ELSE //at error GVL.asCalDate[bAddr] := ''; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next sensor ELSE //after last sensor bAddr := 1; GVL.bStep := GVL.bStep + 1; //next step END_IF END_IF //frequency axis for FFT diagram 11: FOR n := 0 TO 499 BY 1 DO //save 50 FFT points GVL.arFFT[n, 0] := n * 2.8610; //frequency axis for 1.4 kHz FFT END_FOR GVL.bStep := 20; //next step: measurement //*************************** // RMS/peak/FFT measurement //*************************** 20: IF GVL.bMeasure = 1 AND GVL.awMode[bAddr] = 1 THEN //if measurement enabled and M14 in FFT mode (1.4 kHz only) //FFT MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0010 + FFT_Segm; //segment no. (0 to 9) MasterMB.utQuery.uiReadQuantity := 16#0064; //50 FFT points per segment (float, 4 bytes each) MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error FOR n := 0 TO 49 BY 1 DO //save 50 FFT points pwH := ADR(GVL.arFFT[n + (50 * FFT_Segm), bAddr]); //convert FFT amplitude from 2 WORD pwL := ADR(GVL.arFFT[n + (50 * FFT_Segm), bAddr]) + SIZEOF(WORD); pwH^ := MasterMB.utResponse.awData[2 * n + 1]; //into REAL pwL^ := MasterMB.utResponse.awData[2 * n]; END_FOR FFT_Segm := FFT_Segm + 1; //next segment of 50 points END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF FFT_Segm = 10 OR MasterMB.oStatus.xError THEN //after last segment or error FFT_Segm := 0; IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next M14 ELSE bAddr := 1; //first M14 END_IF END_IF IF GVL.axError[bAddr] THEN GVL.adwErrorCount[bAddr] := GVL.adwErrorCount[bAddr] + 1; //error counter END_IF END_IF ELSIF GVL.bMeasure = 1 AND GVL.awMode[bAddr] = 0 THEN //if measurement enabled and in RMS/peak mode //RMS/peak MasterMB.utQuery.bUnitId := bAddr; MasterMB.utQuery.bFunctionCode := 16#0003; //read MasterMB.utQuery.uiReadAddress := 16#0001; MasterMB.utQuery.uiReadQuantity := 16#0004; //RMS (float), peak (float) (Word) MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError = FALSE THEN //if no error pwH := ADR(GVL.arRMS[bAddr]); //convert RMS from 2 WORD pwL := ADR(GVL.arRMS[bAddr]) + SIZEOF(WORD); pwH^ := MasterMB.utResponse.awData[1]; //into REAL pwL^ := MasterMB.utResponse.awData[0]; pwH := ADR(GVL.arPeak[bAddr]); //convert Peak from 2 WORD pwL := ADR(GVL.arPeak[bAddr]) + SIZEOF(WORD); pwH^ := MasterMB.utResponse.awData[3]; //into REAL pwL^ := MasterMB.utResponse.awData[2]; ELSE //if error GVL.arRMS[bAddr] := 0; GVL.arPeak[bAddr] := 0; GVL.adwErrorCount[bAddr] := GVL.adwErrorCount[bAddr] + 1; //error counter END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit aValues[bAddr] := GVL.arRMS[bAddr]; aValues[bAddr + 5] := GVL.arPeak[bAddr]; xActivate := TRUE; IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF GVL.axError[bAddr] := MasterMB.oStatus.xError; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next M14 ELSE bAddr := 1; //first M14 END_IF END_IF ELSE//if no M14 detected IF GVL.bMeasure = 1 THEN GVL.asStatus[bAddr] := 'M14 not found'; //connection status text ELSE GVL.asStatus[bAddr] := 'Stopped'; //connection status text END_IF GVL.axError[bAddr] := TRUE; //connection error bit IF bAddr < GVL.LastAddr THEN bAddr := bAddr + 1; //next M14 ELSE bAddr := 1; //first M14 END_IF END_IF //flags set by buttons IF GVL.xSetup = 1 THEN GVL.xSetup := 0; //reset flag bAddr := GVL.bInitChannel; GVL.xErr := FALSE; GVL.bStep := 35; //next step: set M14 baudrate ELSIF GVL.xSetup = 2 THEN GVL.xSetup := 0; //reset flag bAddr := GVL.bInitChannel; GVL.xErr := FALSE; GVL.bStep := 40; //next step: set M14 gain ELSIF GVL.xSetup = 3 THEN GVL.xSetup := 0; //reset flag bAddr := GVL.bInitChannel; GVL.xErr := FALSE; GVL.bStep := 41; //next step: set M14 filters ELSIF GVL.xSetup = 4 THEN GVL.xSetup := 0; //reset flag bAddr := GVL.bInitChannel; GVL.xErr := FALSE; GVL.bStep := 42; //next step: save M14 device name ELSIF GVL.xSetup = 5 THEN GVL.xSetup := 0; //reset flag bAddr := GVL.bInitChannel; GVL.xErr := FALSE; GVL.bStep := 43; //next step: save M14 mode ELSIF GVL.xReload THEN GVL.xReload := FALSE; //reset flag bAddr := 1; //first sensor GVL.bStep := 5; //next step: read settings ELSIF GVL.xAddrChange THEN GVL.xAddrChange := FALSE; //reset flag GVL.bStep := 30; //next step: change address END_IF GVL.Init := FALSE; //reset init. flag //write address of M14 with particular serial no. 30: MasterMB.utQuery.bUnitId := 0; //broadcast address MasterMB.utQuery.bFunctionCode := 16#10; //write MasterMB.utQuery.uiWriteAddress := 16#0030; //register MasterMB.utQuery.uiWriteQuantity := 16#0002; MasterMB.utQuery.awWriteData[0] := DWORD_TO_WORD(SHR(STRING_TO_DWORD(PLC_PRG.sSerNoIn), 16)) OR SHL(PLC_PRG.wAddrIn, 8); MasterMB.utQuery.awWriteData[1] := STRING_TO_WORD(PLC_PRG.sSerNoIn); MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent bAddr := 1; GVL.bStep := 5; //read settings of all M14 units END_IF //write baudrate of addressed M14 35: MasterMB.utQuery.bUnitId := bAddr; //address MasterMB.utQuery.bFunctionCode := 16#06; //write MasterMB.utQuery.uiWriteAddress := 16#0032; //register MasterMB.utQuery.uiWriteQuantity := 16#0001; //1 word MasterMB.utQuery.awWriteData[0] := GVL.wBaudrateM14; //baudrate index MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError THEN //if error GVL.xErr := TRUE; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF bAddr := 1; //first sensor GVL.bStep := 5; //read settings of all M14 units END_IF //write gain of addressed M14 40: MasterMB.utQuery.bUnitId := bAddr; //address MasterMB.utQuery.bFunctionCode := 16#06; //write MasterMB.utQuery.uiWriteAddress := 16#0025; //register MasterMB.utQuery.uiWriteQuantity := 16#0001; //1 word MasterMB.utQuery.awWriteData[0] := GVL.wGain; //gain index MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError THEN //if error GVL.xErr := TRUE; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF bAddr := 1; //first sensor GVL.bStep := 5; //read settings of all M14 units END_IF //write filters/integrators of addressed M14 41: MasterMB.utQuery.bUnitId := bAddr; //address MasterMB.utQuery.bFunctionCode := 16#06; //write MasterMB.utQuery.uiWriteAddress := 16#0022; //register MasterMB.utQuery.uiWriteQuantity := 16#0001; //1 word MasterMB.utQuery.awWriteData[0] := SHL(GVL.wHP, 8) + GVL.wLP; //high pass / low pass index MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError THEN //if error GVL.xErr := TRUE; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF bAddr := 1; //first sensor GVL.bStep := 5; //read settings of all M14 units END_IF //write device name of addressed M14 42: MasterMB.utQuery.bUnitId := bAddr; //address MasterMB.utQuery.bFunctionCode := 16#10; //write MasterMB.utQuery.uiWriteAddress := 16#0080; //register MasterMB.utQuery.uiWriteQuantity := 16#000A; //10 words / 20 charcters xPad := FALSE; FOR n := 0 TO 19 BY 1 DO IF sDevNameIn[n] = 0 THEN xPad := TRUE; END_IF IF xPad THEN sDevNameIn[n] := 16#20; //pad name string with blanks END_IF END_FOR; FOR n := 0 TO 9 BY 1 DO MasterMB.utQuery.awWriteData[n] := SHL(BYTE_TO_WORD(sDevNameIn[2 * n]), 8) + sDevNameIn[2 * n + 1]; //copy device name from input field (2 bytes to word) END_FOR; MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError THEN //if error GVL.xErr := TRUE; bAddr := 1; //first sensor GVL.bStep := 5; //read settings of all M14 units END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF END_IF //write mode of addressed M14 43: MasterMB.utQuery.bUnitId := bAddr; //address MasterMB.utQuery.bFunctionCode := 16#06; //write MasterMB.utQuery.uiWriteAddress := 16#0023; //register MasterMB.utQuery.uiWriteQuantity := 16#0001; //1 word MasterMB.utQuery.awWriteData[0] := GVL.wMode; //gain index MasterMB(); //MODBUS master IF MasterMB.xTxTrigger = FALSE THEN //if MODBUS command sent IF MasterMB.oStatus.xError THEN //if error GVL.xErr := TRUE; END_IF IF MasterMB.utResponse.wError > 0 THEN //at M14 error GVL.asStatus[bAddr] := GVL.asErrors[MasterMB.utResponse.wError]; ELSE GVL.asStatus[bAddr] := MasterMB.oStatus.sDescription; //connection status text END_IF bAddr := 1; //first sensor GVL.bStep := 5; //read settings of all M14 units END_IF END_CASE