/// <summary> /// Initializes a new instance of class <see cref="XBee16BitAddress"/>. /// </summary> /// <remarks>The string must be the hexadecimal representation of a 16-bit address.</remarks> /// <param name="address">A string containing the 16-bit address.</param> /// <exception cref="ArgumentNullException">If <c><paramref name="address"/> == null</c>.</exception> /// <exception cref="ArgumentOutOfRangeException">If Length of <paramref name="address"/> is /// lower than 1.</exception> /// <exception cref="FormatException">If <paramref name="address"/> contains non-hexadecimal /// characters or if it is longer than 2 bytes.</exception> public XBee16BitAddress(string address) { if (address == null) { throw new ArgumentNullException("Address cannot be null."); } if (address.Length < 1) { throw new ArgumentOutOfRangeException("Address must contain at least 1 character."); } if (!XBEE_16_BIT_ADDRESS_PATTERN.IsMatch(address)) { throw new FormatException("Address must follow this pattern: (0x)XXXX."); } // Convert the string into a byte array. byte[] byteAddress = HexUtils.HexStringToByteArray(address); // Check array size. this.address = new byte[2]; int diff = this.address.Length - byteAddress.Length; for (int i = 0; i < diff; i++) { this.address[i] = 0; } for (int i = diff; i < this.address.Length; i++) { this.address[i] = byteAddress[i - diff]; } }
/// <summary> /// Initializes a new instance of class <see cref="XBee64BitAddress"/>. /// </summary> /// <remarks>The string must be the hexadecimal representation of a 64-bit address.</remarks> /// <param name="address">A string containing the 64-bit address.</param> /// <exception cref="ArgumentNullException">If <c><paramref name="address"/> == null</c>.</exception> /// <exception cref="ArgumentOutOfRangeException">If Length of <paramref name="address"/> is /// lower than 1.</exception> /// <exception cref="FormatException">If <paramref name="address"/> contains non-hexadecimal /// characters or if it is longer than 8 bytes.</exception> public XBee64BitAddress(string address) { if (address == null) { throw new ArgumentNullException("Address cannot be null."); } if (address.Length < 1) { throw new ArgumentOutOfRangeException("Address must contain at least 1 character."); } if (!XBEE_64_BIT_ADDRESS_PATTERN.IsMatch(address)) { throw new FormatException("Address must follow this pattern: (0x)0013A20040XXXXXX."); } byte[] byteAddress = HexUtils.HexStringToByteArray(address); this.address = new byte[8]; int diff = this.address.Length - byteAddress.Length; for (int i = 0; i < diff; i++) { this.address[i] = 0; } for (int i = diff; i < this.address.Length; i++) { this.address[i] = byteAddress[i - diff]; } }
/// <summary> /// Parses the given hexadecimal string and returns an XBee packet. /// </summary> /// <remarks>The string can contain white spaces.</remarks> /// <param name="packet">The hexadecimal string to parse.</param> /// <param name="mode">The operating mode to parse the packet (<see cref="OperatingMode.API"/> or /// <see cref="OperatingMode.API_ESCAPE"/>).</param> /// <returns>The generated XBee Packet.</returns> /// <exception cref="ArgumentNullException">If <c><paramref name="packet"/> == null</c>.</exception> /// <seealso cref="OperatingMode.API"/> /// <seealso cref="OperatingMode.API_ESCAPE"/> public static XBeePacket ParsePacket(string packet, OperatingMode mode) { if (packet == null) { throw new ArgumentNullException("Packet cannot be null."); } return(ParsePacket(HexUtils.HexStringToByteArray(packet.Trim().Replace(" ", "")), mode)); }
/// <summary> /// Class constructor. Instantiates a new object of type <see cref="XBeeIMEIAddress"/> with the /// given parameter. /// </summary> /// <param name="address">The IMEI address as string.</param> /// <exception cref="ArgumentNullException">If <c><paramref name="address"/> == null</c>.</exception> /// <exception cref="ArgumentException">If <paramref name="address"/> does not match the IMEI /// address pattern.</exception> public XBeeIMEIAddress(string address) { if (address == null) { throw new ArgumentNullException(ERROR_IMEI_NULL); } if (!Regex.IsMatch(IMEI_PATTERN, address)) { throw new ArgumentException(ERROR_IMEI_INVALID); } byte[] byteAddress = HexUtils.HexStringToByteArray(address); GenerateByteAddress(byteAddress); }
/// <summary> /// Starts the read or write operation. /// </summary> /// <param name="read"><c>true</c> to start the read operation, <c>false</c> to start the /// write operation.</param> private void StartOperation(bool read) { // Show a progress dialog while performing the operation. ShowLoadingDialog(read ? "Reading settings..." : "Writing settings..."); // The read/write process blocks the UI interface, so it must be done in a different thread. Task.Run(() => { try { if (read) { // Read the values. ShValue = HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("SH")); SlValue = HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("SL")); BlValue = HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("BL")); NiValue = Encoding.Default.GetString(BleDevice.XBeeDevice.GetParameter("NI")); ApValue = int.Parse(HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("AP"))); D9Value = int.Parse(HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("D9"))); VrValue = HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("VR")); HvValue = HexUtils.ByteArrayToHexString(BleDevice.XBeeDevice.GetParameter("HV")); } else { // Write the values. BleDevice.XBeeDevice.SetParameter("NI", Encoding.Default.GetBytes(NiValue)); BleDevice.XBeeDevice.SetParameter("AP", HexUtils.HexStringToByteArray(ApValue.ToString())); BleDevice.XBeeDevice.SetParameter("D9", HexUtils.HexStringToByteArray(D9Value.ToString())); } } catch (XBeeException e) { ShowErrorDialog("Error performing operation", e.Message); } // Close the dialog. HideLoadingDialog(); }); }
/** * Parses the given hexadecimal string and returns a Generic XBee packet. * * <p>The string can contain white spaces.</p> * * @param packet The hexadecimal string to parse. * @param mode The operating mode to parse the packet (API 1 or API 2). * * @return The generated Generic XBee Packet. * * @throws ArgumentException if {@code mode != OperatingMode.API } and * if {@code mode != OperatingMode.API_ESCAPE}. * @throws InvalidPacketException if the given string does not represent a * valid frame: invalid checksum, Length, * start delimiter, etc. * @throws ArgumentNullException if {@code packet == null}. * * @see com.digi.xbee.api.models.OperatingMode#API * @see com.digi.xbee.api.models.OperatingMode#API_ESCAPE */ public static XBeePacket ParsePacket(String packet, OperatingMode mode) /*throws InvalidPacketException*/ { Contract.Requires <ArgumentNullException>(packet != null, "Packet cannot be null."); return(ParsePacket(HexUtils.HexStringToByteArray(packet.Trim().Replace(" ", "")), mode)); }