@@ -633,7 +633,8 @@ def _read_device_info(self, reason, init=True, fire_event=True):
633
633
updated = True
634
634
# Node ID:
635
635
if init or not self ._node_id :
636
- node_id = self .get_parameter (ATStringCommand .NI , apply = False ).decode ()
636
+ node_id = str (self .get_parameter (ATStringCommand .NI , apply = False ),
637
+ encoding = 'utf8' , errors = 'ignore' )
637
638
if self ._node_id != node_id :
638
639
self ._node_id = node_id
639
640
updated = True
@@ -2597,15 +2598,15 @@ def _send_data_64_16(self, x64addr, x16addr, data,
2597
2598
raise ValueError ("64-bit address cannot be None" )
2598
2599
if x16addr is None :
2599
2600
raise ValueError ("16-bit address cannot be None" )
2600
- if data is None :
2601
- raise ValueError ("Data cannot be None " )
2601
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2602
+ raise ValueError ("Data must be a string or bytearray " )
2602
2603
2603
2604
if self .is_remote ():
2604
2605
raise OperationNotSupportedException (
2605
2606
message = "Cannot send data to a remote device from a remote device" )
2606
2607
2607
2608
if isinstance (data , str ):
2608
- data = data .encode ("utf8" )
2609
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2609
2610
2610
2611
packet = TransmitPacket (self .get_next_frame_id (), x64addr , x16addr ,
2611
2612
0 , transmit_options , rf_data = data )
@@ -2647,15 +2648,15 @@ def _send_data_64(self, x64addr, data, transmit_options=TransmitOptions.NONE.val
2647
2648
"""
2648
2649
if x64addr is None :
2649
2650
raise ValueError ("64-bit address cannot be None" )
2650
- if data is None :
2651
- raise ValueError ("Data cannot be None " )
2651
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2652
+ raise ValueError ("Data must be a string or bytearray " )
2652
2653
2653
2654
if self .is_remote ():
2654
2655
raise OperationNotSupportedException (
2655
2656
message = "Cannot send data to a remote device from a remote device" )
2656
2657
2657
2658
if isinstance (data , str ):
2658
- data = data .encode ("utf8" )
2659
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2659
2660
2660
2661
if self .get_protocol () == XBeeProtocol .RAW_802_15_4 :
2661
2662
packet = TX64Packet (self .get_next_frame_id (), x64addr ,
@@ -2702,15 +2703,15 @@ def _send_data_16(self, x16addr, data, transmit_options=TransmitOptions.NONE.val
2702
2703
"""
2703
2704
if x16addr is None :
2704
2705
raise ValueError ("16-bit address cannot be None" )
2705
- if data is None :
2706
- raise ValueError ("Data cannot be None " )
2706
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2707
+ raise ValueError ("Data must be a string or bytearray " )
2707
2708
2708
2709
if self .is_remote ():
2709
2710
raise OperationNotSupportedException (
2710
2711
message = "Cannot send data to a remote device from a remote device" )
2711
2712
2712
2713
if isinstance (data , str ):
2713
- data = data .encode ("utf8" )
2714
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2714
2715
2715
2716
packet = TX16Packet (self .get_next_frame_id (), x16addr ,
2716
2717
transmit_options , rf_data = data )
@@ -2806,15 +2807,15 @@ def _send_data_async_64_16(self, x64addr, x16addr, data,
2806
2807
raise ValueError ("64-bit address cannot be None" )
2807
2808
if x16addr is None :
2808
2809
raise ValueError ("16-bit address cannot be None" )
2809
- if data is None :
2810
- raise ValueError ("Data cannot be None " )
2810
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2811
+ raise ValueError ("Data must be a string or bytearray " )
2811
2812
2812
2813
if self .is_remote ():
2813
2814
raise OperationNotSupportedException (
2814
2815
message = "Cannot send data to a remote device from a remote device" )
2815
2816
2816
2817
if isinstance (data , str ):
2817
- data = data .encode ("utf8" )
2818
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2818
2819
2819
2820
packet = TransmitPacket (self .get_next_frame_id (), x64addr , x16addr , 0 ,
2820
2821
transmit_options , rf_data = data )
@@ -2848,15 +2849,15 @@ def _send_data_async_64(self, x64addr, data, transmit_options=TransmitOptions.NO
2848
2849
"""
2849
2850
if x64addr is None :
2850
2851
raise ValueError ("64-bit address cannot be None" )
2851
- if data is None :
2852
- raise ValueError ("Data cannot be None " )
2852
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2853
+ raise ValueError ("Data must be a string or bytearray " )
2853
2854
2854
2855
if self .is_remote ():
2855
2856
raise OperationNotSupportedException (
2856
2857
message = "Cannot send data to a remote device from a remote device" )
2857
2858
2858
2859
if isinstance (data , str ):
2859
- data = data .encode ("utf8" )
2860
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2860
2861
2861
2862
if self .get_protocol () == XBeeProtocol .RAW_802_15_4 :
2862
2863
packet = TX64Packet (self .get_next_frame_id (), x64addr ,
@@ -2895,15 +2896,15 @@ def _send_data_async_16(self, x16addr, data, transmit_options=TransmitOptions.NO
2895
2896
"""
2896
2897
if x16addr is None :
2897
2898
raise ValueError ("16-bit address cannot be None" )
2898
- if data is None :
2899
- raise ValueError ("Data cannot be None " )
2899
+ if not isinstance ( data , ( str , bytearray , bytes )) :
2900
+ raise ValueError ("Data must be a string or bytearray " )
2900
2901
2901
2902
if self .is_remote ():
2902
2903
raise OperationNotSupportedException (
2903
2904
message = "Cannot send data to a remote device from a remote device" )
2904
2905
2905
2906
if isinstance (data , str ):
2906
- data = data .encode ("utf8" )
2907
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
2907
2908
2908
2909
packet = TX16Packet (self .get_next_frame_id (),
2909
2910
x16addr ,
@@ -3900,7 +3901,7 @@ def _exit_at_command_mode(self):
3900
3901
raise InvalidOperatingModeException (
3901
3902
message = "Invalid mode. Command mode can be only be exited while in AT mode" )
3902
3903
3903
- self ._serial_port .write ("ATCN\r " .encode ("utf-8 " ))
3904
+ self ._serial_port .write ("ATCN\r " .encode ("utf8 " ))
3904
3905
time .sleep (self .__DEFAULT_GUARD_TIME )
3905
3906
3906
3907
def _determine_operating_mode (self ):
@@ -4089,7 +4090,7 @@ def __build_expldata_packet(self, remote_xbee, data, src_endpoint, dest_endpoint
4089
4090
x16addr = XBee16BitAddress .UNKNOWN_ADDRESS
4090
4091
4091
4092
if isinstance (data , str ):
4092
- data = data .encode ("utf8" )
4093
+ data = data .encode (encoding = "utf8" , errors = 'ignore' )
4093
4094
4094
4095
return ExplicitAddressingPacket (self ._get_next_frame_id (), x64addr ,
4095
4096
x16addr , src_endpoint , dest_endpoint ,
@@ -4115,14 +4116,18 @@ def __get_actual_mode(self):
4115
4116
# Clear the serial input stream.
4116
4117
self ._serial_port .flushInput ()
4117
4118
# Send the 'AP' command.
4118
- self ._serial_port .write ("ATAP\r " .encode ("utf-8 " ))
4119
+ self ._serial_port .write ("ATAP\r " .encode (encoding = "utf8 " ))
4119
4120
time .sleep (0.1 )
4120
4121
# Read the 'AP' answer.
4121
- ap_answer = self ._serial_port .read_existing ().decode ("utf-8" ).rstrip ()
4122
+ ap_answer = self ._serial_port .read_existing () \
4123
+ .decode (encoding = "utf8" , errors = 'ignore' ).rstrip ()
4122
4124
if len (ap_answer ) == 0 :
4123
4125
return OperatingMode .UNKNOWN
4124
4126
# Return the corresponding operating mode for the AP answer.
4125
- return OperatingMode .get (int (ap_answer , 16 ))
4127
+ try :
4128
+ return OperatingMode .get (int (ap_answer , 16 ))
4129
+ except ValueError :
4130
+ return OperatingMode .UNKNOWN
4126
4131
4127
4132
def get_next_frame_id (self ):
4128
4133
"""
@@ -6297,8 +6302,8 @@ def get_dest_ip_addr(self):
6297
6302
.. seealso::
6298
6303
| :class:`ipaddress.IPv4Address`
6299
6304
"""
6300
- resp = self . get_parameter ( ATStringCommand . DL , apply = False )
6301
- return IPv4Address ( resp . decode ( "utf8" ))
6305
+ return IPv4Address (
6306
+ str ( self . get_parameter ( ATStringCommand . DL , apply = False ), encoding = "utf8" ))
6302
6307
6303
6308
def add_ip_data_received_callback (self , callback ):
6304
6309
"""
@@ -6383,8 +6388,8 @@ def send_ip_data(self, ip_addr, dest_port, protocol, data, close_socket=False):
6383
6388
raise ValueError ("IP address cannot be None" )
6384
6389
if protocol is None :
6385
6390
raise ValueError ("Protocol cannot be None" )
6386
- if data is None :
6387
- raise ValueError ("Data cannot be None " )
6391
+ if not isinstance ( data , ( str , bytearray , bytes )) :
6392
+ raise ValueError ("Data must be a string or bytearray " )
6388
6393
6389
6394
if not 0 <= dest_port <= 65535 :
6390
6395
raise ValueError ("Destination port must be between 0 and 65535" )
@@ -6400,7 +6405,7 @@ def send_ip_data(self, ip_addr, dest_port, protocol, data, close_socket=False):
6400
6405
src_port = 0
6401
6406
6402
6407
if isinstance (data , str ):
6403
- data = data .encode ("utf8" )
6408
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
6404
6409
6405
6410
opts = TXIPv4Packet .OPTIONS_CLOSE_SOCKET if close_socket else TXIPv4Packet .OPTIONS_LEAVE_SOCKET_OPEN
6406
6411
@@ -6438,8 +6443,8 @@ def send_ip_data_async(self, ip_addr, dest_port, protocol, data, close_socket=Fa
6438
6443
raise ValueError ("IP address cannot be None" )
6439
6444
if protocol is None :
6440
6445
raise ValueError ("Protocol cannot be None" )
6441
- if data is None :
6442
- raise ValueError ("Data cannot be None " )
6446
+ if not isinstance ( data , ( str , bytearray , bytes )) :
6447
+ raise ValueError ("Data must be a string or bytearray " )
6443
6448
6444
6449
if not 0 <= dest_port <= 65535 :
6445
6450
raise ValueError ("Destination port must be between 0 and 65535" )
@@ -6456,7 +6461,7 @@ def send_ip_data_async(self, ip_addr, dest_port, protocol, data, close_socket=Fa
6456
6461
src_port = 0
6457
6462
6458
6463
if isinstance (data , str ):
6459
- data = data .encode ("utf8" )
6464
+ data = data .encode (encoding = "utf8" , errors = "ignore " )
6460
6465
6461
6466
opts = TXIPv4Packet .OPTIONS_CLOSE_SOCKET if close_socket else TXIPv4Packet .OPTIONS_LEAVE_SOCKET_OPEN
6462
6467
@@ -7669,9 +7674,9 @@ def __parse_access_point(self, ap_data):
7669
7674
return None
7670
7675
7671
7676
signal_quality = self .__get_signal_quality (version , signal_strength )
7672
- ssid = (ap_data [index :]).decode ("utf8" )
7673
7677
7674
- return AccessPoint (ssid , WiFiEncryptionType .get (encryption_type ),
7678
+ return AccessPoint (str (ap_data [index :], encoding = "utf8" ),
7679
+ WiFiEncryptionType .get (encryption_type ),
7675
7680
channel = channel , signal_quality = signal_quality )
7676
7681
7677
7682
@staticmethod
@@ -8905,7 +8910,7 @@ def export(self, dir_path=None, name=None, desc=None):
8905
8910
date_time = time .localtime (date_now .timestamp ()))
8906
8911
info .compress_type = ZIP_DEFLATED
8907
8912
with xnet_zip .open (info , 'w' ) as xnet_file :
8908
- tree .write (xnet_file , encoding = 'utf-8 ' , xml_declaration = False )
8913
+ tree .write (xnet_file , encoding = 'utf8 ' , xml_declaration = False )
8909
8914
except (OSError , IOError ) as exc :
8910
8915
return 1 , "%s (%d): %s" % (exc .strerror , exc .errno , exc .filename )
8911
8916
@@ -10283,10 +10288,12 @@ def __discover_devices(self, node_id=None):
10283
10288
try :
10284
10289
timeout = self ._calculate_timeout (default_timeout = XBeeNetwork ._DEFAULT_DISCOVERY_TIMEOUT )
10285
10290
# send "ND" async
10286
- self ._local_xbee .send_packet (ATCommPacket (self ._local_xbee .get_next_frame_id (),
10287
- ATStringCommand .ND .command ,
10288
- parameter = None if node_id is None else bytearray (node_id , 'utf8' )),
10289
- sync = False )
10291
+ self ._local_xbee .send_packet (
10292
+ ATCommPacket (self ._local_xbee .get_next_frame_id (),
10293
+ ATStringCommand .ND .command ,
10294
+ parameter = None if node_id is None
10295
+ else bytearray (node_id , encoding = 'utf8' , errors = 'ignore' )),
10296
+ sync = False )
10290
10297
10291
10298
self .__nd_processes .update ({str (self ._local_xbee .get_64bit_addr ()): self })
10292
10299
@@ -10612,7 +10619,7 @@ def __get_data_for_remote(self, data):
10612
10619
# role is the next byte
10613
10620
role = Role .get (utils .bytes_to_int (data [i :i + 1 ]))
10614
10621
return XBee16BitAddress (data [0 :2 ]), XBee64BitAddress (data [2 :10 ]), \
10615
- node_id .decode (), role , parent_addr
10622
+ node_id .decode ('utf8' , errors = 'ignore' ), role , parent_addr
10616
10623
10617
10624
def _set_node_reachable (self , node , reachable ):
10618
10625
"""
0 commit comments