Block with dropdown who's list is populated by Serial port list.

37 views
Skip to first unread message

Adam Bryant

unread,
Feb 28, 2026, 4:41:15 AMFeb 28
to blo...@googlegroups.com
Sorry I am not sure how to word this.

I have a python function that list active serial ports and assigns them to the variable IFACE_1/IFACE_2/IFACE_n

How do I create a block that will create a dropdown menu populated but this list.

// {
// "type": "additional_interface_b_init",
// "message0": "Initialise aditional Interface B: %1",
// "args0": [
// { "type": "field_dropdown", "name": "NAME", "text": "IFACE_1" },
// { "type": "field_dropdown", "name": "NAME", "text": "IFACE_2" }
// ],
// "previousStatement": null,
// "nextStatement": null,
// "colour": 20,
// },

//py.forBlock['additional_interface_b_init'] = (block) => {
// const name = block.getFieldValue('NAME');
// const port = getInterfaceMapping()[name] || "/dev/ttyUSB0";
// return `import serial, time\nif 'ifaces' not in globals(): ifaces = {}\nif '${name}' not in ifaces:\n ifaces['${name}'] = {'ser': serial.Serial('${port}', 9600, timeout=0)}\n ifaces['${name}']['ser'].write(b'p\\0###Do you byte, when I knock?$$$')\n time.sleep(0.1)\n`;
//};

I have tried this but it means I have to populate the list first on the assumption that their could be these ports.

Can someone point me in the correct direction please?

Mark Friedman

unread,
Mar 2, 2026, 3:15:42 PMMar 2
to blo...@googlegroups.com
Adam,

  The Blockly part of this is relatively simple. You would use a dynamic dropdown field (doc here).  The gist is that you provide a JavaScript function which will get called to populate the options in the dropdown.

  What is more complex in your scenario is how to implement that function. You mentioned that you have a Python function which can get the active serial ports.  I'm guessing that Python function is running on a separate device from the one running your Blockly-based web app?  Is that the case?  If so, you'll need a way to communicate between those two disparate devices and you'll need the JavaScript dropdown function to communicate with the other device to get that list of active ports.  

  If the Python function is meant to run on the same machine as the Blockly app, you'll need the JavaScript dropdown function to communicate with an external process on that machine that runs that Python code.

  Things get a little more complicated if your communication for either of the above options is asynchronous.  In that situation you may want to use the setOptions function on the dropdown field when the asynchronous communication completes.

  Hope this helps.

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/A5DADE66-B695-449A-92FF-887037DEAE07%40gmail.com.
Reply all
Reply to author
Forward
0 new messages