dial_a_device_node 0.0.153

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +674 -0
  5. data/README.md +94 -0
  6. data/Rakefile +1 -0
  7. data/app/assets/images/denver_summit.jpg +0 -0
  8. data/app/assets/images/eppendorf_innova_42.jpg +0 -0
  9. data/app/assets/images/heidolph.jpg +0 -0
  10. data/app/assets/images/ika_ret_control.jpg +0 -0
  11. data/app/assets/images/kern.jpg +0 -0
  12. data/app/assets/images/knf_rc900.jpg +0 -0
  13. data/app/assets/images/knf_sc920.jpg +0 -0
  14. data/app/assets/images/knf_simdos_02.jpg +0 -0
  15. data/app/assets/images/pce_balance.jpg +0 -0
  16. data/app/assets/images/purebeaglebone.jpg +0 -0
  17. data/app/assets/images/weathercape.jpg +0 -0
  18. data/app/assets/javascripts/dial_a_device_node/consolelogger.js +176 -0
  19. data/app/assets/javascripts/dial_a_device_node/deviceconnection.js +225 -0
  20. data/app/assets/javascripts/dial_a_device_node/devices/denver_summit.js +153 -0
  21. data/app/assets/javascripts/dial_a_device_node/devices/denver_summit_SIM.js +111 -0
  22. data/app/assets/javascripts/dial_a_device_node/devices/denver_summit_UI.js +121 -0
  23. data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42.js +128 -0
  24. data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42_SIM.js +66 -0
  25. data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42_UI.js +46 -0
  26. data/app/assets/javascripts/dial_a_device_node/devices/heidolph.js +62 -0
  27. data/app/assets/javascripts/dial_a_device_node/devices/heidolph_SIM.js +24 -0
  28. data/app/assets/javascripts/dial_a_device_node/devices/heidolph_UI.js +17 -0
  29. data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control.js +192 -0
  30. data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control_SIM.js +233 -0
  31. data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control_UI.js +96 -0
  32. data/app/assets/javascripts/dial_a_device_node/devices/kern.js +137 -0
  33. data/app/assets/javascripts/dial_a_device_node/devices/kern_SIM.js +118 -0
  34. data/app/assets/javascripts/dial_a_device_node/devices/kern_UI.js +121 -0
  35. data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900.js +289 -0
  36. data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900_SIM.js +188 -0
  37. data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900_UI.js +179 -0
  38. data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920.js +309 -0
  39. data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920_SIM.js +298 -0
  40. data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920_UI.js +441 -0
  41. data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02.js +283 -0
  42. data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02_SIM.js +130 -0
  43. data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02_UI.js +188 -0
  44. data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc.js +106 -0
  45. data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc_SIM.js +32 -0
  46. data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc_UI.js +26 -0
  47. data/app/assets/javascripts/dial_a_device_node/devices/pce_balance.js +111 -0
  48. data/app/assets/javascripts/dial_a_device_node/devices/pce_balance_SIM.js +80 -0
  49. data/app/assets/javascripts/dial_a_device_node/devices/pce_balance_UI.js +69 -0
  50. data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone.js +286 -0
  51. data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone_SIM.js +13 -0
  52. data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone_UI.js +256 -0
  53. data/app/assets/javascripts/dial_a_device_node/devices/weathercape.js +75 -0
  54. data/app/assets/javascripts/dial_a_device_node/devices/weathercape_SIM.js +33 -0
  55. data/app/assets/javascripts/dial_a_device_node/devices/weathercape_UI.js +42 -0
  56. data/app/assets/javascripts/dial_a_device_node/folderwatcher.js +43 -0
  57. data/app/assets/javascripts/dial_a_device_node/helper/WebSocket-Node-wrapper.js +36 -0
  58. data/app/assets/javascripts/dial_a_device_node/helper/XMLHttpRequest.js +579 -0
  59. data/app/assets/javascripts/dial_a_device_node/helper/agent.js +125 -0
  60. data/app/assets/javascripts/dial_a_device_node/helper/ajax.js +300 -0
  61. data/app/assets/javascripts/dial_a_device_node/helper/type.js +10 -0
  62. data/app/assets/javascripts/dial_a_device_node/php/comport.php +49 -0
  63. data/app/assets/javascripts/dial_a_device_node/php/php_serial.class.php +622 -0
  64. data/app/assets/javascripts/dial_a_device_node/systemstatus.js +124 -0
  65. data/app/assets/javascripts/dial_a_device_node/webconnection.js +197 -0
  66. data/app/assets/javascripts/dial_a_device_node/websocket_rails/abstract_connection.js.coffee +45 -0
  67. data/app/assets/javascripts/dial_a_device_node/websocket_rails/channel.js.coffee +70 -0
  68. data/app/assets/javascripts/dial_a_device_node/websocket_rails/event.js.coffee +42 -0
  69. data/app/assets/javascripts/dial_a_device_node/websocket_rails/http_connection.js.coffee +89 -0
  70. data/app/assets/javascripts/dial_a_device_node/websocket_rails/websocket_connection.js.coffee +37 -0
  71. data/app/assets/javascripts/dial_a_device_node/websocket_rails/websocket_rails.js.coffee +164 -0
  72. data/app/views/devices/ui/_denver_summit.html.erb +52 -0
  73. data/app/views/devices/ui/_eppendorf_innova_42.html.erb +72 -0
  74. data/app/views/devices/ui/_heidolph.html.erb +54 -0
  75. data/app/views/devices/ui/_ika_ret_control.html.erb +81 -0
  76. data/app/views/devices/ui/_kern.html.erb +52 -0
  77. data/app/views/devices/ui/_knf_rc900.html.erb +143 -0
  78. data/app/views/devices/ui/_knf_sc920.html.erb +194 -0
  79. data/app/views/devices/ui/_knf_simdos_02.html.erb +98 -0
  80. data/app/views/devices/ui/_pce_balance.html.erb +37 -0
  81. data/app/views/devices/ui/_purebeaglebone.html.erb +137 -0
  82. data/app/views/devices/ui/_weathercape.html.erb +313 -0
  83. data/beaglebonechip.js +57 -0
  84. data/dial-a-device-web.js +84 -0
  85. data/dial_a_device_node.gemspec +23 -0
  86. data/dial_a_device_node.js +419 -0
  87. data/lib/dial_a_device_node.rb +6 -0
  88. data/lib/dial_a_device_node/version.rb +3 -0
  89. data/package.json +35 -0
  90. data/start.js +40 -0
  91. metadata +161 -0
