This shows you the differences between two versions of the page.
sdk:scripts:serial-readwrite [2015/05/05 15:04] |
sdk:scripts:serial-readwrite [2015/05/05 15:04] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== SDK Script serial-readwrite.are ====== | ||
+ | <code c serial-readwrite.are> | ||
+ | /* DESC: This script will write to and read from the serial port. | ||
+ | * Copyright (C) 2012 NetModule AG, Switzerland | ||
+ | */ | ||
+ | |||
+ | DEV = "SERIAL1"; | ||
+ | |||
+ | /* check serial port config */ | ||
+ | st = nb_config_get("serial.0.status"); | ||
+ | if (st != "2") { | ||
+ | printf("Serial port is not enabled for us\n"); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* set attributes first */ | ||
+ | ret = nb_serial_setattr(DEV, 115200, 8, 1, 0, 0); | ||
+ | if (ret != 0) { | ||
+ | printf("Could not set serial attributes\n"); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | |||
+ | if (nb_serial_write(DEV, "hello world\n") == -1) { | ||
+ | printf("Could not write to serial port\n"); | ||
+ | } | ||
+ | |||
+ | |||
+ | while (1) { | ||
+ | msg = nb_serial_read(DEV); | ||
+ | if (msg) { | ||
+ | printf("read: %s\n", msg); | ||
+ | } else { | ||
+ | sleep(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | exit(0); | ||
+ | |||
+ | </code> | ||