CF-test动态库函数

该博客介绍了如何在程序中使用动态库,包括操作Excel、串口通信以及获取动态库地址等关键步骤,为相关开发提供参考。
摘要由CSDN通过智能技术生成
// AccessingArrays.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <mbstring.h>            
#include "math.h"
#include "tsapivc.h"
using namespace TS;
#include "utilityFunctions.h"
#include "AccessingArrays.h"
#include<vector>
#include<iostream>
#include"Windows.h"
#include"CanCtr.h"
#include "PCI8201Ctr.h"
#include"ControlCAN.h"
#include"PCI2362Ctr.h"
#include "PCI8602Ctr.h"
#include <bitset>
#include<Windows.h>
#include"Serial.h"
#include<algorithm>
#include<stdlib.h>
//#include"CnComm.h"
using namespace std;
//#include<thread>
//#include<chrono>
#include "Mytimer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

typedef struct tagBOUNDSELEMENT {    
    long lowerBound;    
    long upperBound;
} BOUNDSELEMENT;

  //CnComm m_com;//串口类对象
     //CAN配置文件变量
     vector<CString>m_can_dataname;
	 vector<CString>m_send_id;
     vector<CString>m_id;
	 vector<int>m_cycle_time;
	 vector<int>m_start_bit;
	 vector<double>m_length;
	 vector<double>m_factor;
	 vector<CString>m_byte_order;
     vector<int>m_offset;
	 vector<CString>m_type;
	 vector<double>m_db_dataname_value;//存储can数据库的dataname列对应的模型中的值
	 vector<int>m_db_flag;//存储can数据库的s/r列,表示can数据时发送还是接收
	 vector<double>m_recvcan;
	 vector<int>m_can_time_count;//计数
	 //IO配置文件变量
	 vector<CString>m_io_dataname;
	 vector<int>m_io_flag;
	 vector<long>m_io_port;
	 vector<int>m_io_low_high;
	 vector<long>m_io_usemanuval;

	 CApplication ExcelApp;
	 CWorkbooks books;
	 CWorkbook book;
	 CWorksheets sheets;
	 CWorksheet sheet;
	 CRange range;
	 HANDLE m_hDevice2362;//PCI2362采集开句柄
	 HANDLE m_hDevice8602;//PCI8602采集开句柄
	 HANDLE m_hDevice8201;//PCI8201采集开句柄
	 HANDLE m_hDevice8602_2;//PCI8602采集开句柄
	 CCanCtr m_PCI9840_CAN;//PCI9840控制对象
	 CCanCtr m_PCI9840_CAN_2;//PCI9840控制对象
	 PCI2362Ctr m_PCI2362;//PCI2362控制对象
	
     PCI8602Ctr m_PCI8602_2; //PCI8602控制对象
	 PCI8602Ctr m_PCI8602; //PCI8602控制对象
	  PCI8201Ctr m_PCI8201; //PCI8201控制对象
	 int BP_kaidu;//制动踏板开度
	 CMytimer timer;//定时器对象
	 WORD Volttemp=0;//存储上读取的上一个电压值
	 BOOL m_htl=FALSE;//高电平到低电平的标识
	 BOOL m_lth=FALSE;//低电平到高电平的标识
	 bool m_getcycle_flag=false;
	 int count_low=0;//低电平计数
	int count_high=0;//高电平计数
	int count_flag=0;//一个周期计数
	int m_dutycycle=0;//占空比
	BOOL m_activebms=FALSE;
	VCI_CAN_OBJ frameinfo_send[30];
	HANDLE hThread1;
	BYTE Send_Frame_Count;
	HANDLE SendThread;
	 BOOL m_time_count=FALSE;//can发送计时器线程
	BOOL  m_send_can=FALSE;//can发送线程开启标志
	BOOL m_recv_can=FALSE;
	BOOL m_1AAflag=FALSE;//开启设备1二路CAN的标识
	BOOL cash_flag=FALSE;//碰撞信号发送标识
//
//  Note!
//
//      If this DLL is dynamically linked against the MFC
//      DLLs, any functions exported from this DLL which
//      call into MFC must have the AFX_MANAGE_STATE macro
//      added at the very beginning of the function.
//
//      For example:
//
//      extern "C" BOOL PASCAL EXPORT ExportedFunction()
//      {
//          AFX_MANAGE_STATE(AfxGetStaticModuleState());
//          // normal function body here
//      }
//
//      It is very important that this macro appear in each
//      function, prior to any calls into MFC.  This means that
//      it must appear as the first statement within the 
//      function, even before any object variable declarations
//      as their constructors may generate calls into the MFC
//      DLL.
//
//      Please see MFC Technical Notes 33 and 58 for additional
//      details.
//

