Skip to content

Sending data to the hub with Pybricksdev #181

@laurensvalk

Description

@laurensvalk

Originally posted by @johnscary-ev3 in #154 (comment)

Hi,
I have solved the install problems and I am trying to use the pybricksdev-demo in jupyter-notebook now.
I took the communication demo and made my own program to just take input from the keyboard and send it to the hub.
The "fowarder" program below basically is working, but I always get an "except" branch at the hub,write() statememt.
I am able to read print outs from the hub ok and I can enter a character form the keyboard but can't send anything to the hub.
Hub is doing a getchar().
After the code below is some sample output from the program. There is some normal output from the hub about distance detection and arm (motor) moves, and then I enter a 'c' from the keyboard which is supposed to be a remote command to the robot.
Any clues for me or docs on the hub.write() routine?
The robot code I am using works ok under Pybricks Code getting commands from the keyboard.
Thanks.

async def forwarder(hub):
    
    # Give the hubs some time to start
    await sleep(2)
    print("In forwarder")

    while True:
        if hub.state == hub.RUNNING:
            print("running ", len(hub.output))
            # Check if the remote has printed anything
            if len(hub.output) > 0:
            
                # If so, print it
                print(hub.output)
                hub.output = []
                
            # get a char from input
            a= input()
            print("len=",len(a))
            
            # Try to send it as one byte to the receiving hub
            if len(a) > 0:
                try:
                    print(a)
                    await hub.write(bytes(a))
                except:
                    print("write exception")
        await sleep(0.05)

len= 0
running 3
[bytearray(b'object detected dist = 7.3'), bytearray(b'right_arm rot_angle = -97'), bytearray(b'right_arm rot_angle = 83')]
c
len= 1
c
write exception
running 0

Code fragment on the robot side trying to read the command character
# Read all available characters, keeping only the most recent.
    c = None
    while True:
        read = getchar()
        if read is None:
            break
        c = read

Metadata

Metadata

Assignees

No one assigned

    Labels

    software: pybricksdevIssues related to the pybrickdev Python packagesupportRequest for technical support for a problem that is not a bug or feature requesttopic: communicationIssues related to hub-to-hub/phone/computer communcation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions