Connecting multiple USB hubs
In this week's video we answered the popular question of: "Can I connect more than one USBHub2x4/BrainStem device to my computer?". Not only did we prove that the answer is yes, but we also wanted to see exactly how many devices we could connect using our programmable USB hubs. In addition, we also discussed a few methods for dealing with multiple devices using our C++ and Python API's.
If you didn't know already, all of Acroname's software is cross platform meaning it will run on Mac, Windows and Linux. Although the video example is only shown on Mac the methods and code used will run on the other platforms too.
The most important takeaway from this video is how to work with multiple BrainStem devices either statically or dynamically. When working with multiple BrainStem devices statically, you will need to reference specific serial numbers and the corresponding objects you created. ie:
//Declare hub objects aUSBHub2x4 Hub1, Hub2; // Connect to Hub1 err = Hub1.linkDiscoverAndConnect(USB, 0x56F68799); // Connect to Hub2 err = Hub2.linkDiscoverAndConnect(USB, 0x57F3829C); //Get Port Voltage Hub1.usb.getPortVoltage(i, &voltage1); Hub1.usb.getPortCurrent(i, ¤t1); //Get Port Current Hub2.usb.getPortVoltage(i, &voltage2); Hub2.usb.getPortCurrent(i, ¤t2);
However, working with BrainStem devices dynamically allows you to be a bit more vague, like this:
//Declare an array of hub objects aUSBHub2x4 Hub[MAX_HUBS]; //Find all devices and put into a list err = Acroname::BrainStem::Link::sDiscover(USB, devices); //Connect to all the devices you found. Hub[x].linkConnect(*it); //Get Port Voltage Hub[i].usb.getPortVoltage(j, &data->voltageOn[j]); //Get Port Current Hub[i].usb.getPortCurrent(j, &data->currentOn[j]);
This can be done similarly in python:
#Finds all devices moduleslist = brainstem.discover.find_all(1) #Creates a list of type USBHub2x4 stemList = list(brainstem.stem.USBHub2x4() for i in range(127)) #Connect to all of the devices you found stemList[x].connect_from_spec(spec) #Control Device # for x, hub in enumerate(stemList): #Get Port Voltage voltage = hub.usb.getPortVoltage(i) #Get Port Current current = hub.usb.getPortCurrent(i)
The above code are snippets from the original source files used in the video. If you would like a copy of the code used in this video please click here. For more examples see our public Github account.
You will also need the BrainStem development kit to run this code.