回答編集履歴

2

送信コマンドとデータ受信を1対1にする例を追加

2015/08/19 16:01

投稿

退会済みユーザー
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
 
16
16
 
17
+ 単純に考えれば下記のように後で実行したいコマンドのまとまりを関数にして行く方法でしょうか。いわゆるCallback地獄なので、[Promise](http://qiita.com/koki_cheese/items/c559da338a3d307c9d88)を使うと幸せになりそうですね。
18
+
17
19
  ```
18
20
 
19
21
  var serial = require('serialport').SerialPort;
@@ -22,15 +24,17 @@
22
24
 
23
25
 
24
26
 
25
- serial.on('open',function(){
26
-
27
- serial.on('data',function(data){
27
+ var initialize = function(next){
28
-
29
- console.log('data : '+data);
30
-
31
- });
32
28
 
33
29
  serial.write('i,6\r\n',function(err){
30
+
31
+ serial.on('data',function(data){
32
+
33
+ console.log('data : '+data);
34
+
35
+ next();
36
+
37
+ });
34
38
 
35
39
  console.log('write \'i,6\'');
36
40
 
@@ -38,6 +42,40 @@
38
42
 
39
43
  });
40
44
 
45
+ }
46
+
47
+
48
+
49
+ var nextCommand = function(next){
50
+
51
+ serial.write('i,1\r\n',function(err){
52
+
53
+ serial.on('data',function(data){
54
+
55
+ console.log('data : '+data);
56
+
57
+ next();
58
+
59
+ });
60
+
61
+ console.log('write \'i,1\'');
62
+
63
+ console.log(err || 'no err');
64
+
65
+ });
66
+
67
+ }
68
+
69
+
70
+
71
+ serial.on('open',function(){
72
+
73
+ initialize(nextCommand);
74
+
41
75
  });
42
76
 
43
77
  ```
78
+
79
+
80
+
81
+ [GitHubにはgo-irMagician](https://github.com/tknhs/go-irMagician)があるので、node-irMagicianができるといいですね。

1

送信コマンドとデータ受信を1対1にする例wo

2015/08/19 16:01

投稿

退会済みユーザー
test CHANGED
@@ -3,3 +3,41 @@
3
3
 
4
4
 
5
5
  参考: [serial.write()](https://www.npmjs.com/package/serialport#write-buffer-callback) には`Note: The write operation is non-blocking. `と書かれています。non-blockingなので、非同期呼び出しです。
6
+
7
+
8
+
9
+ ---
10
+
11
+
12
+
13
+ ### 送信コマンドとデータ受信を1対1にする例
14
+
15
+
16
+
17
+ ```
18
+
19
+ var serial = require('serialport').SerialPort;
20
+
21
+ serial = new serial('/dev/ttyACM0',{baudrate:9600});
22
+
23
+
24
+
25
+ serial.on('open',function(){
26
+
27
+ serial.on('data',function(data){
28
+
29
+ console.log('data : '+data);
30
+
31
+ });
32
+
33
+ serial.write('i,6\r\n',function(err){
34
+
35
+ console.log('write \'i,6\'');
36
+
37
+ console.log(err || 'no err');
38
+
39
+ });
40
+
41
+ });
42
+
43
+ ```