Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Problems with reading UART
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
smuryginim
n00b
n00b


Joined: 04 Oct 2012
Posts: 3

PostPosted: Thu Oct 04, 2012 8:05 am    Post subject: Problems with reading UART Reply with quote

Hi, all. I'm a beginner in Linux programming. And when i tried to read symbols from UART I had some problems.
I used a serial programming guide, and create a code:

Code:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>

main() {
   int fd_serialport, rd;
   struct termios options;
   char data_in[255];
   
   data_in[0] = 1;
   
   fd_serialport = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
   
   if(fd_serialport == -1){
      perror("Unable to open /dev/ttyUSB0");
   }
   
   tcgetattr(fd_serialport, &options);
   
   cfsetispeed(&options, B230400);
   cfsetospeed(&options, B230400);
   
   options.c_lflag = 0;
   options.c_iflag = 0;
   options.c_oflag = 0;
   options.c_lflag = 0;
   options.c_cflag |= B230400 | CREAD | CS8 | HUPCL | CLOCAL;
//   options.c_iflag |= ISTRIP;//(INPCK | ISTRIP);
   
   tcflush(fd_serialport, TCIFLUSH);
   tcsetattr(fd_serialport, TCSANOW, &options);
      
   fcntl(fd_serialport, F_SETFL, 0);
   
   printf("Hello\n");
   
   rd = read(fd_serialport, &data_in[0], 1);
   printf("Cod of reading = %d\n", rd);
   printf("%d ",data_in[0]);
   
   while( read(fd_serialport, &data_in[0], 1) != -1 )
   {
//      printf("In loop");
      printf("%02x ",data_in[0]);
      if (data_in[0] == 0x40) // New string from the end of package
         printf("\n");
   }
   
}


The problem is that when i use
options.c_iflag |= ISTRIP
It will work, and read symbols from USART, but trip 8bits for 7bits.
If i disable ISTRIP it won't work.
It give the code of 1 for read operation.

Could someone help me?
Back to top
View user's profile Send private message
smuryginim
n00b
n00b


Joined: 04 Oct 2012
Posts: 3

PostPosted: Thu Oct 04, 2012 12:46 pm    Post subject: Reply with quote

it was OK
It necessery to use:

fflush(stdout);

affter read() operation
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum