PDA

View Full Version : چرا این برنامه خطای کمبود منبع میدهد؟



haniyeh.ghassami
یک شنبه 02 اسفند 1394, 08:33 صبح
سلام
من برنامه ای برای دستگاه امبد لینوکسی نوشتم که از پورت سریال داده را میخواند
اما دستور رید را نمی تواند اجرا کند و برنامه خطای Resource temporarly unavailable میدهد

#include "mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <string> /*To use string type*/
#include <iostream>
#include<string>
#include <QtSql/qsql.h>
#include <QProcess>

//select
#include <stdio.h> /* Standard input/output definitions */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>


using namespace std;


//--------------------------------------------
int fd; /* File descriptor for the port */
#define MAXDATASIZE 100
int numbytes;
char buf2[MAXDATASIZE];
//-------------------------------------
void closeport(void)
{
close(fd);
}
//-------------------------------------
void configport(void)
{
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( fd, &tty ) != 0 ) {
// std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);

/* 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;// 0; // read doesn't block
tty.c_cc[VTIME] = 10;// 2 but befor it 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) {
// std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}
}

//------------------------------------


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

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

void MainWindow::on_pushButton_clicked()
{
fd_set set;
struct timeval timeout;

QString portname="ttySAC3";//ui->txtport->text();
//QString portname=ui->txtport->text();

portname="/dev/"+portname;
fd=open(portname.toLatin1().data(),O_RDWR|O_NOCTTY |O_NDELAY);//O_NONBLOCK=>O_NDELAY

FD_ZERO(&set); /* clear the set */
FD_SET(fd, &set); /* add our file descriptor to the set */

timeout.tv_sec = 0;
timeout.tv_usec = 10000;

if (fd==-1)
{
ui->lblerr->setText("unable to open port ");
return;
}
else
{
ui->lblerr->setText("<span style='color:green;'>succesfully open port </span>");
fcntl(fd,F_SETFL,O_NONBLOCK);

//*****************************
// SerialCodes
//*****************************]
ui->txtcardno->setText("befor fork");
// if(!fork())
// {
ui->txtcardno->setText("befor while");
while(1)
{
memset(buf2,'\0',MAXDATASIZE);

ui->txtcardno->setText("befor read");
//------------------------------------------------
numbytes = select(fd + 1, &set, NULL, NULL, &timeout);
if(numbytes == -1)
{
QString s(strerror(errno));
ui->txtcardno->setText(s);
//perror("select"); /* an error accured */
}
else if(numbytes == 0)
{
QString s("timeout");
ui->txtcardno->setText(s);
//printf("timeout"); /* a timeout occured */
}
else
{
//read( filedesc, buff, len ); /* there was data to read */
//------------------------------------------------
if ((numbytes = read(fd,buf2, MAXDATASIZE-1)) == -1)
{
ui->txtcardno->setText("Error");
}
else
{
ui->txtcardno->setText("");
buf2[numbytes] = '\0';
//printf("RFID MONITORING => '%s'\n",buf2);
QString s(buf2);
//ui->txtcardno->setText("ok......................");
ui->txtcardno->setText(s);
//break;
}
}
}
// }
// else
// {

// QString s(strerror(errno));
// ui->txtcardno->setText(s);
// }
}
}

void MainWindow::on_pushButton_2_clicked()
{

}


مشکل این کدنویسی در کجا است؟