diff --git a/pdu_rx.py b/pdu_rx.py index ba8ae93..10e2a82 100644 --- a/pdu_rx.py +++ b/pdu_rx.py @@ -2,20 +2,71 @@ import asyncio from time import sleep +import framing +from header import Onbeat_Header -PACKET_LEN=128 +PACKET_LEN=255*4 + 32 +DATA_LEN=223*4 +HEADER_LEN=32 -async def tcp_client(): +async def tcp_client(file_2_send: str, callsign: str, rs_coding: bool = True): port = 50001 reader, writer = await asyncio.open_connection( '127.0.0.1', port) - with open("loremipsum", "rb") as file_to_send: - while message := file_to_send.read(PACKET_LEN): - writer.write(message) - sleep(1) + with open(file_2_send, "rb") as file_to_send: + seq = 0 + sent: list[list[int]] = [] + while message := file_to_send.read(DATA_LEN if rs_coding == True else (PACKET_LEN - HEADER_LEN)): + sent.append([i for i in message]) + frame = bytes(framing.encode_packet([i for i in message], callsign, seq, rs_coding)) + writer.write(frame) + seq += 1 + sleep(0.1) + else: + frame = bytes(framing.encode_packet([], callsign, seq, rs_coding)) + for _ in range(0,16): + writer.write(frame) + sleep(0.1) + sent.append([]) + output: list[tuple[Onbeat_Header, list[int]]] = [] + packet_loss = 0 while message := await reader.read(PACKET_LEN): - print(f"Got: {message.decode()}\n\n") + try: + received = framing.decode_packet([i for i in message]) + output.append(received) + if received[0].pkt_len == 0: + break + except Exception as e: + packet_loss += 1 + print(e) -asyncio.run(tcp_client()) + return sent, output + + +rs_coding = False +filename = "loremipsum" +sent, output = asyncio.run(tcp_client(filename, "HA5PLS", rs_coding)) +output.sort(key=lambda packet: packet[0].pkt_sequence_id) +output.pop() + +def hamming_dist(msg:int, got:int): + return bin(msg ^ got).count('1') + +total_errors = sum([sum([hamming_dist(msg, got) for msg, got in zip(sent[pkt[0].pkt_sequence_id], pkt[1])]) for pkt in output]) +rcv_len = sum([pkt[0].pkt_len for pkt in output]) * 8 +ber = total_errors/rcv_len +print(f"Total errors: {total_errors}, BER: {ber}") + + + +# file : bytes = b'' +# filesize = 0 +# for pkt in output: +# payload = bytes(pkt[1]) +# file += payload +# filesize += pkt[0].pkt_len +# print(file.decode(errors="ignore")) + +# print(f"Lost {packet_loss} packets during the transfer, filesize: {filesize}") diff --git a/simulation/awgn.grc b/simulation/awgn.grc index 1e8596e..c86b0f2 100644 --- a/simulation/awgn.grc +++ b/simulation/awgn.grc @@ -37,7 +37,7 @@ blocks: id: variable parameters: comment: '' - value: '10' + value: '2' states: bus_sink: false bus_source: false @@ -102,7 +102,7 @@ blocks: label: '' short_id: '' type: eng_float - value: '100' + value: '3' states: bus_sink: false bus_source: false @@ -121,7 +121,7 @@ blocks: comment: '' maxoutbuf: '0' minoutbuf: '0' - roc_tolerance: '1' + roc_tolerance: '0.8' samp_rate: samp_rate symbol_map: symbol_map syncword: syncword @@ -140,7 +140,6 @@ blocks: affinity: '' alias: '' comment: '' - data_len: data_len maxoutbuf: '0' minoutbuf: '0' preamble: '[0x33 for i in range(0, 128*2, 8)]' @@ -149,7 +148,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [464, 256.0] + coordinate: [480, 264.0] rotation: 0 state: enabled - name: blocks_tag_gate_0 @@ -168,7 +167,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [752, 264.0] + coordinate: [744, 280.0] rotation: 0 state: enabled - name: blocks_throttle2_0 @@ -189,7 +188,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [1136, 264.0] + coordinate: [1144, 280.0] rotation: 0 state: enabled - name: channels_channel_model_0 @@ -203,14 +202,14 @@ blocks: freq_offset: freq_offset maxoutbuf: '0' minoutbuf: '0' - noise_voltage: (1/EbN0*(1+RRC_ALPHA)/2)**0.5 + noise_voltage: 1.0/sqrt(2* 10**(float(EbN0) / 10)) * sqrt(OVERSAMPLING/2) seed: '0' taps: '1.0' states: bus_sink: false bus_source: false bus_structure: null - coordinate: [928, 232.0] + coordinate: [928, 248.0] rotation: 0 state: enabled - name: data_len @@ -222,7 +221,7 @@ blocks: label: '' short_id: '' type: intx - value: '128' + value: 255*4 + 32 states: bus_sink: false bus_source: false @@ -247,6 +246,19 @@ blocks: coordinate: [960, 136.0] rotation: 0 state: enabled +- name: import_0 + id: import + parameters: + alias: '' + comment: '' + imports: from math import log10, log2, sqrt + states: + bus_sink: false + bus_source: false + bus_structure: null + coordinate: [968, 24.0] + rotation: 0 + state: enabled - name: network_socket_pdu_1 id: network_socket_pdu parameters: @@ -647,7 +659,7 @@ blocks: bus_sink: false bus_source: false bus_structure: null - coordinate: [1320, 272.0] + coordinate: [1320, 288.0] rotation: 0 state: enabled - name: virtual_source_0