如何更改此代码以在一部分(而不是 2 部分)中从串口读取所有数据?

How to change this code to read all data from serial port in one part (not 2 part)?

此代码正在从 RFID 模块 (EM-18) 读取数据。我这里有一个大问题。当我的应用程序是 运行 并且我从 em 模块传递 RFID 卡时,它分两部分读取数据。先读取 8 字节的卡 ID,然后发送 4 字节。像这样:

[root@FriendlyARM /fgit]# ./RFIDMonitor -qws
enter the port name:
ttySAC3
open_port: succesfully open port
open_port: succesfully open port 
RFID MONITORING => '010B7528'
RFID MONITORING => '297E'  
RFID MONITORING => '010B7528'
RFID MONITORING => '297E'  

我改变了VMINVTime但是结果没有改变。

这是我的代码:

 if(!fork())
        {
            while(1)
            {
                memset(buf2,'[=11=]',MAXDATASIZE);
                //------------------------------------------------
                if ((numbytes = read(fd,buf2, MAXDATASIZE-1)) != -1)
                {
                    buf2[numbytes] = '[=11=]';
                    printf("RFID MONITORING => '%s'\n",buf2);
                }

            }
        }

这是我的配置:

   int openport(void)
   {   
       cout<<"enter the port name:\n";
       string portname="";
       cin>>portname;

       portname="/dev/"+portname;
       fd=open(portname.c_str(),O_RDWR|O_NOCTTY|O_NDELAY);
       if (fd==-1)
       {
           //printf("open_port: unable to open port \n");
           return -1;
       }
       else
       {
           //printf("open_port: succesfully open port \n");
           fcntl(fd,F_SETFL,0);
           return 1;
       }
   }
   //-------------------------------------
   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;// change to 0 and 5
       tty.c_cc[VTIME]  = 10;// change to 0 and 5
       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;
       }
   }

我想这样展示(所有数据在一个部分)

RFID MONITORING => '010B7528297E

read 只读可读的内容。如果您必须通读整篇文章,请继续阅读,直到您读完整篇文章。

while(1)
{
    memset(buf2,'[=10=]',MAXDATASIZE);
    int bytesread = 0;
    //------------------------------------------------
    //Keep going until the whole message has been read
    while (bytesread != MAXDATASIZE-1)
    { 
        //read less on successive reads, but write further into buffer
        if ((numbytes = read(fd,buf2[bytesread], MAXDATASIZE-1-bytesread)) == -1)
        {
            printf("Error in reading data");
            // handle error. Probably reopen port and resynch with RFID
        }
        else
        {
            // keep track of where we are in accumulating
            bytesread += numbytes;
        }
    }
    buf2[bytesread] = '[=10=]';
    printf("RFID MONITORING => '%s'\n",buf2);
}

我解决了这个问题(只需将其附加 sleep 1 秒)

int  numbytes;
char buf2[MAXDATASIZE];
//*****************************
//      SerialCodes
//*****************************]
fd=-1;

while(fd==-1)
{
    int a=openport();
    if(a==1)
    {
        pid_t pid;
        if((pid=fork()))
        {
            qDebug()<<("%d",pid);
            while(1)
            {

               //------------------------------------------------
               if ((numbytes = read(fd,buf2, MAXDATASIZE-1)) == -1)
               {
                   //qDebug()<<("Error in reading data");
               }
               else
               {
                   buf2[numbytes] = '[=10=]';
                   usleep(10000);
                   QString s1(buf2);
                   memset(buf2,'[=10=]',MAXDATASIZE);
                   read(fd,buf2, MAXDATASIZE-1) ;
                   QString s2(buf2);
                   memset(buf2,'[=10=]',MAXDATASIZE);
                   qDebug()<<("RFID MONITORING => '%s'\n",s1+s2);
                   kill(pid,SIGTERM);
                   break;
               }
            }
        }
    }
}