/* * The handler function. The instrument session, the type of event, and a * handle to the event are passed to the function along with a user handle * which is basically a label that could be used to reference the handler. * The only thing done in the handler is to set a flag that allows the * program to finish. Always return VI_SUCCESS from your handler. */ ViStatus _VI_FUNCH AsyncHandler(ViSession vi, ViEventType etype, ViEvent event, ViAddr userHandle) { stopflag = VI_TRUE; Sessionparam = vi; EventTypeparam = etype; Addressparam = userHandle; viGetAttribute (event, VI_ATTR_STATUS, &StatusSession); viGetAttribute (event, VI_ATTR_RET_COUNT, &RtCount); return VI_SUCCESS; }
int32_t HAL_GetSerialBytesReceived(int32_t port, int32_t* status) { int32_t bytes = 0; *status = viGetAttribute(m_portHandle[port], VI_ATTR_ASRL_AVAIL_NUM, &bytes); if (*status > 0) *status = 0; return bytes; }
int WaitForOperationCompletion( int sessionHandle , double lfTimeout , double lfLowLevelTimeout ) { double lfStartTime = 0.0, lfCurrentTime = 0.0; int iOpcStatus = 0; char szReadFeedback[LOW_STRING] = {0}; int count = 0; int iTimeout = 0; int iError = 0; viGetAttribute ( sessionHandle , VI_ATTR_TMO_VALUE , &iTimeout ); SetBreakOnLibraryErrors (0); viSetAttribute ( sessionHandle , VI_ATTR_TMO_VALUE , (lfLowLevelTimeout*1E3) ); GetCurrentDateTime(&lfStartTime); do { viPrintf( sessionHandle , "*OPC?\n" ); viRead( sessionHandle, szReadFeedback , (LOW_STRING-1) , &count ); iOpcStatus = atoi(szReadFeedback); if ( iOpcStatus ) break; GetCurrentDateTime(&lfCurrentTime); } while((lfCurrentTime-lfStartTime) < lfTimeout ); if ( iOpcStatus ) { viPrintf( sessionHandle , ":SYST:ERR?\n" ); SetBreakOnLibraryErrors (0); viRead( sessionHandle, szReadFeedback , LOW_STRING , &count ); SetBreakOnLibraryErrors (1); iError = atoi(szReadFeedback); if ( iError == -420 ) { viPrintf( sessionHandle , "*CLS\n" ); } } viSetAttribute ( sessionHandle , VI_ATTR_TMO_VALUE , iTimeout ); SetBreakOnLibraryErrors (1); return iOpcStatus; }
/** * Get the number of bytes currently available to read from the serial port. * * @return The number of bytes available to read. */ INT32 SerialPort::GetBytesReceived() { INT32 bytes = 0; ViStatus status = viGetAttribute(m_portHandle, VI_ATTR_ASRL_AVAIL_NUM, &bytes); wpi_assertCleanStatus(status); return bytes; }
/** * Get the number of bytes currently available to read from the serial port. * * @return The number of bytes available to read. */ INT32 SerialPort::GetBytesReceived() { INT32 bytes = 0; if (!m_consoleModeEnabled) { ViStatus status = viGetAttribute(m_portHandle, VI_ATTR_ASRL_AVAIL_NUM, &bytes); wpi_assertCleanStatus(status); } return bytes; }
/** * Get the number of bytes currently available to read from the serial port. * * @return The number of bytes available to read. */ INT32 SerialPort::GetBytesReceived() { INT32 bytes = 0; if (!m_consoleModeEnabled) { ViStatus localStatus = viGetAttribute(m_portHandle, VI_ATTR_ASRL_AVAIL_NUM, &bytes); wpi_setError(localStatus); } return bytes; }
/** * Clears UART receive buffer. * * @param[in] port UART port to access * @return int32_t Error/success status */ int32_t Uart_Clear(MyRio_Uart* port) { int32_t status = VI_SUCCESS; int32_t oldTimeout = 0; uint8_t read[0xFF] = {0}; ViUInt32 nRead = 0; /* * Get timeout. */ status = viGetAttribute(port->session, VI_ATTR_TMO_VALUE, &oldTimeout); if (status < VI_SUCCESS) { return status; } /* * Set timeout to 1 ms. */ status = viSetAttribute(port->session, VI_ATTR_TMO_VALUE, 1); if (status < VI_SUCCESS) { return status; } /* * Read input buffer, up to 255 bytes at a time, until timeout occurs. */ while((status = viRead(port->session, (ViBuf)read, (ViUInt32)0xFF, &nRead)) >= VI_SUCCESS); if (status == VI_ERROR_TMO) { /* * Timeout expected. */ status = VI_SUCCESS; } /* * Reset timeout to previous value. */ status = viSetAttribute(port->session, VI_ATTR_TMO_VALUE, oldTimeout); if (status < VI_SUCCESS) { return status; } return status; }
int Delay_LastComplete( int handle ) { double lfCurrentTime = 0.0, lfTimeOut = 10.0, lfStartTime = 0.0; int iOpcValue = 0; int status = 0; ViAttrState attrValue = 0; if ( viGetAttribute ( handle , VI_ATTR_TMO_VALUE , &attrValue ) == 0 ) lfTimeOut = attrValue / 1E3; GetCurrentDateTime ( &lfStartTime ); GetCurrentDateTime ( &lfCurrentTime ); while(( lfCurrentTime - lfStartTime ) < lfTimeOut ) { GetCurrentDateTime ( &lfCurrentTime ); status = viPrintf ( handle , "*OPC?\n"); if ( status ) continue; Delay ( 0.2 ); status = viScanf ( handle , "%d" , &iOpcValue); if ( status ) continue; if ( iOpcValue == 1 ) return 0; } return -1; }
int main() { ViSession rmgr; ViStatus rc = viGetDefaultRM(&rmgr); if(rc != VI_SUCCESS) return 1; ViSession rmgr_session; rc = viGetAttribute(rmgr, VI_ATTR_RM_SESSION, &rmgr_session); if(rc != VI_SUCCESS) return 1; if(rmgr_session != VI_NULL) return 1; return 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; }