s2l3.cpp

来自「TI的实时数据交换RTDX上位机实验代码」· C++ 代码 · 共 101 行

CPP
101
字号
#import "c:\ti\cc\bin\rtdxint.dll"
#include <iostream.h>
 
/*Option Explicit
RTDX OLE API Status Return codes*/ 
const long Success = 0x0;                         //Method call succussful
const long Failure = 0x80004005;                     //Method call failure
const long ENoDataAvailable = 0x8003001E;         //No data is currently available
const long EEndOfLogFile = 0x80030002;            //End of log file 
using namespace RTDXINTLib;

int main()
{
        IRtdxExpPtr rtdx;       // holds pointer to IRtdxExp interface
        long status;            // holds status of RTDX COM API calls
        HRESULT hr;             // holds status of generic COM API calls
        VARIANT sa;             // holds pointer to SAFEARRAY
        long data;              // holds data from SAFEARRAY
        long i=0;                 // SAFEARRAY incrementer
 
        // initialize COM
        ::CoInitialize( NULL );
 
        // initialize VARIANT
        ::VariantInit( &sa );
 
        // instantiate the RTDX COM Object
        hr = rtdx.CreateInstance( __uuidof(RTDXINTLib::RtdxExp) );
 
        cout.setf( ios::showbase );
 
        if ( FAILED(hr) ) {
                cerr << hex << hr << " - Error: Instantiation failed! \n";
                return -1;
        }
 
        // open a channel (ochan) for reading
        status = rtdx->Open( "ochan", "R" );
 
        if ( status != Success ) {
                cerr << hex << status \
                     << " -  Error: Opening of a channel failed! \n";
                return -1;
        }
 
        // loop until we have read all of our messages
        do {
                // read a 32-bit integer message
                status = rtdx->ReadSAI4( &sa );
 
                // test status returned from ReadSAI4
                switch ( status ) {
                        case Success:
                                // Display data
                                cout << "\n";
                                for ( i = 0; i < (signed)sa.parray->rgsabound[0].cElements; i++) {
                                        hr = ::SafeArrayGetElement( sa.parray, &i, (long*)&data );
                                        cout << data << "\t";
                                }
                                break;
                        case Failure:
                                cerr << hex \
                                     << status \
                                     << " - Error: ReadSAI4 returned failure! \n";
                                return -1;
                        case ENoDataAvailable:
                                cout << "\n\nNo Data is currently available!\n";
                                cout << "\n\nWould you like to continue reading [y or n]?\n";
                                char option;
                                cin >> option;
                                if ( (option == 'y') || (option == 'Y') )
                                        break;
                                else
                                        return -1;
                        case EEndOfLogFile:
                                cout << "\n\nData Processing Complete!\n";
                                break;
                        default:
                                cerr << hex << status << " - Error: Unknown return code! \n";
                                return -1;
                }
			
 
        } while ( status != EEndOfLogFile );
 
        // close the channel
        status = rtdx->Close();
 
        // release the RTDX COM Object
        rtdx.Release();
 
        // clear Variant
        ::VariantClear( &sa );
 
        // uninitialize COM
        ::CoUninitialize();
 
         return 0;
}
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?