Update
Ok! Thanks to the advice of @Gerben and @chrisl I ended up routing everything around with PySerial , and (for the extremely limited purposes of this little project) it works well! Here's what is currently working:
import serial
from time import sleep
import os
# Connect to each Arduino individually
serialUno = serial.Serial("/dev/cu.usbmodem141101", 9600, timeout=0)
serialTrinket1 = serial.Serial("/dev/cu.usbmodem1414401", 9600, timeout=0)
# (+ 6 more. serialTrinket2, serialTrinket3, etc)
# Open a text file to save the encoder value in
f = open("encoderVal.txt", "w")
while True:
# Read encoderVal from the UNO
data = serialUno.read(9999)
if len(data) > 0:
# Send data to each trinket
serialTrinket1.write(data)
# Save val to text file to Unity
with open('encoderVal.txt', 'r+') as f:
# Wipe the file so it's only the most recent value
f.truncate()
dataStripped = data.strip()
# Return the last int in the data stream
dataSplit = dataStripped.split('\n')
f.write(dataSplit[-1])
sleep(0.05)
# Close serial connections & file on interrupt
serialUno.close()
serialTrinket1.close()
f.close()
As for the laptop end of the equation, I abandoned the serial library that I had been using in favor of this Unity-to-Python tool , which works perfectly.
The only issue thus far is that PySerial loses the occasional few bytes here and there, which outputs a wonky number ever 30 or so. That'll be tomorrow's project. Thanks everyone for your help!
Update
Ok! Thanks to the advice of @Gerben and @chrisl I ended up routing everything around with PySerial , and (for the extremely limited purposes of this little project) it works well! Here's what is currently working:
import serial
from time import sleep
import os
# Connect to each Arduino individually
serialUno = serial.Serial("/dev/cu.usbmodem141101", 9600, timeout=0)
serialTrinket1 = serial.Serial("/dev/cu.usbmodem1414401", 9600, timeout=0)
# (+ 6 more. serialTrinket2, serialTrinket3, etc)
# Open a text file to save the encoder value in
f = open("encoderVal.txt", "w")
while True:
# Read encoderVal from the UNO
data = serialUno.read(9999)
if len(data) > 0:
# Send data to each trinket
serialTrinket1.write(data)
# Save val to text file to Unity
with open('encoderVal.txt', 'r+') as f:
# Wipe the file so it's only the most recent value
f.truncate()
dataStripped = data.strip()
# Return the last int in the data stream
dataSplit = dataStripped.split('\n')
f.write(dataSplit[-1])
sleep(0.05)
# Close serial connections & file on interrupt
serialUno.close()
serialTrinket1.close()
f.close()
As for the laptop end of the equation, I abandoned the serial library that I had been using in favor of this Unity-to-Python tool , which works perfectly.
The only issue thus far is that PySerial loses the occasional few bytes here and there, which outputs a wonky number ever 30 or so. That'll be tomorrow's project. Thanks everyone for your help!
Sharing serial output from one to many arduinos (plus a computer) via USB hub
I am trying to send rotary encoder output from an UNO to seven Trinket M0s and a computer via a (powered) USB hub.
Currently the UNO → Laptop bit works great, serial monitoring works via the hub for everyone and I'm able to get a master/slave connection going between the UNO and a Trinket via Arduino's Wire library. But Wire uses pins, and I haven't had any luck finding a similar solution for USB. Thus far, the best leads seem to be around using something like stty
in a bash script to listen for what each port is up to but I'm so far in over my head at this point that that might just be wishful thinking.
I'm not looking for a solution here, just some suggestions as to where to head next. Specifically:
- Is the Wire library useful for USB communication? Is there something similar (master/slave-wise) that would be helpful?
- Assuming the serial/listen thing works, are there good ways to re-route data to different ports? Currently my best guess is something like
$ echo $encoderVal > /dev/cu.usbmodem?????
to each one individually ... but that might be a completely noobish notion.) - i2c keeps on coming up in searches, but looks like overkill. Is that a direction to head?
- Is this all completely nuts?
Anyways, thanks to everyone in advance.