I'm using sama5d3 development board. I'm trying to read and write data to the device connected to the USB port. 2.i can write the data to the device.
3.How can i read data from the device?
4.How can i know whether the data is writing to the device?`
int main()
{
char fd1,retval,speed;
struct termios usb_parm;
fd1=open("/dev/ttyACM0",O_RDONLY|O_WRONLY|O_NONBLOCK|O_NOCTTY);
if(fd1>0)
{
printf("Device detected\n");
}
else
{
printf("Port free\n");
return -1;
}
ioctl(fd1,TCSETS2,&usb_parm);
usb_parm.c_cflag &=~CBAUD;
usb_parm.c_iflag=speed;
usb_parm.c_oflag=speed;
usb_parm.c_cflag |=BOTHER;
retval=ioctl(fd1,TCSETS2,&usb_parm);
printf("retval=%d\n",retval);
tcflush(fd1,TCIFLUSH);
tcsetattr(fd1,TCSANOW,&usb_parm);
tcgetattr(fd1,&usb_parm);
char write_buff[]="q,e";
int bytes_written=0;
bytes_written =write(fd1,write_buff,sizeof(write_buff));
printf("written to ttyUSB0=%s\n",write_buff);
printf("Bytes written to ttyUSB0=%d\n", bytes_written);
char read_buff[]={"gdfjhtfgjif"};
int bytes_read=0;
int byteavailable=0;
tcflush(fd1, TCIFLUSH);
ioctl(fd1,FIONREAD,&byteavailable);
memset(read_buff,0,sizeof(read_buff));
if(byteavailable>0)
{
bytes_read=read(fd1,read_buff,sizeof(read_buff));
}
else
{
printf("error\n");
return -1;
}
printf("Read buff=%s\n",read_buff);
printf("Bytes read=%d\n",bytes_read);
}