smuryginim n00b
Joined: 04 Oct 2012 Posts: 3
|
Posted: Thu Oct 04, 2012 8:05 am Post subject: Problems with reading UART |
|
|
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? |
|