Upload packaging folder
[platform/upstream/iotjs.git] / docs / api / IoT.js-API-UART.md
1 ### Platform Support
2
3 The following shows uart module APIs available for each platform.
4
5 |  | Linux<br/>(Ubuntu) | Raspbian<br/>(Raspberry Pi) | Nuttx<br/>(STM32F4-Discovery) |
6 | :---: | :---: | :---: | :---: |
7 | uart.open | O | O | O |
8 | uartport.write | O | O | O |
9 | uartport.writeSync | O | O | O |
10 | uartport.close | O | O | O |
11 | uartport.closeSync | O | O | O |
12
13
14 ## Contents
15   * [UART](#uart)
16     * [Constructor](#uart-constructor)
17       * [`new UART()`](#new-uart)
18     * [Prototype Methods](#uart-prototype-methods)
19       * [`uart.open(configurable[, callback])`](#uart-open)
20   * [UARTPort](#uartport)
21     * [Prototype Methods](#uartport-prototype-methods)
22       * [`uartport.write(data[, callback])`](#uartport-write)
23       * [`uartport.writeSync(data)`](#uartport-write-sync)
24       * [`uartport.close([callback])`](#uartport-close)
25       * [`uartport.closeSync()`](#uartport-close-sync)
26     * [Event](#uartport-event)
27
28
29 ## Class: UART <a name="uart"></a>
30
31
32 ## Constructor <a name="uart-constructor"></a>
33
34
35 ### `new UART()` <a name="uart-new"></a>
36
37 Returns a new UART object which can open UART port.
38
39
40 ## Prototype methods <a name="uart-prototype-methods"></a>
41
42
43 ### `uart.open(configuration[, callback])` <a name="uart-open"></a>
44 * `configuration <Object>`
45   * `device <String>`, mandatory configuration
46   * `baudRate <Number> (Default value is 9600)`
47   * `dataBits <Number> (Default value is 8)`
48 * `callback <Function(err: Error | null)>`
49 * Returns: `<UARTPort>`
50
51 On Nuttx, you can set the properties of `configuration` only on Nuttx config. `Device Drivers -> Serial Driver Support -> U[S]ART(N) Configuration` will help. And if you want to get information about `device`, please see below list.
52   * [STM32F4-discovery](../../targets/nuttx-stm32f4/Stm32f4dis.md#uart)
53
54 Opens UART port with the specified configuration. Following methods can be called with UARTPort object.
55
56
57 **Example**
58 ```javascript
59 var Uart = require('uart');
60
61 var uart = new Uart();
62 var configuration = {
63   device: '/dev/ttyUSB0'
64   baudRate: 115200,
65   dataBits: 8,
66 }
67
68 var serial = uart.open(configuration, function(err) {
69   // Do something.
70 });
71 ```
72
73
74 ## Class: UARTPort <a name="uartport"></a>
75
76
77 ## Prototype methods <a name="uartport-prototype-methods"></a>
78
79
80 ### `uartport.write(data[, callback])` <a name="uartport-write"></a>
81 * `data <String>`
82 * `callback <Function(err: Error | null)>`
83
84 Writes data to UART device asynchronously.
85
86 **Example**
87 ```javascript
88 serial.write("Hello?", function(err) {
89   if (err) {
90     // Do something.
91   }
92   serial.close();
93 });
94 ```
95
96 ### `uartport.writeSync(data)` <a name="uartport-write-sync"></a>
97 * `data <String>`
98
99 Writes data to UART device synchronously.
100
101 **Example**
102 ```javascript
103 serial.writeSync("Hello?");
104 serial.close();
105 ```
106
107
108 ### `uartport.close([callback])` <a name="uartport-close"></a>
109 * `callback <Function(err: Error | null)>`
110
111 Closes UART device asynchronously.
112
113
114 ### `uartport.closeSync()` <a name="uartport-close-sync"></a>
115
116 Closes UART device synchronously.
117
118
119 ## Events <a name="uartport-event"></a>
120
121
122 ### `'data'`
123 * `callback <Function(data)>`
124
125  `data` is a string from sender
126
127 For example,
128 ```javascript
129 ...
130 serial.on('data', function(data) {
131   console.log('read result: ' + data.toString());
132 });
133
134 ```