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