1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Posprint\Extras; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Serial port control class |
7
|
|
|
* |
8
|
|
|
* Refactoring from original, https://github.com/Xowap/PHP-Serial, to meet PSR standards |
9
|
|
|
* and propose improvements and fixes to the fact that the original is not actively |
10
|
|
|
* maintained for many years. |
11
|
|
|
* by Roberto L. Machado <linux dot rlm at gmail dot com> |
12
|
|
|
* |
13
|
|
|
* IMPORTANT: check and adjust permissions for serial port access by server user like www-data |
14
|
|
|
* |
15
|
|
|
* @author Rémy Sanchez <[email protected]> |
16
|
|
|
* @author Rizwan Kassim <[email protected]> |
17
|
|
|
* @thanks Aurélien Derouineau for finding how to open serial ports with windows |
18
|
|
|
* @thanks Alec Avedisyan for help and testing with reading |
19
|
|
|
* @thanks Jim Wright for OSX cleanup/fixes. |
20
|
|
|
* @copyright under GPL 2 licence |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use RuntimeException; |
24
|
|
|
|
25
|
|
|
class PhpSerial |
26
|
|
|
{ |
27
|
|
|
const OS_UNKNOWN = 0; |
28
|
|
|
const OS_WIN = 1; //WINS32 WINNT Windows |
29
|
|
|
const OS_LINUX = 2; |
30
|
|
|
const OS_CYGWIN = 3; //Cygwin Windows Linux like commands |
31
|
|
|
const OS_UNIX = 4; |
32
|
|
|
const OS_BSD = 5; //FreeBSD or NetBSD or OpenBSD /dev/ttyu1 |
33
|
|
|
const OS_OSX = 6; //Darwin MacOS |
34
|
|
|
const OS_HPUX = 7; //tty1p0 |
35
|
|
|
|
36
|
|
|
const SERIAL_DEVICE_NOTSET = 0; |
37
|
|
|
const SERIAL_DEVICE_SET = 1; |
38
|
|
|
const SERIAL_DEVICE_OPENED = 2; |
39
|
|
|
|
40
|
|
|
const PARITY_NONE = 0; |
41
|
|
|
const PARITY_ODD = 1; |
42
|
|
|
const PARITY_EVEN = 2; |
43
|
|
|
|
44
|
|
|
const FLOW_NONE = 0; //no flow control |
45
|
|
|
const FLOW_RTSCTS = 1; // use RTS/CTS handshaking |
46
|
|
|
const FLOW_XONXOFF = 2; //use XON/XOFF protocol |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Pointer for device |
50
|
|
|
* |
51
|
|
|
* @var resource |
52
|
|
|
*/ |
53
|
|
|
protected $handle = null; |
54
|
|
|
/** |
55
|
|
|
* Data buffer |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $buffer = ""; |
60
|
|
|
/** |
61
|
|
|
* This var says if buffer should be flushed by write (true) or |
62
|
|
|
* manually (false) |
63
|
|
|
* |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
protected $autoflush = false; |
67
|
|
|
/** |
68
|
|
|
* Wait time after send data to serial |
69
|
|
|
* |
70
|
|
|
* @var float |
71
|
|
|
*/ |
72
|
|
|
protected $waittime = 0.1; |
73
|
|
|
/** |
74
|
|
|
* OS type where php is running |
75
|
|
|
* linux is default |
76
|
|
|
* |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
|
|
protected $ostype = 2; |
80
|
|
|
/** |
81
|
|
|
* Mode command to set up serial port |
82
|
|
|
* formated device mode for especific OS use |
83
|
|
|
* |
84
|
|
|
* @var string |
85
|
|
|
*/ |
86
|
|
|
protected $mode = ''; |
87
|
|
|
/** |
88
|
|
|
* Status of port |
89
|
|
|
* NoSet, Set or Open |
90
|
|
|
* |
91
|
|
|
* @var int |
92
|
|
|
*/ |
93
|
|
|
protected $state = self::SERIAL_DEVICE_NOTSET; |
94
|
|
|
/** |
95
|
|
|
* Port name |
96
|
|
|
* |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
protected $port = '/dev/ttyS0'; |
100
|
|
|
/** |
101
|
|
|
* Data bits |
102
|
|
|
* |
103
|
|
|
* @var int |
104
|
|
|
*/ |
105
|
|
|
protected $databits = 8; |
106
|
|
|
/** |
107
|
|
|
* Baud Rate |
108
|
|
|
* |
109
|
|
|
* @var int |
110
|
|
|
*/ |
111
|
|
|
protected $baudrate = 9600; |
112
|
|
|
/** |
113
|
|
|
* Parity |
114
|
|
|
* |
115
|
|
|
* @var int |
116
|
|
|
*/ |
117
|
|
|
protected $parity = self::PARITY_NONE; |
118
|
|
|
/** |
119
|
|
|
* Stop Bits |
120
|
|
|
* |
121
|
|
|
* @var float |
122
|
|
|
*/ |
123
|
|
|
protected $stopbits = 1; |
124
|
|
|
/** |
125
|
|
|
* Flow Control |
126
|
|
|
* |
127
|
|
|
* @var int |
128
|
|
|
*/ |
129
|
|
|
protected $flowcontrol = self::FLOW_NONE; |
130
|
|
|
/** |
131
|
|
|
* Formated device name command |
132
|
|
|
* |
133
|
|
|
* @var string |
134
|
|
|
*/ |
135
|
|
|
protected $device = '/dev/ttyS0'; |
136
|
|
|
/** |
137
|
|
|
* Formated Data Bits command |
138
|
|
|
* |
139
|
|
|
* @var string |
140
|
|
|
*/ |
141
|
|
|
protected $formatedDataBits = 'cs8'; |
142
|
|
|
/** |
143
|
|
|
* Formated Baud Rate command |
144
|
|
|
* |
145
|
|
|
* @var string |
146
|
|
|
*/ |
147
|
|
|
protected $formatedBaudRate = '9600'; |
148
|
|
|
/** |
149
|
|
|
* Formated parity command |
150
|
|
|
* |
151
|
|
|
* @var string |
152
|
|
|
*/ |
153
|
|
|
protected $formatedParity = '-parenb'; |
154
|
|
|
/** |
155
|
|
|
* Formated stop bits command |
156
|
|
|
* |
157
|
|
|
* @var string |
158
|
|
|
*/ |
159
|
|
|
protected $formatedStopBits = '-cstopb'; |
160
|
|
|
/** |
161
|
|
|
* Formated flow control command |
162
|
|
|
* |
163
|
|
|
* @var string |
164
|
|
|
*/ |
165
|
|
|
protected $formatedFlowControl = 'clocal -crtscts -ixon -ixoff'; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Parity data |
169
|
|
|
* |
170
|
|
|
* @var array |
171
|
|
|
*/ |
172
|
|
|
private $parityargs = [ |
173
|
|
|
"none" => [0, "-parenb"], |
174
|
|
|
"odd" => [1, "parenb parodd"], |
175
|
|
|
"even" => [2, "parenb -parodd"] |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Basud Rate data |
180
|
|
|
* |
181
|
|
|
* @var array |
182
|
|
|
*/ |
183
|
|
|
private $baudsargs = array ( |
184
|
|
|
110 => 11, |
185
|
|
|
150 => 15, |
186
|
|
|
300 => 30, |
187
|
|
|
600 => 60, |
188
|
|
|
1200 => 12, |
189
|
|
|
2400 => 24, |
190
|
|
|
4800 => 48, |
191
|
|
|
9600 => 96, |
192
|
|
|
19200 => 19, |
193
|
|
|
38400 => 38400, |
194
|
|
|
57600 => 57600, |
195
|
|
|
115200 => 115200 |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Constructor |
200
|
|
|
* Set ostype parameter |
201
|
|
|
* |
202
|
|
|
* @param int $forceOS |
203
|
|
|
*/ |
204
|
15 |
|
public function __construct($forceOS = null) |
205
|
|
|
{ |
206
|
15 |
|
if (! is_null($forceOS)) { |
207
|
|
|
if ($this->ostype !== $forceOS && ($forceOS > 0 && $forceOS < 8)) { |
208
|
|
|
$this->ostype = $forceOS; |
209
|
|
|
//clear params |
210
|
|
|
$this->clearParams(); |
211
|
|
|
} |
212
|
|
|
} else { |
213
|
15 |
|
$this->ostype = $this->getOs(); |
214
|
|
|
} |
215
|
15 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Clear class params |
219
|
|
|
* Used for testing proporses |
220
|
|
|
*/ |
221
|
|
|
protected function clearParams() |
222
|
|
|
{ |
223
|
|
|
$this->mode = null; |
224
|
|
|
$this->state = null; |
225
|
|
|
$this->port = null; |
226
|
|
|
$this->databits = null; |
227
|
|
|
$this->baudrate = null; |
228
|
|
|
$this->parity = null; |
229
|
|
|
$this->stopbits = null; |
230
|
|
|
$this->flowcontrol = null; |
231
|
|
|
$this->device = null; |
232
|
|
|
$this->formatedDataBits = null; |
233
|
|
|
$this->formatedBaudRate = null; |
234
|
|
|
$this->formatedParity = null; |
235
|
|
|
$this->formatedStopBits = null; |
236
|
|
|
$this->formatedFlowControl = null; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Close port |
241
|
|
|
*/ |
242
|
15 |
|
public function __destruct() |
243
|
|
|
{ |
244
|
15 |
|
$this->close(); |
245
|
15 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Open set port |
249
|
|
|
* |
250
|
|
|
* @return boolean |
251
|
|
|
*/ |
252
|
2 |
|
public function open() |
253
|
|
|
{ |
254
|
2 |
|
if ($this->state === self::SERIAL_DEVICE_OPENED && is_resource($this->handle)) { |
255
|
|
|
return true; |
256
|
|
|
} |
257
|
2 |
|
$timeout = 10; //seconds |
258
|
2 |
|
for ($i = 0; $i < $timeout; $i++) { |
259
|
2 |
|
$this->handle = @fopen($this->device, 'r+bn'); |
260
|
2 |
|
if ($this->handle) { |
261
|
1 |
|
break; |
262
|
|
|
} |
263
|
1 |
|
sleep(1); |
264
|
1 |
|
} |
265
|
2 |
|
if ($this->handle === false) { |
266
|
1 |
|
$this->handle = null; |
267
|
1 |
|
$this->state = self::SERIAL_DEVICE_NOTSET; |
268
|
1 |
|
throw new RuntimeException('Fail to open device. Check permissions.'); |
269
|
|
|
} |
270
|
1 |
|
stream_set_blocking($this->handle, false); |
271
|
1 |
|
$this->state = self::SERIAL_DEVICE_OPENED; |
272
|
1 |
|
return true; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Close serial port |
277
|
|
|
* |
278
|
|
|
* @return boolean |
279
|
|
|
*/ |
280
|
15 |
|
public function close() |
281
|
|
|
{ |
282
|
15 |
|
if ($this->state !== self::SERIAL_DEVICE_OPENED || ! is_resource($this->handle)) { |
283
|
14 |
|
return true; |
284
|
|
|
} |
285
|
1 |
|
if (fclose($this->handle)) { |
286
|
1 |
|
$this->handle = null; |
287
|
1 |
|
$this->state = self::SERIAL_DEVICE_SET; |
288
|
1 |
|
return true; |
289
|
|
|
} |
290
|
|
|
return false; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Returns the setup configuration for serial port |
295
|
|
|
* this command will be exectuted in terminal |
296
|
|
|
* |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
public function getSetUp() |
300
|
|
|
{ |
301
|
|
|
return $this->mode; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Use class parameters to configure the serial port |
306
|
|
|
* before the serial port is opened it must be configured, |
307
|
|
|
* and in windows environment, all sets at a single time |
308
|
|
|
* |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
1 |
|
public function setUp() |
312
|
|
|
{ |
313
|
1 |
|
if ($this->state === self::SERIAL_DEVICE_SET) { |
314
|
|
|
return true; |
315
|
|
|
} |
316
|
1 |
|
if ($this->ostype == 0) { |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
$modesos = [ |
320
|
1 |
|
1 => 'MODE', //windows mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off |
321
|
1 |
|
2 => "stty -F", //linux |
322
|
1 |
|
3 => "stty -F", //cygwin |
323
|
1 |
|
4 => 'stty -F', //unix |
324
|
1 |
|
5 => 'stty -F', //BSD |
325
|
1 |
|
6 => "stty -f", //MacOS |
326
|
|
|
7 => 'stty -F' //HPUX |
327
|
1 |
|
]; |
328
|
1 |
|
$mode = $modesos[$this->ostype] |
329
|
|
|
. " " |
330
|
1 |
|
. "$this->device " |
331
|
1 |
|
. "$this->formatedBaudRate " |
332
|
1 |
|
. "$this->formatedParity " |
333
|
1 |
|
. "$this->formatedDataBits " |
334
|
1 |
|
. "$this->formatedStopBits " |
335
|
1 |
|
. "$this->formatedFlowControl"; |
336
|
|
|
|
337
|
1 |
|
$out = ''; |
338
|
1 |
|
if ($this->execCommand($mode, $out) != 0) { |
|
|
|
|
339
|
1 |
|
throw new RuntimeException("SetUP fail with: ".$out[1]); |
340
|
|
|
} |
341
|
|
|
$this->mode = $mode; |
342
|
|
|
$this->state = self::SERIAL_DEVICE_SET; |
343
|
|
|
return true; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Set automatic send massage to serial |
348
|
|
|
* |
349
|
|
|
* @param bool $auto |
350
|
|
|
* @param float $waittime |
351
|
|
|
*/ |
352
|
|
|
public function setAuto($auto, $waittime) |
353
|
|
|
{ |
354
|
|
|
if (! is_bool($auto)) { |
355
|
|
|
$data = false; |
|
|
|
|
356
|
|
|
} |
357
|
|
|
if (! is_float($waittime)) { |
358
|
|
|
$waittime = 0.1; |
359
|
|
|
} |
360
|
|
|
$this->waittime = $waittime; |
361
|
|
|
$this->autoflush = $auto; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Returns automatic mode |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
|
public function getAuto() |
370
|
|
|
{ |
371
|
|
|
return $this->autoflush; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Read serial port |
376
|
|
|
* |
377
|
|
|
* @param int $count Number of characters to be read (will stop before |
378
|
|
|
* if less characters are in the buffer) |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
public function read($count = 0) |
382
|
|
|
{ |
383
|
|
|
if ($this->state !== self::SERIAL_DEVICE_OPENED) { |
384
|
|
|
return ''; |
385
|
|
|
} |
386
|
|
|
$content = ""; |
387
|
|
|
$i = 0; |
388
|
|
|
// Windows port reading procedures still buggy |
389
|
|
|
// Behavior in OSX isn't to wait for new data to recover, but just |
390
|
|
|
// grabs what's there! Doesn't always work perfectly for me in OSX |
391
|
|
|
if ($count !== 0) { |
392
|
|
|
do { |
393
|
|
|
if ($i > $count) { |
394
|
|
|
$content .= fread($this->handle, ($count - $i)); |
395
|
|
|
} else { |
396
|
|
|
$content .= fread($this->handle, 128); |
397
|
|
|
} |
398
|
|
|
} while (($i += 128) === strlen($content)); |
399
|
|
|
return $content; |
400
|
|
|
} |
401
|
|
|
do { |
402
|
|
|
$content .= fread($this->handle, 128); |
403
|
|
|
} while (($i += 128) === strlen($content)); |
404
|
|
|
return $content; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Write data to buffer or serial port |
409
|
|
|
* depends of getAuto() |
410
|
|
|
* if getAuto() == true this command writes directly to port |
411
|
|
|
* if getAuto() == false this command writes to buffer (default) |
412
|
|
|
* |
413
|
|
|
* @param string $data |
414
|
|
|
* @return boolean |
415
|
|
|
*/ |
416
|
|
|
public function write($data) |
417
|
|
|
{ |
418
|
|
|
if ($this->state !== self::SERIAL_DEVICE_OPENED) { |
419
|
|
|
return ''; |
420
|
|
|
} |
421
|
|
|
$this->buffer .= $data; |
422
|
|
|
if ($this->autoflush === true) { |
423
|
|
|
$this->flush(); |
424
|
|
|
usleep((int) ($this->waittime * 1000000)); |
425
|
|
|
} |
426
|
|
|
return true; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Flushs imediatly data to serial port |
431
|
|
|
* |
432
|
|
|
* @return boolean |
433
|
|
|
*/ |
434
|
|
|
public function flush() |
435
|
|
|
{ |
436
|
|
|
if ($this->state !== self::SERIAL_DEVICE_OPENED) { |
437
|
|
|
return ''; |
438
|
|
|
} |
439
|
|
|
if (fwrite($this->handle, $this->buffer) !== false) { |
440
|
|
|
$this->buffer = ""; |
441
|
|
|
return true; |
442
|
|
|
} |
443
|
|
|
return false; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Set port name |
448
|
|
|
* |
449
|
|
|
* @param string $port |
450
|
|
|
*/ |
451
|
1 |
|
public function setPort($port) |
452
|
|
|
{ |
453
|
|
|
//identify input if $port like COM?? even in others OS |
454
|
1 |
|
$flagWinMode = preg_match("@^COM(\d+):?$@i", $port, $matches); |
455
|
|
|
//select port from OS type |
456
|
1 |
|
switch ($this->ostype) { |
457
|
1 |
|
case self::OS_WIN: |
458
|
|
|
$this->device = ($flagWinMode) ? "COM$matches[1]:" : $port; |
459
|
|
|
break; |
460
|
1 |
|
case self::OS_LINUX: |
461
|
1 |
|
case self::OS_CYGWIN: |
462
|
1 |
|
$this->device = ($flagWinMode) ? "/dev/ttyS".($matches[1]-1) : $port; |
463
|
1 |
|
break; |
464
|
|
|
case self::OS_UNIX: |
465
|
|
|
case self::OS_BSD: |
466
|
|
|
case self::OS_OSX: |
467
|
|
|
case self::OS_HPUX: |
468
|
|
|
default: |
469
|
|
|
$this->device = $port; |
470
|
1 |
|
} |
471
|
1 |
|
$this->port = $port; |
472
|
1 |
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Returns port name |
476
|
|
|
* |
477
|
|
|
* @return string |
478
|
|
|
*/ |
479
|
1 |
|
public function getPort() |
480
|
|
|
{ |
481
|
1 |
|
return $this->port; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Returns device formated name |
486
|
|
|
* |
487
|
|
|
* @return string |
488
|
|
|
*/ |
489
|
1 |
|
public function getDevice() |
490
|
|
|
{ |
491
|
1 |
|
return $this->device; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Sets the length of a character. |
496
|
|
|
* length of a character (5 <= length <= 8) |
497
|
|
|
* |
498
|
|
|
* @param int $length |
499
|
|
|
* @return boolean |
500
|
|
|
*/ |
501
|
1 |
|
public function setDataBits($length) |
502
|
|
|
{ |
503
|
1 |
|
if ($length < 5 || $length > 8) { |
504
|
|
|
$length = 8; |
505
|
|
|
} |
506
|
1 |
|
$this->databits = $length; |
507
|
1 |
|
$this->formatedDataBits = $this->zDataBits($length); |
508
|
1 |
|
return true; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Returns char length |
513
|
|
|
* |
514
|
|
|
* @return int |
515
|
|
|
*/ |
516
|
2 |
|
public function getDataBits() |
517
|
|
|
{ |
518
|
2 |
|
return $this->databits; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Format data bits commands |
523
|
|
|
* |
524
|
|
|
* @param int $length |
525
|
|
|
* @return string |
526
|
|
|
*/ |
527
|
1 |
|
protected function zDataBits($length) |
528
|
|
|
{ |
529
|
1 |
|
$fdatabits = "cs$length"; |
530
|
1 |
|
if ($this->ostype == self::OS_WIN) { |
531
|
|
|
//windows |
532
|
|
|
$fdatabits = "DATA=$length"; |
533
|
|
|
} |
534
|
1 |
|
return $fdatabits; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Set serial baud rate |
539
|
|
|
* |
540
|
|
|
* @param int $rate |
541
|
|
|
* @return boolean |
542
|
|
|
*/ |
543
|
1 |
|
public function setBaudRate($rate) |
544
|
|
|
{ |
545
|
1 |
|
if (! isset($this->baudsargs[$rate])) { |
546
|
|
|
$rate = 9600; |
547
|
|
|
} |
548
|
1 |
|
$this->baudrate = $rate; |
549
|
1 |
|
$this->formatedBaudRate = $this->zBaudRate($rate); |
550
|
1 |
|
return true; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Return baud rate |
555
|
|
|
* |
556
|
|
|
* @return int |
557
|
|
|
*/ |
558
|
2 |
|
public function getBaudRate() |
559
|
|
|
{ |
560
|
2 |
|
return $this->baudrate; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Format baud rate command |
565
|
|
|
* |
566
|
|
|
* @param int $rate |
567
|
|
|
* @return string |
568
|
|
|
*/ |
569
|
1 |
|
protected function zBaudRate($rate) |
570
|
|
|
{ |
571
|
1 |
|
$baud = "$rate"; |
572
|
1 |
|
if ($this->ostype == self::OS_WIN) { |
573
|
|
|
//windows |
574
|
|
|
$baud = "BAUD=".$this->baudsargs[$rate]; |
575
|
|
|
} |
576
|
1 |
|
return $baud; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Sets parity mode |
582
|
|
|
* |
583
|
|
|
* @param string $parity odd, even, none |
584
|
|
|
* @return boolean |
585
|
|
|
*/ |
586
|
1 |
|
public function setParity($parity) |
587
|
|
|
{ |
588
|
1 |
|
if (! isset($this->parityargs[$parity])) { |
589
|
|
|
$parity = 'none'; |
590
|
|
|
} |
591
|
1 |
|
$this->parity = $this->parityargs[$parity][0]; |
592
|
1 |
|
$this->formatedParity = $this->zParity($parity); |
593
|
1 |
|
return true; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Get parity mode set |
598
|
|
|
* |
599
|
|
|
* @return string |
600
|
|
|
*/ |
601
|
2 |
|
public function getParity() |
602
|
|
|
{ |
603
|
2 |
|
switch ($this->parity) { |
604
|
2 |
|
case 0: |
605
|
1 |
|
return 'none'; |
606
|
1 |
|
case 1: |
607
|
1 |
|
return 'odd'; |
608
|
|
|
case 2: |
609
|
|
|
return 'even'; |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Format parity command |
615
|
|
|
* |
616
|
|
|
* @param string $parity |
617
|
|
|
* @return string |
618
|
|
|
*/ |
619
|
1 |
|
protected function zParity($parity) |
620
|
|
|
{ |
621
|
1 |
|
$fparity = $this->parityargs[$parity][1]; |
622
|
1 |
|
if ($this->ostype == self::OS_WIN) { |
623
|
|
|
//windows |
624
|
|
|
$fparity = "PARITY=" . strtoupper(substr($parity, 0, 1)); |
625
|
|
|
} |
626
|
1 |
|
return $fparity; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Set length of stop bits |
632
|
|
|
* the length of a stop bit. |
633
|
|
|
* It must be either 1, 1.5 or 2. |
634
|
|
|
* 1.5 is not supported under linux and on some computers. |
635
|
|
|
* |
636
|
|
|
* @param float $length |
637
|
|
|
* @return boolean |
638
|
|
|
*/ |
639
|
1 |
|
public function setStopBits($length) |
640
|
|
|
{ |
641
|
1 |
|
if ($length !== 1 && $length !== 1.5 && $length !== 2) { |
642
|
|
|
$length = 1; |
643
|
|
|
} |
644
|
1 |
|
$this->stopbits = $length; |
|
|
|
|
645
|
1 |
|
$this->formatedStopBits = $this->zStopBits($length); |
646
|
1 |
|
return true; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Return stop bits set |
651
|
|
|
* |
652
|
|
|
* @return float |
653
|
|
|
*/ |
654
|
2 |
|
public function getStopBits() |
655
|
|
|
{ |
656
|
2 |
|
return $this->stopbits; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Format stop bit command |
661
|
|
|
* |
662
|
|
|
* @param float $length |
663
|
|
|
* @return string |
664
|
|
|
*/ |
665
|
1 |
|
public function zStopBits($length) |
666
|
|
|
{ |
667
|
1 |
|
$stopb = (($length == 1) ? "-" : "") . "cstopb"; |
668
|
1 |
|
if ($this->ostype === self::OS_WIN) { |
669
|
|
|
$stopb = "STOP=" . $length; |
670
|
|
|
} |
671
|
1 |
|
return $stopb; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* Set the flow control mode. |
676
|
|
|
* Availible modes : |
677
|
|
|
* "none" : no flow control |
678
|
|
|
* "rts/cts" : use RTS/CTS handshaking |
679
|
|
|
* "xon/xoff" : use XON/XOFF protocol |
680
|
|
|
* |
681
|
|
|
* @param string $flow |
682
|
|
|
* @return boolean |
683
|
|
|
*/ |
684
|
1 |
|
public function setFlowControl($flow) |
685
|
|
|
{ |
686
|
|
|
switch ($flow) { |
687
|
1 |
|
case 'rts/cts': |
688
|
|
|
$this->flowcontrol = self::FLOW_RTSCTS; |
689
|
|
|
break; |
690
|
1 |
|
case 'xon/xoff': |
691
|
1 |
|
$this->flowcontrol = self::FLOW_XONXOFF; |
692
|
1 |
|
break; |
693
|
|
|
default: |
694
|
|
|
$this->flowcontrol = self::FLOW_NONE; |
695
|
|
|
} |
696
|
1 |
|
$this->formatedFlowControl = $this->zFlowControl($this->flowcontrol); |
697
|
1 |
|
return true; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Returns flow control set |
702
|
|
|
* |
703
|
|
|
* @return string |
704
|
|
|
*/ |
705
|
2 |
|
public function getFlowControl() |
706
|
|
|
{ |
707
|
2 |
|
switch ($this->flowcontrol) { |
708
|
2 |
|
case 0: |
709
|
1 |
|
return 'none'; |
710
|
1 |
|
case 1: |
711
|
|
|
return 'rts/cts'; |
712
|
1 |
|
case 2: |
713
|
1 |
|
return 'xon/xoff'; |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Return flow control command formated for OP type |
719
|
|
|
* |
720
|
|
|
* @param int $flow |
721
|
|
|
* @return string |
722
|
|
|
*/ |
723
|
1 |
|
protected function zFlowControl($flow) |
724
|
|
|
{ |
725
|
|
|
$modeos = [ |
726
|
|
|
//windows |
727
|
1 |
|
self::OS_WIN => [ |
728
|
1 |
|
"xon=off octs=off rts=on", |
729
|
1 |
|
"xon=off octs=on rts=hs", |
730
|
|
|
"xon=on octs=off rts=on" |
731
|
1 |
|
], |
732
|
|
|
//linux |
733
|
1 |
|
self::OS_LINUX => [ |
734
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
735
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
736
|
|
|
"-clocal -crtscts ixon ixoff" |
737
|
1 |
|
], |
738
|
|
|
//cygwin |
739
|
1 |
|
self::OS_CYGWIN => [ |
740
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
741
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
742
|
|
|
"-clocal -crtscts ixon ixoff" |
743
|
1 |
|
], |
744
|
|
|
//unix |
745
|
1 |
|
self::OS_UNIX => [ |
746
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
747
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
748
|
|
|
"-clocal -crtscts ixon ixoff" |
749
|
1 |
|
], |
750
|
|
|
//bsd |
751
|
1 |
|
self::OS_BSD => [ |
752
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
753
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
754
|
|
|
"-clocal -crtscts ixon ixoff" |
755
|
1 |
|
], |
756
|
|
|
//macos |
757
|
1 |
|
self::OS_OSX => [ |
758
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
759
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
760
|
|
|
"-clocal -crtscts ixon ixoff" |
761
|
1 |
|
], |
762
|
|
|
//hpux |
763
|
1 |
|
self::OS_HPUX => [ |
764
|
1 |
|
"clocal -crtscts -ixon -ixoff", |
765
|
1 |
|
"-clocal crtscts -ixon -ixoff", |
766
|
|
|
"-clocal -crtscts ixon ixoff" |
767
|
1 |
|
] |
768
|
1 |
|
]; |
769
|
1 |
|
return (string) $modeos[$this->ostype][$flow]; |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Find OS type |
774
|
|
|
* |
775
|
|
|
* @return int |
776
|
|
|
*/ |
777
|
15 |
|
protected function getOs() |
778
|
|
|
{ |
779
|
15 |
|
$oss = strtoupper(substr(PHP_OS, 0, 3)); |
780
|
|
|
switch ($oss) { |
781
|
15 |
|
case 'DAR': |
782
|
|
|
return self::OS_OSX; |
783
|
15 |
|
case 'WIN': |
784
|
|
|
return self::OS_WIN; |
785
|
15 |
|
case 'LIN': |
786
|
15 |
|
return self::OS_LINUX; |
787
|
|
|
case 'CYG': |
788
|
|
|
return self::OS_CYGWIN; |
789
|
|
|
case 'HPU': |
790
|
|
|
return self::OS_HPUX; |
791
|
|
|
case 'BSD': |
792
|
|
|
return self::OS_BSD; //este esta incorreto |
793
|
|
|
case 'UNI': |
794
|
|
|
return self::OS_UNIX; |
795
|
|
|
default: |
796
|
|
|
return self::OS_UNKNOWN; |
797
|
|
|
} |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Exec command line in OS console |
802
|
|
|
* |
803
|
|
|
* @param string $cmd comand line to execute |
804
|
|
|
* @param array $out retorn of this command in terminal |
805
|
|
|
* @return int |
806
|
|
|
*/ |
807
|
1 |
|
public function execCommand($cmd, &$out = null) |
808
|
|
|
{ |
809
|
|
|
$desc = array( |
810
|
1 |
|
1 => array("pipe", "w"), |
811
|
1 |
|
2 => array("pipe", "w") |
812
|
1 |
|
); |
813
|
1 |
|
$proc = proc_open($cmd, $desc, $pipes); |
814
|
1 |
|
$ret = stream_get_contents($pipes[1]); |
815
|
1 |
|
$err = stream_get_contents($pipes[2]); |
816
|
1 |
|
fclose($pipes[1]); |
817
|
1 |
|
fclose($pipes[2]); |
818
|
1 |
|
$retVal = proc_close($proc); |
819
|
1 |
|
if (func_num_args() == 2) { |
820
|
1 |
|
$out = array($ret, $err); |
821
|
1 |
|
} |
822
|
1 |
|
return $retVal; |
823
|
|
|
} |
824
|
|
|
} |
825
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: