Jump to content

dadreamer

Members
  • Posts

    350
  • Joined

  • Last visited

  • Days Won

    34

Posts posted by dadreamer

  1. 5 minutes ago, Rolf Kalbermatter said:

    I have absolutely no idea what you mean when you say that no RealHandle data is visible. As far as that log looks, no event is ever sent. This could be happening if your cbState boolean was never set to true.

    If I got it right, he says that on one run of the VI the events are being sent OK, but on another run (after he stops the VI and runs it again) no events is sent regardless of the button being pressed.

  2. 16 minutes ago, Rolf Kalbermatter said:

    And ohhhhhhhhhhhh my God!!!!!! You defined the event data cluster totally wrong. That "handle" is not an integer value. It is a full and complete LabVIEW byte array!! It shouldn't cause your crash .. at least not immediately! But since you don't tell LabVIEW that it is a handle it can't manage its memory. Meaning with every message that is sent to your event loop, you simply leak that handle each time. That will drive your application into out of memory in no time, just as the incorrect handling of the cbState boolean did. You not only were leaking handles when the cbState boolean was false but with EVERY single callback!!!!!!!!

    This explains why he was seeing unstable behaviour from run to run. This is what happens, when two advisers are trying to help simultaneously. 😃 I often prefer a slightly different way to transfer data from the callback. I post a pointer to data and in LabVIEW I read it out with MoveBlock (because I like to see a minimalistic external code and do most of the coding in LV instead).

    By the way, I kind of think posting an U32/U64 number might work, if DSDisposeHandle would be moved to the LV Event frame from the callback code and in that frame MoveBlock + DSDisposeHandle would be called.

  3. 10 minutes ago, alvise said:

    Why was the "Datacallback" function in the picture above created, is it necessary to get information using that function?

    Because you exported it. Leave it as is.

    11 minutes ago, alvise said:

    Now I'm trying to figure out how can I send the true-falsa flag.

    Take a boolean button with Switch when released mechanical action and create a new event frame "Value Change" with that button inside. Put a CLFN with SetCbState call, add one parameter in its settings with Adapt to Type -> Handles by Values data type and format. Wire your button to the CLFN. Now you're controlling the callback event flow.

  4. 1 hour ago, alvise said:

    -Boolean variable switch must be created inside the  dll file and it is necessary to send a value (1-0) to it from the labview, right?

    Yes, something like this:

    extern "C" __declspec(dllexport) void __cdecl SetCbState(LVBoolean *state);
    // ...
    LVBoolean cbState = LVBooleanFalse;
    // ...
    extern "C" __declspec(dllexport) void __cdecl SetCbState(LVBoolean *state)
    {
      cbState = *state;
    }
    // ...
    extern "C" __declspec(dllexport) void __stdcall DataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, DWORD dwUser)
    {
      if (cbState==LVBooleanTrue){
        // callback code
      }
    }

     

  5. I see two main issues here.

    1. Too many events coming - your program just can't handle them all at once (you were already warned about that). How can you deal with it? You decide. I often use a boolean variable switch for that. You may consider something else.
    2. You want to have UninstallStandardCallback function to stop posting into LV, when the loop finishes or no matter what. Now your DLL seems to be trying to post even when the main VI finishes plus the event queue is full with unprocessed events. Try to implement that function on your own, it's not that complex.
  6. Don't pass Event Registration Refnum to the DLL! Pass Event Refnum.

    2022-05-30_21-21-00.jpg.14123817a2e2e69c31a2c671387132ba.jpg

    Also you need to pass this wire through all the frames of the structure, because when the loop will be stopped, Destroy User Event VI will be confused getting invalid refnum. Or wire it around the loop instead. Oh, first do unregister, second do destroy, in that order.

    Moreover your calling convention is wrong!

    Plus you didn't insert an User Event frame where you're going to catch your events.

    ! Reg Events, Unregister and Destroy should be either outside the loop completely or inside the "Start" (for Reg Events) and "Stop" (for Unregister and Destroy) frames ! Otherwise the UE will be created and freed on each iteration.

  7. You better rework Rolf's example a bit to not reinvent the wheel.

    1. Remove this as no longer needed.

    2022-05-30_17-39-26.jpg.b50b1db30f05158a0e76a0f31591136a.jpg

    2. Remove this Case Structure and that VI Ref terminal. Pass NULL (0) as hPlayWnd.

    2022-05-30_17-40-46.jpg.7af936e80d64a4f473dcf2aad847fe44.jpg

    3. Before the While loop create User Event of cluster type, containing these three fields:

    typedef struct
    {
    	LONG realHandle;
    	DWORD dataType;
    	LVByteArrayHdl handle;
    } LVEventData;

    You should set handle type as U32, if you're working in 32-bit LabVIEW only, or U64 in all other cases.

    4. In "Start" frame of the Event Structure right after Start.vi put CLFN with InstallStandardCallback function call, set it properly and pass two parameters to it - lSession from Start.vi and your fresh UE reference.

    2022-05-30_17-57-20.jpg.b21f18075f7cd71e13e31fc22f79a994.jpg

    5. Activate Dynamic Events terminals on your Event Structure and set it to accept your User Event. Also here register for events and pass the event refnum to the right terminal.

    6. In "Stop" frame of the Event Structure unregister for events.

    7. After the loop destroy the UE.

    Now after running your program and pressing "Connect" + "Start" buttons you should receive some events (if no mistakes made). Check Event Inspector window to make sure.

  8. 10 minutes ago, alvise said:

    There is no problem here, is there?

    There is! Alter the function declarations and add these prototypes.

    extern "C" __declspec(dllexport) void __stdcall DataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, DWORD dwUser);
    extern "C" __declspec(dllexport) BOOL __cdecl InstallStandardCallback(LONG lRealHandle, LVUserEventRef *refnum);
  9. 11 minutes ago, alvise said:

    To export functions like here, it is necessary to make changes in the code, right?

    Yes. Your code does already have all that necessary stuff for export:

    #define LibAPI(retval)       __declspec(dllexport) EXTERNC retval __cdecl
    #define Callback(retval)     __declspec(dllexport) EXTERNC retval __stdcall
    // ...
    Callback(void) DataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, DWORD dwUser)
    {
    // ...
    }
    
    LibAPI(BOOL) InstallStandardCallback(LONG lRealHandle, LVUserEventRef *refnum)
    {
    // ...
    }

    Hence it's rather odd that nothing is exported. As you're using __declspec(dllexport), no need to use .def file - you may disable it in the settings.

    Maybe your functions are exported, but have decorated names... In this case LabVIEW won't show them. Could you check full export table with some tool like Dependency Walker or similar?

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.