ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language 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.
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
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.
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
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..
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
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....
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.