Class: XBee::BaseAPIModeInterface

Inherits:
RFModule
  • Object
show all
Defined in:
lib/apimode/xbee_api.rb

Overview

This is the main class for API mode for XBee radios.

Constant Summary collapse

VERSION =

Version of this class

"1.2.0"

Instance Attribute Summary

Attributes inherited from RFModule

#api_mode, #command_character, #command_mode_timeout, #firmware_rev, #guard_time, #hardware_rev, #node_discover_timeout, #node_identifier, #operation_mode, #serial_number, #transmission_mode, #xbee_serialport, #xbee_uart_config

Instance Method Summary collapse

Methods inherited from RFModule

#in_command_mode, #read_timeout

Methods included from XBee

#getresults, new

Constructor Details

#initialize(xbee_usbdev_str, uart_config = XBeeUARTConfig.new, operation_mode = :API, transmission_mode = :SYNC) ⇒ BaseAPIModeInterface

Attributes

  • xbee_usbdev_str - USB Device as a string

Options

  • uart_config - XBeeUARTConfig

  • operation_mode - Either :AT or :API for XBee operation mode

  • transmission_mode - :SYNC for Synchronous communication or :ASYNC for Asynchonrous communication.

A note on the asynchronous vs synchronous communication modes - A simplistic network of a few XBee nodes can pretty much work according to expected flows where requests and responses are always handled in synchronous ways. However, if bigger radio networks are being deployed (real scenarios) you cannot guarantee the synchronous nature of the network. You will have nodes joining/removing themselves, sleeping, sending data samples etc. Although the default behaviour is set as :SYNC, if you have a real network, then by design the network is Asynchronous, use :ASYNC instead. Otherwise you will most definitely run into “Invalid responses” issues.

For handling the Asynchronous communication logic, use an external Queue and database to effectively handle the command/response and other frames that are concurrently being conversed on the PAN.

Example

require 'ruby_xbee'
@uart_config = XBee::Config::XBeeUARTConfig.new()
@xbee_usbdev_str = '/dev/tty.usbserial-A101KYF6'
@xbee = XBee::BaseAPIModeInterface.new(@xbee_usbdev_str, @uart_config, :API, :ASYNC)


41
42
43
44
45
46
47
# File 'lib/apimode/xbee_api.rb', line 41

def initialize(xbee_usbdev_str, uart_config = XBeeUARTConfig.new, operation_mode = :API, transmission_mode = :SYNC)
  super(xbee_usbdev_str, uart_config, operation_mode, transmission_mode)
  @frame_id = 1
  if self.operation_mode == :AT
    start_apimode_communication
  end
end

Instance Method Details

#association_indicationObject

Association Indication. Read information regarding last node join request:

  • 0x00 - Successful completion - Coordinator started or Router/End Device found and joined with a parent.

  • 0x21 - Scan found no PANs

  • 0x22 - Scan found no valid PANs based on current SC and ID settings

  • 0x23 - Valid Coordinator or Routers found, but they are not allowing joining (NJ expired) 0x27 - Node Joining attempt failed

  • 0x2A - Coordinator Start attempt failed‘

  • 0xFF - Scanning for a Parent



159
160
161
162
# File 'lib/apimode/xbee_api.rb', line 159

def association_indication
  @association_indication ||= get_param("AI","n")
  if @association_indication == nil then @association_indication = 0 end
end

#baudObject

retrieves the baud rate of the device. Generally, this will be the same as the rate you’re currently using to talk to the device unless you’ve changed the device’s baud rate and are still in the AT command mode and/or have not exited command mode explicitly for the new baud rate to take effect.



360
361
362
363
364
# File 'lib/apimode/xbee_api.rb', line 360

def baud
  @xbee_serialport.write("ATBD\r")
  baudcode = getresponse
  @baudcodes.index( baudcode.to_i )
end

#baud!(baud_rate) ⇒ Object

sets the given baud rate into the XBee device. The baud change will not take effect until the AT command mode times out or the exit command mode command is given. acceptable baud rates are: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200



370
371
372
373
# File 'lib/apimode/xbee_api.rb', line 370

def baud!( baud_rate )
  @xbee_serialport.write("ATBD#{@baudcodes[baud_rate]}\r")
  getresponse if self.transmission_mode == :SYNC
end

#channelObject

returns the channel number of the XBee device. this value, along with the PAN ID, and MY address determines the addressability of the device and what it can listen to



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/apimode/xbee_api.rb', line 276

