Hardware Reference
In-Depth Information
The possible errors related to blocking calls for serial port writes include the
following:
Error
Description
EBADF
fd is not a valid file descriptor or is not open for writing.
EFAULT
buf is outside your accessible address space.
EINTR
The call was interrupted by a signal before any data was written.
Normally, only an error ( -1 ) or a value of count is returned. If the serial port was
opened for nonblocking I/O, the returned count can be less than the requested count
(this mode of operation is not discussed here). In blocking mode (which we are assuming
here), the call will return only when the full count requested has been written. Any failure
would otherwise result in an error being returned instead.
The following is an example of its use, as it pertains to a serial port:
int fd;
char buf[256];
int rc, n;
strcpy(buf,"Hello World!\n");
n = strlen(buf);
rc = write(fd,buf,n);
if ( rc < 0 ) {
fprintf(stderr,"%s: writing serial link.\n",strerror(errno));
...
}
assert(rc == n);
readv(2) and writev(2)
An often neglected option for reading and writing are the readv(2) and writev(2)
system calls. These tend to be more useful for programs that work with packets than for
interactive terminal sessions. These are presented because the serial port application
developer may want to use a protocol that has more than one buffer containing headers,
data, and trailer. Using the scatter-gather routines can enhance your code communicating
with an AVR class microcontroller. The use of an I/O vector here is similar in concept to
the I/O vectors used by I2C I/O operations in the ioctl(2,I2C_RDWR) system call (see
Chapter 12).
#include <sys/uio.h>
ssize_t readv(int fd, const struct iovec * iov, int iovcnt);
ssize_t writev(int fd, const struct iovec * iov, int iovcnt);
 
 
Search WWH ::




Custom Search