unable to read from serial port
I have been trying to communicate with device trhough /dev/ttyS0.
The device sends out data as soon as it powers up but the read just gets stuck and I cant proceed any further. After the initial startup data from the device, commands can be sent to it to control it. I can send one command to it successfully but after that I cant control the device.
Could the serial port be blocked by the kernel for some reason or is my code itself wrong. I have posted the code below and I would be grateful for any help regarding this.
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<termios.h>
int main()
{
int fd=-1,res,n,i;
char buff[100];
struct termios ol;
struct termios nw;
fd=open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);//opening port
fcntl(fd,F_SETFL,0);//returning control
if(fd<0)
printf("%s","error opening port\n\n");
tcgetattr(fd,&ol); //getting serial port attributes
nw.c_cflag |=B115200; //baud rate
nw.c_cflag |=(CLOCAL|CREAD);
nw.c_cflag &=~PARENB;
nw.c_cflag &=~CSTOPB;//8N1 settings
nw.c_cflag &=~CSIZE;
nw.c_cflag |=CS8;
nw.c_lflag |=~(ICANON | ECHO | ECHOE | ISIG);
if( tcsetattr(fd,TCSANOW,&nw)<0)
printf("%s","error in setting up device\n\n");
tcgetattr(fd,&nw);
printf("%s","trying to read");
bytes=fcntl(fd,F_SETFL,FNDELAY); //to avoid serial port blocking
res=read(fd,buff,100); //trying to read initial setup data-not working
if(res<0)
printf("%s","read failed\n");
else
for(i=0;i<=99;i++)
printf(":%c:%d\n",buff[i],res);
n=write(fd,"debug d 35 5 0\r",1000); //writing command to WSS
if(n<0)
printf("%s","write failed");
else
printf("\n\n\n%d",n);
return 0;
}
|