LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2010, 03:27 AM   #1
anujmehta
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
Serial port : Read data problem, not reading complete data


Hi

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

Code:
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

Code:
    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


Code:
  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 data 
            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
 
Old 03-06-2010, 10:58 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by anujmehta View Post
Hi

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
Not sure what we can tell you. What version/distro of Linux are you using? What error(s) are you getting? Is the data received always off by the same amount, no matter what you send? What have you done to try to debug this? Are you using a 'real' serial port, or a USB to serial converter?

Without seeing the program work, having the complete source, and knowing what you're trying to do, it's tough to say what it could be, other than "your program has a bug, fix it"
 
Old 03-06-2010, 08:05 PM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
The problem is your breaking out of the loop before you've finished reading data

Once you've started reading you head into the else section where if it gets a read error it'll break out of the while(1) loop and if no new bytes are ready for consumption you'll get a -1 with errno set to EAGAIN since you are using no delay/non-blocking io, so depending on the order in which things execute compared to incoming byte speed you probably are only getting data from the first read.
 
Old 03-07-2010, 09:43 PM   #4
anujmehta
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the replies. I followed following link http://www.easysw.com/~mike/serial/serial.html and was able to resolve the issue.
 
Old 08-26-2010, 03:41 AM   #5
cgtykynk
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Hi ,

I am in trouble with the same problem (EAGAIN return).

How did you solve this problem? I am an intern student and i need to learn this programming technique.

Thanks in advance.Please share your solving with us
 
Old 09-06-2010, 06:10 AM   #6
cgtykynk
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Hi again ,

I solved this problem, and now i can send command to GSM modem by serial port.

If somebody needs help, i can share my C codes here and i can explain If you send me a p.m or mail i can help you.Or you can add me to your msn contact list.

Good works.
cagatay_kaynak@hotmail.com
 
  


Reply

Tags
c++, serial port



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
the get data from serial port function read() blocks when data not available DEF. Programming 3 11-17-2014 07:11 AM
Displaying I/O possible when reading data from the serial port makphy Programming 9 06-18-2013 05:14 AM
Reading data from serial port at 8 Kb/sec harshadnatiye Linux - Newbie 2 07-20-2009 07:42 AM
help reading data from a serial port skydemon Linux - General 1 08-10-2006 08:48 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration