Results 1 to 5 of 5

Thread: ๊Uneble to open SPI device

  1. #1
    Join Date
    Apr 2018
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Unhappy ๊Uneble to open SPI device

    I have a problem when I setup SPI on Qt it show me an error "Unable to open SPI device: No such file or directory".

    Thanks for help.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ๊Uneble to open SPI device

    Do you have the appropriate back end library installed (for modbus or CAN bus)? If it is a shared library or DLL, is it in a directory where it can be found at run time?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2018
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: ๊Uneble to open SPI device

    I've included <sys/ioctl.h> but ioctl.h isn't in the sys directory, it is in the linux directory and I can't move this file. I think that is a problem.
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <wiringPi.h>
    4. #include <wiringPiSPI.h>
    5. #include <QTimer>
    6. #include <linux/spi/spidev.h>
    7. #include <sys/ioctl.h>
    8. int fd;
    9. QTimer *timer;
    10.  
    11. MainWindow::MainWindow(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::MainWindow)
    14. {
    15. ui->setupUi(this);
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::on_InitSPIBot_clicked()
    24. {
    25. if((fd=wiringPiSPISetup(0,2000000))<0)
    26. {
    27. ui->textEdit->setText("Open SPI Error");
    28. }
    29. else
    30. {
    31. int8_t mode=1;
    32. int32_t speed;
    33. int ret=ioctl(fd,SPI_IOC_WR_MODE,&mode);
    34. if(ret == -1)
    35. {
    36. ui->textEdit->setText("ChangeSPImode Fail");
    37. }
    38. else
    39. {
    40. ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,&speed);
    41. ui->textEdit->setText("SPI Speed "+QString::number(speed)+" Hz");
    42. ioctl(fd,SPI_IOC_RD_MODE,&mode);
    43. ui->textEdit->setText("SPI mode "+QString::number(mode));
    44. timer = new QTimer(this);
    45. connect(timer,SIGNAL(timeout()),this,SLOT(interval()));
    46. }
    47. }
    48. }
    49.  
    50. void MainWindow::write_register()
    51. {
    52. unsigned char data_out[10];
    53.  
    54. data_out[0] = 0x02;
    55. data_out[1] = 0x02;
    56. data_out[2] = 0x00;
    57. wiringPiSPIDataRW(0,data_out,3);
    58. }
    59.  
    60. QByteArray MainWindow::read_register()
    61. {
    62. unsigned char data_out[10];
    63. data_out[0] = 0x03;
    64. data_out[1] = 0x00;
    65. data_out[2] = 0x10;
    66. wiringPiSPIDataRW(0,data_out,3);
    67. for(int i=0;i<4;i++)
    68. {
    69. dat[i]=data_out[i+1];
    70. }
    71. return(dat);
    72. }
    73.  
    74. void MainWindow::on_StartBot_2_clicked()
    75. {
    76. timer->start(200);
    77. }
    78.  
    79. void MainWindow::on_StopBot_clicked()
    80. {
    81. timer->stop();
    82. }
    83.  
    84. void MainWindow::interval()
    85. {
    86. ui->textEdit->clear();
    87. out = read_register();
    88. int32_t B0 = 0;
    89.  
    90. B0 = (out.at(0)<<16) | (out.at(1)<<8) | out.at(2);
    91.  
    92. float B;
    93.  
    94. B = B0;
    95.  
    96. ui->textEdit->append(QString::number(B)+" G");
    97. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ๊Uneble to open SPI device

    Your problem is a run-time problem. It has nothing to do with header files. If the compiler couldn't locate the header files it needs, it would not have built the executable for you to run.

    I don't know a lot about linux, but it seems to me that you are missing a device driver or haven't added / configured the device you are trying to use. That's what your error message seems to imply. Aren't linux devices typically mapped to "files" in the /etc directory?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Apr 2018
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: ๊Uneble to open SPI device

    I found the problem, I didn't enable spi mode on Raspberry Pi. It's easy problem Haha .

    Thanks for your help.

Similar Threads

  1. QIODevice::read (QProcess): device not open
    By sagarsadhu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2017, 11:58
  2. How to open a device for Asynchronous IO in QT4
    By WDT in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2011, 21:58
  3. Replies: 1
    Last Post: 2nd December 2010, 15:01
  4. Not able to open Qt Mobility applications on Nokia N97 Device!
    By rajeevsahu in forum Installation and Deployment
    Replies: 0
    Last Post: 16th July 2010, 06:50
  5. Problem whis open device in Windows
    By Pavka in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 10:13

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.