Ejemplo n.º 1
0
float KeithGetVoltage ( void ){
	strcpy(nextstringinput,":SOUR:VOLT:AMPL?\n");
	status = viWrite (inst, (ViBuf)nextstringinput, (ViUInt32)strlen(nextstringinput), &rcount);
	if (status < VI_SUCCESS)
	{
		printf("There was an error writing the command to get the data\n");
		return 0;
	}     

	status = viRead (inst, data, 3000, &rcount);
	if (status < VI_SUCCESS)
	{
		printf("There was an error reading the data\n");
		return 0;
	}

	status = viClose (ehandle);
	if (status < VI_SUCCESS)
	{
		printf("There was an error closing the event handle\n");
		return 0;
	}
	std::ostringstream formatter;
	formatter << data;
	std::string myString = formatter.str();
	float volt = (float)atof(myString.c_str());
	return volt;
}
Ejemplo n.º 2
0
uint32_t serialWrite(uint8_t port, const char *buffer, int32_t count, int32_t *status) {
	uint32_t retCount = 0;

	*status = viWrite(m_portHandle[port], (ViPBuf)buffer, count, (ViPUInt32)&retCount);
	
	if(*status > 0)
		*status = 0;
	return retCount;
}
Ejemplo n.º 3
0
int KeithWrite(char *cmd) {
   strcpy(stringinput,cmd);
   status = viWrite (inst, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &rcount);
   if (status < VI_SUCCESS)
   {
      printf("Error writing to the instrument\n");
	  return 1;
   }
   return 0;
}
Ejemplo n.º 4
0
int32_t HAL_WriteSerial(int32_t port, const char* buffer, int32_t count,
                        int32_t* status) {
  uint32_t retCount = 0;

  *status =
      viWrite(m_portHandle[port], (ViPBuf)buffer, count, (ViPUInt32)&retCount);

  if (*status > 0) *status = 0;
  return static_cast<int32_t>(retCount);
}
Ejemplo n.º 5
0
/**
 * Writes data to a UART transmit FIFO. If not enough space is available in the
 * FIFO, this function blocks until sufficient space is available or a timeout
 * occurs.
 *
 * @param[in]   port    UART port to access
 * @param[in]   data    Array of data to write
 * @param[in]   nData   Size of data array
 * @return      int32_t Error/success status
 */
int32_t Uart_Write(MyRio_Uart* port, const uint8_t* const data,
                   const size_t nData)
{
    int32_t status = VI_SUCCESS;
    ViUInt32 numberWritten = 0;

    status = viWrite(port->session, (ViBuf)data, (ViUInt32)nData,
                     &numberWritten);

    return status;
}
Ejemplo n.º 6
0
void* DLLEXPORT SpectrumAnalyzer_RestartMeasure( ViSession viInstrumentHandle )  
{
	STD_ERROR               StdError                    =   {0};        

	unsigned int			iCount						=	0;
	
	CHK_ERR( viWrite ( viInstrumentHandle , (unsigned char*)"TS\n" , 3 , &iCount )); 

	CHK_ERR( hp8563e_waitUntilDone ( viInstrumentHandle , MAX_TIMEOUT ));   
	
Error: 

	RETURN_STDERR_POINTER;
} 
Ejemplo n.º 7
0
	void VisaEmitter::EIO_Write(GenericBaton* data) {
		if (!sizeof(data->command) || !this->isConnected || session < 1) {
			ErrorCodeToString("not connected or bad empty command", 11, data->errorString);
			return;
		}
      
		char temp[QUERY_STRING_SIZE];
		memset(temp, 0, sizeof(temp));
		ViInt32 rdBufferSize = sizeof(temp);
		ViUInt32 returnCount;
		ViStatus status = viWrite(session, (ViBuf)data->command, (ViUInt32) strlen(data->command), &returnCount);
		if ((status < VI_SUCCESS)) {
			_snprintf(temp, sizeof(temp), "%d viWrite, query: %s string length: %d; wrote: %d", session, data->command, strlen(data->command), returnCount);
			ErrorCodeToString(temp, status, data->errorString);
			return;
		}
    _snprintf_s(data->result, _countof(data->result), _TRUNCATE, "%d", returnCount); 
	}
Ejemplo n.º 8
0
ViStatus CViRemote::RCT_viWrite(ViSession vi, ViBuf buf, ViUInt32 count, ViPUInt32 retCount)
{
    m_ViStat = viWrite(vi, buf, count, retCount);
    GpibLogFunction("%X: %s", m_ViStat, buf);
	return m_ViStat;
}
Ejemplo n.º 9
0
// ================================================================================================
// FUNCTION  : mexVISA
// ------------------------------------------------------------------------------------------------
// Purpose   : Implement basic VISA functions for a MATLAB toolbox
// Parameters: passed from MATLAB mexFunction
// Returns   : {0=success, -1=failure}
// ================================================================================================
int mexVISA(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    ViStatus Status;      // returned status from called VISA function
    char *tcmd, cmd[256]; // passed command parameter

    // ----------------------
    // Get the command string
    // ----------------------
    if(nrhs<1) mexErrMsgTxt("A string argument must be given as the first parameter.");
    tcmd = ViMexUtil_GetStringValue(prhs[0],0);
    strncpy(cmd, tcmd, 256); mxFree(tcmd); cmd[255] = 0; // copied and free tcmd as it is not free'd below

    // ******************************************
    // *****                                *****
    // *****   COMMAND LOOKUP-AND-EXECUTE   *****
    // *****                                *****
    // ******************************************

    // --------------
    // viOpen
    // --------------
    if(!strcmp(cmd, "viOpen()"))
    {
        ViSession RMSession, Session = 0;
        ViString  Resource; ViUInt32 Timeout;

        if( (nrhs!=3) && (nrhs!=4) )
            mexErrMsgTxt("Incorrect number of input parameters (needs to be 3 or 4)");
        if( nlhs != 2)
            mexErrMsgTxt("2 output terms were expected\n");

        if(!mexVISA_GetViRM(&RMSession))
        {
            Resource = ViMexUtil_GetStringValue(prhs[1], 0);
            Timeout  = (ViUInt32)ViMexUtil_GetUIntValue(prhs[2]);
            
            if(ResourceChecking)
            {
                if(mexVISA_ResourceIsOpen(Resource, &Session))
                {
                    if(AllowSharedSessions) 
                        Status = VI_SUCCESS;
                    else
                        mexErrMsgTxt(" Resource is already open");
                }
            }
            if(!Session)
            {
                Status = viOpen(RMSession, Resource, VI_NULL, Timeout, &Session);
                if(ResourceChecking && (Status == VI_SUCCESS))
                {
                    if(nrhs == 3)
                        mexVISA_InsertSession(Session, Resource, "no stack trace available\n");
                    else
                    {

                        ViString StackTrace = ViMexUtil_GetStringValue(prhs[3], 0);
                        mexVISA_InsertSession(Session, Resource, StackTrace);
                        mxFree(StackTrace);
                    }
                }
            }
            ViMexUtil_PutIntValue(Status, &plhs[0]);
            ViMexUtil_PutUIntValue((unsigned)Session, &plhs[1]);
            mxFree(Resource);
            return 0;
        }
        else mexErrMsgTxt("mexVISA: Call to viOpenDefaultRM prior to viOpen failed.");
    }
    // ---------------
    // viClose
    // ---------------
    if(!strcmp(cmd, "viClose()"))
    {
        ViSession Session;
        ViMexUtil_CheckParameters(nlhs,nrhs,1,2);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Status = viClose(Session);
        if(ResourceChecking && (Status == VI_SUCCESS))
            mexVISA_RemoveSession(Session);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        return 0;
    }
    // ---------------
    // viWrite
    // ---------------
    if(!strcmp(cmd, "viWrite()"))
    {
        ViSession Session;
        ViUInt32 Count, Return_Count;
        ViBuf Buffer; char *Message;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Buffer  = (ViBuf)ViMexUtil_GetStringValue(prhs[2], ((ViUInt32 *)&Count));
        Message = (char *)mxCalloc(Count+2, sizeof(char));
        if(!Message) mexErrMsgTxt("Memory allocation error.");
        sprintf(Message,"%s",Buffer);
        Status = viWrite(Session, Message, Count-1, &Return_Count);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutUIntValue(Return_Count, &plhs[1]);
        mxFree(Message); mxFree((char *)Buffer);
        return 0;
    }
    // ---------------
    // viWriteBinary
    // ---------------
    if(!strcmp(cmd, "viWriteBinary()"))
    {
        ViSession Session;
        ViUInt32 Count, Return_Count;
        ViBuf Buffer;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Buffer  = (ViBuf)ViMexUtil_GetBinaryArray(prhs[2], ((ViUInt32 *)&Count));
        Status = viWrite(Session, Buffer, Count, &Return_Count);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutUIntValue(Return_Count, &plhs[1]);
        mxFree((char *)Buffer);
        return 0;
    }
    // ---------------
    // viPrintf
    // ---------------
    if(!strcmp(cmd, "viPrintf()"))
    {
        ViSession Session;
        ViUInt32 Count;
        ViBuf Buffer;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Buffer  = (ViBuf)ViMexUtil_GetStringValue(prhs[2], ((ViUInt32 *)&Count));
        Status = viPrintf(Session,"%s",Buffer);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree((char *)Buffer);
        return 0;
    }
    // --------------
    // viRead
    // --------------
    if(!strcmp(cmd, "viRead()"))
    {
        ViSession Session;
        ViUInt32 Count, Return_Count;
        unsigned char *Buffer;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Count   = (ViUInt32)ViMexUtil_GetUIntValue(prhs[2]);
        Buffer  = (unsigned char *)mxCalloc(Count+1,sizeof(char));
        if(!Buffer) mexErrMsgTxt("Memory Allocation Failed.\n");
        Status = viRead(Session, Buffer, Count, &Return_Count);
        if(Return_Count < Count) Buffer[Return_Count-1] = '\0';
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutString(Buffer, &plhs[1]);
        mxFree(Buffer); // removed 6/4/04 for debug
        return 0;
    }
    // --------------
    // viReadBinary
    // --------------
    if(!strcmp(cmd, "viReadBinary()"))
    {
        ViSession Session;
        ViUInt32 Count, Return_Count;
        unsigned char *Buffer;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Count   = (ViUInt32)ViMexUtil_GetUIntValue(prhs[2]);
        Buffer  = (unsigned char *)mxCalloc(Count+1,sizeof(char));
        if(!Buffer) mexErrMsgTxt("Memory Allocation Failed.\n");
        Status = viRead(Session, Buffer, Count, &Return_Count);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutBinaryArray(Return_Count, Buffer, &plhs[1]);
        mxFree(Buffer);
        return 0;
    }
    // -------
    // viClear
    // -------
    if(!strcmp(cmd, "viClear()"))
    {
        ViSession Session;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,2);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Status = viClear(Session);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        return 0;
    }
    // ---------
    // viReadSTB
    // ---------
    if(!strcmp(cmd, "viReadSTB()"))
    {
        ViSession Session;
        ViUInt16 Status_Byte;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,2);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Status = viReadSTB(Session, &Status_Byte);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutUIntValue((unsigned)Status_Byte, &plhs[1]);
        return 0;
    }
    // ---------------
    // viAssertTrigger
    // ---------------
    if(!strcmp(cmd, "viAssertTrigger()"))
    {
        ViSession Session;
        ViUInt16 Protocol;
        char *ProtocolString;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        ProtocolString = ViMexUtil_GetStringValue(prhs[2], 0);

             if(!strcmp(ProtocolString, "VI_TRIG_PROT_DEFAULT")) Protocol = VI_TRIG_PROT_DEFAULT;
        else if(!strcmp(ProtocolString, "VI_TRIG_PROT_ON"))      Protocol = VI_TRIG_PROT_ON;
        else if(!strcmp(ProtocolString, "VI_TRIG_PROT_OFF"))     Protocol = VI_TRIG_PROT_OFF;
        else if(!strcmp(ProtocolString, "VI_TRIG_PROT_SYNC"))    Protocol = VI_TRIG_PROT_SYNC;  
        else 
        {
            mxFree(ProtocolString);
            mexErrMsgTxt("Unrecognized protocol definition");
        }
        Status = viAssertTrigger(Session, Protocol);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree(ProtocolString);
        return 0;
    }
    // ------------
    // viStatusDesc
    // ------------
    if(!strcmp(cmd, "viStatusDesc()"))
    {
        ViSession Session;
        char *Description;

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        Status  = (ViStatus) ViMexUtil_GetIntValue(prhs[2]);
        Description = (char *)mxCalloc(256,sizeof(char));
        Status = viStatusDesc(Session, Status, Description);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutString(Description, &plhs[1]);
        //mxFree(Description);
        return 0;
    }
    // --------------
    // viGetAttribute
    // --------------
    if(!strcmp(cmd, "viGetAttribute()"))
    {
        ViSession Session;
        ViAttr Attribute; char *AttrString;
        int Type; double dVal; char buffer[256];

        ViMexUtil_CheckParameters(nlhs,nrhs,2,3);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        AttrString = ViMexUtil_GetStringValue(prhs[2], 0);

        if(mexVISA_Get_VI_ATTR(AttrString, &Attribute, &Type)) {
            mxFree(AttrString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Attribute type.");
        }
        Status = viGetAttribute(Session, Attribute, ((void *)buffer));
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);

        if(Status >= 0)
        {
            switch(Type)
            {
                case TYPE_ViString    : break; // handle below
                case TYPE_ViStatus    : dVal = (double)(*((ViStatus    *)buffer)); break;
                case TYPE_ViBoolean   : dVal = (double)(*((ViBoolean   *)buffer)); break;
                case TYPE_ViInt8      : dVal = (double)(*((ViInt8      *)buffer)); break;
                case TYPE_ViInt16     : dVal = (double)(*((ViInt16     *)buffer)); break;
                case TYPE_ViInt32     : dVal = (double)(*((ViInt32     *)buffer)); break;
                case TYPE_ViUInt8     : dVal = (double)(*((ViUInt8     *)buffer)); break;
                case TYPE_ViUInt16    : dVal = (double)(*((ViUInt16    *)buffer)); break;
                case TYPE_ViUInt32    : dVal = (double)(*((ViUInt32    *)buffer)); break;
                case TYPE_ViSession   : dVal = (double)(*((ViSession   *)buffer)); break;
                case TYPE_ViBusAddress: dVal = (double)(*((ViBusAddress*)buffer)); break;
                case TYPE_ViBusSize   : dVal = (double)(*((ViBusSize   *)buffer)); break;
                case TYPE_ViVersion   : dVal = (double)(*((ViVersion   *)buffer)); break;
                case TYPE_ViAccessMode: dVal = (double)(*((ViAccessMode*)buffer)); break;
                case TYPE_ViEventType : dVal = (double)(*((ViEventType *)buffer)); break;
                case TYPE_ViJobId     : dVal = (double)(*((ViJobId     *)buffer)); break;
                default: 
                {
                    mxFree(AttrString);
                    mexErrMsgTxt("Unhandled case in mexVISA.c under viGetAttribute.");
                }
            }
            if(Type == TYPE_ViString) {
                ViMexUtil_PutString(buffer, &plhs[1]);
            }
            else
                ViMexUtil_PutRealValue(dVal, &plhs[1]);
        }
        else ViMexUtil_PutRealValue(-1.0, &plhs[1]);
        mxFree(AttrString);
        return 0;
    }
    // --------------
    // viSetAttribute
    // --------------
    if(!strcmp(cmd, "viSetAttribute()"))
    {
        ViSession Session;
        ViAttr Attribute; char *AttrString; 
        double dVal; int Type;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,4);
        Session = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        AttrString = ViMexUtil_GetStringValue(prhs[2], 0);

        if(mexVISA_Get_VI_ATTR(AttrString, &Attribute, &Type)) {
            mxFree(AttrString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Attribute type.");
        }
        if(Type != TYPE_ViString) {
            dVal = ViMexUtil_GetRealValue(prhs[3]);
        }
        switch(Type)
        {
            case TYPE_ViInt8:
            case TYPE_ViInt16:
            case TYPE_ViInt32:
            {
                ViAttrState attrState = (ViAttrState)((ViInt32)dVal);
                Status = viSetAttribute(Session, Attribute, attrState);
            }  break;
 
            case TYPE_ViStatus:
            case TYPE_ViBoolean:
            case TYPE_ViUInt8:
            case TYPE_ViUInt16:
            case TYPE_ViUInt32:
            case TYPE_ViSession:
            case TYPE_ViBusAddress:
            case TYPE_ViBusSize:
            case TYPE_ViVersion:
            case TYPE_ViAccessMode:
            case TYPE_ViEventType:
            case TYPE_ViJobId:
            {
                ViAttrState attrState = (ViAttrState)((ViUInt32)dVal);
                Status = viSetAttribute(Session, Attribute, attrState);
            }  break;

            case TYPE_ViString:
            {
                mxFree(AttrString);
                mexErrMsgTxt("Setting string attributes is not allowed.");
            }  break;

            default: 
            {
                mxFree(AttrString);
                mexErrMsgTxt("Unhandled case in mexVISA.c under viGetAttribute.");
            }
        }
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree(AttrString);
        return 0;
    }
    // -------------
    // viEnableEvent
    // -------------
    if(!strcmp(cmd, "viEnableEvent()"))
    {
        ViSession Session;
        ViEventType Event; char *EventString;
        ViUInt16 Mechanism;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,4);
        Session     = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        EventString = ViMexUtil_GetStringValue(prhs[2], 0);
        Mechanism   = (ViUInt16)ViMexUtil_GetIntValue(prhs[3]);

        if(Mechanism != 1) {
            mxFree(EventString);
            mexErrMsgTxt("EventMechanism must be VI_QUEUE = 1");
        }
        if(mexVISA_Get_VI_EVENT(EventString, &Event)) {
            mxFree(EventString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Event type.");
        }
        Status = viEnableEvent(Session, Event, Mechanism, VI_NULL);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree(EventString);
        return 0;
    }
    // ---------------------
    // viDisableEvent
    // ---------------------
    if(!strcmp(cmd, "viDisableEvent()"))
    {
        ViSession Session;
        ViEventType Event; char *EventString;
        ViUInt16 Mechanism;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,4);
        Session     = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        EventString = ViMexUtil_GetStringValue(prhs[2], 0);
        Mechanism   = (ViUInt16)ViMexUtil_GetIntValue(prhs[3]);

        if(Mechanism != 1) 
        {
            mxFree(EventString);
            mexErrMsgTxt("EventMechanism must be VI_QUEUE = 1");
        }
        if(mexVISA_Get_VI_EVENT(EventString, &Event)) 
        {
            mxFree(EventString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Event type.");
        }
        Status = viDisableEvent(Session, Event, Mechanism);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree(EventString);
        return 0;
    }
    // ---------------
    // viDiscardEvents
    // ---------------
    if(!strcmp(cmd, "viDiscardEvents()"))
    {
        ViSession Session;
        ViEventType Event; char *EventString;
        ViUInt16 Mechanism;

        ViMexUtil_CheckParameters(nlhs,nrhs,1,4);
        Session     = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        EventString = ViMexUtil_GetStringValue(prhs[2], 0);
        Mechanism   = (ViUInt16)ViMexUtil_GetIntValue(prhs[3]);

        if(Mechanism != 1) 
        {
            mxFree(EventString);
            mexErrMsgTxt("EventMechanism must be VI_QUEUE = 1");
        }
        if(mexVISA_Get_VI_EVENT(EventString, &Event)) 
        {
            mxFree(EventString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Event type.");
        }
        Status = viDiscardEvents(Session, Event, Mechanism);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        mxFree(EventString);
        return 0;
    }
    // -------------
    // viWaitOnEvent
    // -------------
    if(!strcmp(cmd, "viWaitOnEvent()"))
    {
        ViSession Session;
        ViEventType Event, OutEventType; char *EventString;
        ViEvent EventHandle;
        ViUInt32 Timeout;

        ViMexUtil_CheckParameters(nlhs,nrhs,3,4);
        Session     = (ViSession)ViMexUtil_GetUIntValue(prhs[1]);
        EventString = ViMexUtil_GetStringValue(prhs[2], 0);
        Timeout     = (ViUInt32)ViMexUtil_GetUIntValue(prhs[3]);

        if(mexVISA_Get_VI_EVENT(EventString, &Event)) 
        {
            mxFree(EventString);
            mexErrMsgTxt("Unrecognized/unsupported VISA Event type.");
        }
        Status = viWaitOnEvent(Session, Event, Timeout, &OutEventType, &EventHandle);
        ViMexUtil_PutIntValue((int)Status, &plhs[0]);
        ViMexUtil_PutUIntValue((unsigned)OutEventType, &plhs[1]);
        ViMexUtil_PutUIntValue((unsigned)EventHandle, &plhs[2]);
        mxFree(EventString);
        return 0;
    }
    // ******************************************************************
    // -------------------
    // No Command Found...
    // -------------------
    return -1;
}
Ejemplo n.º 10
0
int vxi11_send(VXI11_CLINK * clink, const char *cmd, size_t len)
{
#ifdef WIN32
	ViStatus status;
	char buf[256];
	unsigned char *send_cmd;
#else
	Device_WriteParms write_parms;
	char *send_cmd;
#endif
	size_t bytes_left = len;
	ssize_t write_count;

#ifdef WIN32
	send_cmd = (unsigned char *)malloc(len);
	if (!send_cmd) {
		return 1;
	}
	memcpy(send_cmd, cmd, len);

	while (bytes_left > 0) {
		status =
		    viWrite(clink->session, send_cmd + (len - bytes_left),
			    bytes_left, &write_count);
		if (status == VI_SUCCESS) {
			bytes_left -= write_count;
		} else {
			free(send_cmd);
			viStatusDesc(clink->session, status, buf);
			printf("%s\n", buf);
			return status;
		}
	}
#else
	send_cmd = (char *)malloc(len);
	if (!send_cmd) {
		return 1;
	}
	memcpy(send_cmd, cmd, len);

	write_parms.lid = clink->link->lid;
	write_parms.io_timeout = VXI11_DEFAULT_TIMEOUT;
	write_parms.lock_timeout = VXI11_DEFAULT_TIMEOUT;

/* We can only write (link->maxRecvSize) bytes at a time, so we sit in a loop,
 * writing a chunk at a time, until we're done. */

	do {
		Device_WriteResp write_resp;
		memset(&write_resp, 0, sizeof(write_resp));

		if (bytes_left <= clink->link->maxRecvSize) {
			write_parms.flags = 8;
			write_parms.data.data_len = bytes_left;
		} else {
			write_parms.flags = 0;
			/* We need to check that maxRecvSize is a sane value (ie >0). Believe it
			 * or not, on some versions of Agilent Infiniium scope firmware the scope
			 * returned "0", which breaks Rule B.6.3 of the VXI-11 protocol. Nevertheless
			 * we need to catch this, otherwise the program just hangs. */
			if (clink->link->maxRecvSize > 0) {
				write_parms.data.data_len =
				    clink->link->maxRecvSize;
			} else {
				write_parms.data.data_len = 4096;	/* pretty much anything should be able to cope with 4kB */
			}
		}
		write_parms.data.data_val = send_cmd + (len - bytes_left);

		if (device_write_1(&write_parms, &write_resp, clink->client) !=
		    RPC_SUCCESS) {
			free(send_cmd);
			return -VXI11_NULL_WRITE_RESP;	/* The instrument did not acknowledge the write, just completely
							   dropped it. There was no vxi11 comms error as such, the 
							   instrument is just being rude. Usually occurs when the instrument
							   is busy. If we don't check this first, then the following 
							   line causes a seg fault */
		}
		if (write_resp.error != 0) {
			printf("vxi11_user: write error: %d\n",
			       (int)write_resp.error);
			free(send_cmd);
			return -(write_resp.error);
		}
		bytes_left -= write_resp.size;
	} while (bytes_left > 0);
#endif
	free(send_cmd);

	return 0;
}
Ejemplo n.º 11
0
int main(void)
{
    /*
     * First we must call viOpenDefaultRM to get the manager
     * handle.  We will store this handle in defaultRM.
     */
    status=viOpenDefaultRM (&defaultRM);
    if (status < VI_SUCCESS)
    {
        printf ("Could not open a session to the VISA Resource Manager!\n");
        exit (EXIT_FAILURE);
    }

    /*
     * Now we will open a VISA session to the serial port (COM1).
     * We must use the handle from viOpenDefaultRM and we must
     * also use a string that indicates which instrument to open.  This
     * is called the instrument descriptor.  The format for this string
     * can be found in the function panel by right clicking on the
     * descriptor parameter. After opening a session to the
     * device, we will get a handle to the instrument which we
     * will use in later VISA functions.  The AccessMode and Timeout
     * parameters in this function are reserved for future
     * functionality.  These two parameters are given the value VI_NULL.
     */
    status = viOpen (defaultRM, "ASRL1::INSTR", VI_NULL, VI_NULL, &instr);
    if (status < VI_SUCCESS)
    {
        printf ("Cannot open a session to the device.\n");
        goto Close;
    }

    /*
     * At this point we now have a session open to the serial instrument.
     * Now we need to configure the serial port:
     */

    /* Set the timeout to 5 seconds (5000 milliseconds). */
    status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 5000);

    /* Set the baud rate to 4800 (default is 9600). */
    status = viSetAttribute (instr, VI_ATTR_ASRL_BAUD, 4800);

    /* Set the number of data bits contained in each frame (from 5 to 8).
     * The data bits for  each frame are located in the low-order bits of
     * every byte stored in memory.
     */
    status = viSetAttribute (instr, VI_ATTR_ASRL_DATA_BITS, 8);

    /* Specify parity. Options:
     * VI_ASRL_PAR_NONE  - No parity bit exists,
     * VI_ASRL_PAR_ODD   - Odd parity should be used,
     * VI_ASRL_PAR_EVEN  - Even parity should be used,
     * VI_ASRL_PAR_MARK  - Parity bit exists and is always 1,
     * VI_ASRL_PAR_SPACE - Parity bit exists and is always 0.
     */
    status = viSetAttribute (instr, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE);

    /* Specify stop bit. Options:
     * VI_ASRL_STOP_ONE   - 1 stop bit is used per frame,
     * VI_ASRL_STOP_ONE_5 - 1.5 stop bits are used per frame,
     * VI_ASRL_STOP_TWO   - 2 stop bits are used per frame.
     */
    status = viSetAttribute (instr, VI_ATTR_ASRL_STOP_BITS, VI_ASRL_STOP_ONE);

    /* Specify that the read operation should terminate when a termination
     * character is received.
     */
    status = viSetAttribute (instr, VI_ATTR_TERMCHAR_EN, VI_TRUE);

    /* Set the termination character to 0xA
     */
    status = viSetAttribute (instr, VI_ATTR_TERMCHAR, 0xA);


    /* We will use the viWrite function to send the device the string "*IDN?\n",
     * asking for the device's identification.
    */
    strcpy (stringinput,"*IDN?\n");
    status = viWrite (instr, (ViBuf)stringinput, strlen(stringinput), &writeCount);
    if (status < VI_SUCCESS)
    {
        printf ("Error writing to the device.\n");
        goto Close;
    }

    /*
    * Now we will attempt to read back a response from the device to
    * the identification query that was sent.  We will use the viRead
    * function to acquire the data.  We will try to read back 100 bytes.
    * This function will stop reading if it finds the termination character
    * before it reads 100 bytes.
    * After the data has been read the response is displayed.
    */
    status = viRead (instr, buffer, 100, &retCount);
    if (status < VI_SUCCESS)
    {
        printf ("Error reading a response from the device.\n");
    }
    else
    {
        printf ("\nData read: %*s\n", retCount, buffer);
    }


    /*
     * Now we will close the session to the instrument using
     * viClose. This operation frees all system resources.
     */
