Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Reading from serial port
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 07-02-2007   #1 (permalink)
Just Joined!
 
Join Date: Jul 2007
Posts: 1
Reading from serial port

Hi,All.
I'm trying to write a program that can write to and read from serial port(ttys0).
I can open port and write it successfully but can not read from it.The number of read bytes always is zero.Below is the source code.Any idea?
/*******************************************
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
using namespace std;
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
int n;

for (int i=0;i<3;i++)
{
char s[10];
cout<<"\n input string:";
cin >>s;

//**************************************
n = write(fd, s, strlen(s));

if (n>0)
printf("writes %d bytes\n",n);
else
printf("doese not write\n");

//**************************************
char buf[100];
n=read(fd,buf,n);
if (n<0)
printf("doese not read");
else
printf("\n%d bytes read",n);
}
tcsetattr(fd,TCSANOW,&oldtio);
}
minoo is offline   Reply With Quote
Old 07-04-2007   #2 (permalink)
Just Joined!
 
Join Date: Nov 2006
Location: Hyderabad
Posts: 52
hi,

try to add some changes which i written in code.

fcntl(fd,F_SETFL,O_NDELAY);
newtio.c_cc[VMIN] = 1;




Quote:
Originally Posted by minoo View Post
Hi,All.
I'm trying to write a program that can write to and read from serial port(ttys0).
I can open port and write it successfully but can not read from it.The number of read bytes always is zero.Below is the source code.Any idea?
/*******************************************
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
using namespace std;
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 0;
//newtio.c_cc[VMIN] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
int n;

for (int i=0;i<3;i++)
{
char s[10];
cout<<"\n input string:";
cin >>s;

//**************************************
n = write(fd, s, strlen(s));

if (n>0)
printf("writes %d bytes\n",n);
else
printf("doese not write\n");

//**************************************
char buf[100];
fcntl(fd,F_SETFL,O_NDELAY);
usleep(50000);

n=read(fd,buf,n);
if (n<0)
printf("doese not read");
else
printf("\n%d bytes read",n);
}
tcsetattr(fd,TCSANOW,&oldtio);
}
rajkumarmadhani is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows key on my keyboard......... gassman SuSE Linux Help 25 05-04-2005 08:50 PM
Problems mounting digicam kodak dx6340 m3thamph Linux Newbie 1 10-02-2004 03:51 PM
APF Firewall help Blue|Fusion Linux Security 0 08-01-2004 01:33 AM

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 10:20 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0