Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-02-2007, 07:58 PM   #1
LQ Newbie
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
Difficult to read serial port

I am reading outputs from many sensor through an ADC with the aid of UART but not succesful for more than three weeks. Following is my code cause I think it will be faster to get my problem this way

#include <stdio.h>	
#include <string.h>	
#include <unistd.h>	
#include <fcntl.h>	
#include <termios.h>	
#include <errno.h>	
#include <sys/select.h>	
#include <stdlib.h>	//
#include <sys/types.h>	//

#define BAUDRATE B9600	//Baud rate set to 9600 bit/sec
#define SERIALDEVICE "/dev/ttyS0"	//Serial port to be used for communication

int main(int argc, char *argv[]){
	int sfd, ret, i;
	struct termios oldtio, newtio;
	char buf[256];
	char buf1[256];
	fd_set	readfds;	//file descripto set
	int maxfd;		//maximum file descriptor used
	long disable;

	//open serial port to be non-blocking
	sfd = open(SERIALDEVICE, O_RDWR );//| O_NOCTTY | O_NONBLOCK);//change later pg(241)
	if( sfd < 0 ){
	//current maximum file descriptor
	maxfd = sfd + 1;

	//struct newtio for new serial  port setting
	//struct oldtio for holding the original port setting

	//save current options for the port 

	/*---set the baudrate---*/
	//set the input speed
	cfsetispeed(&newtio, BAUDRATE);
	//set the output speed
	cfsetospeed(&newtio, BAUDRATE);

	/*---new port setting---*/
	//enable receiver and set local mode
	newtio.c_cflag |= (CLOCAL | CREAD);

/*Setting Raw Terminal I/O*/
	//select 8 data bits, no parity and 1 stop bit
	newtio.c_cflag &= ~PARENB;
	newtio.c_cflag &= ~CSTOPB;	
	newtio.c_cflag &= ~CSIZE;	//Mask the character size bits
	newtio.c_cflag |= CS8;		//select 8 data bits	

	//Clear ISTRIP to get all eight bits also clear clear parity checking
	//clear IEXTEN to turn off extended character processing
	//no flow contro clear IXON
	//clear BRKINT 
	newtio.c_iflag &= ~(INLCR | ICRNL | ISTRIP | INPCK | IXON | BRKINT);
	//clear OPOST to turn off output processing
	newtio.c_oflag &= ~OPOST;

	//clear ECHO and set MIN and TIME.
	//clear ISIG	
	newtio.c_lflag &=~(ICANON | ECHO | IEXTEN | ISIG);

	for (i = 0; i < NCCS; i++)		//NCCS
		newtio.c_cc[i] = (cc_t)disable;		//what is this?
		newtio.c_cc[VMIN] = 1;
		newtio.c_cc[VTIME] = 0;

	tcflush(sfd, TCIFLUSH);
	ret = tcsetattr(sfd, TCSANOW, &newtio);
	if (ret < 0){
		perror("Failure to set new port options\n");

	/*---loop for reading from the port---*/

		FD_SET(sfd, &readfds);
		FD_SET(0, &readfds);
		ret = select(maxfd, &readfds, NULL, NULL, NULL);	
		if (ret < 0){
			perror("select failure\n");
		//check for calling file descriptor
		if ( FD_ISSET(sfd, &readfds)){
			//handle input from the serial port
			ret = read(sfd, buf1, sizeof(buf1));
			if(ret < 0){
				perror("failure to read serial port\n");
			printf("Contents from serial input : %s\n", buf1);

			//read input from keyboard
			fgets(buf, sizeof(buf), stdin);
			if (strncmp("quit", buf, 4)==0 ){
				//prepare to close serial port
				printf("now closing serial port\n");
				//restore old port setting before closing port
				tcsetattr(sfd, TCSANOW, &oldtio);
				  return 0;		
		}else{//may be try writting the serial port


Last edited by omwoyo; 07-03-2007 at 09:30 AM.
Old 07-02-2007, 10:16 PM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I'm not sure that I can help with your problem, but you are more likely to get a useful response if you do two things:
  1. Post your code with the CODE tag around it. Use the go advanced button, select your code with your mouse, and click on the # icon above the editing area (in advanced mode).
  2. Rather than tell us that it didn't work, be more detailed. Where in your code are things getting stuck? Would it be useful to put more detailed error messages in your code, tell us what messages you're getting from that code, and post the changed program which emits those additional error messages?

Last edited by; 07-02-2007 at 10:20 PM.
Old 07-03-2007, 06:30 AM   #3
LQ Newbie
Registered: Jul 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Difficult in reading the serial port

I am sorry that My circuit keeps on continuously sending and there was something wrong with the contents I was printing. It seems to be working now for one of the sensors and I hope the same will apply to the others. Once more thanks a lot for the very fast responses I have received.

The other issue was with the signal levels. Thanks to Wim Sturkenboom located in South Africa.


Last edited by omwoyo; 07-03-2007 at 09:37 AM.
Old 07-03-2007, 07:47 AM   #4
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
It usually will work with TTL levels if you don't forget to invert the signal(s); 0V TTL equals 12V RS232 and 5V equals -12V RS232.

It however is better to use RS232 levels.

And as requested, place your code between code tags; this will preserve indentations and makes it easier to read. The opening tag is [ c o d e ] and the closing tag is [ / c o d e ], both without the spaces.


code, serial port

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
How to read serial port (/dev/ttyS0) seraph-seph Linux - Hardware 4 11-01-2006 07:55 AM
When to read from the serial port? kad_79 Programming 3 06-15-2006 03:39 PM
Problem with Non-blocking serial port read ZX_SA Programming 0 01-18-2005 09:50 AM
Serial Port read in shell script tjt Linux - Newbie 1 08-12-2004 10:18 AM
Serial port Read Write SeanatIL Programming 2 07-14-2004 04:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

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