Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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.

enter image description here

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:

  1. Is the Wire library useful for USB communication? Is there something similar (master/slave-wise) that would be helpful?
  2. 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.)
  3. i2c keeps on coming up in searches, but looks like overkill. Is that a direction to head?
  4. Is this all completely nuts?

Anyways, thanks to everyone in advance.

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!

Answer*

Draft saved
Draft discarded
Cancel
1
  • Thanks for putting all the time in to this answer. As you'll see I ended up with a far hackier solution than you'd probably advise, but hey, it kinda works! Commented May 27, 2019 at 8:28

AltStyle によって変換されたページ (->オリジナル) /