Upload packaging folder
[platform/upstream/iotjs.git] / test / run_pass / test_uart.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 var assert = require('assert');
17 var Uart = require('uart');
18
19 var uart = new Uart();
20
21 var configuration = {
22   device: '/dev/ttyUSB0',
23   baudRate: 115200,
24   dataBits: 8
25 };
26
27 var read = 0;
28 var write = 0;
29
30 var serial = uart.open(configuration, function(err){
31   assert.equal(err, null);
32   console.log('open done');
33
34   serial.on('data', function(data) {
35     console.log('read result: ' + data.toString());
36     read = 1;
37
38     if (read && write) {
39       serial.closeSync();
40       console.log('close done');
41     }
42   });
43
44   serial.writeSync("Hello IoT.js.\n\r");
45
46   serial.write("Hello there?\n\r", function(err) {
47     assert.equal(err, null);
48     console.log('write done');
49     write = 1;
50
51     if (read && write) {
52       serial.closeSync();
53       console.log('close done');
54     }
55   });
56 });