/
// CAccessingArraysApp

BEGIN_MESSAGE_MAP(CAccessingArraysApp, CWinApp)
    //{
  {AFX_MSG_MAP(CAccessingArraysApp)
        // NOTE - the ClassWizard will add and remove mapping macros here.
        //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CAccessingArraysApp construction

CAccessingArraysApp::CAccessingArraysApp()
{
    // NOTE: add construction code here,
    // Place all significant initialization in InitInstance
}

/
// The one and only CAccessingArraysApp object

CAccessingArraysApp theApp;

extern "C" {

void __declspec(dllexport) __stdcall FillLocalArray(SequenceContext *seqContext,  short *errorOccurred,  long *errorCode,  char errorMsg[1024])
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    double *        numArray = NULL;
    unsigned int    numElements;
   double          numCycles = 2.0;
    PropertyObjectPtr   seqContextPOPtr;

    TRY
        // Set sequence context as a property object
    seqContextPOPtr = seqContext->AsPropertyObject();
   
        // The following code shows how to accesses properties via the ActiveX Automation API
    if (seqContextPOPtr->Exists("Locals.NumericArray",0)) 
        {
            // Create a Safe Array from the VARIANT which contains a
            // copy of the Locals.NumericArray. 
        COleSafeArray safeArray;
        safeArray.Attach(seqContextPOPtr->GetValVariant("Locals.NumericArray",0));
    
            // Check the size of the array of data - assuming 1D array  
        numElements = safeArray.GetOneDimSize();

            // Lock array for data access and get a pointer to the data.
            // (assuming it's an array of doubles)
        safeArray.AccessData((void**)&numArray);
        

            // Create sine pattern
        for (unsigned int i=0; i<numElements; i++)
           {
			   numArray[i] = sin((2*3.14) * i *numCycles/ numElements);

		      //numArray[i] =m_first_lie[i]*1.0 ;
		    }
            // unlock array 
        safeArray.UnaccessData();

            // Set the value of the property the lookupString parameter specifies with a variant.  
            // Use this method to set the value of an entire array at once.                         
        seqContextPOPtr->SetValVariant("Locals.NumericArray", 0, safeArray);

        }
    
    CATCH(COleDispatchException, e) 
        *errorOccurred = TRUE;
        _mbsnbcpy_s((unsigned char *)errorMsg, 1024, (unsigned char *)LPCTSTR(e->m_strDescription), 1024 - 1);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = e->m_scError;

    AND_CATCH(CMemoryException, e)
        *errorOccurred = TRUE;
        e->GetErrorMessage(errorMsg, 1024);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = TS_Err_OutOfMemory;

    END_CATCH 
}

void __declspec(dllexport) __stdcall AccessLocalArray(SequenceContext *seqContext, short *errorOccurred, long *errorCode,  char errorMsg[1024])
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    double *        numArray = NULL;
    unsigned int    numElements;
  
    PropertyObjectPtr   seqContextPOPtr;

    TRY 
        // Set sequence context as a property object
    seqContextPOPtr = seqContext->AsPropertyObject();

        // The following code shows how to accesses properties via the ActiveX Automation API
    if (seqContextPOPtr->Exists("Locals.NumericArray",0)) 
        {
            // Create a Safe Array from the VARIANT which contains a
            // copy of the Locals.NumericArray. 
        COleSafeArray safeArray;
        safeArray.Attach(seqContextPOPtr->GetValVariant("Locals.NumericArray",0));

            // Check the size of the array of data - assuming 1D array  
        numElements = safeArray.GetOneDimSize( );

            // Lock array for data access and get a pointer to the data.
            // (assuming it's an array of doubles)
        safeArray.AccessData((void**)&numArray);

            // Display data using LabWindows/CVI library function   
        if (CVI_YGraphPopup(seqContext->EngineAsDispatch, "Data in Local Array", numArray, numElements) < 0)
            AfxThrowMemoryException();

            // unlock array 
        safeArray.UnaccessData();
        }

    CATCH(COleDispatchException, e) 
        *errorOccurred = TRUE;
        _mbsnbcpy_s((unsigned char *)errorMsg, 1024, (unsigned char *)LPCTSTR(e->m_strDescription), 1024 - 1);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = e->m_scError;

    AND_CATCH(CMemoryException, e)
        *errorOccurred = TRUE;
        e->GetErrorMessage(errorMsg, 1024);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = TS_Err_OutOfMemory;

    END_CATCH 
}

