⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visacomm.cpp

📁 压力控制器通讯源代码
💻 CPP
字号:
/******************************************************************************/
/* File:        VISACOMM.CPP                                                  */
/*                                                                            */
/* Class:       cCommunications                                               */
/* Author:      Alaster Jones                                                 */
/* Description: Encapsulate the VISA functionality aimed at comms. protocol   */
/*              more specific to pressure instruments. Hide some of the VISA  */
/*              stuff as it's a bit farty.                                    */
/* Date:        02/03/01                                                      */
/******************************************************************************/
//---------------------------------------------------------------------------
//#include <vcl.h>
//#pragma hdrstop
#include "stdafx.h"

#include "VISAComm.h"
//#include "visatype.h"
//#include "visa.h"


// event handler parameters
ViAddr      uhandle;
ViUInt16    SRQStatus;
ViSession   Sessionparam;
ViEventType EventTypeparam;
ViAddr      Addressparam;
ViBoolean   stopflag = VI_FALSE;

//---------------------------------------------------------------------------
//#pragma package(smart_init)

/******************************************************************************/
/* Function:    cCommunications                                               */
/* Author:      Alaster Jones                                                 */
/* Purpose:     constructor                                                   */
/* Inputs:      none                                                          */
/* Outputs:     none                                                          */
/******************************************************************************/
cCommunications::cCommunications()
{
    // VISA communications constructor
    findResources();
    commsType = NOCOMMS;
}

/******************************************************************************/
/* Function:    ~cCommunications                                              */
/* Author:      Alaster Jones                                                 */
/* Purpose:     destructor                                                    */
/* Inputs:      none                                                          */
/* Outputs:     none                                                          */
/******************************************************************************/
cCommunications::~cCommunications()
{
    // VISA communications destructor
    if(instr)
        viClose(instr);

    if(defaultRM)
        viClose(defaultRM);
}

/******************************************************************************/
/* Function:    findResources                                                 */
/* Author:      Alaster Jones                                                 */
/* Purpose:     find the comms resources on the PC / external connections     */
/* Inputs:      none                                                          */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::findResources(void)
{
    long status = 0;

    /* First we will need to open the default resource manager. */
    numResources = 0;
    status = viOpenDefaultRM (&defaultRM);

    if (!status)
        status = viFindRsrc (defaultRM, "?*INSTR", &findList, &numInstrs, instrDescriptor);

    if (status < VI_SUCCESS)
    {
        // an error occurred, need to show an error message
        viClose (defaultRM);
        return status;
    }

    // otherwise copy the first resource into a list
    strcpy(strResources[numResources++], instrDescriptor);

    /* Now we will open a session to the instrument we just found. */
    status = viOpen (defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr);
    if (status < VI_SUCCESS)
    {
        return status;
    }
    else
    {
        /* Now close the session we just opened.                            */
        /* In actuality, we would probably use an attribute to determine    */
        /* if this is the instrument we are looking for.                    */
        viClose (instr);
    }

    while (--numInstrs)
    {
        /* stay in this loop until we find all instruments */
        status = viFindNext (findList, instrDescriptor);  /* find next desriptor */
        if (status < VI_SUCCESS)
        {   /* did we find the next resource? */
            viClose (defaultRM);
            return status;
        }

        // if ASRL10 is found we won't add it to the list (parallel port)
        if (strncmp(instrDescriptor, "ASRL10", 6))
        {
            strcpy(strResources[numResources++], instrDescriptor);

            /* Now we will open a session to the instrument we just found */
            status = viOpen (defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr);
            if (status < VI_SUCCESS)
            {
                return status;
            }
            else
            {
                viClose(instr);
            }
        }
    }
    /* end while */

    status = viClose(findList);
    status = viClose(defaultRM);

    return status;
}

