11import ax25
2- import sys
2+ import binascii
33
44class AX25 :
55 """
@@ -23,13 +23,23 @@ def ax25EncodeIFrame(self, data_to_send: str, src_callsign: str = GROUND_STATION
2323 :param ns: Send Sequence Number
2424 :return: True if the frame is successfully made otherwise False
2525 """
26-
26+
27+ # Generate Frame Object as per Library Specfications
2728 control_block = ax25 .Control (frame_type = ax25 .FrameType .I , poll_final = False , send_seqno = ns )
2829 src_address = ax25 .Address (call = src_callsign , ssid = self ._DEFAULT_SSID )
2930 dst_address = ax25 .Address (call = dst_callsign , ssid = self ._DEFAULT_SSID )
30- frame_bytes = ax25 .Frame (dst = dst_address , src = src_address , via = None , control = control_block , pid = 0 , data = data_to_send .encode ('utf-8' )).pack ()
31+ frame_bytes = bytearray ( ax25 .Frame (dst = dst_address , src = src_address , via = None , control = control_block , pid = 0 , data = data_to_send .encode ('utf-8' )).pack () )
3132
32- return frame_bytes ;
33+ # Calculate fcs using CRC 16
34+ fcs = bytearray (binascii .crc_hqx (frame_bytes , 0 ).to_bytes (2 , 'big' ))
35+ # Define the flags
36+ flag = bytearray (bytes .fromhex ("7E" ))
37+ # Use the mutability of bytearrays to append everything into a huge bytearray that contains what we want to send
38+ frame_bytes = flag + frame_bytes + fcs + flag
39+ # Convert the bytearray to bytes
40+ return_frame = bytes (frame_bytes )
41+
42+ return return_frame ;
3343
3444 def ax25DecodeIFrame (self , data : bytes ) -> ax25 .Frame :
3545 """
@@ -41,12 +51,24 @@ def ax25DecodeIFrame(self, data: bytes) -> ax25.Frame:
4151 :param ns: Send Sequence Number
4252 :return: True if the frame is successfully made otherwise False
4353 """
44-
45- return ax25 .Frame .unpack (data )
54+ # Extract the original 2 fcs bytes (16 bits)
55+ fcs_original = int .from_bytes (data [- 3 :- 1 ], byteorder = 'big' , signed = True )
56+
57+ # Use splicing to get rid of the start and end byte and 2 fcs bytes
58+ data = data [1 :- 3 ]
59+
60+ # Calculate fcs of recieved frame
61+ fcs_data = binascii .crc_hqx (data , 0 )
62+
63+ # TODO: Error handle this properly
64+ if (fcs_original != fcs_data ):
65+ print ("Oh no" )
4666
67+ return ax25 .Frame .unpack (data )
4768
4869
70+ # Example Usage
4971coder = AX25 ()
50- frame = coder .ax25EncodeIFrame ("Hallo " , AX25 .CUBE_SAT_CALLSIGN , AX25 .GROUND_STATION_CALLSIGN , 0 )
72+ frame = coder .ax25EncodeIFrame ("A " , AX25 .CUBE_SAT_CALLSIGN , AX25 .GROUND_STATION_CALLSIGN , 0 )
5173print (frame )
5274print (coder .ax25DecodeIFrame (frame ).dst )
0 commit comments