Skip to content

example_rxtx_threaded.py does not work #35

@noeldum

Description

@noeldum

example_rxtx_threaded.py does not work and should probably not work.

To help us diagnose the issue I have cut down the code to a bare minimum.

This is first a simple code that works. Just to make sure my hardware and OS setup are right.

from RFM69 import Radio, FREQ_433MHZ

node_id = 2
network_id = 0

with Radio(FREQ_433MHZ, node_id, network_id, encryptionKey="TOPSECRETPASSWRD") as radio:
    while True:
        packet = radio.get_packet()
        print (packet)

This works pretty well and I do get messages transmitted here.

Now I adapted this same code to work with a single thread similar to the structure of the example

from RFM69 import Radio, FREQ_433MHZ
import threading

node_id = 2
network_id = 0

def receiveFunction(radio):
    while True:
        packet = radio.get_packet()
        print (packet)

with Radio(FREQ_433MHZ, node_id, network_id, encryptionKey="TOPSECRETPASSWRD") as radio:
    receiveThread = threading.Thread(target = receiveFunction, args=(radio,))
    receiveThread.start()

This second script does not work. When I transmit to this node the print does not output anything.
My speculation is that it should not work. The variable radio defined inside the thread has maybe escaped the with statement and it is maybe what is causing the issue.

This adapted code below does work however but has a major problem.

from RFM69 import Radio, FREQ_433MHZ
import threading

node_id = 2
network_id = 0

def receiveFunction():
    with Radio(FREQ_433MHZ, node_id, network_id, encryptionKey="TOPSECRETPASSWRD") as radio:
        while True:
            packet = radio.get_packet()
            print (packet)
receiveThread = threading.Thread(target = receiveFunction)
receiveThread.start()

The problem with this last code (although correctly receiving packets) does not allow us to use the variable radio for the tx part. Which is the whole point of the threading.

If someone has any ideas how to fix this example that would help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions