LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Visa callback on \n on USB

I can talk to a serial device using Visa, either doing direct read/write, or setting up a handler to trigger on a \n TERMCHAR.

I can also talk to a similar device that uses USB using direct read/writes.

But all my attempts at getting a callback on a TERMCHAR have failed. Am I missing something obvious or is it that devious ?

I have also tried viWrite/ReadAsync and it does trigger on the write, but not on the read.

Simplified code here:

 

#define USE_CALLBACK // (or not)

viOpenDefaultRM (&defaultRM);
viOpen (defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr);

viGetAttribute (instr, VI_ATTR_INTF_TYPE, &Attr);
if (Attr==VI_INTF_ASRL) {
    viSetAttribute (instr, VI_ATTR_TERMCHAR, '\n');
    viSetAttribute (instr, VI_ATTR_TERMCHAR_EN, VI_TRUE);
    viSetAttribute (instr, VI_ATTR_ASRL_END_IN, VI_ASRL_END_TERMCHAR);
    viSetAttribute (instr, VI_ATTR_ASRL_END_OUT, VI_ASRL_END_TERMCHAR);
    #ifdef USE_CALLBACK
        viInstallHandler(instr, VI_EVENT_ASRL_TERMCHAR, cbs_Reply, NULL);
        viEnableEvent (instr, VI_EVENT_ASRL_TERMCHAR, VI_HNDLR, VI_NULL);
    #endif
} else if (Attr==VI_INTF_USB) {\
    viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, 0);/* Without callback: both 0 and 1 work */
    viSetAttribute(instr, VI_ATTR_TERMCHAR, '\n');
    viSetAttribute(instr, VI_ATTR_SEND_END_EN, 1);
    #ifdef USE_CALLBACK
       viInstallHandler(instr, VI_EVENT_IO_COMPLETION, cbs_Reply, NULL));
        viEnableEvent (instr, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL);
       viInstallHandler(instr, VI_EVENT_SERVICE_REQ, cbs_Reply, NULL);
       viEnableEvent (instr, VI_EVENT_SERVICE_REQ, VI_HNDLR, VI_NULL);
       // Also tried other event types
    #endif
}



ViStatus _VI_FUNCH cbs_Reply(ViSession vi, ViEventType eventType, ViEvent event, ViAddr userHandle) {
    char Reply[256], Str[256];
    int Nb=0;
    sprintf(Str, "EventType:0x%X, Event:0x%X, Handle:0x%p\n", eventType, event, userHandle);
    viRead(vi, (unsigned char*)Reply, 255, &Nb);
    Reply[Nb]='\0'; // Otherwise the string is not terminated
    return 0;
}



// Sending
char Send[]="*IDN?";
viWrite(instr, (unsigned char*)Send, strlen(Send), &BufSize);
#ifndef USE_CALLBACK
    Delay(0.1);
    cbs_Reply(instr, 0, 0, NULL); // Direct call to read the buffer
#endif

 
0 Kudos
Message 1 of 1
(183 Views)