Hardware Reference
In-Depth Information
The errors that pertain to blocking calls on a serial port include the following:
Error
Description
EBADF
fd is not a valid file descriptor.
EFAULT
buf is outside your accessible address space.
EINTR
The call was interrupted by a signal before any data was read.
More will be said about EINTR near the end of this chapter.
The following example reads up to 256 bytes into the array buf , from the serial port
open on the file unit fd :
int fd; /* Opened serial port */
char buf[256];
int rc;
rc = read(fd,buf,sizeof buf);
if ( rc < 0 ) {
fprintf(stderr,"%s: reading serial port.\n",strerror(errno));
...
} else if ( !rc ) {
/* End file */
} else {
/* Process rc bytes in buf[] */
}
write(2)
To transmit data on a serial link, you can use the write(2) system call:
#include <unistd.h>
ssize_t write(int fd, const void * buf, size_t count);
where
fd is the file unit of the opened serial port.
buf is the buffer containing the bytes to be transmitted.
count is the number of bytes to transmit.
returns an int , where
-1 indicates that an error has occurred, with the error found in
errno .
0 indicates no bytes were transmitted (end-of-file, port was
closed).
>0 indicates the number of bytes transmitted.
 
 
Search WWH ::




Custom Search