def channel
  # channel often takes more than 1000ms to return data
  tmp = @xbee_serialport.read_timeout
  @xbee_serialport.read_timeout = read_timeout(:long)
  @xbee_serialport.write("ATCH\r")
  if self.tranmission_mode == :SYNC
    response = getresponse
    @xbee_serialport.read_timeout = tmp
    response.strip.chomp
  end
end

#channel!(new_channel) ⇒ Object

sets the channel number of the device. The valid channel numbers are those of the 802.15.4 standard.



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/apimode/xbee_api.rb', line 290

def channel!(new_channel)
  # channel takes more than 1000ms to return data
  tmp = @xbee_serialport.read_timeout
  @xbee_serialport.read_timeout = read_timeout(:long)
  @xbee_serialport.write("ATCH#{new_channel}\r")
  if self.transmission_mode == :SYNC
    response = getresponse
    @xbee_serialport.read_timeout = tmp
    response.strip.chomp
  end
end

#destination_highObject

Returns the high portion of the XBee device’s current destination address



243
244
245
# File 'lib/apimode/xbee_api.rb', line 243

def destination_high
  @destination_high ||= get_param("DH")
end

#destination_high!(high_addr) ⇒ Object

Sets the high portion of the XBee device’s current destination address Parameter range: 0 - 0xFFFFFFFF



250
251
252
253
# File 'lib/apimode/xbee_api.rb', line 250

def destination_high!(high_addr)
  self.xbee_serialport.write("ATDH#{high_addr}\r")
  getresponse if self.transmission_mode == :SYNC
end

#destination_lowObject

Returns the low portion of the XBee device’s current destination address



229
230
231
# File 'lib/apimode/xbee_api.rb', line 229

def destination_low
  @destination_low ||= get_param("DL")
end

#destination_low!(low_addr) ⇒ Object

Sets the low portion of the XBee device’s destination address Parameter range: 0 - 0xFFFFFFFF



236
237
238
239
# File 'lib/apimode/xbee_api.rb', line 236

def destination_low!(low_addr)
  @xbee_serialport.write("ATDL#{low_addr}\r")
  getresponse if self.transmission_mode == :SYNC
end

#dio(port) ⇒ Object

reads an i/o port configuration on the XBee for analog to digital or digital input or output (GPIO) this method returns an I/O type symbol of:

:Disabled :ADC :DI :DO_Low :DO_High :Associated_Indicator :RTS :CTS :RS485_Low :RS485_High

Not all DIO ports are capable of every configuration listed above. This method will properly translate the XBee’s response value to the symbol above when the same value has different meanings from port to port.

The port parameter may be any symbol :D0 through :D8 representing the 8 I/O ports on an XBee



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/apimode/xbee_api.rb', line 425

def dio( port )
  at = "AT#{port.to_s}\r"
  @xbee_serialport.write( at )
  if self.transmission_mode == :SYNC
    response = getresponse.to_i

    if response == 1  # the value of 1 is overloaded based on port number
      case port
      when :D5
        return :Associated_Indicator
      when :D6
        return :RTS
      when :D7
        return :CTS
      end
    else
      @iotypes.index(response)
    end
  end
end

#dio!(port, iotype) ⇒ Object

configures an i/o port on the XBee for analog to digital or digital input or output (GPIO)

port parameter valid values are the symbols :D0 through :D8

iotype parameter valid values are symbols: :Disabled :ADC :DI :DO_Low :DO_High :Associated_Indicator :RTS :CTS :RS485_Low :RS485_High

note: not all iotypes are compatible with every port type, see the XBee manual for exceptions and semantics

note: it is critical you have upgraded firmware in your XBee or DIO ports 0-4 cannot be read (ie: ATD0 will return ERROR - this is an XBee firmware bug that’s fixed in revs later than 1083)

note: tested with rev 10CD, fails with rev 1083



469
470
471
472
473
# File 'lib/apimode/xbee_api.rb', line 469

def dio!( port, iotype )
  at = "AT#{port.to_s}#{@iotypes[iotype]}\r"
  @xbee_serialport.write( at )
  getresponse if self.transmission_mode == :SYNC
end

#dio_change_detectObject

reads the bitfield values for change detect monitoring. returns a bitmask indicating which DIO lines, 0-7 are enabled or disabled for change detect monitoring



