LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-11-2009, 04:51 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
the get data from serial port function read() blocks when data not available


I am using read() in c++ to get data from a serial port. However, if no data is available on the serial port the function blocks until dta arrives.

Example code:

//------------------------------------------------------------
char m_readBuffer[255] = {0};

char* p_curChar = m_readBuffer;
ssize_t numberOfBytes = 0;
do {
const ssize_t BYTES_READ = read(m_fd, p_curChar, 1);
if (0 != BYTES_READ)
{
if (numberOfBytes < (SIZEOF_READBUFFER - 1))
{
numberOfBytes += BYTES_READ;
p_curChar = m_readBuffer + static_cast<int>(numberOfBytes);
}
else
{
p_curChar = 0;
}
}
} while ((sz_LF != p_curChar) ||
(sz_CR != p_curChar) ||
(0 != p_curChar));
//------------------------------------------------------------

Is there a way a either a) setting the read() funtion to timeout, b) stop the read() funtion from blocking so it returns if no data available or c) use of another funtion?
 
Old 06-11-2009, 05:21 AM   #2
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
OK it would seem my post was a little hasty!

Change the open() function to include the flag O_NONBLOCKING does the trick.
 
Old 11-17-2014, 07:38 AM   #3
amitbpl5
LQ Newbie
 
Registered: Jul 2014
Posts: 7

Rep: Reputation: Disabled
read function goes to block state

#include <iostream>
#include <stdio.h> /* Standard input/output definitions */
#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.h>
#include <string>
using namespace std;


//Initialize serial port
int initport(int fd)
{
int portstatus = 0;

struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 115200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag = CS8;
//options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable XON XOFF (for transmit and receive)
//options.c_cflag |= CRTSCTS; /* enable hardware flow control */


options.c_cc[VMIN] = 1; //min carachters to be read
options.c_cc[VTIME] = 10; //Time to wait for data (tenths of seconds)


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


//Set the new options for the port...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options)==-1)
{
perror("On tcsetattr:");
portstatus = -1;
}
else
portstatus = 1;


return portstatus;
}


/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);

if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 --- \n");
}
else
fcntl(fd, F_SETFL, 0);

return (fd);
}

int main(void)
{

int serial_fd = open_port();

if(serial_fd == -1)
printf("Error opening serial port /dev/ttyS0 \n");
else
{
printf("Serial Port /dev/ttyS0 is now open \n");

if(initport(serial_fd) == -1)
{
printf("Error Initializing port");
close(serial_fd);
return 0;
}

sleep(.5);
//usleep(500000);
//printf("size of data being sent = %ld", sizeof("~ver~\n\r"));
unsigned char linkpack[2];
string str = "@R5-ON#";
char str1[8] = {'@','R','6','-','O','N','#'};
linkpack[0] = '@';
linkpack[1] = 'T';
linkpack[2] = '5';
linkpack[3] = '-';
linkpack[4] = 'O';
linkpack[5] = 'N';
linkpack[6] = '#';
//linkpack[7] = '@';
int n;
//write(serial_fd,str1, 8);
for(int i=0; i <8; i++)
{
n = write(serial_fd,&linkpack[i], 1);
usleep(5000);
printf("hell ");
}


if (n < 0)
fputs("write() of 8 bytes failed!\n", stderr);
else
{
printf("Successfully wrote 8 bytes\n");



//sleep(1);
char buffer[32];


int n = read(serial_fd, buffer, sizeof(buffer));
usleep(10000);
if (n < 0)
fputs("read failed!\n", stderr);
else
{
printf("Successfully read from serial port -- %s\n", buffer);

}


}

sleep(1);


printf("\n\nNow closing Serial Port /dev/ttyS0 \n\n");

close(serial_fd);
}


return 0;
}
 
Old 11-17-2014, 08:11 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Never hesitate to use [code] and [/code] tags.
Also we could mention functions select and poll.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Minicom -- want to read data coming in a serial port ihopeto Linux - Newbie 2 04-12-2009 10:46 PM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 09:35 PM
Bad data in socket read function nhydra Programming 2 05-08-2006 04:16 AM
accessing data from serial port PrerakParikh Linux - Hardware 2 08-24-2004 11:17 PM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 01:21 AM


All times are GMT -5. The time now is 03:15 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration