Ive looked all through the tutorials and various example codes, but none of them even mention sending data to arduino from linux (python). Anyone have an example or source I could take a gander at?
This may be of interest
Serial would seem the obvious way - as illustrated in the link provided by @UKHeliBob
but there is no serial connection its all rpc and message pack, using just serial would require a tty-like port.
So why have you marked Post #2 as the solution? ![]()
There must be some physical (or wireless) connection - what is it?
Ok I removed solution, lol. I'm working with the Portenta X8 that doesn't do serial communication directly, it uses RPC through m4-proxy service on port 5000. I've only ever found documentation on how to send data from arduino to linux but never vice versa.
Did you ever find an answer @blankrobot ?
This seems to suggest that on the Arduino side you can just do Serial.read() like normal. But I'm still struggling to figure out what to do on the Python side to send to that port some strings.
I think I found the key after some time spent with reading the implementation code!
When you want to read the bytes from the Serial object on the M4, you can use the msgpack-rpc-python library and docker as shown in this tutorial (the suggested debug method/code for the actual tutorial is what I'm referring to) to let the m4-proxy/RPC/etc. know you want to be bound (?) to "tty" in this line:
result = client.call('register', 5005, ['tty'])
If you follow the regular tutorial at that link, you will will be sending calls to invoke functions on the M4 core. You can modify the code given to generate the docker image/container by changing the call to refer to "tty" also. You may call it and provide a string argument by doing rpc_client.call("tty", "abcdefg") and this will be available on the M4 as usual with the Serial object. So my modified python code for the container is now:
from msgpackrpc import Address as RpcAddress, Client as RpcClient, error as RpcError
# Fixed configuration parameters
port = 8884
# The M4 Proxy address needs to be mapped via Docker's extra hosts
m4_proxy_address = 'm4-proxy'
m4_proxy_port = 5001
rpc_address = RpcAddress(m4_proxy_address, m4_proxy_port)
rpc_client = RpcClient(rpc_address)
rpc_client.call('tty', 'abcdefg')
Also, the Dockerfile given in the tutorial uses a lower version of python, which seemed to cause some import errors with msgpack-rpc-python, something about the module "backports" not being found. I was using python:3.11-slim-bookworm as an image instead of what they had for other things on the Portenta, so I switched to that image which by my luck avoids that error.
It's also worth noting that I had to switch the docker-compose.yaml file's entry for the extra-hosts to be (found on some forum):
extra_hosts:
- "m4-proxy:host-gateway"
The trick for me was that although there is no real typical UART Serial between the M4 and IMX8 core, the code for the M4 core seems to just wrap up RPC calls and listens to the "tty" service (?) with a Serial/PrintStream object.
I know I'm a bit all over the place, so please let me know if I can clarify or elaborate, but I wanted to post this here since I couldn't find the answer posted anywhere to this problem and this post was the first hit on Google.