modem_open()
Open a serial port
Synopsis:
#include <sys/modem.h>
int modem_open( char* device,
speed_t baud );
Arguments:
- device
- The path name of the serial port that you want to open.
- baud
- Zero, or the baud rate that you want to use.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The modem_open() function opens a serial port identified by device. The device is set to raw mode by changing the control flags using tcgetattr() and tcsetattr() as follows:
termio.c_cflag = CS8|IHFLOW|OHFLOW|CREAD|HUPCL;
termio.c_iflag = BRKINT;
termio.c_lflag = IEXTEN;
termio.c_oflag = 0;
Any pending input or output characters are discarded.
If baud is nonzero, then the baud rate is changed to that value.
Returns:
An open file descriptor, or -1 on failure (errno is set).
Errors:
- EACCES
- Search permission is denied on a component of the path prefix, or the file doesn't exist.
- EBADFSYS
- While attempting to open the named file, either the file itself or a component of the path prefix was found to be corrupted. A system failure—from which no automatic recovery is possible—occurred while the file was being written to, or while the directory was being updated. You'll need to invoke appropriate systems-administration procedures to correct this situation before proceeding.
- EBUSY
- The file named by device is a block special device that's already open for writing, or device names a file that's on a filesystem mounted on a block special device that's already open for writing, or device is in use.
- EINTR
- The open operation was interrupted by a signal.
- EISDIR
- The named device is a directory.
- ELOOP
- Too many levels of symbolic links or prefixes.
- EMFILE
- All file descriptors available to the process are currently open.
- ENAMETOOLONG
- The length of the device string exceeds PATH_MAX.
- ENFILE
- Too many files are currently open in the system.
- ENOENT
- The named device doesn't exist, or the path argument points to an empty string.
- ENOSYS
- The modem_open() function isn't implemented for the filesystem underlying the path specified in device.
- ENOTDIR
- A component of the path prefix isn't a directory.
- ENXIO
- No process has the file open for reading.
Examples:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/modem.h>
#include <stdio.h>
#include <errno.h>
/*
curstate curflags newstate newflags newtimeout
newquiet retvalue pattern response
*/
struct modem_script table[] ={
{1, 0, 1, 0, 2, 5, 0, NULL, "ATZ\\r\\P0a"},
{1, 0, 2, 0, 30, 5, 0, "*ok*", "ATDT5910934"},
{2, MODEM_BAUD, 3, MODEM_LASTLINE, 10, 5, 0, "*connect*", NULL},
{3, 0, 4, 0, 8, 5, 0, "*login:*", "guest"},
{4, MODEM_NOECHO, 5, 0, 15, 5, 0, "*password:*", "xxxx"},
{5, 0, 0, 0, 0, 0, 0, "*$ *", NULL},
{0, 0, 0, 0, 0, 0, 1, "*no carrier*", NULL},
{0, 0, 0, 0, 0, 0, 2, "*no answer*", NULL},
{0, 0, 0, 0, 0, 0, 3, "*no dialtone*", NULL},
{0, 0, 0, 0, 0, 0, 4, "*busy*", NULL},
{ NULL }
};
void io(char* progress, char* in, char* out) {
if(progress)
printf("progress: %s\n", progress);
if(in)
printf("input: %s\n", in);
if(out)
printf("output: %s\n", out);
}
int main(int argc, char* argv[]) {
int fd, status;
speed_t baud = -1;
if((fd = modem_open(argv[1], 0)) == -1) {
fprintf(stderr, "Unable to open %s: %s\n",
argv[1], strerror(errno));
exit(1);
}
status = modem_script( fd, table, &baud,
&io, NULL );
printf("status=%d baud=%d\n", status, baud);
exit(status);
}
Classification:
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
Page updated:
