Rs 232 pgm - Please help
Hi,
I am a newbie. I have to do Linux rs232 programming to connect a device.
the device settings,
9600, 7 bit, 1 start bit, 1 stop bit, Even parity.
I have written the below code. But there is no response.
Please, .. someone help me. I am very much struck in this.
*************************************************
struct termios newtio;
struct timeval tv;
int retval;
char buf[255];
FD_ZERO(&read_set);
tv.tv_sec = 5;
tv.tv_usec = 0;
fd=open("/dev/ttyAM1",O_RDWR | O_NOCTTY |O_NDELAY);
if(fd < 0)
{
fprintf(stderr, "Unable to open /dev/ttyAM1");
exit(1);
}
fcntl(fd,F_SETFL,FNDELAY);
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
newtio.c_cflag |= CREAD;
newtio.c_cflag |= CLOCAL;
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_cflag |= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS7;
newtio.c_cflag &= ~CRTSCTS;
newtio.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR);
newtio.c_lflag &= ~(ICANON | ECHO | ISIG );
newtio.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONLRET);
tcsetattr(fd,TCSANOW,&newtio);
FD_SET(fd,&read_set);
write(fd,&cmd,sizeof(cmd));
retval = select(fd+1,&read_set,NULL,NULL,&tv);
if(retval)
{
printf("Data is available");
res=read(fd,buf,255);
printf("\n");
printf("%d\n",res);
for(i=0;i<res;i++)
{
printf("%x",buf[i]);
}
}
else
printf("No data within five seconds");
printf("retval=%d",retval);
close(fd);
**********************************************************
Regards,
Madhi.
|