How to send a 5v spike through USB port (serial IO) when a function is called in Unity?

Hi All,

I’m not that well experienced with C#, so bear with me if I don’t explain this very well. I have this VR game developed in Unity that has targets appear at random periods of time. Another part of the game is that I need to send a 5v pulse (a marker) to synchronize the appearance of the targets to something else on another computer. I’ve written code in Python before to send a voltage out through a USB port when a key is pressed, but I am having difficulty doing it in Unity C#.

I would assume the steps involve:

  1. find the USB port
  2. set the configurations (baudrate etc…)
  3. open() the port
  4. send a voltage
  5. close() the port

How would I go about sending that voltage spike every time a certain function gets called? Let’s say the function is:

private void SpawnTarget(int prefabIndex = -1)
{

}

The whole point of USB is the serial communication. I don’t believe using USB port power as the line of communication is a use case the creators of .Net anticipated.

My guess is you’ll have to do some low level system call with admin privileges in C/C++ and bring it into Unity as a Native Plugin. But you’re probably better off just switching to a more traditional method of network communication. My 2 cents.

Are you talking about switching on and off the 5V USB power supply? This seems to be a poor way to signal anything. You risk damaging the little SMPS that supplies the 5V (which is not really designed to tolerate being switched on and off continuously), and some other computers just output 5V straight from the PSU and can’t be controlled.

Is there a reason you need to use USB power for signalling, instead of e.g. networking, or a traditional serial port?

1 Like