PDA

View Full Version : ๊Uneble to open SPI device



jobzayul
30th April 2018, 11:42
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.

d_stranz
30th April 2018, 22:59
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?

jobzayul
1st May 2018, 05:14
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.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <QTimer>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
int fd;
QTimer *timer;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_InitSPIBot_clicked()
{
if((fd=wiringPiSPISetup(0,2000000))<0)
{
ui->textEdit->setText("Open SPI Error");
}
else
{
int8_t mode=1;
int32_t speed;
int ret=ioctl(fd,SPI_IOC_WR_MODE,&mode);
if(ret == -1)
{
ui->textEdit->setText("ChangeSPImode Fail");
}
else
{
ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,&speed);
ui->textEdit->setText("SPI Speed "+QString::number(speed)+" Hz");
ioctl(fd,SPI_IOC_RD_MODE,&mode);
ui->textEdit->setText("SPI mode "+QString::number(mode));
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(interval ()));
}
}
}

void MainWindow::write_register()
{
unsigned char data_out[10];

data_out[0] = 0x02;
data_out[1] = 0x02;
data_out[2] = 0x00;
wiringPiSPIDataRW(0,data_out,3);
}

QByteArray MainWindow::read_register()
{
QByteArray dat;
unsigned char data_out[10];
data_out[0] = 0x03;
data_out[1] = 0x00;
data_out[2] = 0x10;
wiringPiSPIDataRW(0,data_out,3);
for(int i=0;i<4;i++)
{
dat[i]=data_out[i+1];
}
return(dat);
}

void MainWindow::on_StartBot_2_clicked()
{
timer->start(200);
}

void MainWindow::on_StopBot_clicked()
{
timer->stop();
}

void MainWindow::interval()
{
QByteArray out;
ui->textEdit->clear();
out = read_register();
int32_t B0 = 0;

B0 = (out.at(0)<<16) | (out.at(1)<<8) | out.at(2);

float B;

B = B0;

ui->textEdit->append(QString::number(B)+" G");
}

d_stranz
1st May 2018, 17:25
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?

jobzayul
3rd May 2018, 04:44
I found the problem, I didn't enable spi mode on Raspberry Pi. It's easy problem Haha .

Thanks for your help.:)