@@ -0,0 +1,622 @@
1
+ <?php
2
+ define ("SERIAL_DEVICE_NOTSET", 0);
3
+ define ("SERIAL_DEVICE_SET", 1);
4
+ define ("SERIAL_DEVICE_OPENED", 2);
5
+
6
+ /**
7
+ * Serial port control class
8
+ *
9
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
10
+ * USE IT AT YOUR OWN RISKS !
11
+ *
12
+ * Changes added by Rizwan Kassim <rizwank@uwink.com> for OSX functionality
13
+ * default serial device for osx devices is /dev/tty.serial for machines with a built in serial device
14
+ *
15
+ * @author R�my Sanchez <thenux@gmail.com>
16
+ * @thanks Aur�lien Derouineau for finding how to open serial ports with windows
17
+ * @thanks Alec Avedisyan for help and testing with reading
18
+ * @copyright under GPL 2 licence
19
+ */
20
+ class phpSerial
21
+ {
22
+ var $_device = null;
23
+ var $_windevice = null;
24
+ var $_dHandle = null;
25
+ var $_dState = SERIAL_DEVICE_NOTSET;
26
+ var $_buffer = "";
27
+ var $_os = "";
28
+
29
+ /**
30
+ * This var says if buffer should be flushed by sendMessage (true) or manualy (false)
31
+ *
32
+ * @var bool
33
+ */
34
+ var $autoflush = true;
35
+
36
+ /**
37
+ * Constructor. Perform some checks about the OS and setserial
38
+ *
39
+ * @return phpSerial
40
+ */
41
+ function phpSerial ()
42
+ {
43
+ setlocale(LC_ALL, "en_US");
44
+
45
+ $sysname = php_uname();
46
+
47
+ if (substr($sysname, 0, 5) === "Linux")
48
+ {
49
+ $this->_os = "linux";
50
+
51
+
52
+ register_shutdown_function(array($this, "deviceClose"));
53
+
54
+ }
55
+ elseif (substr($sysname, 0, 6) === "Darwin")
56
+ {
57
+ $this->_os = "osx";
58
+ // We know stty is available in Darwin.
59
+ // stty returns 1 when run from php, because "stty: stdin isn't a
60
+ // terminal"
61
+ // skip this check
62
+ // if($this->_exec("stty") === 0)
63
+ // {
64
+ register_shutdown_function(array($this, "deviceClose"));
65
+ // }
66
+ // else
67
+ // {
68
+ // trigger_error("No stty availible, unable to run.", E_USER_ERROR);
69
+ // }
70
+ }
71
+ elseif(substr($sysname, 0, 7) === "Windows")
72
+ {
73
+ $this->_os = "windows";
74
+ register_shutdown_function(array($this, "deviceClose"));
75
+ }
76
+ else
77
+ {
78
+ trigger_error("Host OS is neither osx, linux nor windows, unable tu run.", E_USER_ERROR);
79
+ exit();
80
+ }
81
+ }
82
+
83
+ //
84
+ // OPEN/CLOSE DEVICE SECTION -- {START}
85
+ //
86
+
87
+ /**
88
+ * Device set function : used to set the device name/address.
89
+ * -> linux : use the device address, like /dev/ttyS0
90
+ * -> osx : use the device address, like /dev/tty.serial
91
+ * -> windows : use the COMxx device name, like COM1 (can also be used
92
+ * with linux)
93
+ *
94
+ * @param string $device the name of the device to be used
95
+ * @return bool
96
+ */
97
+ function deviceSet ($device)
98
+ {
99
+ if ($this->_dState !== SERIAL_DEVICE_OPENED)
100
+ {
101
+ if ($this->_os === "linux")
102
+ {
103
+ if (preg_match("@^COM(\d+):?$@i", $device, $matches))
104
+ {
105
+ $device = "/dev/ttyS" . ($matches[1] - 1);
106
+ }
107
+
108
+ if ($this->_exec("stty -F " . $device) === 0)
109
+ {
110
+ $this->_device = $device;
111
+ $this->_dState = SERIAL_DEVICE_SET;
112
+ return true;
113
+ }
114
+ }
115
+ elseif ($this->_os === "osx")
116
+ {
117
+ if ($this->_exec("stty -f " . $device) === 0)
118
+ {
119
+ $this->_device = $device;
120
+ $this->_dState = SERIAL_DEVICE_SET;
121
+ return true;
122
+ }
123
+ }
124
+ elseif ($this->_os === "windows")
125
+ {
126
+ if (preg_match("@^COM(\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device . " xon=on BAUD=9600")) === 0)
127
+ {
128
+ $this->_windevice = "COM" . $matches[1];
129
+ $this->_device = "\\.\com" . $matches[1];
130
+ $this->_dState = SERIAL_DEVICE_SET;
131
+ return true;
132
+ }
133
+ }
134
+
135
+ trigger_error("Specified serial port " . $device . " is not valid", E_USER_WARNING);
136
+ return false;
137
+ }
138
+ else
139
+ {
140
+ trigger_error("You must close your device before to set an other one", E_USER_WARNING);
141
+ return false;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Opens the device for reading and/or writing.
147
+ *
148
+ * @param string $mode Opening mode : same parameter as fopen()
149
+ * @return bool
150
+ */
151
+ function deviceOpen ($mode = "r+b")
152
+ {
153
+ if ($this->_dState === SERIAL_DEVICE_OPENED)
154
+ {
155
+ trigger_error("The device is already opened", E_USER_NOTICE);
156
+ return true;
157
+ }
158
+
159
+ if ($this->_dState === SERIAL_DEVICE_NOTSET)
160
+ {
161
+ trigger_error("The device must be set before to be open", E_USER_WARNING);
162
+ return false;
163
+ }
164
+
165
+ if (!preg_match("@^[raw]\+?b?$@", $mode))
166
+ {
167
+ trigger_error("Invalid opening mode : ".$mode.". Use fopen() modes.", E_USER_WARNING);
168
+ return false;
169
+ }
170
+
171
+ $this->_dHandle = @fopen($this->_device, $mode);
172
+
173
+ if ($this->_dHandle !== false)
174
+ {
175
+ stream_set_blocking($this->_dHandle, 0);
176
+ $this->_dState = SERIAL_DEVICE_OPENED;
177
+ return true;
178
+ }
179
+
180
+ $this->_dHandle = null;
181
+ trigger_error("Unable to open the device", E_USER_WARNING);
182
+ return false;
183
+ }
184
+
185
+ /**
186
+ * Closes the device
187
+ *
188
+ * @return bool
189
+ */
190
+ function deviceClose ()
191
+ {
192
+ if ($this->_dState !== SERIAL_DEVICE_OPENED)
193
+ {
194
+ return true;
195
+ }
196
+
197
+ if (fclose($this->_dHandle))
198
+ {
199
+ $this->_dHandle = null;
200
+ $this->_dState = SERIAL_DEVICE_SET;
201
+ return true;
202
+ }
203
+
204
+ trigger_error("Unable to close the device", E_USER_ERROR);
205
+ return false;
206
+ }
207
+
208
+ //
209
+ // OPEN/CLOSE DEVICE SECTION -- {STOP}
210
+ //
211
+
212
+ //
213
+ // CONFIGURE SECTION -- {START}
214
+ //
215
+
216
+ /**
217
+ * Configure the Baud Rate
218
+ * Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400,
219
+ * 57600 and 115200.
220
+ *
221
+ * @param int $rate the rate to set the port in
222
+ * @return bool
223
+ */
224
+ function confBaudRate ($rate)
225
+ {
226
+ if ($this->_dState !== SERIAL_DEVICE_SET)
227
+ {
228
+ trigger_error("Unable to set the baud rate : the device is either not set or opened", E_USER_WARNING);
229
+ return false;
230
+ }
231
+
232
+ $validBauds = array (
233
+ 110 => 11,
234
+ 150 => 15,
235
+ 300 => 30,
236
+ 600 => 60,
237
+ 1200 => 12,
238
+ 2400 => 24,
239
+ 4800 => 48,
240
+ 9600 => 96,
241
+ 19200 => 19,
242
+ 38400 => 38400,
243
+ 57600 => 57600,
244
+ 115200 => 115200
245
+ );
246
+
247
+ if (isset($validBauds[$rate]))
248
+ {
249
+ if ($this->_os === "linux")
250
+ {
251
+ $ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);
252
+ }
253
+ if ($this->_os === "darwin")
254
+ {
255
+ $ret = $this->_exec("stty -f " . $this->_device . " " . (int) $rate, $out);
256
+ }
257
+ elseif ($this->_os === "windows")
258
+ {
259
+ $ret = $this->_exec("mode " . $this->_windevice . " BAUD=" . $validBauds[$rate], $out);
260
+ }
261
+ else return false;
262
+
263
+ if ($ret !== 0)
264
+ {
265
+ trigger_error ("Unable to set baud rate: " . $out[1], E_USER_WARNING);
266
+ return false;
267
+ }
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Configure parity.
273
+ * Modes : odd, even, none
274
+ *
275
+ * @param string $parity one of the modes
276
+ * @return bool
277
+ */
278
+ function confParity ($parity)
279
+ {
280
+ if ($this->_dState !== SERIAL_DEVICE_SET)
281
+ {
282
+ trigger_error("Unable to set parity : the device is either not set or opened", E_USER_WARNING);
283
+ return false;
284
+ }
285
+
286
+ $args = array(
287
+ "none" => "-parenb",
288
+ "odd" => "parenb parodd",
289
+ "even" => "parenb -parodd",
290
+ );
291
+
292
+ if (!isset($args[$parity]))
293
+ {
294
+ trigger_error("Parity mode not supported", E_USER_WARNING);
295
+ return false;
296
+ }
297
+
298
+ if ($this->_os === "linux")
299
+ {
300
+ $ret = $this->_exec("stty -F " . $this->_device . " " . $args[$parity], $out);
301
+ }
302
+ else
303
+ {
304
+ $ret = $this->_exec("mode " . $this->_windevice . " PARITY=" . $parity{0}, $out);
305
+ }
306
+
307
+ if ($ret === 0)
308
+ {
309
+ return true;
310
+ }
311
+
312
+ trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING);
313
+ return false;
314
+ }
315
+
316
+ /**
317
+ * Sets the length of a character.
318
+ *
319
+ * @param int $int length of a character (5 <= length <= 8)
320
+ * @return bool
321
+ */
322
+ function confCharacterLength ($int)
323
+ {
324
+ if ($this->_dState !== SERIAL_DEVICE_SET)
325
+ {
326
+ trigger_error("Unable to set length of a character : the device is either not set or opened", E_USER_WARNING);
327
+ return false;
328
+ }
329
+
330
+ $int = (int) $int;
331
+ if ($int < 5) $int = 5;
332
+ elseif ($int > 8) $int = 8;
333
+
334
+ if ($this->_os === "linux")
335
+ {
336
+ $ret = $this->_exec("stty -F " . $this->_device . " cs" . $int, $out);
337
+ }
338
+ else
339
+ {
340
+ $ret = $this->_exec("mode " . $this->_windevice . " DATA=" . $int, $out);
341
+ }
342
+
343
+ if ($ret === 0)
344
+ {
345
+ return true;
346
+ }
347
+
348
+ trigger_error("Unable to set character length : " .$out[1], E_USER_WARNING);
349
+ return false;
350
+ }
351
+
352
+ /**
353
+ * Sets the length of stop bits.
354
+ *
355
+ * @param float $length the length of a stop bit. It must be either 1,
356
+ * 1.5 or 2. 1.5 is not supported under linux and on some computers.
357
+ * @return bool
358
+ */
359
+ function confStopBits ($length)
360
+ {
361
+ if ($this->_dState !== SERIAL_DEVICE_SET)
362
+ {
363
+ trigger_error("Unable to set the length of a stop bit : the device is either not set or opened", E_USER_WARNING);
364
+ return false;
365
+ }
366
+
367
+ if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->_os === "linux"))
368
+ {
369
+ trigger_error("Specified stop bit length is invalid", E_USER_WARNING);
370
+ return false;
371
+ }
372
+
373
+ if ($this->_os === "linux")
374
+ {
375
+ $ret = $this->_exec("stty -F " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
376
+ }
377
+ else
378
+ {
379
+ $ret = $this->_exec("mode " . $this->_windevice . " STOP=" . $length, $out);
380
+ }
381
+
382
+ if ($ret === 0)
383
+ {
384
+ return true;
385
+ }
386
+
387
+ trigger_error("Unable to set stop bit length : " . $out[1], E_USER_WARNING);
388
+ return false;
389
+ }
390
+
391
+ /**
392
+ * Configures the flow control
393
+ *
394
+ * @param string $mode Set the flow control mode. Availible modes :
395
+ * -> "none" : no flow control
396
+ * -> "rts/cts" : use RTS/CTS handshaking
397
+ * -> "xon/xoff" : use XON/XOFF protocol
398
+ * @return bool
399
+ */
400
+ function confFlowControl ($mode)
401
+ {
402
+ if ($this->_dState !== SERIAL_DEVICE_SET)
403
+ {
404
+ trigger_error("Unable to set flow control mode : the device is either not set or opened", E_USER_WARNING);
405
+ return false;
406
+ }
407
+
408
+ $linuxModes = array(
409
+ "none" => "clocal -crtscts -ixon -ixoff",
410
+ "rts/cts" => "-clocal crtscts -ixon -ixoff",
411
+ "xon/xoff" => "-clocal -crtscts ixon ixoff"
412
+ );
413
+ $windowsModes = array(
414
+ "none" => "xon=off octs=off rts=on",
415
+ "rts/cts" => "xon=off octs=on rts=hs",
416
+ "xon/xoff" => "xon=on octs=off rts=on",
417
+ );
418
+
419
+ if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") {
420
+ trigger_error("Invalid flow control mode specified", E_USER_ERROR);
421
+ return false;
422
+ }
423
+
424
+ if ($this->_os === "linux")
425
+ $ret = $this->_exec("stty -F " . $this->_device . " " . $linuxModes[$mode], $out);
426
+ else
427
+ $ret = $this->_exec("mode " . $this->_windevice . " " . $windowsModes[$mode], $out);
428
+
429
+ if ($ret === 0) return true;
430
+ else {
431
+ trigger_error("Unable to set flow control : " . $out[1], E_USER_ERROR);
432
+ return false;
433
+ }
434
+ }
435
+
436
+ /**
437
+ * Sets a setserial parameter (cf man setserial)
438
+ * NO MORE USEFUL !
439
+ * -> No longer supported
440
+ * -> Only use it if you need it
441
+ *
442
+ * @param string $param parameter name
443
+ * @param string $arg parameter value
444
+ * @return bool
445
+ */
446
+ function setSetserialFlag ($param, $arg = "")
447
+ {
448
+ if (!$this->_ckOpened()) return false;
449
+
450
+ $return = exec ("setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1");
451
+
452
+ if ($return{0} === "I")
453
+ {
454
+ trigger_error("setserial: Invalid flag", E_USER_WARNING);
455
+ return false;
456
+ }
457
+ elseif ($return{0} === "/")
458
+ {
459
+ trigger_error("setserial: Error with device file", E_USER_WARNING);
460
+ return false;
461
+ }
462
+ else
463
+ {
464
+ return true;
465
+ }
466
+ }
467
+
468
+ //
469
+ // CONFIGURE SECTION -- {STOP}
470
+ //
471
+
472
+ //
473
+ // I/O SECTION -- {START}
474
+ //
475
+
476
+ /**
477
+ * Sends a string to the device
478
+ *
479
+ * @param string $str string to be sent to the device
480
+ * @param float $waitForReply time to wait for the reply (in seconds)
481
+ */
482
+ function sendMessage ($str, $waitForReply = 0.1)
483
+ {
484
+ $this->_buffer .= $str;
485
+
486
+ if ($this->autoflush === true) $this->serialflush();
487
+
488
+ usleep((int) ($waitForReply * 1000000));
489
+ }
490
+
491
+ /**
492
+ * Reads the port until no new datas are availible, then return the content.
493
+ *
494
+ * @pararm int $count number of characters to be read (will stop before
495
+ * if less characters are in the buffer)
496
+ * @return string
497
+ */
498
+ function readPort ($count = 0)
499
+ {
500
+ if ($this->_dState !== SERIAL_DEVICE_OPENED)
501
+ {
502
+ trigger_error("Device must be opened to read it", E_USER_WARNING);
503
+ return false;
504
+ }
505
+
506
+ if ($this->_os === "linux" || $this->_os === "osx")
507
+ {
508
+ // Behavior in OSX isn't to wait for new data to recover, but just grabs what's there!
509
+ // Doesn't always work perfectly for me in OSX
510
+ $content = ""; $i = 0;
511
+
512
+ if ($count !== 0)
513
+ {
514
+ do {
515
+ if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
516
+ else $content .= fread($this->_dHandle, 128);
517
+ } while (($i += 128) === strlen($content));
518
+ }
519
+ else
520
+ {
521
+ do {
522
+ $content .= fread($this->_dHandle, 128);
523
+ } while (($i += 128) === strlen($content));
524
+ }
525
+
526
+ return $content;
527
+ }
528
+ elseif ($this->_os === "windows")
529
+ {
530
+ $content = "";
531
+
532
+ if ($count > 0)
533
+ {
534
+ $content = fread($this->_dHandle, $count);
535
+ }
536
+
537
+ return $content;
538
+ }
539
+
540
+
541
+ return false;
542
+ }
543
+
544
+ /**
545
+ * Flushes the output buffer
546
+ * Renamed from flush for osx compat. issues
547
+ *
548
+ * @return bool
549
+ */
550
+ function serialflush ()
551
+ {
552
+ if (!$this->_ckOpened()) return false;
553
+
554
+ if (fwrite($this->_dHandle, $this->_buffer) !== false)
555
+ {
556
+ $this->_buffer = "";
557
+ return true;
558
+ }
559
+ else
560
+ {
561
+ $this->_buffer = "";
562
+ trigger_error("Error while sending message", E_USER_WARNING);
563
+ return false;
564
+ }
565
+ }
566
+
567
+ //
568
+ // I/O SECTION -- {STOP}
569
+ //
570
+
571
+ //
572
+ // INTERNAL TOOLKIT -- {START}
573
+ //
574
+
575
+ function _ckOpened()
576
+ {
577
+ if ($this->_dState !== SERIAL_DEVICE_OPENED)
578
+ {
579
+ trigger_error("Device must be opened", E_USER_WARNING);
580
+ return false;
581
+ }
582
+
583
+ return true;
584
+ }
585
+
586
+ function _ckClosed()
587
+ {
588
+ if ($this->_dState !== SERIAL_DEVICE_CLOSED)
589
+ {
590
+ trigger_error("Device must be closed", E_USER_WARNING);
591
+ return false;
592
+ }
593
+
594
+ return true;
595
+ }
596
+
597
+ function _exec($cmd, &$out = null)
598
+ {
599
+ $desc = array(
600
+ 1 => array("pipe", "w"),
601
+ 2 => array("pipe", "w")
602
+ );
603
+
604
+ $proc = proc_open($cmd, $desc, $pipes);
605
+
606
+ $ret = stream_get_contents($pipes[1]);
607
+ $err = stream_get_contents($pipes[2]);
608
+
609
+ fclose($pipes[1]);
610
+ fclose($pipes[2]);
611
+
612
+ $retVal = proc_close($proc);
613
+
614
+ if (func_num_args() == 2) $out = array($ret, $err);
615
+ return $retVal;
616
+ }
617
+
618
+ //
619
+ // INTERNAL TOOLKIT -- {STOP}
620
+ //
621
+ }
622
+ ?>