LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-30-2010, 11:39 PM   #1
lalithasankh
LQ Newbie
 
Registered: May 2010
Posts: 28

Rep: Reputation: 15
termios programming in linux


Dear All,

I trying to write a UART(interfacing of serial devices) to linux machine but after I execute the following code to receive data I need to enter key (carriage return)....

but I don't want to remove carriage return/enter key

please any one can help me....

Code:
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <sys/time.h>
 #include <stdio.h>
 
 #include <string.h>
 #include <sys/signal.h>
 #include <termios.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #define BAUDRATE 	B9600
 /***************************************************************************
 *******************************************/	
 	/*	Serial Communication Settings	*/
 
 #define MODEMDEVICE  "/dev/ttyAM1"
 
 struct termios oldtio,newtio;
 int fd_serial;	
 char Rx_databuf_M8051[100];
 
 int read_write();
 int  main()
 {
 
 	fd_serial = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); 
        	if (fd_serial <0) {perror(MODEMDEVICE); exit(-1); }
         
 	tcgetattr(fd_serial,&oldtio); 	/* save current serial port   settings
 */
 	bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings 
 */
 	newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
 	newtio.c_iflag = IGNPAR | ICRNL;
 	newtio.c_oflag = 0;
 	newtio.c_lflag =ICANON;
      	newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 haracter
 arrives */
        	tcflush(fd_serial, TCIFLUSH);
         tcsetattr(fd_serial,TCSANOW,&newtio);
 	while(1)
 	{	
 	read_write();
 	}
 return 0;
 }
 int read_write()
 {
 	write(fd_serial,"#09,R$",6);
 	read(fd_serial,Rx_databuf_M8051,255);
 	printf("received data=%s\n",Rx_databuf_M8051); return 0;
 
 }

Last edited by XavierP; 07-02-2010 at 03:01 AM. Reason: code tags added
 
Old 07-01-2010, 07:24 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Please ask the moderators to move your post to the "Programming" sub-forum, and read the information linked to by the BB code link below the reply box so you can post your code inside a code block. That preserves the code formatting and makes it easier to read.
 
Old 07-02-2010, 12:06 AM   #3
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
Quote:
but after I execute the following code to receive data I need to enter key (carriage return)....
Can you elaborate a bit? Why do you need to do that? Is nothing happening without it? Or ...?

What I don't trust in your code is the printf in the read_write function; your Rx_databuf_M8051 is not zeroed so printf will display a string that might not be terminated (with '\0').

Further you read a max of 255 chars into an array that can hold 100 chars; calling for trouble

PS I've reported your post to be moved to the programming section
 
Old 07-02-2010, 03:00 AM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-09-2010, 12:00 AM   #5
lalithasankh
LQ Newbie
 
Registered: May 2010
Posts: 28

Original Poster
Rep: Reputation: 15
trermios

yes I tried to modify the what have suggested earlier mail
but still the same issue I need to hit enter key to read data ...
 
Old 07-09-2010, 10:52 AM   #6
lbuccola
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Rep: Reputation: 0
Check your code where you set the options for termios.
Code:
 	newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
 	newtio.c_iflag = IGNPAR | ICRNL;
 	newtio.c_oflag = 0;
 	newtio.c_lflag =ICANON;
You probably want to do an "|="
 
Old 07-09-2010, 11:28 AM   #7
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 492

Rep: Reputation: 50
Not sure to understand what do you want to achieve but see if removing the "ICANON" flag solve your issue (google for termios canonic)
 
Old 07-10-2010, 12:04 AM   #8
lalithasankh
LQ Newbie
 
Registered: May 2010
Posts: 28

Original Poster
Rep: Reputation: 15
without carriage return receive data from UART

thanks for your reply...

I think you did not get my problem let me explain you once again..

I am trying to write and read data from uart (serial port), where I am able write data successfully but while reading data once I hit enter key or
carriage return only I able to see data on hyper-terminal... other wise no data is displayed on hyper terminal...

