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

📄 dpi515com.cpp

📁 压力控制器通讯源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************/
/* File:        DPI515COM.CPP                                                 */
/*                                                                            */
/* Class:       cDPI515Communications                                         */
/* Author:      Alaster Jones                                                 */
/* Description: Derived from the VISA class specifically for DPI515 instru-   */
/*              ment communications, serial and IEEE.                         */
/*                                                                            */
/* Date:        02/03/01                                                      */
/******************************************************************************/
//---------------------------------------------------------------------------
#include "stdafx.h"

#include "dpi515Com.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys\timeb.h>

#define MAXUNITS    26

//---------------------------------------------------------------------------

/******************************************************************************/
/* Function:    cDPI515Communications                                         */
/* Author:      Alaster Jones                                                 */
/* Purpose:     constructor                                                   */
/* Inputs:      none                                                          */
/* Outputs:     none                                                          */
/******************************************************************************/
cDPI515Communications::cDPI515Communications()
{
    // constructor
    commsProtocol = e515;   // default to 515 comms protocol SCPI
    pressureReading = 0.0;
    currentRangeFS = 0.0;
    pUnit = NULL;
    numRanges = 0;
    numUnits = 0;
    trap_SRQ = false;
    trap_SRQ_Value = 0;
}

/******************************************************************************/
/* Function:    ~cDPI515Communications                                        */
/* Author:      Alaster Jones                                                 */
/* Purpose:     destructor                                                    */
/* Inputs:      none                                                          */
/* Outputs:     none                                                          */
/******************************************************************************/
cDPI515Communications::~cDPI515Communications()
{
    // destructor
}

/******************************************************************************/
/* Function:    instrumentInitialisation                                      */
/* Author:      Alaster Jones                                                 */
/* Purpose:     initialise the instrument                                     */
/* Inputs:      none                                                          */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cDPI515Communications::instrumentInitialisation(void)
{
//    char * ptr;
    char sTemp[20];
    char strWrite[100];
    long status = 0;

    // clear the bus and enable the registers
    strcpy(strWrite, "*CLS\r\n");
    status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), strlen(strWrite), &rtnCount);
    sndBytes = sndBytes + rtnCount; // number of bytes actually written

    if(!status) // enable pressure operations register
    {
        strcpy(strWrite, ":STAT:OPER:PRES:ENAB 511\r\n");
        status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), strlen(strWrite), &rtnCount);
        sndBytes = sndBytes + rtnCount; // number of bytes actually written
    }

    if(!status) // enable operations register
    {
        strcpy(strWrite, ":STAT:OPER:ENAB 1024\r\n");
        status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), strlen(strWrite), &rtnCount);
        sndBytes = sndBytes + rtnCount; // number of bytes actually written
    }

    if(!status) // enable SRQ events
    {
        strcpy(strWrite, ":SRQ:ENAB 128\r\n");
        status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), strlen(strWrite), &rtnCount);
        sndBytes = sndBytes + rtnCount; // number of bytes actually written
    }

    if(!status) // lock out the local UI
    {
        strcpy(strWrite, ":LLO\r\n");
        status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), strlen(strWrite), &rtnCount);
        sndBytes = sndBytes + rtnCount; // number of bytes actually written
    }

    // get the available ranges and the current range
    getInstrumentRanges();
    getCurrentRange(sTemp);

    // ..and the available and current units
    getInstrumentUnits();
    getCurrentUnit(sTemp);

    return status;
}

/******************************************************************************/
/* Function:    getInstrumentID                                               */
/* Author:      Alaster Jones                                                 */
/* Purpose:     get the ID, really a comms test                               */
/* Inputs:      none                                                          */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cDPI515Communications::getInstrumentID(char * instrumentID)
{
    char * ptr;
    char strWrite[100];
    char strRead[100];
    long status = 0;

    // flush the buffers before we do a read every time
    status = viFlush(instr, VI_WRITE_BUF_DISCARD);
    status = viFlush(instr, VI_READ_BUF_DISCARD);

    // query pressure
    strcpy(strWrite,"*IDN?\r\n");
    wrtCount = strlen(strWrite);
    status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), wrtCount, &rtnCount);
    sndBytes = sndBytes + rtnCount; // number of bytes actually written

    // read the reply
    if(!status) // no r/w error
    {
        status = viRead (instr, reinterpret_cast<unsigned char *>(strRead), 100, &rtnCount);
        if(ptr = strtok(strRead, "\r"))
            strcpy(instrumentID, strRead);
        rcdBytes = rcdBytes + rtnCount; // number of bytes actually read
    }

    return status;
}

