SDK Script serial-read.are

serial-read.are
/* DESC: This script can be used to read messages from the serial port.
 * Copyright (C) 2012 NetModule AG, Switzerland
 */
 
DEV = "SERIAL1";
READ_TIMEOUT = 5; /* seconds */
 
void usage()
{
    printf("usage: serial-read.are\n");
    exit(1);
}
 
/* check serial port config */
st = nb_config_get("serial.0.status");
if (st != "2") {
    nb_syslog("Serial port is not enabled for us");
    exit(1);
}
 
 
/* set attributes */
ret = nb_serial_setattr(DEV, 115200, 8, 1, 0, 0);
 
if (ret != 0) {
    printf("Could not set serial attributes: %i \n",ret);
    exit(1);
}
 
 
/* open serial port */
fd = nb_serial_open(DEV);
if (fd < 0) {
    printf("Unable to open %s\n", DEV);
    exit(1);
}
 
while (1) {
    rc = select(fd, READ_TIMEOUT);
    if (rc == -1) {
        printf("ERROR: select failed\n");
        close(fd);
        exit(1);
    } else if (rc == 0) {
        /* nothing received */
        printf("timeout reached\n");
    } else {
        /* received something */
        msg = read(fd, 1024);
        if (msg) {
            printf("read: %s\n", msg);
        }
    }
}
close(fd);
 
exit(0);