478
479
480
481
# File 'lib/apimode/xbee_api.rb', line 478

def dio_change_detect
  @xbee_serialport.write("ATIC\r")
  getresponse if self.transmission_mode == :SYNC
end

#dio_change_detect!(hexbitmap) ⇒ Object

sets the bitfield values for change detect monitoring. The hexbitmap parameter is a bitmap which enables or disables the change detect monitoring for any of the DIO ports 0-7



486
487
488
489
# File 'lib/apimode/xbee_api.rb', line 486

def dio_change_detect!( hexbitmap )
  @xbee_serialport.write("ATIC#{hexbitmask}\r")
  getresponse if self.transmission_mode == :SYNC
end

#exit_command_modeObject

exits the AT command mode - all changed parameters will take effect such as baud rate changes after the exit is complete. exit_command_mode does not permanently save the parameter changes when it exits AT command mode. In order to permanently change parameters, use the save! method



608
609
610
# File 'lib/apimode/xbee_api.rb', line 608

def exit_command_mode
  @xbee_serialport.write("ATCN\r")
end

#fw_revObject

Retrieve XBee firmware version



166
167
168
# File 'lib/apimode/xbee_api.rb', line 166

def fw_rev
  @fw_rev ||= get_param("VR","n")
end

#get_param(at_param_name, at_param_unpack_string = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/apimode/xbee_api.rb', line 69

def get_param(at_param_name, at_param_unpack_string = nil)
  frame_id = self.next_frame_id
  at_command_frame = XBee::Frame::ATCommand.new(at_param_name,frame_id,nil,at_param_unpack_string)
  puts "Sending ... [#{at_command_frame._dump(self.api_mode.in_symbol).unpack("C*").join(", ")}]" if $VERBOSE
  self.xbee_serialport.write(at_command_frame._dump(self.api_mode.in_symbol))
  if self.transmission_mode == :SYNC
    r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
    if r.kind_of?(XBee::Frame::ATCommandResponse) && r.status == :OK && r.frame_id == frame_id
      if block_given?
        yield r
      else
        #### DEBUG ####
        if $DEBUG then
          print "At parameter unpack string to be used: #{at_param_unpack_string} | "
          puts "Debug Return value for value: #{r.retrieved_value.unpack(at_param_unpack_string)}"
        end
        #### DEBUG ####
        at_param_unpack_string.nil? ? r.retrieved_value : r.retrieved_value.unpack(at_param_unpack_string).first
      end
    else
      raise "Response did not indicate successful retrieval of that parameter: #{r.inspect}"
    end
  end
end

#get_remote_param(at_param_name, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/apimode/xbee_api.rb', line 113

def get_remote_param(at_param_name, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil)
  frame_id = self.next_frame_id
  at_command_frame = XBee::Frame::RemoteCommandRequest.new(at_param_name, remote_address, remote_network_address, frame_id, nil, at_param_unpack_string)
  puts "Sending ... [#{at_command_frame._dump(self.api_mode.in_symbol).unpack("C*").join(", ")}]"
  self.xbee_serialport.write(at_command_frame._dump(self.api_mode.in_symbol))
  if self.transmission_mode == :SYNC
    r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
    if r.kind_of?(XBee::Frame::RemoteCommandResponse) && r.status == :OK && r.frame_id == frame_id
      if block_given?
        yield r
      else
        at_param_unpack_string.nil? ? r.retrieved_value : r.retrieved_value.unpack(at_param_unpack_string).first
      end
    else
      raise "Response did not indicate successful retrieval of that parameter: #{r.inspect}"
    end
  end
end

#getresponse(echo = false) ⇒ Object

returns results from the XBee echo is disabled by default



621
622
623
624
625
626
627
# File 'lib/apimode/xbee_api.rb', line 621

def getresponse( echo = false )
  if echo == true
    r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
  else
    getresults( @xbee_serialport, echo )
  end
end

#hw_revObject

Retrieve XBee hardware version



172
173
174
# File 'lib/apimode/xbee_api.rb', line 172

def hw_rev
  @hw_rev ||= get_param("HV","n")
end

#io_inputObject

Forces a sampling of all DIO pins configured for input via dio! Returns a hash with the following key/value pairs: :NUM => number of samples :CM => channel mask :DIO => dio data if DIO lines are enabled :ADCn => adc sample data (one for each ADC channel enabled)



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
# File 'lib/apimode/xbee_api.rb', line 507