Close:
    status = viClose (instr);
    status = viClose (defaultRM);
    printf ("Hit enter to continue.");
    fflush (stdin);
    getchar();

    return 0;
}
Ejemplo n.º 12
0
int main (void)
{
   /*
    *  First we open a session to the VISA resource manager.  We are
    *  returned a handle to the resource manager session that we must
    *  use to open sessions to specific instruments.
    */
   status = viOpenDefaultRM (&defaultRM);
   if (status < VI_SUCCESS)
   {
      printf("Could not open a session to the VISA Resource Manager!\n");
      exit (EXIT_FAILURE);
   }
   
   /*
    *  Next we use the resource manager handle to open a session to a
    *  GPIB instrument at device 2.  A handle to this session is
    *  returned in the handle inst.  Please consult the NI-VISA User Manual 
    *  for the syntax for using other instruments.
    */
   status = viOpen (defaultRM, "GPIB::2::INSTR", VI_NULL, VI_NULL, &inst);
   
   /*
    *  Now we install the handler for asynchronous i/o completion events.
    *  To install the handler, we must pass our instrument session, the type of
    *  event to handle, the handler function name and a user handle
    *  which acts as a handle to the handler function.
    */
   status = viInstallHandler (inst, VI_EVENT_IO_COMPLETION, AsyncHandler, uhandle);
   
   /*  Now we must actually enable the I/O completion event so that our
    *  handler will see the events.  Note, one of the parameters is 
    *  VI_HNDLR indicating that we want the events to be handled by
    *  an asynchronous event handler.  The alternate mechanism for handling
    *  events is to queue them and read events off of the queue when
    *  you want to check them in your program.
    */
   status = viEnableEvent (inst, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);

   /* 
    *  Now the VISA write command is used to send a request to the
    *  instrument to generate a sine wave.  This demonstrates the 
    *   synchronous read operation that blocks the application until viRead()
    *   returns.  Note that the command syntax is instrument specific.
    */

   /*
    * Here you specify which string you wish to send to your instrument.
    * The command listed below is device specific. You may have to change
    * command to accommodate your instrument.
    */
   strcpy(stringinput,"SOUR:FUNC SIN; SENS: DATA?\n");
   BytesToWrite = strlen(stringinput);
   status = viWrite (inst, (ViBuf)stringinput,BytesToWrite, &rcount);

   /*
    *  Next the asynchronous read command is called to read back the 
    *  date from the instrument.  Immediately after this is called
    *  the program goes into a loop which will terminate
    *  on an i/o completion event triggering the asynchronous callback.
    *  Note that the asynchronous read command returns a job id that is
    *  a handle to the asynchronous command.  We can use this handle
    *  to terminate the read if too much time has passed.
    */
   status = viReadAsync (inst, data, 1024, &job);
   
   printf("\n\nHit enter to continue.");
   letter = getchar();

   /* 
    *  If the asynchronous callback was called and the flag was set
    *  we print out the returned data otherwise we terminate the
    *  asynchronous job.
    */
   if (stopflag == VI_TRUE)
   {
      /* RtCount was set in the callback */
       printf ("Here is the data:  %*s", RtCount, data);
   }
   else
   {
      status = viTerminate (inst, VI_NULL, job);  
      printf("The asynchronous read did not complete.\n");
   }

    printf ("\nHit enter to continue.");
    fflush(stdin);
    getchar();

   /*
    *  Now we close the instrument session and the resource manager
    *  session to free up resources.
    */
   status = viClose(inst);
   status = viClose(defaultRM);
   
   return 0;
}