/******************************************************************************/
/* Function:    setSetpoint                                                   */
/* Author:      Alaster Jones                                                 */
/* Purpose:     send the setpoint                                             */
/* Inputs:      pressure double                                               */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cDPI515Communications::setSetpoint(double dSetpoint)
{
//    char * ptr;
    char strWrite[100];
    long status = 0;

    // query pressure
    sprintf(strWrite, ":SOUR %f\r\n", dSetpoint);
    wrtCount = strlen(strWrite);
    status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), wrtCount, &rtnCount);
    sndBytes = sndBytes + rtnCount; // number of bytes actually written

    return status;
}

/******************************************************************************/
/* Function:    getSetpoint                                                   */
/* Author:      Alaster Jones                                                 */
/* Purpose:     get the setpoint                                              */
/* Inputs:      ptr to pressure double                                        */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cDPI515Communications::getSetpoint(double * pdSetpoint)
{
    char * ptr;
    char strWrite[100];
    char strRead[50];
    long status = 0;

    // flush the buffers before we do a read every time
    status = viFlush(instr, VI_WRITE_BUF_DISCARD);
    status = viFlush(instr, VI_READ_BUF_DISCARD);

    // query pressure
    strcpy(strWrite,":SOUR?\r\n");
    wrtCount = strlen(strWrite);
    status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), wrtCount, &rtnCount);
    sndBytes = sndBytes + rtnCount; // number of bytes actually written

    // read pressure request
    if(!status) // no r/w error
    {
        status = viRead (instr, reinterpret_cast<unsigned char *>(strRead), 50, &rtnCount);
        rcdBytes = rcdBytes + rtnCount; // number of bytes actually read
    }

    if(!status) // no r/w error
    {
        if(strncmp(strRead, ":SOUR:PRES:LEV:IMM:AMPL", 23)==NULL)
        {
            ptr = strtok(strRead, " ");
            ptr = strtok(NULL, "\r");
            *pdSetpoint = atof(ptr);
        }
        else if(strncmp(strRead, ":SRQ", 4)==NULL)
        {
            // trap an SRQ here
            trap_SRQ = true;
            ptr = strtok(strRead, " ");
            ptr = strtok(NULL, "\r");
            trap_SRQ_Value = atol(ptr);
        }
        else

            status = -1; // failed to get the right reply

    }

    return status;
}

/******************************************************************************/
/* Function:    getPressure                                                   */
/* Author:      Alaster Jones                                                 */
/* Purpose:     get the pressure                                              */
/* Inputs:      ptr to pressure double                                        */
/* Outputs:     long status                                                   */
/******************************************************************************/
long cDPI515Communications::getPressure(double * pPressure)
{
    char * ptr;
    char strWrite[100];
    char strRead[100];
    long status = 0;

    // flush the buffers before we do a read every time
    status = viFlush(instr, VI_WRITE_BUF_DISCARD);
    status = viFlush(instr, VI_READ_BUF_DISCARD);

    // query pressure
    strcpy(strWrite,":SENS:PRES?\r\n");
    wrtCount = strlen(strWrite);
    status = viWrite (instr, reinterpret_cast<unsigned char *>(strWrite), wrtCount, &rtnCount);
    sndBytes = sndBytes + rtnCount; // number of bytes actually written

    // read pressure request
    if (!status) // no r/w error
    {
        status = viRead (instr, reinterpret_cast<unsigned char *>(strRead), 50, &rtnCount);
        rcdBytes = rcdBytes + rtnCount; // number of bytes actually read
   }

    if(!status) // no r/w error
    {
        if(strncmp(strRead, ":SENS:PRES", 10)==NULL)
        {
            // correct string returned
            ptr = strtok(strRead, " ");
            ptr = strtok(NULL, "\r");
            *pPressure = pressureReading = atof(ptr);
        }
        else if(strncmp(strRead, ":SRQ", 4)==NULL)
        {
            // trap an SRQ here

⌨️ 快捷键说明

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