def io_input

  tmp = @xbee_serialport.read_timeout
  @xbee_serialport.read_timeout = read_timeout(:long)

  @xbee_serialport.write("ATIS\r")
  if self.transmission_mode == :SYNC
    response = getresponse
    linenum = 1
    adc_sample = 1
    samples = Hash.new

    if response.match("ERROR")
      samples[:ERROR] = "ERROR"
      return samples
    end

    # otherwise parse input data
    response.each_line do | line |
      case linenum
      when 1
        samples[:NUM] = line.to_i
      when 2
        samples[:CM] = line.strip.chomp
      when 3
        samples[:DIO] = line.strip.chomp
      else
        sample = line.strip.chomp
        if ( !sample.nil? && sample.size > 0 )
          samples["ADC#{adc_sample}".to_sym] = line.strip.chomp
          adc_sample += 1
        end
      end

      linenum += 1
    end

    @xbee_serialport.read_timeout = tmp
    samples
  end
end

#io_output!(hexbitmap) ⇒ Object

Sets the digital output levels of any DIO lines which were configured for output using the dio! method. The parameter, hexbitmap, is a hex value which represents the 8-bit bitmap of the i/o lines on the XBee.



495
496
497
498
# File 'lib/apimode/xbee_api.rb', line 495

def io_output!( hexbitmap )
  @xbee_serialport.write("ATIO#{hexbitmap}\r")
  getresponse if self.transmission_mode == :SYNC
end

#neighborsObject

Neighbor node discovery. Returns an array of hashes each element of the array contains a hash each hash contains keys: :MY, :SH, :SL, :DB, :NI representing addresses source address, Serial High, Serial Low, Received signal strength, node identifier respectively. Aan example of the results returned (hash as seen by pp):

[{:NI=>" ", :MY=>"0", :SH=>"13A200", :SL=>"4008A642", :DB=>-24},
 {:NI=>" ", :MY=>"0", :SH=>"13A200", :SL=>"4008A697", :DB=>-33},
 {:NI=>" ", :MY=>"0", :SH=>"13A200", :SL=>"40085AD5", :DB=>-52}]

Signal strength (:DB) is reported in units of -dBM.



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
# File 'lib/apimode/xbee_api.rb', line 187

def neighbors
  frame_id = self.next_frame_id
  # neighbors often takes more than 1000ms to return data
  node_discover_cmd = XBee::Frame::ATCommand.new("ND",frame_id,nil)
  #puts "Node discover command dump: #{node_discover_cmd._dump(self.api_mode.in_symbol).unpack("C*").join(", ")}"
  tmp = @xbee_serialport.read_timeout
  @xbee_serialport.read_timeout = Integer(self.node_discover_timeout.in_seconds * 1050)
  @xbee_serialport.write(node_discover_cmd._dump(self.api_mode.in_symbol))
  responses = []
  #read_thread = Thread.new do
  begin
    loop do
      r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
      # puts "Got a response! Frame ID: #{r.frame_id}, Command: #{r.at_command}, Status: #{r.status}, Value: #{r.retrieved_value}"
      if r.kind_of?(XBee::Frame::ATCommandResponse) && r.status == :OK && r.frame_id == frame_id
        if r.retrieved_value.empty?
          # w00t - the module is telling us it's done with the discovery process.
          break
        else
        responses << r
        end
      else
        raise "Unexpected response to ATND command: #{r.inspect}"
      end
    end
  rescue Exception => e
    puts "Okay, must have finally timed out on the serial read: #{e}."
  end
  @xbee_serialport.read_timeout = tmp
  responses.map do |r|
    unpacked_fields = r.retrieved_value.unpack("nNNZxnCCnn")
    return_fields = [:SH, :SL, :NI, :PARENT_NETWORK_ADDRESS, :DEVICE_TYPE, :STATUS, :PROFILE_ID, :MANUFACTURER_ID]
    unpacked_fields.shift #Throw out the junk at the start of the discover packet
    return_fields.inject(Hash.new) do |return_hash, field_name|
      return_hash[field_name] = unpacked_fields.shift
      return_hash
    end
  end
end

#network_reset!(reset_range) ⇒ Object

Performs a network reset on one or more modules within a PAN. The module responds immediately with an “OK” and then restarts the network. All network configuration and routing information is lost if not saved.

Parameter range: 0-1

  • 0: Resets network layer parameters on the node issuing the command.

  • 1: Sends broadcast transmission to reset network layer parameters on all nodes in the PAN.



