-
Notifications
You must be signed in to change notification settings - Fork 6
/
phpXBee.php
1369 lines (1223 loc) · 32 KB
/
phpXBee.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* +--------------------------------------------------------------------------+
* | Copyright (c) 2012 Chris Barnes |
* +--------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or modify |
* | it under the terms of the GNU General Public License as published by |
* | the Free Software Foundation; either version 2 of the License, or |
* | (at your option) any later version. |
* | |
* | This program is distributed in the hope that it will be useful, |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* | GNU General Public License for more details. |
* | |
* | You should have received a copy of the GNU General Public License |
* | along with this program; if not, write to the Free Software |
* | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
* +--------------------------------------------------------------------------+
*/
define ("SERIAL_DEVICE_NOTSET", 0);
define ("SERIAL_DEVICE_SET", 1);
define ("SERIAL_DEVICE_OPENED", 2);
/**
* Serial port control class
*
* THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
* USE IT AT YOUR OWN RISKS !
*
* Changes added by Rizwan Kassim <[email protected]> for OSX functionality
* default serial device for osx devices is /dev/tty.serial for machines with a built in serial device
*
* @author Rémy Sanchez <[email protected]>
* @thanks Aurélien Derouineau for finding how to open serial ports with windows
* @thanks Alec Avedisyan for help and testing with reading
* @thanks Jim Wright for OSX cleanup/fixes.
* @copyright under GPL 2 licence
*/
class phpSerial
{
var $_device = null;
var $_windevice = null;
var $_dHandle = null;
var $_dState = SERIAL_DEVICE_NOTSET;
var $_buffer = "";
var $_os = "";
/**
* This var says if buffer should be flushed by sendMessage (true) or manualy (false)
*
* @var bool
*/
var $autoflush = true;
/**
* Constructor. Perform some checks about the OS and setserial
*
* @return phpSerial
*/
function phpSerial ()
{
setlocale(LC_ALL, "en_US");
$sysname = php_uname();
if (substr($sysname, 0, 5) === "Linux")
{
$this->_os = "linux";
if($this->_exec("stty --version") === 0)
{
register_shutdown_function(array($this, "deviceClose"));
}
else
{
trigger_error("No stty availible, unable to run.", E_USER_ERROR);
}
}
elseif (substr($sysname, 0, 6) === "Darwin")
{
$this->_os = "osx";
// We know stty is available in Darwin.
// stty returns 1 when run from php, because "stty: stdin isn't a
// terminal"
// skip this check
// if($this->_exec("stty") === 0)
// {
register_shutdown_function(array($this, "deviceClose"));
// }
// else
// {
// trigger_error("No stty availible, unable to run.", E_USER_ERROR);
// }
}
elseif(substr($sysname, 0, 7) === "Windows")
{
$this->_os = "windows";
register_shutdown_function(array($this, "deviceClose"));
}
else
{
trigger_error("Host OS is neither osx, linux nor windows, unable to run.", E_USER_ERROR);
exit();
}
}
//
// OPEN/CLOSE DEVICE SECTION -- {START}
//
/**
* Device set function : used to set the device name/address.
* -> linux : use the device address, like /dev/ttyS0
* -> osx : use the device address, like /dev/tty.serial
* -> windows : use the COMxx device name, like COM1 (can also be used
* with linux)
*
* @param string $device the name of the device to be used
* @return bool
*/
function deviceSet ($device)
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
if ($this->_os === "linux")
{
if (preg_match("@^COM(\d+):?$@i", $device, $matches))
{
$device = "/dev/ttyS" . ($matches[1] - 1);
}
if ($this->_exec("stty -F " . $device) === 0)
{
$this->_device = $device;
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
}
elseif ($this->_os === "osx")
{
if ($this->_exec("stty -f " . $device) === 0)
{
$this->_device = $device;
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
}
elseif ($this->_os === "windows")
{
if (preg_match("@^COM(\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device . " xon=on BAUD=9600")) === 0)
{
$this->_windevice = "COM" . $matches[1];
$this->_device = "\\.\com" . $matches[1];
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
}
trigger_error("Specified serial port is not valid", E_USER_WARNING);
return false;
}
else
{
trigger_error("You must close your device before to set an other one", E_USER_WARNING);
return false;
}
}
/**
* Opens the device for reading and/or writing.
*
* @param string $mode Opening mode : same parameter as fopen()
* @return bool
*/
function deviceOpen ($mode = "r+b")
{
if ($this->_dState === SERIAL_DEVICE_OPENED)
{
trigger_error("The device is already opened", E_USER_NOTICE);
return true;
}
if ($this->_dState === SERIAL_DEVICE_NOTSET)
{
trigger_error("The device must be set before to be open", E_USER_WARNING);
return false;
}
if (!preg_match("@^[raw]\+?b?$@", $mode))
{
trigger_error("Invalid opening mode : ".$mode.". Use fopen() modes.", E_USER_WARNING);
return false;
}
$this->_dHandle = @fopen($this->_device, $mode);
if ($this->_dHandle !== false)
{
stream_set_blocking($this->_dHandle, 0);
$this->_dState = SERIAL_DEVICE_OPENED;
return true;
}
$this->_dHandle = null;
trigger_error("Unable to open the device", E_USER_WARNING);
return false;
}
/**
* Closes the device
*
* @return bool
*/
function deviceClose ()
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
return true;
}
if (fclose($this->_dHandle))
{
$this->_dHandle = null;
$this->_dState = SERIAL_DEVICE_SET;
return true;
}
trigger_error("Unable to close the device", E_USER_ERROR);
return false;
}
//
// OPEN/CLOSE DEVICE SECTION -- {STOP}
//
//
// CONFIGURE SECTION -- {START}
//
/**
* Configure the Baud Rate
* Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400,
* 57600 and 115200. Note there was a paramter added from original class.
*
* @param int $rate the rate to set the port in
* @return bool
*/
function confBaudRate ($rate)
{
if ($this->_dState !== SERIAL_DEVICE_SET)
{
trigger_error("Unable to set the baud rate : the device is either not set or opened", E_USER_WARNING);
return false;
}
$validBauds = array (
110 => 11,
150 => 15,
300 => 30,
600 => 60,
1200 => 12,
2400 => 24,
4800 => 48,
9600 => 96,
19200 => 19,
38400 => 38400,
57600 => 57600,
115200 => 115200
);
if (isset($validBauds[$rate]))
{
if ($this->_os === "linux")
{
//note this is the only place modified from the original, I submitted a patch on Google Code for this change
//See switches here: http://www.daemon-systems.org/man/stty.1.html
$ret = $this->_exec("stty -F " . $this->_device . " raw speed -echo " . (int) $rate, $out);
}
if ($this->_os === "osx")
{
$ret = $this->_exec("stty -f " . $this->_device . " " . (int) $rate, $out);
}
elseif ($this->_os === "windows")
{
$ret = $this->_exec("mode " . $this->_windevice . " BAUD=" . $validBauds[$rate], $out);
}
else return false;
if ($ret !== 0)
{
trigger_error ("Unable to set baud rate: " . $out[1], E_USER_WARNING);
return false;
}
}
}
/**
* Configure parity.
* Modes : odd, even, none
*
* @param string $parity one of the modes
* @return bool
*/
function confParity ($parity)
{
if ($this->_dState !== SERIAL_DEVICE_SET)
{
trigger_error("Unable to set parity : the device is either not set or opened", E_USER_WARNING);
return false;
}
$args = array(
"none" => "-parenb",
"odd" => "parenb parodd",
"even" => "parenb -parodd",
);
if (!isset($args[$parity]))
{
trigger_error("Parity mode not supported", E_USER_WARNING);
return false;
}
if ($this->_os === "linux")
{
$ret = $this->_exec("stty -F " . $this->_device . " " . $args[$parity], $out);
}
elseif ($this->_os === "osx")
{
$ret = $this->_exec("stty -f " . $this->_device . " " . $args[$parity], $out);
}
else
{
$ret = $this->_exec("mode " . $this->_windevice . " PARITY=" . $parity{0}, $out);
}
if ($ret === 0)
{
return true;
}
trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING);
return false;
}
/**
* Sets the length of a character.
*
* @param int $int length of a character (5 <= length <= 8)
* @return bool
*/
function confCharacterLength ($int)
{
if ($this->_dState !== SERIAL_DEVICE_SET)
{
trigger_error("Unable to set length of a character : the device is either not set or opened", E_USER_WARNING);
return false;
}
$int = (int) $int;
if ($int < 5) $int = 5;
elseif ($int > 8) $int = 8;
if ($this->_os === "linux")
{
$ret = $this->_exec("stty -F " . $this->_device . " cs" . $int, $out);
}
elseif ($this->_os === "osx")
{
$ret = $this->_exec("stty -f " . $this->_device . " cs" . $int, $out);
}
else
{
$ret = $this->_exec("mode " . $this->_windevice . " DATA=" . $int, $out);
}
if ($ret === 0)
{
return true;
}
trigger_error("Unable to set character length : " .$out[1], E_USER_WARNING);
return false;
}
/**
* Sets the length of stop bits.
*
* @param float $length the length of a stop bit. It must be either 1,
* 1.5 or 2. 1.5 is not supported under linux and on some computers.
* @return bool
*/
function confStopBits ($length)
{
if ($this->_dState !== SERIAL_DEVICE_SET)
{
trigger_error("Unable to set the length of a stop bit : the device is either not set or opened", E_USER_WARNING);
return false;
}
if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->_os === "linux"))
{
trigger_error("Specified stop bit length is invalid", E_USER_WARNING);
return false;
}
if ($this->_os === "linux")
{
$ret = $this->_exec("stty -F " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
}
elseif ($this->_os === "osx")
{
$ret = $this->_exec("stty -f " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
}
else
{
$ret = $this->_exec("mode " . $this->_windevice . " STOP=" . $length, $out);
}
if ($ret === 0)
{
return true;
}
trigger_error("Unable to set stop bit length : " . $out[1], E_USER_WARNING);
return false;
}
/**
* Configures the flow control
*
* @param string $mode Set the flow control mode. Availible modes :
* -> "none" : no flow control
* -> "rts/cts" : use RTS/CTS handshaking
* -> "xon/xoff" : use XON/XOFF protocol
* @return bool
*/
function confFlowControl ($mode)
{
if ($this->_dState !== SERIAL_DEVICE_SET)
{
trigger_error("Unable to set flow control mode : the device is either not set or opened", E_USER_WARNING);
return false;
}
$linuxModes = array(
"none" => "clocal -crtscts -ixon -ixoff",
"rts/cts" => "-clocal crtscts -ixon -ixoff",
"xon/xoff" => "-clocal -crtscts ixon ixoff"
);
$windowsModes = array(
"none" => "xon=off octs=off rts=on",
"rts/cts" => "xon=off octs=on rts=hs",
"xon/xoff" => "xon=on octs=off rts=on",
);
if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") {
trigger_error("Invalid flow control mode specified", E_USER_ERROR);
return false;
}
if ($this->_os === "linux")
$ret = $this->_exec("stty -F " . $this->_device . " " . $linuxModes[$mode], $out);
elseif ($this->_os === "osx")
$ret = $this->_exec("stty -f " . $this->_device . " " . $linuxModes[$mode], $out);
else
$ret = $this->_exec("mode " . $this->_windevice . " " . $windowsModes[$mode], $out);
if ($ret === 0) return true;
else {
trigger_error("Unable to set flow control : " . $out[1], E_USER_ERROR);
return false;
}
}
/**
* Sets a setserial parameter (cf man setserial)
* NO MORE USEFUL !
* -> No longer supported
* -> Only use it if you need it
*
* @param string $param parameter name
* @param string $arg parameter value
* @return bool
*/
function setSetserialFlag ($param, $arg = "")
{
if (!$this->_ckOpened()) return false;
$return = exec ("setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1");
if ($return{0} === "I")
{
trigger_error("setserial: Invalid flag", E_USER_WARNING);
return false;
}
elseif ($return{0} === "/")
{
trigger_error("setserial: Error with device file", E_USER_WARNING);
return false;
}
else
{
return true;
}
}
//
// CONFIGURE SECTION -- {STOP}
//
//
// I/O SECTION -- {START}
//
/**
* Sends a string to the device
*
* @param string $str string to be sent to the device
* @param float $waitForReply time to wait for the reply (in seconds)
*/
function sendMessage ($str, $waitForReply = 0.1)
{
$this->_buffer .= $str;
if ($this->autoflush === true) $this->serialflush();
usleep((int) ($waitForReply * 1000000));
}
/**
* Reads the port until no new datas are availible, then return the content.
*
* @param int $count number of characters to be read (will stop before
* if less characters are in the buffer)
* @return string
*/
function readPort ($count = 0)
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
trigger_error("Device must be opened to read it", E_USER_WARNING);
return false;
}
if ($this->_os === "linux" || $this->_os === "osx")
{
// Behavior in OSX isn't to wait for new data to recover, but just grabs what's there!
// Doesn't always work perfectly for me in OSX
$content = ""; $i = 0;
if ($count !== 0)
{
do {
if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
else $content .= fread($this->_dHandle, 128);
} while (($i += 128) === strlen($content));
}
else
{
do {
$content .= fread($this->_dHandle, 128);
} while (($i += 128) === strlen($content));
}
return $content;
}
elseif ($this->_os === "windows")
{
// Windows port reading procedures still buggy
$content = ""; $i = 0;
if ($count !== 0)
{
do {
if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
else $content .= fread($this->_dHandle, 128);
} while (($i += 128) === strlen($content));
}
else
{
do {
$content .= fread($this->_dHandle, 128);
} while (($i += 128) === strlen($content));
}
return $content;
}
return false;
}
/**
* Flushes the output buffer
* Renamed from flush for osx compat. issues
*
* @return bool
*/
function serialflush ()
{
if (!$this->_ckOpened()) return false;
if (fwrite($this->_dHandle, $this->_buffer) !== false)
{
$this->_buffer = "";
return true;
}
else
{
$this->_buffer = "";
trigger_error("Error while sending message", E_USER_WARNING);
return false;
}
}
//
// I/O SECTION -- {STOP}
//
//
// INTERNAL TOOLKIT -- {START}
//
function _ckOpened()
{
if ($this->_dState !== SERIAL_DEVICE_OPENED)
{
trigger_error("Device must be opened", E_USER_WARNING);
return false;
}
return true;
}
function _ckClosed()
{
if ($this->_dState !== SERIAL_DEVICE_CLOSED)
{
trigger_error("Device must be closed", E_USER_WARNING);
return false;
}
return true;
}
function _exec($cmd, &$out = null)
{
$desc = array(
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$proc = proc_open($cmd, $desc, $pipes);
$ret = stream_get_contents($pipes[1]);
$err = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$retVal = proc_close($proc);
if (func_num_args() == 2) $out = array($ret, $err);
return $retVal;
}
//
// INTERNAL TOOLKIT -- {STOP}
//
}
/**
* XBee represents an XBee connection
*
* Be sure to configure the serial connection first.
* If on linux run these commands at the shell first:
*
* Add apache user to dialout group:
* sudo adduser www-data dialout
*
* Restart Apache:
* sudo /etc/init.d/apache2 restart
*
* THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
* USE IT AT YOUR OWN RISKS !
* @author Chris Barnes
* @thanks Rémy Sanchez, Aurélien Derouineau and Alec Avedisyan for the original serial class
* @copyright GPL 2 licence
* @package phpXBee
*/
class XBee extends phpSerial {
/**
* Constructor. Parent is phpSerial
*
* @return Xbee
*/
function XBee() {
parent::phpSerial();
}
/**
* Sets up typical Connection 9600 8-N-1
*
* @param String $device is the path to the xbee, defaults to /dev/ttyUSB0
* @return void
*/
public function confDefaults($device = '/dev/ttyUSB0') {
$this -> deviceSet($device);
$this -> confBaudRate(9600);
$this -> confParity('none');
$this -> confCharacterLength(8);
$this -> confStopBits(1);
$this -> confFlowControl('none');
}
/**
* Opens this XBee connection.
*
* Note that you can send raw serial with sendMessage from phpSerial
* @return void
* @param $waitForOpened int amount to sleep after openeing in seconds. Defaults to 0.1
*/
public function open($waitForOpened=0.1) {
$this -> deviceOpen();
usleep((int) ($waitForOpened * 1000000));
}
/**
* Closes this XBee connection
* @return void
*/
public function close() {
$this -> deviceClose();
}
/**
* Sends an XBee frame. $waitForReply is how long to wait on recieving
*
* @param XBeeFrame $frame
* @param int $waitForRply
* @return void
*/
public function send($frame , $waitForReply=0.1) {
$this -> sendMessage($frame -> getFrame(), $waitForReply);
//echo 'Sent: ';print_r(unpack('H*', $frame -> getFrame())); //debug
}
/**
* Reads the XBee until no new data is availible, then returns the content.
* Note that the return is an array of XBeeResponses
*
* @param int $count number of characters to be read (will stop before
* if less characters are in the buffer)
* @return Array $XBeeResponse
*/
public function recieve($count = 0) {
$rawResponse = $this -> readPort($count);
$rawResponse = unpack('H*', $rawResponse);
$response = explode('7e', $rawResponse[1]);
//echo ' responseArr:';print_r($response); //debug
for ($i=1; $i < count($response); $i++) {
$response[$i] = new XBeeResponse($response[$i]);
}
return $response;
}
}
/**
* XbeeFrameBase represents common functions for all types of frames
*
* @package XBeeFrameBase
* @subpackage XBeeFrame
* @subpackage XBeeResponse
*/
abstract class _XBeeFrameBase {
const DEFAULT_START_BYTE = '7E', DEFAULT_FRAME_ID = '01',
REMOTE_API_ID = '17', LOCAL_API_ID = '08',
QUEUED_API_ID = '09', TX_API_ID = '10', TX_EXPLICIT_API_ID = '11';
protected $frame, $frameId, $apiId, $cmdData, $startByte, $address16, $address64, $options, $cmd, $val;
/**
* Contructor for abstract class XbeeFrameBase.
*
*/
protected function _XBeeFrameBase() {
$this -> setStartByte(_XBeeFrameBase::DEFAULT_START_BYTE);
$this -> setFrameId(_XBeeFrameBase::DEFAULT_FRAME_ID);
}
/**
* Assembles frame after all values are set
*
* @return void
*/
protected function _assembleFrame() {
$this -> setFrame(
$this -> getStartByte() .
$this -> _getFramelength($this -> getCmdData()) .
$this -> getCmdData() .
$this -> _calcChecksum($this -> getCmdData())
);
//echo 'Assembled: ';print_r($this -> _unpackBytes($this -> getFrame())); //debug
}
/**
* Calculates checksum for cmdData. Leave off start byte, length and checksum
*
* @param String $data Should be a binary string
* @return String $checksum Should be a binary string
*/
protected function _calcChecksum($data) {
$checksum = 0;
for ($i = 0; $i < strlen($data); $i++) {
$checksum += ord($data[$i]);
}
$checksum = $checksum & 0xFF;
$checksum = 0xFF - $checksum;
$checksum = chr($checksum);
return $checksum;
}
/**
* Calculates lenth for cmdData. Leave off start byte, length and checksum
*
* @param String $data Should be a binary string
* @return String $length Should be a binary string
*/
protected function _getFramelength($data) {
$length = strlen($data);
$length = sprintf("%04x", $length);
$length = $this -> _packBytes($length);
return $length;
}
/**
* Transforms hex into a string
*
* @param String $hex
* @return String $string Should be a binary string
*/
protected function _hexstr($hex) {
$string = '';
for ($i=0; $i < strlen($hex); $i+=2) {
$string .= chr(hexdec($hex[$i] . $hex[$i+1]));
}
return $string;
}
/**
* Transforms string into hex
*
* @param String $str Should be a binary string
* @return String $hex Sould be a hex string
*/
protected function _strhex($str) {
$hex = '';
for ($i=0; $i < strlen($str); $i+=2) {
$hex .= dechex(ord($str[$i])) . dechex(ord($str[$i+1]));
}
return $hex;
}
/**
* Packs a string into binary for sending
*
* @param String $data
* @return String $data Should be a binary string
*/
protected function _packBytes($data) {
return pack('H*', $data);
}
/**
* Unpacks bytes into an array
*
* @param String $data Should be a binary string
* @return Array $data
*/
protected function _unpackBytes($data) {
return unpack('H*', $data);
}
/**
* Sets raw frame, including start byte etc
*
* @param String $frame
* @return void
*/
public function setFrame($frame) {
$this -> frame = $frame;
}
/**
* Gets raw frame data
*
* @return String $FrameData
*/
public function getFrame() {
return $this -> frame;
}
/**
* Sets FrameId according to XBee API
*
* @param String $frameId
* @return void
*/
public function setFrameId($frameId) {
$this -> frameId = $frameId;
}
/**
* Gets frame ID according to XBee API
*
* @return String $frameId
*/
public function getFrameId() {
return $this -> frameId;
}
/**
* Sets ApiId according to XBee API
*
* @param String $apiId
*/
public function setApiId($apiId) {
$this -> apiId = $apiId;
}
/**
* Gets API ID
*
* @return String $apiId
*/
public function getApiId() {
return $this -> apiId;
}
/**
* Sets raw command data, without start byte etc
*
* @param String $cmdData
* @return void
*/
public function setCmdData($cmdData) {
$this -> cmdData = $this -> _packBytes($cmdData);
}
/**
* Gets raw command data, without start byte etc
*
* @return String $cmdData
*/
public function getCmdData() {
return $this -> cmdData;
}
/**
* Sets Start Byte according to XBee API, defaults to 7E
*
* @param String $startByte
*/
public function setStartByte($startByte) {
$this -> startByte = $this -> _packBytes($startByte);
}
/**
* Gets Start Byte according to XBee API, default is 7E
*
* @return String $startByte
*/
public function getStartByte() {
return $this -> startByte;
}
/**
* Sets the 16 bit address
*
* @param String $address16
*/
public function setAddress16($address16) {
$this->address16 = $address16;
}
/**
* Gets the 16 bit address
*
* @return String $address16
*/
public function getAddress16() {
return $this->address16;
}
/**
* Sets the 64 bit address
*