rtlsdr.rtlsdrtcp

This module allows client/server communication.

The RtlSdrTcpServer class is meant to be connected physically to an SDR dongle and communicate with an instance of RtlSdrTcpClient.

The client is intended to function as closely as possible to the base RtlSdr class (as if it had a physical dongle attached to it).

Both of these classes have the same arguments as the base RtlSdr class with the addition of hostname and port.

Examples

server = RtlSdrTcpServer(hostname='192.168.1.100', port=12345)
server.run_forever()
# Will listen for clients until Ctrl-C is pressed
# On another machine (typically)
client = RtlSdrTcpClient(hostname='192.168.1.100', port=12345)
client.center_freq = 2e6
data = client.read_samples()

Note

On platforms where the librtlsdr library cannot be installed/compiled, it is possible to import RtlSdrTcpClient only by setting the environment variable "RTLSDR_CLIENT_MODE" to "true". If this is set, no other modules will be available.

Feature added in v0.2.4

rtlsdr.rtlsdrtcp.server

class rtlsdr.rtlsdrtcp.server.RequestHandler(request, client_address, server)[source]

Bases: BaseRequestHandler

close()[source]
finish()[source]
handle(rx_message=None)[source]
handle_method_call(rx_message)[source]
handle_prop_get(rx_message)[source]
handle_prop_set(rx_message)[source]
setup()[source]
class rtlsdr.rtlsdrtcp.server.RtlSdrTcpServer(device_index=0, test_mode_enabled=False, serial_number=None, hostname='127.0.0.1', port=None)[source]

Bases: RtlSdr, RtlSdrTcpBase

Server that connects to a physical dongle to allow client connections.

close()[source]

Stops the server (if it’s running) and closes the connection to the dongle.

open(device_index=0, test_mode_enabled=False, serial_number=None)[source]

Connect to the device through the underlying wrapper library

Initializes communication with the device and retrieves information from it with a call to init_device_values().

Parameters
  • device_index (int, optional) – The device index to use if there are multiple dongles attached. If only one is being used, the default value (0) will be used.

  • test_mode_enabled (bool, optional) – If True, enables a special test mode, which will return the value of an internal RTL2832 8-bit counter with calls to read_bytes().

  • serial_number (str, optional) – If not None, the device will be searched for by the given serial_number by get_device_index_by_serial() and the device_index returned will be used automatically.

Notes

The arguments used here are passed directly from object initialization.

Raises

IOError – If communication with the device could not be established.

read_bytes(num_bytes=1024)[source]

Return a packed string of bytes read along with the struct_fmt.

read_samples(num_samples=1024)[source]

This overrides the base implementation so that the raw data is sent. It will be unpacked to I/Q samples on the client side.

run()[source]

Runs the server thread and returns. Use this only if you are running mainline code afterwards. The server must explicitly be stopped by the stop method before exit.

run_forever()[source]

Runs the server and begins a mainloop. The loop will exit with Ctrl-C.

class rtlsdr.rtlsdrtcp.server.Server(rtl_sdr)[source]

Bases: TCPServer

server_close()[source]

Called to clean-up the server.

May be overridden.

REQUEST_RECV_SIZE = 1024
class rtlsdr.rtlsdrtcp.server.ServerThread(rtl_sdr)[source]

Bases: Thread

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

stop()[source]
rtlsdr.rtlsdrtcp.server.run_server()[source]

Convenience function to run the server from the command line with options for hostname, port and device index.

rtlsdr.rtlsdrtcp.client

class rtlsdr.rtlsdrtcp.client.RtlSdrTcpClient(device_index=0, test_mode_enabled=False, hostname='127.0.0.1', port=None)[source]

Bases: RtlSdrTcpBase

Client object that connects to a remote server.

Exposes most of the methods and descriptors that are available in the RtlSdr class in a transparent manner allowing an interface that is nearly identical to the core API.

close()[source]
get_bandwidth()[source]
get_center_freq()[source]
get_freq_correction()[source]
get_gain()[source]
get_gains()[source]
get_sample_rate()[source]
get_tuner_type()[source]
open(*args)[source]
read_bytes(num_bytes=1024)[source]
read_bytes_async(*args)[source]
read_samples(num_samples=1024)[source]
read_samples_async(*args)[source]
set_bandwidth(value)[source]
set_center_freq(value)[source]
set_direct_sampling(value)[source]
set_freq_correction(value)[source]
set_gain(value)[source]
set_sample_rate(value)[source]
property bandwidth
property center_freq
property fc
property freq_correction
property gain
property rs
property sample_rate

rtlsdr.rtlsdrtcp.base

exception rtlsdr.rtlsdrtcp.base.CommunicationError(msg, source_exc=None)[source]

Bases: Exception

class rtlsdr.rtlsdrtcp.base.AckMessage(**kwargs)[source]

Bases: MessageBase

Simple message type meant for ACKnolegemnt of message receipt

get_header(**kwargs)[source]

Builds the header data for the message

The timestamp is added to the header if not already present.

Return type

dict

class rtlsdr.rtlsdrtcp.base.ClientMessage(**kwargs)[source]

Bases: MessageBase

get_header(**kwargs)[source]

Builds the header data for the message

The timestamp is added to the header if not already present.

Return type

dict

get_response_class()[source]
send_message(sock)[source]

Serializes and sends the message

Parameters

sock – The socket object to write to

class rtlsdr.rtlsdrtcp.base.MessageBase(**kwargs)[source]

Bases: object

Base class for messages sent between clients and servers.

Handles serialization/deserialization and communication with socket type objects.

timestamp

Timestamp given from time.time()

Type

float

header

A dict containing message type and payload information

Type

dict

data

The payload containing either the request or response data

classmethod from_remote(sock)[source]

Reads data from the socket and parses an instance of MessageBase

Parameters

sock – The socket object to read from

get_ack_response(sock)[source]
get_data(**kwargs)[source]
get_header(**kwargs)[source]

Builds the header data for the message

The timestamp is added to the header if not already present.

Return type

dict

get_response(sock)[source]

Waits for a specific response message

The message class returned from get_response_class() is used to parse the message (called from from_remote())

Parameters

sock – The socket object to read from

send_message(sock)[source]

Serializes and sends the message

Parameters

sock – The socket object to write to

class rtlsdr.rtlsdrtcp.base.RtlSdrTcpBase(device_index=0, test_mode_enabled=False, hostname='127.0.0.1', port=None)[source]

Bases: object

Base class for all rtlsdrtcp functionality

Parameters
  • device_index (int, optional) –

  • test_mode_enabled (bool, optional) –

  • hostname (str, optional) –

  • port (int, optional) –

packed_bytes_to_iq(bytes)[source]

A direct copy of rtlsdr.BaseRtlSdr.packed_bytes_to_iq()

DEFAULT_PORT = 1235
class rtlsdr.rtlsdrtcp.base.ServerMessage(**kwargs)[source]

Bases: MessageBase

classmethod from_remote(sock)[source]

Reads data for the socket buffer and reconstructs the appropriate message that was sent by the other end.

This method is used by clients to reconstruct ServerMessage objects and if necessary, use multiple read calls to get the entire message (if the message size is greater than the buffer length)

get_data(**kwargs)[source]
get_header(**kwargs)[source]

Builds the header data for the message

The timestamp is added to the header if not already present.

Return type

dict

get_response_class()[source]
send_message(sock)[source]

Sends the message data to clients.

If necessary, uses multiple calls to send to ensure all data has actually been sent through the socket objects’s buffer.