so my intention to receive data as we type character not only after hitting enter key..
 
Old 07-10-2010, 03:56 AM   #9
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
I still don't understand your problem exactly. From what I understand, your program runs on linux and, for now, communicates with a windows machine where you run hyper-terminal. In hyperterm you don't see anything from your program when you start your program till you press <enter> in hyperterm. Is that indeed your problem?

If that is your problem, do you start hyperterm before you start your program?
If not, your program has already send the data '#09,R$' so you have to press <enter> before it will send it again as your read is blocking the loop.
If yes, sounds like a hyperterm configuration and at this stage I can't help you with that (as I don't have a hyperterm available at this moment).

Your program does send the data immediately (and works from that perspective); I have confirmed this using minicom under Ubuntu using pseudo terminals.

You can configure minicom to use /dev/ptyp0 (and correct baudrate/parity settings etc) and your program to use /dev/ttyp0. Note that the names of the pseudo terminal pairs might depend on your OS.

PS I also don't know if you can configure hyperterm to send a character at the moment that you type it. I also don't know if this is possible in minicom (still trying).

Last edited by Wim Sturkenboom; 07-10-2010 at 04:01 AM. Reason: Added PS
 
Old 07-10-2010, 04:43 AM   #10
lalithasankh
LQ Newbie
 
Registered: May 2010
Posts: 28

Original Poster
Rep: Reputation: 15
Dear Wim Sturkenboom,

Thanks for your mail. I actually has arm 9 board and it starts and bootups from serial and I have connected arm9 serial port to windows hyper-terminal. But I develope compile application in fedora (linux) machine and download this file to arm9 board through windows hyperterminal. by sftp...


I think this is very clear now... (I am not using minicom any whrere)...

as u said in your mail once I run the application I get #09,R$ (since hyper terminal is already connected) but while reading data I am facing the problem (I want to receive data any time, it sholud not block the application)

not by waiting for number of charcter, any no of character should be received....
 
Old 07-10-2010, 06:02 AM   #11
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
Your read is fine. The problem is that hyperterm more than likely uses buffered IO in its current configuration and will only send once you have pressed <enter>. So you must look in the hyperterm documentation if you can change that behaviour.
 
Old 07-10-2010, 06:21 AM   #12
lalithasankh
LQ Newbie
 
Registered: May 2010
Posts: 28

Original Poster
Rep: Reputation: 15
termios setting

I don't think it is hyper terminal setting problem..
since i have interfaced another controller once I put in code 0x0D the data isread by ARM9 board ...

I feel it all settings of termios...

do have any ready made code which receives data on interrupt so that it will not block the normal flow...
 
Old 07-10-2010, 06:33 AM   #13
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
You might be right

From man termios:
Code:
In non-canonical mode input is available immediately (without the  user
having  to  type  a line-delimiter character), and line editing is dis‐
abled.  The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME])  deter‐
mine  the  circumstances  in  which a read(2) completes; there are four
distinct cases:
...
...
So don't use ICANON might do the trick

Last edited by Wim Sturkenboom; 07-10-2010 at 06:34 AM.
 
Old 07-10-2010, 06:37 AM   #14
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
You are right; I've just changed ICANON in your code to ~ICANON and it works. Learned something again that I had forgotten.
 
  


Reply

Tags
linux, programming


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
termios confused jeroends Programming 16 11-09-2009 02:51 PM
set baudrate to 256000 within termios.h agostain Linux - Hardware 3 01-02-2009 07:56 AM
Assistance With Termios Settings Please Peatmoss Linux - Newbie 3 06-01-2008 09:54 PM
managing termios for ser ports DavidHB Linux - Software 2 09-21-2006 07:37 AM
Figuring out a 'c_oflag'(man termios) that will help in stdout supression. Tarts Programming 13 08-11-2003 09:04 AM


All times are GMT -5. The time now is 05:40 PM.

Main Menu
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