void __declspec(dllexport) __stdcall Create2DArray(SequenceContext *seqContext, short *errorOccurred, long *errorCode, char errorMsg[1024])
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    double          *array2D;
    DWORD   rgElements[2] = {2,100};
    PropertyObjectPtr   seqContextPOPtr;

    TRY
        // Set sequence context as a property object
    seqContextPOPtr = seqContext->AsPropertyObject();

        // To dynamically create variables, you would normally use the InsertIfMissing 
        // Property Option.  In TestStand 1.0 and 1.0.1, the InsertIfMissing option does 
        // not work with arrays.
        // This example demonstrates how to create an array using NewSubProperty
    seqContextPOPtr->NewSubProperty ("Locals.Array2D", PropValType_Number, TRUE, "", 0);

        // You do not need to do a SetDimension as SetValVariant 
        // auto dimensions the property object to be atleast as big as the safe array
        //  property.SetDimensions ("Locals.Array2D", 0, "[0][0]", "[1][99]");
    
        // Create a Safe Array from the VARIANT which contains a
        // copy of the Locals.NumericArray. 
    COleSafeArray safeArray;
    
        // Create a Safe Array of the required size
    safeArray.Create( VT_R8,2, rgElements); 
      //加?VARIANT varRead = range.get_Value2();
    //COleSafeArray safeArray(varRead);              
                                       
        // Lock array for data access and get a pointer to the data.
        // (assuming it's an array of doubles)         
    safeArray.AccessData((void**)&array2D);

	//加?VARIANT Read = range.get_Value2();
    //COleSafeArray safeArray(varRead);
        // Create sine pattern正选余弦函数
	
    for (int i=0; i<100; i++)
        {
        array2D[i] = sin((2*3.14) * i / 100);
			//array2D[i] =m_factor[i];
			//array2D[i+173] =m_offset[i];
        array2D[i+100] = cos((2*3.14) * i / 100);

        }

        // unlock array 
    safeArray.UnaccessData();

        
        // Set the value of the property the lookupString parameter specifies with a variant.  
        // Use this method to set the value of an entire array at once.                         
    seqContextPOPtr->SetValVariant("Locals.Array2D", 0, safeArray);

    CATCH(COleDispatchException, e) 
        *errorOccurred = TRUE;
        _mbsnbcpy_s((unsigned char *)errorMsg, 1024, (unsigned char *)LPCTSTR(e->m_strDescription), 1024 - 1);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = e->m_scError;

    AND_CATCH(CMemoryException, e)
        *errorOccurred = TRUE;
        e->GetErrorMessage(errorMsg, 1024);
        *(errorMsg + (1024 - 1)) = '\0';
        *errorCode = TS_Err_OutOfMemory;

    END_CATCH
}


void __declspec(dllexport) __stdcall Access2DArray(SequenceContext *seqContext,  short *errorOccurred, long *errorCode, char errorMsg[1024])
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    double *        numArray = NULL;
    long            firstDimSize, secondDimSize, numberDimensions;
    BOUNDSELEMENT   *pBounds = NULL;
    PropertyObjectPtr   seqContextPOPtr;

    TRY
        // Set sequence context as a property object
    seqContextPOPtr = seqContext->AsPropertyObject();

    if (seqContextPOPtr->Exists("Locals.Array2D", 0)) 
        {
            // Create a Safe Array from the VARIANT which contains a
            // copy of the Locals.NumericArray. 
        COleSafeArray safeArray;
        safeArray.Attach(seqContextPOPtr->GetValVariant("Locals.Array2D",0));

            // Lock array for data access and get a pointer to the data.
            // (assuming it's an array of doubles)
        safeArray.AccessData((void**)&numArray);

            // Get number of dimensions of safearray
        numberDimensions = safeArray.GetDim();

            // Allocate data to hold dimension sizes
        pBounds = (BOUNDSELEMENT*)malloc(numberDimensions * sizeof(BOUNDSELEMENT));
        if (pBounds == NULL)
            AfxThrowMemoryException();
        
            // Get dimension sizes
        for (long i = 0; i<numberDimensions; i++)
            {
            safeArray.GetLBound(i+1, &pBounds[i].lowerBound);
            safeArray.GetUBound(i+1, &pBounds[i].upperBound);
            }

        if (numberDimensions == 2)  // 2-D array only
            {
            firstDimSize = pBounds[0].upperBound - pBounds[0].lowerBound + 1;
            secondDimSize = pBounds[1].upperBound - pBounds[1].lowerBound + 1;              

                // Display data using LabWind
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值