[SOLVED] Serial port : Read data problem, not reading complete data
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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"
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.