从Linux设备/ dev / ttyS0在read()上暂时不可用的资源

时间:2018-07-26 08:41:32

标签: c++ linux qt serial-port qt4

我阅读了所有解决方案并进行了测试。有人说使用过ColID = c(1, 1, 1, 1, 1, 2, 2, 2) ColStatus = c("A", "B", "B", "C", "D", "A", "C", "C") ColDate = c("01/01/2018","02/03/2018", "05/04/2018", "04/05/2018", "04/05/2018", "02/01/2018", "04/03/2018", "05/04/2018") df <- data.frame(ColID, ColStatus, ColDate) 并删除了O_NDELAY,另一个说了使用了fnctl,依此类推,但是我的问题没有解决。
我有O_NONBLOCK,并且想使用nanopi neo。这是我的代码:
Main.cpp:

UART0

以及portSerial.h和.cpp:

#include <QCoreApplication>
//#include "serialport.h"
#include "portserial.h"
#include <iostream>
#include <istream>
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString port;PortSerial *p;

    cout<<"Enter tty:";
    QTextStream s(stdin);
    port = s.readLine();
    p=new PortSerial(port,1);
    if(p->openport(port)==1)
    {
        cout << "connected\n";
    }
    else
    {
        qDebug()<<"Not serial port : "<<port;
        delete p;
    }

    return a.exec();
}

.cpp是:

#ifndef PORTSERIAL_H
#define PORTSERIAL_H

#include <stdio.h>   /* Standard input/output definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include "errno.h"   /* Error number definitions */
#include "termio.h" /* POSIX terminal control definitions */
#include <stdlib.h>
#include "QDebug"
#define MAXDATASIZE 2048
#include <QByteArray>
#include <QThread>
#include <pthread.h>

class PortSerial:public QObject
{
    Q_OBJECT
public:
    QThread t;
//    pthread_t  pthrd;
    int fd;
    int status;
    char * buffer;
    char * buffertemp;
    char buf2[MAXDATASIZE];
    QString output;
    QByteArray ba;
    QByteArray batemp;
    fd_set readfs;
    timeval tv;
    int n;
    int devid;
    bool wait;
    PortSerial(QString port, int dev);
    ~PortSerial();
public slots:
    int openport(QString port);
    int init();
    void closeport();
    void readData();
    void ifExist();
//    int readifexist(char &tmp);
    void writeData(QByteArray ba);
    signals:
    void readyread(QByteArray ba,int devids);
};

#endif // PORTSERIAL_H

当我将此串行端口:#include "portserial.h" PortSerial::PortSerial(QString port,int dev) { // qDebug()<<"Start the thread"; buffer = new char[1024]; devid=dev; openport(port); moveToThread(&t); connect(&t, SIGNAL(started()), this, SLOT(ifExist())); t.start(); } PortSerial::~PortSerial() { closeport(); } //____________________________ int PortSerial::openport(QString port) { fd=open(port.toStdString().c_str(),O_RDWR|O_NOCTTY|O_NDELAY/*O_NONBLOCK*/);//ARM if (fd==-1) { return -1;//return open port error } else { // fcntl(fd,F_SETFL,O_NDELAY); init(); return 1; } } //------------------------------------- void PortSerial::closeport() { qDebug()<<"Close with : " << close(fd); } //------------------------------------- int PortSerial::init() { struct termios tty; struct termios tty_old; memset (&tty, 0, sizeof tty); /* Error Handling */ if ( tcgetattr ( fd, &tty ) != 0 ) { //qDebug() << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl; return -2; } /* Save old tty parameters */ tty_old = tty; /* Set Baud Rate */ if(devid==1){//rfid cfsetospeed (&tty, (speed_t)B9600); cfsetispeed (&tty, (speed_t)B9600); qDebug()<<"fd is : "<< fd<<" with this baud rate: 9600"; } else{//fp cfsetospeed (&tty, (speed_t)B115200); cfsetispeed (&tty, (speed_t)B115200); qDebug()<<"fd is : "<< fd<<" with this baud rate: 115200"; } /* Setting other Port Stuff */ tty.c_cflag &= ~PARENB; // Make 8n1 tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; tty.c_cflag &= ~CRTSCTS; // no flow control tty.c_cc[VMIN] = 1; // read doesn't block tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines /* Make raw */ cfmakeraw(&tty); /* Flush Port, then applies attributes */ tcflush( fd, TCIFLUSH ); if ( tcsetattr ( fd, TCSANOW, &tty ) != 0) { //qDebug() << "Error " << errno << " from tcsetattr" << std::endl; return -3; } return 1; } ///new code /// ******************************************************* /// /// /// ******************************************************* void PortSerial::readData() { buffer[0]=0; usleep(20000); int res = read(fd,buffer,2048); if(res <=0) qDebug() << strerror(errno) ; if(res >0) { buffer[res]=0; // batemp = QByteArray((char*)buffer, res); batemp.append(QByteArray((char*)buffer, res)); // emit readyread(ba,devid); // qDebug()<<"_____READ________\n"<<ba.toHex()<<"\n"; } } void PortSerial::ifExist() { int c=0; while(1){ //set timeout for wait tv.tv_sec = 0; tv.tv_usec = 1000; FD_ZERO(&readfs); FD_SET(fd, &readfs); n=select(fd+1, &readfs, NULL, NULL, &tv /* no timeout */); if(n==1) { qDebug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@reading"); readData(); } else { if(batemp.length()>0){ qDebug()<<"batemp len is:"<<batemp.length(); emit readyread(batemp,devid); qDebug()<<"\n________________________READ byte_____________________________\n"<<batemp.toHex() <<"\nnon hex is: "<<batemp<<"\n"; batemp.clear(); } } } } ///new code /// ******************************************************* /// /// /// ******************************************************* //------------------------------------ void PortSerial::writeData(QByteArray ba) { int res=write(fd,ba,ba.length()); qDebug()<<"port write : "/*<<ba.toHex()*/<<"******* res is : "<<res; } 赋予此端口时,请先读取Null(十六进制的0a03),然后再读取:

/dev/ttyS0

我该如何解决?要读取确切的RFID卡数据并多次读取,而资源暂时不可用。

0 个答案:

没有答案