Results 1 to 8 of 8

Thread: Program crashes after sending data over libusb/Qthread for 3d time

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    4

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    im sorry, i am still very new to this.
    i have read about QMutex and QWaitCondition but i cant seem to figure out how to implement this in my code.

    i tried different aproaches:

    Qt Code:
    1. void UsbCom::run()
    2. {
    3. int r;
    4. r = libusb_init(NULL);
    5. send = false;
    6. if (r < 0)
    7. {
    8. emit error("Failed to initialize USB!");
    9. }
    10. devh = libusb_open_device_with_vid_pid(NULL, idVENDOR, idPRODUCT);
    11. if(!devh)
    12. {
    13. emit error("Could not locate device");
    14. }
    15. else
    16. {
    17. r = libusb_kernel_driver_active (devh, 0);
    18. if (r != 0)
    19. {
    20. if(r==1)
    21. {
    22. r = libusb_detach_kernel_driver(devh, 0);
    23. if (r > 0)
    24. {
    25. emit error("some other random error:"+ QString::number(r));
    26. }
    27. }
    28. }
    29. r = libusb_claim_interface(devh, 0);
    30. if (r < 0)
    31. {
    32. emit error("usb_claim_interface error"+ QString::number(r));
    33. }
    34. int a = 0;
    35. unsigned char irqbuf[64];
    36. irq_transfer = libusb_alloc_transfer(0);
    37. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR_IN, irqbuf,sizeof(irqbuf), ep_irq_in_cb, &a, 0);
    38. r = libusb_submit_transfer(irq_transfer);
    39. while(1)
    40. {
    41. wait.wait(mutex); //QMutex *mutex
    42. if(send)
    43. {
    44. sendData();
    45. }
    46. if (a == 1)
    47. {
    48. emit dataRecieved(irqbuf);
    49. a = 0;
    50. }
    51. r = libusb_handle_events(NULL);
    52. }
    53. }
    54. libusb_close(devh);
    55. }
    56.  
    57. void UsbCom::sendData()
    58. {
    59. int r,transf;
    60. a.append((const char*)data);
    61. r = libusb_interrupt_transfer(devh, EP_INTR_OUT, data, a.length(),&transf,1000);
    62. if (r == 0)
    63. {
    64. emit error("Configuration sent");
    65. }
    66. send = false;
    67. }
    68.  
    69. void ep_irq_in_cb(libusb_transfer *transfer)
    70. {
    71. if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
    72. {
    73. fprintf(stderr, "uncompleted transter\n");
    74. }
    75. else
    76. {
    77. int *data = static_cast<int*>(transfer->user_data);
    78. *data = 1;
    79. }
    80.  
    81. if (libusb_submit_transfer(transfer) < 0)
    82. {
    83. fprintf(stderr,"could not resubmit irq \n");
    84. //exit(1);
    85. }
    86. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. usbcom->mutex = &mutex
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Project::sendSettings()
    2. {
    3. unsigned char *b;
    4. int r, transf;
    5. if (ui->checkBox_RPM->isChecked())
    6. {
    7. sprintf((char*)b,"*1 %d %d %d",ui->spinBox_RPM->value(), ui->spinBox_KOPPEL->value(), ui->spinBox_over->value());
    8. a.append((const char*)b);
    9. usbcom->data = b;
    10. usbcom->send = true;
    11. }
    12. else if (ui->checkBox_TEMP->isChecked())
    13. {
    14. sprintf((char*)b,"*0 %d %d", (char)ui->spinBox_TEMP->value(), (char)ui->spinBox_over->value());
    15. a.append((const char*)b);
    16. usbcom->data = b;
    17. usbcom->send = true;
    18. }
    19. else
    20. {
    21. box.setText("Must at least check one option!");
    22. box.exec();
    23. }
    24. usbcom->wait.wakeAll();
    To copy to clipboard, switch view to plain text mode 

    this compiles fine but still gives a seg fault.
    could you please explain me a little on how i should implement this?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    Learn to use QMutex. There is a nice example of protecting a shared variable in its docs.

    By the way, there is no reason for you to use threads here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    how would i get this so i dont need to use a thread.
    right now the thread has a "while(1)" loop to check for a completed transfer.
    i cant see a way to have this running without hanging the main thread.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Program crashes after sending data over libusb/Qthread for 3d time

    Quote Originally Posted by sisco View Post
    how would i get this so i dont need to use a thread.
    right now the thread has a "while(1)" loop to check for a completed transfer.
    i cant see a way to have this running without hanging the main thread.
    Use signals and slots (probably with a timer) instead of the while loop. There is no point in using up all your cpu resources without a reason.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. real time data display
    By hammer256 in forum Qt Programming
    Replies: 13
    Last Post: 25th March 2013, 16:47
  2. Program crashes on creating new dialog
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 11:52
  3. Replies: 3
    Last Post: 19th February 2009, 19:01
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

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
  •  
Qt is a trademark of The Qt Company.