/******************************************************************************/
/* Function:    openCommsChannel                                              */
/* Author:      Alaster Jones                                                 */
/* Purpose:     opne a communications channel                                 */
/* Inputs:      string pointer                                                */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::openCommsChannel(char * strChannel)
{
    long status = 0;

    status = viOpenDefaultRM (&defaultRM);
    if (!status)
    {
        status = viOpen (defaultRM, strChannel, VI_NULL, VI_NULL, &instr);
        if(strncmp(strChannel, "ASRL", 4)==NULL)
            commsType = RS232;
        else if(strncmp(strChannel, "GPIB", 4)==NULL)
            commsType = IEEE488;
    }

    if(!status)
        status = viFlush(instr, VI_READ_BUF_DISCARD);

    if(!status)
        status = viFlush(instr, VI_WRITE_BUF_DISCARD);

    return status;
}

/******************************************************************************/
/* Function:    Initialisation                                                */
/* Author:      Alaster Jones                                                 */
/* Purpose:     initialise comms                                              */
/* Inputs:      long                                                          */
/* Outputs:     none                                                          */
/******************************************************************************/
long cCommunications::Initialisation(void)
{
    return 0;
}

/******************************************************************************/
/* Function:    setSerial                                                     */
/* Author:      Alaster Jones                                                 */
/* Purpose:     set serial comms parameters                                   */
/* Inputs:      baudrate, database. stopbits, parity, flowcontrol             */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::setSerial(ULONG baudRate, USHORT dataBits, STOPBITS stopBits, PARITY parity, FLOWCONTROL flowControl)
{
    long status = 0;

    status = viSetAttribute(instr, VI_ATTR_ASRL_BAUD, baudRate);

    if(!status)
        status = viSetAttribute(instr, VI_ATTR_ASRL_DATA_BITS, dataBits);

    if(!status)
        status = viSetAttribute(instr, VI_ATTR_ASRL_PARITY, stopBits);

    if(!status)
        status = viSetAttribute(instr, VI_ATTR_ASRL_STOP_BITS, parity);

    if(!status)
        status = viSetAttribute(instr, VI_ATTR_ASRL_FLOW_CNTRL, flowControl);

    return status;
}

/******************************************************************************/
/* Function:    setIEEE                                                       */
/* Author:      Alaster Jones                                                 */
/* Purpose:     set IEEE comms parameters                                     */
/* Inputs:      none                                                          */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::setIEEE(void)
{
    long status = 0;

    // SRQHandler is a global call back function
    status = viInstallHandler(instr, VI_EVENT_SERVICE_REQ, SRQHandler, uhandle);

    if(!status)
        status = viEnableEvent(instr, VI_EVENT_SERVICE_REQ, VI_HNDLR, VI_NULL);

    return status;
}

/******************************************************************************/
/* Function:    setTerminationChar                                            */
/* Author:      Alaster Jones                                                 */
/* Purpose:     set the termination character                                 */
/* Inputs:      bool state                                                    */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::setTerminationChar(bool bEnabled)
{
    long status = 0;

    // 0xA - newline
    // 0xD - carriage return
    // we'll default it to \n for now as this is widely the case
    //status = viSetAttribute(instr, VI_ATTR_TERMCHAR, 0xA);
    status = viSetAttribute(instr, VI_ATTR_TERMCHAR, 0xD);

    if(!status)
        status = viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, bEnabled);

    return status;
}

/******************************************************************************/
/* Function:    setTimeout                                                    */
/* Author:      Alaster Jones                                                 */
/* Purpose:     set the timeout value                                         */
/* Inputs:      long timeout                                                  */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::setTimeout(long timeout)
{
    return viSetAttribute(instr, VI_ATTR_TMO_VALUE, timeout);
}

/******************************************************************************/
/* Function:    closeCommunications                                           */
/* Author:      Alaster Jones                                                 */
/* Purpose:     close communications (viClose)                                */
/* Inputs:      none                                                          */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cCommunications::closeCommunications(void)
{
    // not that bothered about default resource status
    viClose(defaultRM);
    return viClose(instr);
}

⌨️ 快捷键说明

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