EE458 - Embedded Systems Nburn System I/O Outline References

advertisement
EE458 - Embedded Systems
Nburn System I/O
●
Outline
–
–
–
–
●
Introduction
File Descriptors
The write Routine
The read Routine
References
–
–
–
NBEclipse Getting Started Guide (Chs 5 & 8)
Netburner Tools User's Manual (Chs 17-19)
Netburner Runtime Library (Ch 11)
1
Nburn System I/O
Introduction
●
●
The network communication facilities use
the Netburner system I/O routines (read
and write). These are the (mostly)
POSIX I/O routines that are used by many
RTOS and UNIX OSes.
The system I/O routines can be used on the
Netburner for console, TCP socket, and
serial port I/O.
2
Nburn System I/O
Introduction
●
●
open, read, write, close, etc are the only
routines provided by the operating system
for doing I/O.
Programming languages may provide their
own I/O routines (fopen, printf, scanf,
getc, putc, fclose, etc in C and I/O
streams in C++ for example), but these
routines are all built on top of the system I/O
routines.
3
Nburn System I/O
File Descriptors
●
●
●
The system I/O routines use file descriptors.
A file descriptor is an integer value that is
associated with a communication stream.
The Netburner connect() and OpenSerial()
routines return file descriptors.
Standard input, standard output, and
standard error are available automatically
and correspond to file descriptors 0, 1, and
2 respectively.
4
Nburn System I/O
The write Routine
●
The write() function writes data to the
stream associated with a file descriptor:
int write(int fd,const char *buf,int nbyte);
●
fd is the file descriptor of the stream that we
want to write data to. buf is a pointer to the
area of memory containing data. nbyte is
the number of bytes of data that you want to
write. The number of bytes actually written
is returned. A negative number is returned
if an error occurs.
5
Nburn System I/O
The write Routine
●
●
write() writes binary data, it does not
convert numbers to ASCII like
printf or cout.
The write() call will block until at least one
byte is written. It does not have to block
until all requested bytes have been written.
You must compare the return value to the
requested number of bytes and retry until all
data is written.
6
Nburn System I/O
The write Routine
●
Here is one method to keep retrying until
data has been written:
n = 0;
nbytes = 10;
while (n != nbytes) {
nbytes = nbytes – n;
n = write(fd, (char *)ptr + n, nbytes);
if (n <= 0) break;
}
// check for n==0 (okay) or n<0 (error)
7
Nburn System I/O
The write Routine
●
write() examples:
char str[ ] = “Hello world!\n”;
write(1, str, strlen(str));
// Binary write to stdout
int n = 12345;
write(1, (char *)&n, sizeof(int));
// Formatted write to stdout
sprintf(str, “%d\n”, n);
write(1, str, strlen(str));
8
Nburn System I/O
The read Routine
●
The read() function reads data from a
stream associated with a file descriptor:
int read(int fd,const char *buf,int nbytes);
●
fd is the file descriptor of the stream that we
want to read data from. buf is a pointer to
the area of memory where the data should
be stored. nbytes is the number of bytes of
data that you want to read. The number of
bytes actually read is returned. A negative
number is returned if an error occurs.
9
Nburn System I/O
The read Routine
●
●
read() reads binary data, it does not convert
numbers from ASCII like scanf or cin.
The read() call will block until at least
one byte is read. It does not have to block
until all requested bytes have been read.
You must compare the return value to the
requested number of bytes and retry until all
data is read.
10
Nburn System I/O
The read Routine
●
read() examples:
char str[100];
n = read(0, str, 99);
str[n] = '\0';
// Binary read from stdin
int n;
read(0, (char *)&n, sizeof(int));
11
Download