LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   termios programming in linux (http://www.linuxquestions.org/questions/programming-9/termios-programming-in-linux-817301/)

lalithasankh 07-01-2010 12:39 AM

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;
 
 }


PTrenholme 07-01-2010 08:24 PM

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.

Wim Sturkenboom 07-02-2010 01:06 AM

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

PS I've reported your post to be moved to the programming section

XavierP 07-02-2010 04:00 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

lalithasankh 07-09-2010 01:00 AM

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 ...

lbuccola 07-09-2010 11:52 AM

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 "|="

jf.argentino 07-09-2010 12:28 PM

Not sure to understand what do you want to achieve but see if removing the "ICANON" flag solve your issue (google for termios canonic)

lalithasankh 07-10-2010 01:04 AM

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..

Wim Sturkenboom 07-10-2010 04:56 AM

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).

lalithasankh 07-10-2010 05:43 AM

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....

Wim Sturkenboom 07-10-2010 07:02 AM

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.

lalithasankh 07-10-2010 07:21 AM

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...

Wim Sturkenboom 07-10-2010 07:33 AM

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

Wim Sturkenboom 07-10-2010 07:37 AM

You are right; I've just changed ICANON in your code to ~ICANON and it works. Learned something again that I had forgotten.


All times are GMT -5. The time now is 02:35 PM.