1

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

}

Ottavio Campana
  • 4,088
  • 5
  • 31
  • 58
  • The "pro tip" would be to connect the rx line from the MCU into a second port on the PC computer, then view the data there in a terminal program. Otherwise you have to sit with your MCU debugger open at the same time as you code the PC program, which can be tedious, especially if the debugger doesn't live update watched variables. – Lundin May 15 '18 at 12:57
  • u r tellling for reading the data, i need to send some data from other devices? –  May 15 '18 at 13:01
  • im executing the code mentioned in the question. My device is sama5d3 xplained board. –  May 16 '18 at 04:55
  • i tested in minicom its connected.configuration & mode the board is in, which USB port on the board is is fine. Can you help me with that.Read is only not happening. –  May 16 '18 at 09:22
  • what are the issues in my code? can you figure out? –  May 16 '18 at 10:28
  • you are not answering to my question, if you don't know please keep quiet if my code is wrong how the write is happening. if you're a pro help me to solve the errors. i'm not here for reading blank comments. –  May 17 '18 at 05:06

0 Answers0