Result evaluation during Code Reading

This sample illustrates the following

Explanation

You can find a detailed explanation of the return codes for Code Reading functions in the overview chapter Code Reading.

Variables

hr                        :   HRESULT;

// Images
ipImageIn                 :   ITcVnImage;
ipImageInDisp             :   ITcVnDisplayableImage;
ipImageRes                :   ITcVnImage;
ipImageResDisp            :   ITcVnDisplayableImage;

// Barcode
ipCodeDecodedList         :   ITcVnContainer;
ipCodeContourList         :   ITcVnContainer;
sCodeAsString             :   STRING(255);

// Watchdog
hrWD                      :   HRESULT;
tStop                     :   DINT := 50000;
tRest                     :   DINT;

// Color
aColorGreen               :   TcVnVector4_LREAL := [0, 255, 0];

// Return code
nReturnCode               :   DWORD;
sReturnCode               :   STRING;
sResultText               :   STRING;

Code

// Execute the Barcode Reading Function with EAN13 selected monitored by the Watchdog-Function
hrWD := F_VN_StartRelWatchdog(tStop, S_OK);
    hr := F_VN_ReadBarcode(
        ipSrcImage      :=  ipImageIn,
        ipDecodedData   :=  ipCodeDecodedList,
        eBarcodeType    :=  ETcVnBarcodeType.TCVN_BT_EAN13,
        hrPrev          :=  hr
    );
hrWD := F_VN_StopWatchdog(hrWD, tRest => tRest);

// Handle return-code
IF hr = S_OK THEN
    // Prepare code result in string
    hr := F_VN_ExportSubContainer_String(ipCodeDecodedList, 0, sCodeAsString, 255, hr);
    sResultText := CONCAT('Code: ', sCodeAsString);
ELSE
    // Check for succeeded return codes or add specific error handling
    CASE hr OF
        S_FALSE:
            sResultText := 'No code found...';

        S_WATCHDOG_TIMEOUT:
            sResultText := 'Cancelled by watchdog...';
    ELSE
        // Extract error-code from HRESULT & react accordingly
        nReturnCode := DINT_TO_DWORD(hr) AND 16#FFF;

        sReturnCode := DWORD_TO_HEXSTR(nReturnCode, 3, FALSE);
        sResultText := CONCAT('Returncode ', sReturnCode);
    END_CASE
END_IF

// Draw result image
hr := F_VN_PutTextExp(sResultText, ipImageRes, 50, 100, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 4, aColorGreen, 3, TCVN_LT_4_CONNECTED, FALSE, S_OK);
hr := F_VN_TransformIntoDisplayableImage(ipImageRes, ipImageResDisp, hr);

Notice

Reserve hr

Make sure not to use the variable hr between the call of the Code Reading function and the evaluation as a return value of another function.

Results

If a code is found as expected, is hr = S_OK and the result can be output.

Result evaluation during Code Reading 1:

If the expected code cannot be found, the return code is S_FALSE. If execution is aborted by a watchdog, the code is 16#256. A corresponding message can be output.

Result evaluation during Code Reading 2:     Result evaluation during Code Reading 3:

If the function is not executed correctly, the error code can be output. This does not occur in the sample and is only caused by incorrect use of Code Reading functions.

Similar samples