Go Down

Topic: How to read from serial in c ? (under linux) (Read 1 time) previous topic - next topic

Phate867

I've done it..normal kernel module works and I know I cannot use gnu c library, if you look at the code I'm not using it at all.

Phate867

Tried with a normal user space program and still does not work: a lot of strange characters come out.
I see a problem in your code too: you do not set baud rate at all, I think this has to be a problem...

pekkaa


Tried with a normal user space program and still does not work: a lot of strange characters come out.
I see a problem in your code too: you do not set baud rate at all, I think this has to be a problem...


Yes, you obviously have to set the baud rate and possibly other tty parameters. You can use stty from the command line before running the program or cfsetspeed function in your program.

Phate867

I'm trying this way but still no success:
Code: [Select]

        struct termios options;
int nBytesRead = 0;
char buffer[20];

int fd = open(SERIAL_PORT,O_RDONLY | O_NOCTTY);

if(fd == -1){
perror("unable to open serial port");
return -1;
}

      //setting baud rate
tcgetattr(fd, &options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd,TCSANOW,&options);
fcntl(fd,F_SETFL,0); //setting blocking read

nBytesRead = read(fd,buffer,20);

now it just immediately returns from read with 0 bytes read...but read() should be blocking.

pylon

You haven't told the tty after how many characters it should return from a blocking call:

Code: [Select]
options.c_cc[VMIN] = 20; // block until 20 characters are read

This topic gets very Linux specific, don't you think you should ask your questions on a Linux forum? Your questions have nothing to do with the Arduino platform.

Go Up