Hardware Reference
In-Depth Information
where
fd
is the open serial port file descriptor for reading/writing.
iov
is the I/O vector directing the reading/writing.
iovcnt
is the I/O vector count.
returns an
int
, where
-1
indicates an error, leaving the error code in
errno
, see
read(2)
or
write(2)
.
0
indicates that an end-of-file condition occurred.
>n indicates the actual number of bytes read/written.
The I/O vector is shown here:
struct iovec {
void *iov _base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
In the following example, a simple terminal
writev(2)
system call is used to piece
together three pieces of information, to be transmitted to the terminal:
Hello
•
The text
name
•
The person's name provided in the argument
!\n\r
at the end
One of the advantages of the
writev(2)
call is its ability to take separate buffers of
data and transmit them as a whole in one I/O operation:
•
The text
void
fun(int serport, const char *name) {
struct iovec iov[3];
int rc;
iov[0].iov_base = "Hello";
iov[0].iov_len = 6;
iov[1].iov_base = (void *)name;
iov[1].iov_len = strlen(name);
iov[2].iov_base = "!\n\r";
iov[2].iov_len = 3;
rc = writev(serport,iov,3);
if ( rc < 0 ) {
fprintf(stderr,"%s: writev(2)\n",strerror(errno));
abort();
}
}