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