Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:scripts:serial-tcsetattr [2015/05/05 15:04]
sdk:scripts:serial-tcsetattr [2015/05/05 15:04] (current)
Line 1: Line 1:
 +====== SDK Script serial-tcsetattr.are ======
 +<code c serial-tcsetattr.are>​
 +/* DESC: This script can be used to set/get the attributes of the serial port.
 + * Copyright (C) 2012 NetModule AG, Switzerland
 + */
 +
 +void usage() ​
 +{
 +    printf("​usage:​ serial-tcsetattr.are <dev> <​baud>​ <​databit>​ <​stopbit>​ <​parity>​ <​flow>​\n"​);​
 +    printf(" ​   dev        device to open (e.g. SERIAL1)\n"​);​
 +    printf(" ​   baud       ​baudrate (e.g. 115200)\n"​);​
 +    printf(" ​   databit ​   data bit [7|8]\n"​);​
 +    printf(" ​   stopbit ​   stopbit [0|1]\n"​);​
 +    printf(" ​   parity ​    ​parity [0|1]\n"​);​
 +    printf(" ​   flow       ​flow-control [0|1]\n"​);​
 +   
 +    exit(1);
 +}
 +
 +
 +if (argc < 6) {
 +   ​usage();​
 +}
 +
 +DEV     = argv[1];
 +BAUD    = (int)argv[2];​
 +DATABIT = (int)argv[3];​
 +STOPBIT = (int)argv[4];​
 +PARITY ​ = (int)argv[5];​
 +FLOWCTL = (int)argv[6];​
 +
 +/* check serial port config */
 +idx = right(DEV, 1);
 +
 +if (idx < 0 || idx > 1) {
 +    nb_syslog("​Invalid serial port specified"​);​
 +    exit(1);
 +
 +}
 +st = nb_config_get(sprintf("​serial.%d.status",​ idx));
 +if (st != "​2"​) {
 +    nb_syslog("​Serial port is not enabled for us");
 +    exit(1);
 +}
 +
 +
 +ret = nb_serial_setattr(DEV,​ BAUD, DATABIT, STOPBIT, PARITY, FLOWCTL);
 +if (ret == 0) {
 +    printf("​Successfully set serial attributes of %s\n", DEV);
 +} else {
 +    printf("​Unable to set serial attributes of %s\n", DEV);
 +    exit(1);
 +}
 +
 +attr = nb_serial_getattr(DEV);​
 +if (is_void(attr)) {
 +    printf("​Unable to get attributes of %s\n", DEV);
 +    exit(1);
 +}
 +
 +printf("​%s is now configured as follows:​\n",​ DEV);
 +printf("​baudrate:​ %d\ndatabit:​ %d\nstopbit:​ %d\nparity: %d\nflowctl:​ %d\n",
 +        struct_get(attr,​ "​baudrate"​),​
 +        struct_get(attr,​ "​databit"​),​
 +        struct_get(attr,​ "​stopbit"​),​
 +        struct_get(attr,​ "​parity"​),​
 +        struct_get(attr,​ "​flowctl"​)
 +);
 +
 +
 +exit(0);
 +
 +</​code>​