575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/apimode/xbee_api.rb', line 575

def network_reset!(reset_range)
  if reset_range == 0
    @xbee_serialport.write("ATNR0\r")
  elsif reset_range == 1 then
    @xbee_serialport.write("ATNR1\r")
  else
    #### DEBUG ####
    if $DEBUG then
      puts "Invalid parameter provided: #{reset_range}"
    end
    #### DEBUG ####
  end
end

#next_frame_idObject



49
50
51
# File 'lib/apimode/xbee_api.rb', line 49

def next_frame_id
  @frame_id += 1
end

#node_idObject

returns the node ID of the device. Node ID is typically a human-meaningful name to give to the XBee device, much like a hostname.



305
306
307
# File 'lib/apimode/xbee_api.rb', line 305

def node_id
  @node_id ||= get_param("NI")
end

#node_id!(new_id) ⇒ Object

sets the node ID to a user-definable text string to make it easier to identify the device with “human” names. This node id is reported to neighboring XBees so consider it “public”.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/apimode/xbee_api.rb', line 313

def node_id!(new_id)
  tmp = @xbee_serialport.read_timeout
  @xbee_serialport.read_timeout = read_timeout(:long)
  @xbee_serialport.write("ATNI#{new_id}\r")
  if self.transmission_mode == :SYNC
    response = getresponse
    @xbee_serialport.read_timeout = tmp
    if ( response.nil? )
      return ""
    else
      response.strip.chomp
    end
  end
end

#pan_idObject

returns the PAN ID of the device. PAN ID is one of the 3 main identifiers used to communicate with the device from other XBees. All XBees which are meant to communicate must have the same PAN ID and channel number. The 3rd identifier is the address of the device itself represented by its serial number (High and Low) and/or it’s 16-bit MY source address.



334
335
336
# File 'lib/apimode/xbee_api.rb', line 334

def pan_id
  @pan_id ||= get_param("ID").unpack("n")
end

#pan_id!(new_id) ⇒ Object

sets the PAN ID of the device. Modules must have the same PAN ID in order to communicate with each other. The PAN ID value can range from 0 - 0xffff. The default from the factory is set to 0x3332.



342
343
344
345
# File 'lib/apimode/xbee_api.rb', line 342

def pan_id!(new_id)
  @xbee_serialport.write("ATID#{new_id}\r")
  getresponse if self.transmission_mode == :SYNC
end

#parityObject

returns the parity of the device as represented by a symbol: :None - for 8-bit none :Even - for 8-bit even :Odd - for 8-bit odd :Mark - for 8-bit mark :Space - for 8-bit space



382
383
384
385
386
387
388
# File 'lib/apimode/xbee_api.rb', line 382

def parity
  @xbee_serialport.write("ATNB\r")
  if self.transmission_mode == :SYNC
    response = getresponse().strip.chomp
    @paritycodes.index( response.to_i )
  end
end

#parity!(parity_type) ⇒ Object

sets the parity of the device to one represented by a symbol contained in the parity_type parameter :None - for 8-bit none :Even - for 8-bit even :Odd - for 8-bit odd :Mark - for 8-bit mark :Space - for 8-bit space



397
398
399
400
401
402
403
404
# File 'lib/apimode/xbee_api.rb', line 397

def parity!( parity_type )
  # validate symbol before writing parity param
  if !@paritycodes.include?(parity_type)
    return false
  end
  @xbee_serialport.write("ATNB#{@paritycodes[parity_type]}\r")
  getresponse if self.transmission_mode == :SYNC
end

#received_signal_strengthObject

returns the signal strength in dBm units of the last received packet. Expect a negative integer or 0 to be returned. If the XBee device has not received any neighboring packet data, the signal strength value will be 0



351
352
353
# File 'lib/apimode/xbee_api.rb', line 351

def received_signal_strength
  -(get_param("DB").hex)
end

#reset!Object

Resets the XBee module through software and simulates a power off/on. Any configuration changes that have not been saved with the save! method will be lost during reset.

The module responds immediately with “OK” then performs a reset ~2 seconds later. The reset is a required when the module’s SC or ID has been changes to take into affect.



563
564
565
# File 'lib/apimode/xbee_api.rb', line 563

def reset!
  @xbee_serialport.write("ATFR\r")
end

#restore!Object

