public async Task <Bytes> SendAndGetPacket(Bytes packet, byte repeat_count, ushort delay_ms, uint timeout_ms, byte retry_count, ushort preamble_ext_ms) { try { await Connect(); var data = Manchester.Encode(packet.ToArray()); var cmdParams = new Bytes() .Append((byte)0) .Append(repeat_count) .Append(delay_ms) .Append((byte)0) .Append(timeout_ms) .Append(retry_count) .Append(preamble_ext_ms) .Append(data); var result = await this.SendCommand(RileyLinkCommandType.SendAndListen, cmdParams, 5000); if (result != null) { var decoded = new Bytes(Manchester.Decode(result.Sub(2).ToArray())); return(result.Sub(0, 2).Append(decoded)); } else { return(null); } } catch (OmnipyException) { throw; } catch (Exception e) { throw new PacketRadioException("Error while sending and receiving data with RL", e); } }
public Bytes get_data() { var data = new Bytes().Append(this.address); data.Append((byte)((int)this.type | this.sequence)); data.Append(this.body); data.Append(CrcUtil.Crc8(data.ToArray())); return(data); }
private static ushort getChecksum(Bytes body) { ushort checksum = 0; foreach (byte b in body.ToArray()) { checksum += b; } return(checksum); }
private static void parse_version_response(Bytes response, Pod pod) { bool lengthyResponse = false; pod.state_last_updated = DateTime.UtcNow; int i = 1; if (response.Length == 27) { pod.id_version_unknown_7_bytes = response.ToArray(i, i + 7); i += 7; lengthyResponse = true; } var mx = response.Byte(i++); var my = response.Byte(i++); var mz = response.Byte(i++); pod.id_version_pm = $"{mx}.{my}.{mz}"; var ix = response.Byte(i++); var iy = response.Byte(i++); var iz = response.Byte(i++); pod.id_version_pi = $"{ix}.{iy}.{iz}"; pod.id_version_unknown_byte = response.Byte(i++); pod.state_progress = (PodProgress)(response.Byte(i++) & 0x0F); pod.id_lot = response.DWord(i); pod.id_t = response.DWord(i + 4); i += 8; if (!lengthyResponse) { var rb = response.Byte(i++); pod.radio_low_gain = rb >> 6; pod.radio_rssi = rb & 0b00111111; pod.radio_address = response.DWord(i); } else { pod.radio_address = response.DWord(i); } }
public async Task SendPacket(Bytes packet, byte repeat_count, ushort delay_ms, ushort preamble_ext_ms) { try { await Connect(); Debug.WriteLine($"SEND radio packet: {packet}"); var data = Manchester.Encode(packet.ToArray()); var cmdParams = new Bytes((byte)0).Append(repeat_count); cmdParams.Append(delay_ms); cmdParams.Append(preamble_ext_ms); cmdParams.Append(data); await this.SendCommand(RileyLinkCommandType.SendAndListen, cmdParams, 30000); } catch (OmnipyException) { throw; } catch (Exception e) { throw new PacketRadioException("Error while sending data with RL", e); } }
public List <Packet> get_radio_packets(int first_packet_sequence) { this.message_str_prefix = $"{this.address:%08X} {this.sequence:%02X} {this.expect_critical_followup} "; var message_body_len = 0; foreach (var p in this.parts) { var cmd_body = p.Item2; var nonce = p.Item3; message_body_len += (int)cmd_body.Length + 2; if (nonce != null) { message_body_len += 4; } } byte b0 = 0; if (this.expect_critical_followup) { b0 = 0x80; } b0 |= (byte)(this.sequence << 2); b0 |= (byte)((message_body_len >> 8) & 0x03); byte b1 = (byte)(message_body_len & 0xff); var message_body = new Bytes(this.address.Value); message_body.Append(b0); message_body.Append(b1); foreach (var p in this.parts) { var cmd_type = p.Item1; var cmd_body = p.Item2; var nonce = p.Item3; if (nonce == null) { if (cmd_type == (byte)PodResponse.Status) { message_body.Append(cmd_type); } else { message_body.Append(cmd_type); message_body.Append((byte)cmd_body.Length); } } else { message_body.Append(cmd_type); message_body.Append((byte)(cmd_body.Length + 4)); message_body.Append(nonce.Value); } message_body.Append(cmd_body[0]); } var crc_calculated = CrcUtil.Crc16(message_body.ToArray()); message_body.Append(crc_calculated); int index = 0; bool first_packet = true; int sequence = first_packet_sequence; int total_body_len = (int)message_body.Length; var radio_packets = new List <Packet>(); while (index < message_body.Length) { var to_write = Math.Min(31, message_body.Length - index); var packet_body = message_body.Sub(index, index + to_write); radio_packets.Add(new Packet(AckAddressOverride.Value, first_packet ? this.type.Value : RadioPacketType.CON, sequence, packet_body)); first_packet = false; sequence = (sequence + 2) % 32; index += to_write; } if (this.DoubleTake) { var fp = radio_packets[0]; radio_packets.Insert(0, fp); } return(radio_packets); }
private async Task <Bytes> SendCommand(RileyLinkCommandType cmd, Bytes cmdData, int timeout = 2000) { return(new Bytes(await SendCommand(cmd, cmdData.ToArray(), timeout))); }