Results 1 to 7 of 7

Thread: Problem getting libusb datarecieved event to send signal

  1. #1
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Problem getting libusb datarecieved event to send signal

    hi the title may busbcom = new UsbCom();
    connect(usbcom, SIGNAL(error(QString)), SLOT(usbError(QString)));
    usbcom->start();e a little weird but i wasnt sure what to call it.

    il try to explain what im trying to do.

    i am trying to get data from the usb device with libusb_handle_events function
    in my main class i use this code to start the thread for usb communication:
    Qt Code:
    1. usbcom = new UsbCom();
    2. connect(usbcom, SIGNAL(error(QString)), SLOT(usbError(QString)));
    3. connect(usbcom, SIGNAL(dataRecieved(QString)), SLOT(usbDataRecieved(QString)));
    4. usbcom->start();
    To copy to clipboard, switch view to plain text mode 

    UsbCom class:

    Qt Code:
    1. #include "usbcom.h"
    2. #include <QtGui>
    3. #include "/usr/local/include/libusb-1.0/libusb.h"
    4. #include "dataemit.h"
    5. #include <QApplication>
    6.  
    7. #define idVENDOR 0x0483
    8. #define idPRODUCT 0x5750
    9. #define EP_INTR_OUT ( 1 | LIBUSB_ENDPOINT_OUT)
    10. #define EP_INTR_IN ( 1 | LIBUSB_ENDPOINT_IN )
    11.  
    12. UsbCom::UsbCom()
    13. {
    14. }
    15.  
    16. void UsbCom::run()
    17. {
    18. int r;
    19. int transf;
    20. unsigned char data[2]={2,5};
    21. r = libusb_init(NULL);
    22. if (r < 0)
    23. {
    24. emit error("Failed to initialize USB!");
    25. exit(1);
    26. }
    27.  
    28. libusb_device_handle *devh = libusb_open_device_with_vid_pid(NULL, idVENDOR, idPRODUCT);
    29.  
    30. if(!devh)
    31. {
    32. emit error("Could not locate device");
    33. exit();
    34. }
    35.  
    36. r = libusb_kernel_driver_active (devh, 0);
    37.  
    38. if (r != 0)
    39. {
    40. if(r==1)
    41. {
    42. r = libusb_detach_kernel_driver(devh, 0);
    43. if (r > 0)
    44. {
    45. emit error("some other random error:"+ QString::number(r));
    46. exit(1);
    47. }
    48. }
    49. }
    50.  
    51. r = libusb_claim_interface(devh, 0);
    52.  
    53. if (r < 0)
    54. {
    55. emit error("usb_claim_interface error"+ QString::number(r));
    56. }
    57.  
    58. r = libusb_interrupt_transfer(devh, EP_INTR_OUT, data, sizeof(data), &transf,50);
    59.  
    60. emit error("did it work? r:" + QString::number(r));
    61.  
    62. irq_transfer = libusb_alloc_transfer(0);
    63. unsigned char irqbuf[2];
    64.  
    65. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR_IN, irqbuf,sizeof(irqbuf), ep_irq_in_cb, NULL, 0);
    66. r = libusb_submit_transfer(irq_transfer);
    67.  
    68. while(1)
    69. {
    70. r = libusb_handle_events(NULL);
    71. }
    72. }
    73.  
    74. void UsbCom::dataRecieve()
    75. {
    76. }
    77.  
    78. void ep_irq_in_cb(libusb_transfer *transfer)
    79. {
    80. if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
    81. {
    82. // fprintf(stderr, "uncompleted transter\n");
    83. }
    84.  
    85. //[COLOR="red"]dont know what code goes here, try to call UsbCom::DataRecieve[/COLOR]
    86.  
    87. if (libusb_submit_transfer(transfer) < 0){
    88. fprintf(stderr,"couldnot resubmit irq \n");
    89. exit(1);
    90. }
    91.  
    92. }
    To copy to clipboard, switch view to plain text mode 

    the problem is that as you can see the
    void ep_irq_in_cb(libusb_transfer *transfer) function is not in the UsbCom class. but when the code executes i need to call the UsbCom:ataRecieve() function so that it connects to the slot in my main thread.

    if anyone can help me i would be very gratefull.

    Sisco

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting libusb datarecieved event to send signal

    what is the protype of libusb_fill_interrupt_transfer()?
    Usually call back set functions take also a 'user param' to allow access to a user class.
    What is the 'NULL' parameter?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    sisco (3rd December 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Problem getting libusb datarecieved event to send signal

    Quote Originally Posted by high_flyer View Post
    what is the protype of libusb_fill_interrupt_transfer()?
    Usually call back set functions take also a 'user param' to allow access to a user class.
    What is the 'NULL' parameter?
    from libusb documentation:

    Qt Code:
    1. static void libusb_fill_interrupt_transfer ( struct libusb_transfer * transfer,
    2. libusb_device_handle * dev_handle,
    3. unsigned char endpoint,
    4. unsigned char * buffer,
    5. int length,
    6. libusb_transfer_cb_fn callback,
    7. void * user_data,
    8. unsigned int timeout
    9. ) [inline, static]
    10.  
    11. Helper function to populate the required libusb_transfer fields for an interrupt transfer.
    12.  
    13. Parameters:
    14. transfer the transfer to populate
    15. dev_handle handle of the device that will handle the transfer
    16. endpoint address of the endpoint where this transfer will be sent
    17. buffer data buffer
    18. length length of data buffer
    19. callback callback function to be invoked on transfer completion
    20. user_data user data to pass to callback function
    21. timeout timeout for the transfer in milliseconds
    To copy to clipboard, switch view to plain text mode 

    the NULL parameter is the user_data param i thougnt since i didnt have anything i wanted to send with it i just used NULL.

  5. #4
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Problem getting libusb datarecieved event to send signal

    after some searching on the web i tried this:

    Qt Code:
    1. irq_transfer = libusb_alloc_transfer(0);
    2. unsigned char irqbuf[2];
    3. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR_IN, irqbuf,sizeof(irqbuf), ep_irq_in_cb, &a, 0);
    4. //libusb_fill_control_transfer(irq_transfer, devh, irqbuf, ep_irq_in_cb, NULL,0);
    5. r = libusb_submit_transfer(irq_transfer);
    6.  
    7. while(1)
    8. {
    9. if (a == 1)
    10. {
    11. emit dataRecieved("o.o");
    12. a = 0;
    13. }
    14. r = libusb_handle_events(NULL);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ep_irq_in_cb(libusb_transfer *transfer)
    2. {
    3. if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
    4. {
    5. // fprintf(stderr, "uncompleted transter\n");
    6. }
    7. else
    8. {
    9.  
    10. int *data = transfer->user_data;
    11. *data = 1;
    12. }
    13. if (libusb_submit_transfer(transfer) < 0){
    14. fprintf(stderr,"could not resubmit irq \n");
    15. exit(1);
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    wich gave me this error:

    Qt Code:
    1. /home/sisco/Documents/School/Project 3/Project Software/Project/usbcom.cpp:93: error: invalid conversion from ‘void*’ to ‘int*’
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting libusb datarecieved event to send signal

    the NULL parameter is the user_data param i thougnt since i didnt have anything i wanted to send with it i just used NULL.
    Read my last post again.
    As you can see, my prediction was correct, and the parameter was user data.
    You should change the call back function to take the user data parameter too, and then you can pass your Usb class pointer to it, and call the Usb object methods in it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    sisco (3rd December 2009)

  8. #6
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: Problem getting libusb datarecieved event to send signal

    yes, you were right fixed it now the way i posted before. i just had to cast the user data to an int.

    thank you verry much.

    sisco

  9. #7
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting libusb datarecieved event to send signal

    hi every body,
    I have a questoin about the libusb, libusb_fill_iso_transfer function.
    What is the libusb_transfer_cb_fn callback, field in this function?
    Is it a function I myself should write it or it is default function and there is no need to write it?

    tanks
    havij000

Similar Threads

  1. send mail from qt app problem
    By cutie.monkey in forum Qt Programming
    Replies: 12
    Last Post: 30th September 2010, 20:31
  2. problem with emiting a signal
    By msmihai in forum Newbie
    Replies: 2
    Last Post: 3rd January 2009, 15:32
  3. Possible signal mapper problem
    By MarkoSan in forum Qt Programming
    Replies: 13
    Last Post: 25th January 2008, 14:11
  4. Replies: 1
    Last Post: 16th October 2007, 23:41
  5. Replies: 3
    Last Post: 15th April 2007, 20:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.