RMCLink C++ Wrapper Class

Handling CRMCLink Errors

CRMCLink methods that can fail return the standard COM data type HRESULT. The return value of each method that can fail should be checked using standard if-then-else logic after the call returns.

The HRESULT data type is a 32-bit integer divided into three bit fields: severity, facility, and code. The severity field is bit 31, facility is bits 16-30, and code is bits 0-15. However, HRESULTs are usually not accessed on the bit-field level, but are typically first qualified as success or failure using the FAILED/SUCCEEDED macros, and once an error has been determined, the compared with defined HRESULT constants.

FAILED(hr) will be true if hr's severity code is 1, and SUCCEEDED(hr) will be true if hr's severity code is 0. Notice that, even though all of the CRMCLink methods will return S_OK on success, it is not good practice to compare HRESULT values directly with S_OK. Use SUCCEEDED(hr) instead of hr == S_OK.

After a return code has been determined to be an error using FAILED/SUCCEEDED, the exact nature of the error can be determined by comparing the HRESULT value with defined constants. All custom error codes are enumerated in CRMCLink::enumRMCLINKERRORS, plus some standard error codes are used, including E_OUTOFMEMORY and E_INVALIDARG. Refer to each individual CRMCLink method's topic for a complete list of error codes that may be returned.

Example

The following code example illustrates proper return code checking on the CRMCLink class.

C++

#include <windows.h>
#include <stdio.h>
#include "RMCLink.h"
 
void main()
{
    // In order to create COM objects, we must initialize the COM sub-system.
    ::CoInitialize(0);
 
    // Create a new RMCLink object.
    CRMCLink* pRMC = CRMCLink::CreateEthernetLink(CRMCLink::dtRMC70, _T("192.168.0.22"));
    if ( pRMC != NULL )
    {
        // Establish the connection.
        HRESULT hr = pRMC->Connect();
        if ( SUCCEEDED(hr) )
        {
            // Read F8:8 and F8:9 (Axis 0 Actual Position and Velocity)
            float afData[2];
            hr = pRMC->ReadFFile(CRMCLink::fn70StatusAxis0, 8, 2, afData);
            if ( SUCCEEDED(hr) )
            {
                printf("Actual Position = %.3f, Actual Velocity = %.3f\n",
                    afData[0], afData[1]);
            }
 
            // Close the connection.
            pRMC->Disconnect();
        }
 
        // Free the RMCLink object we created.
        delete pRMC;
    }
 
    // Release the COM sub-system.
    ::CoUninitialize();
}

 

See Also

CRMCLink Class | RMCLink C++ Wrapper Class | CRMCLink::enumRMCLINKERRORS


Send comments on this topic.

Copyright (c) 2024 Delta Computer Systems, Inc. dba Delta Motion