Class: XBee::BaseAPIModeInterface
- Defined in:
- lib/ruxbee/xbee_api.rb
Overview
This is the main class for API mode for XBee radios.
Direct Known Subclasses
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
-
#association_indication ⇒ Object
Association Indication.
-
#baud ⇒ Object
retrieves the baud rate of the device.
-
#baud!(baud_rate) ⇒ Object
sets the given baud rate into the XBee device.
-
#channel ⇒ Object
returns the channel number of the XBee device.
-
#channel!(new_channel) ⇒ Object
sets the channel number of the device.
-
#destination_high ⇒ Object
Returns the high portion of the XBee device’s current destination address.
-
#destination_high!(high_addr) ⇒ Object
Sets the high portion of the XBee device’s current destination address Parameter range: 0 - 0xFFFFFFFF.
-
#destination_low ⇒ Object
Returns the low portion of the XBee device’s current destination address.
-
#destination_low!(low_addr) ⇒ Object
Sets the low portion of the XBee device’s destination address Parameter range: 0 - 0xFFFFFFFF.
-
#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:.
-
#dio!(port, iotype) ⇒ Object
configures an i/o port on the XBee for analog to digital or digital input or output (GPIO).
-
#dio_change_detect ⇒ Object
reads the bitfield values for change detect monitoring.
-
#dio_change_detect!(hexbitmap) ⇒ Object
sets the bitfield values for change detect monitoring.
-
#exit_command_mode ⇒ Object
exits the AT command mode - all changed parameters will take effect such as baud rate changes after the exit is complete.
-
#fw_rev ⇒ Object
Retrieve XBee firmware version.
- #get_param(at_param_name, at_param_unpack_string = nil) ⇒ Object
- #get_remote_param(at_param_name, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil) ⇒ Object
-
#getresponse ⇒ Object
returns results from the XBee.
-
#hw_rev ⇒ Object
Retrieve XBee hardware version.
-
#initialize(xbee_usbdev_str, uart_config = XBeeUARTConfig.new, operation_mode = :API, transmission_mode = :SYNC) ⇒ BaseAPIModeInterface
constructor
Attributes *
xbee_usbdev_str
- USB Device as a string. -
#io_input ⇒ Object
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).
-
#io_output!(hexbitmap) ⇒ Object
Sets the digital output levels of any DIO lines which were configured for output using the dio! method.
-
#neighbors ⇒ Object
Neighbor node discovery.
-
#network_reset!(reset_range) ⇒ Object
Performs a network reset on one or more modules within a PAN.
- #next_frame_id ⇒ Object
-
#node_id ⇒ Object
returns the node ID of the device.
-
#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.
-
#pan_id ⇒ Object
returns the PAN ID of the device.
-
#pan_id!(new_id) ⇒ Object
sets the PAN ID of the device.
-
#parity ⇒ Object
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.
-
#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.
-
#received_signal_strength ⇒ Object
returns the signal strength in dBm units of the last received packet.
-
#reset! ⇒ Object
Resets the XBee module through software and simulates a power off/on.
-
#restore! ⇒ Object
Restores all the module parameters to factory defaults Restore (RE) command does not reset the ID parameter.
-
#save! ⇒ Object
writes the current XBee configuration to the XBee device’s flash.
-
#send!(message) ⇒ Object
just a straight pass through of data to the XBee.
-
#serial_num ⇒ Object
Returns the complete serialnumber of XBee device by quering the high and low parts.
-
#serial_num_high ⇒ Object
Returns the high portion of the XBee device’s serial number.
-
#serial_num_low ⇒ Object
Returns the low portion of the XBee device’s serial number.
- #set_param(at_param_name, param_value, at_param_unpack_string = nil) ⇒ Object
- #set_remote_param(at_param_name, param_value, remote_address = 0x000000000000ffff, remote_network_address = 0xfffe, at_param_unpack_string = nil) ⇒ Object
-
#start_apimode_communication ⇒ Object
Switch to API mode - note that in Series 2 the Operation Mode is defined by the firmware flashed to the device.
-
#version ⇒ Object
returns the version of this class.
Methods inherited from RFModule
#getresults, #in_command_mode, #read_timeout
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)
40 41 42 43 44 45 46 |
# File 'lib/ruxbee/xbee_api.rb', line 40 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_indication ⇒ Object
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
158 159 160 161 |
# File 'lib/ruxbee/xbee_api.rb', line 158 def association_indication @association_indication ||= get_param("AI","n") if @association_indication == nil then @association_indication = 0 end end |
#baud ⇒ Object
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.
359 360 361 362 363 |
# File 'lib/ruxbee/xbee_api.rb', line 359 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
369 370 371 372 |
# File 'lib/ruxbee/xbee_api.rb', line 369 def baud!( baud_rate ) @xbee_serialport.write("ATBD#{@baudcodes[baud_rate]}\r") getresponse if self.transmission_mode == :SYNC end |
#channel ⇒ Object
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
275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/ruxbee/xbee_api.rb', line 275 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.
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/ruxbee/xbee_api.rb', line 289 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_high ⇒ Object
Returns the high portion of the XBee device’s current destination address
242 243 244 |
# File 'lib/ruxbee/xbee_api.rb', line 242 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
249 250 251 252 |
# File 'lib/ruxbee/xbee_api.rb', line 249 def destination_high!(high_addr) self.xbee_serialport.write("ATDH#{high_addr}\r") getresponse if self.transmission_mode == :SYNC end |
#destination_low ⇒ Object
Returns the low portion of the XBee device’s current destination address
228 229 230 |
# File 'lib/ruxbee/xbee_api.rb', line 228 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
235 236 237 238 |
# File 'lib/ruxbee/xbee_api.rb', line 235 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
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/ruxbee/xbee_api.rb', line 424 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
468 469 470 471 472 |
# File 'lib/ruxbee/xbee_api.rb', line 468 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_detect ⇒ Object
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
477 478 479 480 |
# File 'lib/ruxbee/xbee_api.rb', line 477 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
485 486 487 488 |
# File 'lib/ruxbee/xbee_api.rb', line 485 def dio_change_detect!( hexbitmap ) @xbee_serialport.write("ATIC#{hexbitmask}\r") getresponse if self.transmission_mode == :SYNC end |
#exit_command_mode ⇒ Object
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
607 608 609 |
# File 'lib/ruxbee/xbee_api.rb', line 607 def exit_command_mode @xbee_serialport.write("ATCN\r") end |
#fw_rev ⇒ Object
Retrieve XBee firmware version
165 166 167 |
# File 'lib/ruxbee/xbee_api.rb', line 165 def fw_rev @fw_rev ||= get_param("VR","n") end |
#get_param(at_param_name, at_param_unpack_string = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruxbee/xbee_api.rb', line 68 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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ruxbee/xbee_api.rb', line 112 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 ⇒ Object
returns results from the XBee
619 620 621 |
# File 'lib/ruxbee/xbee_api.rb', line 619 def getresponse XBee::Frame.new(self.xbee_serialport, self.api_mode.in_symbol) end |
#hw_rev ⇒ Object
Retrieve XBee hardware version
171 172 173 |
# File 'lib/ruxbee/xbee_api.rb', line 171 def hw_rev @hw_rev ||= get_param("HV","n") end |
#io_input ⇒ Object
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)
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 |
# File 'lib/ruxbee/xbee_api.rb', line 506 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.
494 495 496 497 |
# File 'lib/ruxbee/xbee_api.rb', line 494 def io_output!( hexbitmap ) @xbee_serialport.write("ATIO#{hexbitmap}\r") getresponse if self.transmission_mode == :SYNC end |
#neighbors ⇒ Object
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.
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 |
# File 'lib/ruxbee/xbee_api.rb', line 186 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("nNNZ20nCCnn") 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.
574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/ruxbee/xbee_api.rb', line 574 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_id ⇒ Object
48 49 50 |
# File 'lib/ruxbee/xbee_api.rb', line 48 def next_frame_id @frame_id += 1 end |
#node_id ⇒ Object
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.
304 305 306 |
# File 'lib/ruxbee/xbee_api.rb', line 304 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”.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/ruxbee/xbee_api.rb', line 312 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_id ⇒ Object
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.
333 334 335 |
# File 'lib/ruxbee/xbee_api.rb', line 333 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.
341 342 343 344 |
# File 'lib/ruxbee/xbee_api.rb', line 341 def pan_id!(new_id) @xbee_serialport.write("ATID#{new_id}\r") getresponse if self.transmission_mode == :SYNC end |
#parity ⇒ Object
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
381 382 383 384 385 386 387 |
# File 'lib/ruxbee/xbee_api.rb', line 381 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
396 397 398 399 400 401 402 403 |
# File 'lib/ruxbee/xbee_api.rb', line 396 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_strength ⇒ Object
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
350 351 352 |
# File 'lib/ruxbee/xbee_api.rb', line 350 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.
562 563 564 |
# File 'lib/ruxbee/xbee_api.rb', line 562 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.
591 592 593 |
# File 'lib/ruxbee/xbee_api.rb', line 591 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
551 552 553 554 |
# File 'lib/ruxbee/xbee_api.rb', line 551 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.
599 600 601 |
# File 'lib/ruxbee/xbee_api.rb', line 599 def send!() @xbee_serialport.write( ) end |
#serial_num ⇒ Object
Returns the complete serialnumber of XBee device by quering the high and low parts.
268 269 270 |
# File 'lib/ruxbee/xbee_api.rb', line 268 def serial_num self.serial_num_high() << 32 | self.serial_num_low end |
#serial_num_high ⇒ Object
Returns the high portion of the XBee device’s serial number. this value is factory set.
262 263 264 |
# File 'lib/ruxbee/xbee_api.rb', line 262 def serial_num_high @serial_high ||= get_param("SH","N") end |
#serial_num_low ⇒ Object
Returns the low portion of the XBee device’s serial number. this value is factory set.
256 257 258 |
# File 'lib/ruxbee/xbee_api.rb', line 256 def serial_num_low @serial_low ||= get_param("SL","N") end |
#set_param(at_param_name, param_value, at_param_unpack_string = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ruxbee/xbee_api.rb', line 93 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ruxbee/xbee_api.rb', line 131 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_communication ⇒ Object
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
59 60 61 62 63 64 65 66 |
# File 'lib/ruxbee/xbee_api.rb', line 59 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 |
#version ⇒ Object
returns the version of this class
613 614 615 |
# File 'lib/ruxbee/xbee_api.rb', line 613 def version VERSION end |