I\'m attempting to read from my serial port using the following C code. I can successfully write to a listening computer (yay!) but the read throws Error (Code 11 - Resource tem
You have a buffer overflow here:
sprintf(message,"Test%d\r",running);
since message
is declared as:
char message[6];
message
needs to be at least 7 characters in size if it's going to hold a 6 character string, due to the need for a '\0'
terminator.
There may well be other problems too, but you should fix this and see if it makes any difference.
With O_NDELAY gone the program just sits there waiting for input
Looks like termios is setup for canonical input (based on the icanon in the stty output). In canonical (aka cooked) mode, the characters received from the serial port are processed before being made available to the user program using read().
Per the Linux man page:
In canonical mode:
- Input is made available line by line. An input line is available when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at the start of line). Except in the case of EOF, the line delimiter is included in the buffer returned by read(2).
Your termios also has icrnl set, which means that a carriage return is translated to newline on input (unless igncr is set, which is not since it has a preceding hyphen). Both eol and eol2 are undefined (which are the default values).
So for your setup, an end-of-line is defined as a newline or a carriage return (or cntl-D at the start of the line). Verify that your remote device is actually sending a CR or LF control character to terminate the line. Your comment in the code indicates that it is not (i.e. "/r" is not a carriage return).
To properly use the text returned by read() as a string, set the request for one less than the allocated buffer size (to ensure room for appending a null terminator). Then after a good return, use the returned byte count as the index to store the string terminator.
status = read(fd, buffer, sizeof(buffer) - 1);
if (status < 0) {
/* handle error condition */
} else {
buffer[status] = '\0';
printf("%s\n\r", buffer);
}
You're using the O_NDELAY
option in your open
call. This make the file descriptor non-blocking. This means that if you do a read
and there's no data available, the read
call will return EAGAIN
which is the error you're seeing.
For the moment, you can remove the O_NDELAY
from open
. In the future, you should probably make it non-blocking again and use either select
or poll
to determine when you can read.