Restores all the module parameters to factory defaults Restore (RE) command does not reset the ID parameter.



592
593
594
# File 'lib/apimode/xbee_api.rb', line 592

def restore!
  @xbee_serialport.write("ATRE\r")
end

#save!Object

writes the current XBee configuration to the XBee device’s flash. There is no undo for this operation



552
553
554
555
# File 'lib/apimode/xbee_api.rb', line 552

def save!
  @xbee_serialport.write("ATWR\r")
  getresponse if self.transmission_mode == :SYNC
end

#send!(message) ⇒ Object

just a straight pass through of data to the XBee. This can be used to send data when not in AT command mode, or if you want to control the XBee with raw commands, you can send them this way.



600
601
602
# File 'lib/apimode/xbee_api.rb', line 600

def send!(message)
  @xbee_serialport.write( message )
end

#serial_numObject

Returns the complete serialnumber of XBee device by quering the high and low parts.



269
270
271
# File 'lib/apimode/xbee_api.rb', line 269

def serial_num
  self.serial_num_high() << 32 | self.serial_num_low
end

#serial_num_highObject

Returns the high portion of the XBee device’s serial number. this value is factory set.



263
264
265
# File 'lib/apimode/xbee_api.rb', line 263

def serial_num_high
  @serial_high ||= get_param("SH","N")
end

#serial_num_lowObject

Returns the low portion of the XBee device’s serial number. this value is factory set.



257
258
259
# File 'lib/apimode/xbee_api.rb', line 257

def serial_num_low
  @serial_low ||= get_param("SL","N")
end

#set_param(at_param_name, param_value, at_param_unpack_string = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/apimode/xbee_api.rb', line 94

def set_param(at_param_name, param_value, at_param_unpack_string = nil)
  frame_id = self.next_frame_id
  at_command_frame = XBee::Frame::ATCommand.new(at_param_name,frame_id,param_value,at_param_unpack_string)
  # puts "Sending ... [#{at_command_frame._dump(self.api_mode.in_symbol).unpack("C*").join(", ")}]"
  self.xbee_serialport.write(at_command_frame._dump(self.api_mode.in_symbol))
  if self.transmission_mode == :SYNC
  r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
    if r.kind_of?(XBee::Frame::ATCommandResponse) && r.status == :OK && r.frame_id == frame_id
      if block_given?
        yield r
      else
        at_param_unpack_string.nil? ? r.retrieved_value : r.retrieved_value.unpack(at_param_unpack_string).first
      end
    else
      raise "Response did not indicate successful retrieval of that parameter: #{r.inspect}"
    end
  end
end

#set_remote_param(at_param_name, param_value, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/apimode/xbee_api.rb', line 132

def set_remote_param(at_param_name, param_value, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil)
  frame_id = self.next_frame_id
  at_command_frame = XBee::Frame::RemoteCommandRequest.new(at_param_name, remote_address, remote_network_address, frame_id, param_value, at_param_unpack_string)
  puts "Sending ... [#{at_command_frame._dump(self.api_mode.in_symbol).unpack("C*").join(", ")}]"
  self.xbee_serialport.write(at_command_frame._dump(self.api_mode.in_symbol))
  if self.transmission_mode == :SYNC
    r = XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol)
    if r.kind_of?(XBee::Frame::RemoteCommandResponse) && r.status == :OK && r.frame_id == frame_id
      if block_given?
        yield r
      else
        at_param_unpack_string.nil? ? r.retrieved_value : r.retrieved_value.unpack(at_param_unpack_string).first
      end
    else
      raise "Response did not indicate successful retrieval of that parameter: #{r.inspect}"
    end
  end
end

#start_apimode_communicationObject

Switch to API mode - note that in Series 2 the Operation Mode is defined by the firmware flashed to the device. Only Series 1 can switch from AT (Transparent) to API Opearation and back seamlessly.

API Mode 1 - API Enabled API Mode 2 - API Enabled, with escaped control characters



60
61
62
63
64
65
66
67
# File 'lib/apimode/xbee_api.rb', line 60

def start_apimode_communication
  in_command_mode do
    puts "Entering api mode"
    # Set API Mode 2 (include escaped characters)
    self.xbee_serialport.write("ATAP1\r")
    self.xbee_serialport.read(3)
  end
end

#versionObject

returns the version of this class



614
615
616
# File 'lib/apimode/xbee_api.rb', line 614

def version
  VERSION
end