开发者

Serial port : Read data problem, not reading complete data

开发者 https://www.devze.com 2022-12-21 20:43 出处:网络
I have an application where I am sending data via serial port from PC1 (Java App) and reading that data in PC2 (C++ App). The problem that I am facing is that my PC2 (C++ App) is not able to read comp

I have an application where I am sending data via serial port from PC1 (Java App) and reading that data in PC2 (C++ App). The problem that I am facing is that my PC2 (C++ App) is not able to read complete data sent by PC1 i.e. from my PC1 I am sending 190 bytes but PC2 is able to read close to 140 bytes though I am trying to read in a loop.

Below is code snippet of my C++ App

 /**Open the connection to serial port**/



serialfd = open( serialPortName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);

 if (serialfd == -1)
 {
  /*
   * Could not open the port.
   */
  TRACE << "Unable to open port: " << serialPortName << endl;
 }
 else
 {
  TRACE << "Connected to serial port: " << serialPortName << endl;
  fcntl(serialfd, F_SETFL, 0);
 }



 /**Configure the Serial Port parameters**/

  struct termios options;

  /*
   * Get the current options for the port...
   */
  tcgetattr(serialfd, &options);

  /*
   * Set the baud rates to 9600...
   */
  cfsetispeed(&options, B38400);
  cfsetospeed(&options, B38400);

  /*
   * 8N1
   * Data bits - 8
   * Parity    - None
   * Stop bits - 1
   */
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

  /*
   * Enable hardware flow control
   */
  options.c_cflag |= CRTSCTS;

  /*
   *  Enable the receiver and set local mode...
   */
  options.c_cflag |= (CLOCAL | CREAD);

  // Flush the earlier data
  tcflush(serialfd, TCIFLUSH);

  /*
   *  Set the new options for the port...
   */
  tcsetattr(serialfd, TCSANOW, &options);


 **Now I am reading data**
  const int MAXDATASIZE = 512;
  std::vector<char> m_vRequestBuf; 
  char buffer[MAXDATASIZE];
  int totalBytes = 0;

  fcntl(serialfd, F_SETFL, FNDELAY);
  while(1) {
          bytesRead = read(serialfd, &buffer, MAXDATASIZE);
  if(bytesRead == -1)
  {
     //Sleep for some time and read again 
                  usleep(900000);
  }
  else
  {
      totalBytes += bytesRead;  
                  //Add data read to vector
                  for(int i =0; i < bytesRead; i++)
      {
   m_vRequestBuf.push_back(buffer[i]);
      }

                  int newBytesRead = 0;

                  //Now keep trying to read more da开发者_JAVA百科ta
      while(newBytesRead != -1)
      {
   //clear contents of buffer
   memset((void*)&buffer, 0, sizeof(char) * MAXDATASIZE); 
   newBytesRead = read(serialfd, &buffer, MAXDATASIZE);
   totalBytes += newBytesRead;

                        for(int j = 0; j < newBytesRead; j++)
   {
       m_vRequestBuf.push_back(buffer[j]);
   }
       }//inner while    
     break;
  } //while


Lateee response, but if you are reading in non-canonical mode, consider setting VMIN to the number of bytes you want to read.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号