-
|
I'm currently trying to understand how to do PC to hub communication reliably. Right now, I am sending my messages from PC to the CityHub with something like this: async def send_message(self, message):
if isinstance(message, str):
message = bytearray(message + "$", encoding="utf8")
for block in chunk(message, 100): #splits the message into chunks of 100
await self.hub.client.write_gatt_char(NUS_RX_UUID, block, False)
await asyncio.sleep(0.15)and receiving them like this: def update_input(char):
global input_buffer
if char == "$":
input_handler(input_buffer)
input_buffer = ""
else:
input_buffer += char
while running:
if p.poll(int(1000*delta)):
char = stdin.read(1)
if char is not None:
update_input(char)
update()This works quite well when the messages are not too frequent, however when I am sending a large number of messages in a short period of time, the end of a message tends to be cut off, resulting in the terminating character "$" not being received, so the next message will be treated as part of the previous one. This gets better if I increase the delay For example, a message might look like this: The My questions now are:
Would be glad for any kind of advice on this topic. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Probably a firmware limit. There is currently a 100? byte receive buffer, so if
It selects Write With Response or Write Without Response. This is a Bluetooth Low Energy protocol thing. The response is a BLE packet that acknowledges that the data was, in fact, received. This slows down throughput, but as you have seen, Write Without Response requires a delay anyway, so using Write With Response would eliminate the need for an arbitrary delay. In terms of Pybricks, Write With Response also means that, if it succeeds, that the data made it through the Bluetooth chip to the MCU (where Pybricks firmware is running). However a buffer overflow in the Pybricks firmware can still occur.
I would suggest adding something to create backpressure in the pybricksdev program. For example, only send one RPC request at a time, then wait for an ack from the hub that it has processed the message before sending another. |
Beta Was this translation helpful? Give feedback.
-
|
Not really an answer to your question, but rather a question for you. Do you perhaps have a few good (small) examples of communicating between the PC and the hub? This Pybricks user would be quite interested: #470 |
Beta Was this translation helpful? Give feedback.
Probably a firmware limit. There is currently a 100? byte receive buffer, so if
stdin.read()is not called fast enough, then data can be lost. Plus, there are additionally buffers in the Bluetooth chip that we don't have any control over, so if data is receive too fast, it can be dropped there too.It selects Write With Response or Write Without Response. This is a Bluetooth Low Energy protocol thing. The response is a BLE packet that acknowledges that the data was, in fact, received. Th…