Пример #1
0
    def __init__(self, name, tx_queue):
        threading.Thread.__init__(self)
        self.taskState = CC1020_ISR_STATE_INIT
        self.ax_counter = 0
        self.frameIndex = 0
        self.tx_buffer = None
        self.bit_counter = 0
        self.byte = 0
        self._last_bit = 1
        self._bit_in_byte_counter = 0
        self._out_byte = 0
        self._flag_counter = 0
        self._preamble_byte = 0x7E

        self.tx_queue = tx_queue

        # Scrambler variables
        self.d_shift_register = 0
        self.d_taps = [0] * 32
        self.d_tap_count = 0

        self.frame = []

        self.client = TCPClient(IP, PORT)
        # Connect to TCP server
        connected = self.client.connect()
        if connected:
            print('Connection Successful')
        else:
            print('Failed to connect')
Пример #2
0
 def connect_to_server(self):
     self.client = TCPClient(IP, PORT)
     # Connect to TCP server
     connected = self.client.connect()
     if connected:
         print('Connection Successful')
         # self.client.receive_data() # read all data to flush buffers
     else:
         print('Failed to connect')
Пример #3
0
class TransmitThread(threading.Thread):
    def __init__(self, name, tx_queue):
        threading.Thread.__init__(self)
        self.taskState = CC1020_ISR_STATE_INIT
        self.ax_counter = 0
        self.frameIndex = 0
        self.tx_buffer = None
        self.bit_counter = 0
        self.byte = 0
        self._last_bit = 1
        self._bit_in_byte_counter = 0
        self._out_byte = 0
        self._flag_counter = 0
        self._preamble_byte = 0x7E

        self.tx_queue = tx_queue

        # Scrambler variables
        self.d_shift_register = 0
        self.d_taps = [0] * 32
        self.d_tap_count = 0

        self.frame = []

        self.client = TCPClient(IP, PORT)
        # Connect to TCP server
        connected = self.client.connect()
        if connected:
            print('Connection Successful')
        else:
            print('Failed to connect')

    def run(self):
        while (1):
            if not self.tx_queue.empty():
                self.tx_buffer = self.tx_queue.get()
                print("Sending Data")
                self.transmit_preamble()
                self._last_bit = 1
                while not self.transmit_packet():
                    pass
                self.transmit_preamble()
                self.transmit_frame()

    def transmit_frame(self):
        sent = self.client.send_data(bytes(self.frame))
        self.frame = []
        return sent

    def transmit_byte(self):
        self.frame.append(self._out_byte)
        self._out_byte = 0

    def transmit_bit_nrzi(self, out_bit):
        if (out_bit == self._last_bit):
            self._out_byte = self._out_byte | (
                0x80 >> self._bit_in_byte_counter)
            self._last_bit = 1
        else:
            self._out_byte = self._out_byte & ~(
                (0x80 >> self._bit_in_byte_counter) & 0xFF)
            self._last_bit = 0

        self._bit_in_byte_counter += 1
        if self._bit_in_byte_counter == 8:
            self._bit_in_byte_counter = 0
            self.transmit_byte()

    def transmit_bit(self, bit):
        if bit:
            self._out_byte = self._out_byte | (
                0x80 >> self._bit_in_byte_counter)
        else:
            self._out_byte = self._out_byte & ~(
                (0x80 >> self._bit_in_byte_counter) & 0xFF)

        self._bit_in_byte_counter += 1
        if self._bit_in_byte_counter == 8:
            self._bit_in_byte_counter = 0
            self.transmit_byte()

    def transmit_preamble(self):
        count = 0
        while count < 50:
            self._out_byte = 0x55
            self.transmit_byte()
            count += 1

    def transmit_packet(self):

        if self.taskState == CC1020_ISR_STATE_INIT:
            self.frameIndex = 0
            self.taskState = CC1020_ISR_STATE_TX_FLAG
            self.byte = 0x7E  #Preamble value
            self.bit_counter = 0
            self._bit_in_byte_counter = 0

        elif self.taskState == CC1020_ISR_STATE_TX_FLAG:
            if (self.byte & 0x80):
                self.transmit_bit_nrzi(1)
            else:
                self.transmit_bit_nrzi(0)
            self.bit_counter += 1
            if (self.bit_counter == 8):
                self._flag_counter += 1
                self.bit_counter = 0
                self.byte = 0x7E  #Preamble value
                if self._flag_counter >= 2:
                    if (self.frameIndex):
                        self.taskState = CC1020_ISR_STATE_INIT
                        return self.frameIndex

                    self.byte = self.tx_buffer[
                        self.frameIndex]  # Load first element
                    self.taskState = CC1020_ISR_STATE_TX_DATA
                    return 0
            else:
                self.byte <<= 1
            return 0

        elif self.taskState == CC1020_ISR_STATE_TX_DATA:
            if (self.byte & 0x80):
                self.ax_counter += 1
                self.transmit_bit_nrzi(1)
            else:
                self.ax_counter = 0
                self.transmit_bit_nrzi(0)

            self.bit_counter += 1
            if (self.ax_counter == 5):
                self.ax_counter = 0
                self.taskState = CC1020_ISR_STATE_TX_STUF

            if (self.bit_counter == 8):
                self.bit_counter = 0
                if (self.frameIndex == len(self.tx_buffer) - 1):
                    self.byte = 0x7E  # Flag
                    self.tx_buffer = None
                    self.taskState = CC1020_ISR_STATE_TX_FLAG
                    return 0

                self.frameIndex += 1
                self.byte = self.tx_buffer[
                    self.frameIndex]  # Load next element

                return 0
            else:
                self.byte <<= 1

            return 0

        elif self.taskState == CC1020_ISR_STATE_TX_STUF:
            self.transmit_bit_nrzi(0)
            self.taskState = CC1020_ISR_STATE_TX_DATA
            return 0

        else:
            self.taskState = CC1020_ISR_STATE_INIT
            return 0
Пример #4
0
class ReceiverThread(threading.Thread):
    def __init__(self, name, rx_queue):
        threading.Thread.__init__(self)
        self._please_stop = False

        self.taskState = CC1020_ISR_STATE_INIT
        self.ax_counter = 0
        self.frameIndex = 0
        self.rx_buffer = b''
        self.bit_counter = 0
        self.byte = 0
        self._bit_slide_counter = 8
        self._byte_counter = 0
        self._tcp_byte = 0
        self.rx_queue = rx_queue

        if not FROM_FILE:
            self.connect_to_server()

    def run(self):
        while (1):
            # if not bit_queue.empty():
            #     bit = bit_queue.get()
            if self.receive_task(274):
                ax25_packet = AX25Packet()
                if len(self.rx_buffer):
                    ax25_packet.parse_bytes(self.rx_buffer)
                    # print("Enqueued packet")
                    if ax25_packet.valid:
                        print('Valid CRC found!')
                        if global_rx_queue is not None:
                            global_rx_queue.put(self.rx_buffer)
                        else:
                            self.rx_queue.put(self.rx_buffer)

                    self.rx_buffer = b''

    def stop(self):
        self._please_stop = True

    def receive_bit(self):
        if self._bit_slide_counter == 8:
            if FROM_FILE:
                global data_counter
                if data_counter == file_data_length:
                    self.stop()
                    return

                self._tcp_byte = bytes([file_data[data_counter]])
                data_counter += 1
            else:
                self._tcp_byte = self.client.receive_data(1)

            self._bit_slide_counter = 0

        out_bit = 0
        if ord(self._tcp_byte) & (0x80 >> self._bit_slide_counter): out_bit = 1

        self._bit_slide_counter += 1

        return out_bit

    def connect_to_server(self):
        self.client = TCPClient(IP, PORT)
        # Connect to TCP server
        connected = self.client.connect()
        if connected:
            print('Connection Successful')
            # self.client.receive_data() # read all data to flush buffers
        else:
            print('Failed to connect')

    def receive_task(self, max_length):
        if self.taskState == CC1020_ISR_STATE_INIT:  # Init all variable for a new RX Process
            #  timerClearTimeoutFlag(&timerFlagTimeout)
            self.byte = 0x00
            self.bit_counter = 0
            self.frameIndex = 0
            self.ax_counter = 0
            self.rx_buffer = b''
            self.taskState = CC1020_ISR_STATE_WAIT_1ST_FLAG
            return 0

        elif self.taskState == CC1020_ISR_STATE_WAIT_1ST_FLAG:  # Wait for 0x7E Flag
            #  timerClearTimeoutFlag(&timerFlagTimeout)
            in_bit = self.receive_bit()
            # print(in_bit, end = '')
            self.byte = (self.byte << 1) & 0xFF
            if (in_bit == 0):
                self.byte = self.byte & ~(0x01)
            else:
                self.byte = self.byte | 0x01

            if (self.byte == 0x7E):
                self.taskState = CC1020_ISR_STATE_READ_DATA
                self.rx_buffer = b''

            return 0

        elif self.taskState == CC1020_ISR_STATE_READ_DATA:  # Get bistream bit after bit
            in_bit = self.receive_bit()
            self.bit_counter += 1
            self.byte = (self.byte << 1) & 0xFF

            if (in_bit):
                self.byte |= 0x01
                self.ax_counter += 1
                # if received 5 consecutives '1', go to "CC1020_ISR_STATE_UNSTUF" state
                if (self.ax_counter == 5):
                    self.ax_counter = 0
                    self.taskState = CC1020_ISR_STATE_UNSTUF
                    return 0

            else:
                self.byte &= ~(0x01)
                self.ax_counter = 0

            if (self.bit_counter == 8):
                self.bit_counter = 0
                if (self.frameIndex < max_length):
                    # print(self.byte)
                    self.rx_buffer += bytes([self.byte])
                    self.frameIndex += 1
                    self.byte = 0
                else:
                    self.taskState = CC1020_ISR_STATE_WAIT_1ST_FLAG
                    self.frameIndex = 0
                    self.ax_counter = 0

            return 0

        elif self.taskState == CC1020_ISR_STATE_UNSTUF:
            in_bit = self.receive_bit()
            if (in_bit):
                # if the received bit is a '1', it can be either a FLAG or an ABORT
                self.taskState = CC1020_ISR_STATE_WAIT_2ND_FLAG
            else:
                # Unstuffing '0' by dropping next bit 0
                self.taskState = CC1020_ISR_STATE_READ_DATA
            return 0

        elif self.taskState == CC1020_ISR_STATE_WAIT_2ND_FLAG:
            in_bit = self.receive_bit()
            if (in_bit):
                # if the received bit is a '1', it's an ABORT or ERROR
                self.taskState = CC1020_ISR_STATE_INIT
                return 0
            else:
                # if the received bit is a '0', it's a FLAG
                self.taskState = CC1020_ISR_STATE_DELIVER_PACKET
                return 0

        elif self.taskState == CC1020_ISR_STATE_DELIVER_PACKET:
            #  Deliver full self.frameIndex and go back to DATA RX
            #  Since the flag might be the start of another packet
            self.taskState = CC1020_ISR_STATE_READ_DATA
            #  reset local variables
            self.byte = 0x00
            self.bit_counter = 0
            #  save value of frame index to be returned
            #  reset self.frameIndex
            self.frameIndex = 0
            return 1

        else:
            self.taskState = CC1020_ISR_STATE_INIT
            return 0

        return 0