Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Unity - Talking to USB ports

Discussion in 'Scripting' started by Vanz, Sep 20, 2018.

  1. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    Hi,

    I'm trying to create an interface with unity that can send a signal and read a signal to a USB port, can this be done through C# and Unity?

    For now, my goal would be to turn on a small LED light hooked into the USB port.

    I'd be looking at using something like this to convert serial USB signals to parallel:
    https://www.velleman.eu/products/view/?country=be&lang=en&id=351346


    or I believe this could also take a usb signal and convert to parallel:
    https://www.amazon.com/Sabrent-Para...33&sr=8-3&keywords=usb+parallel+printer+cable

    Anyone else that has ideas/suggestions on how to accomplish my above goal as simply as possible would be appreciated.

    Thanks,

    V
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    So... that's not really how USB is designed to work.

    You don't really directly communicate with USB itself since it's more of a 'bus' than a 'port'. USB can have numerous things attached to it, and no given port on the USB bus is easily addressable in a manner that you'd really ever want to deal with in code.

    This is the purpose of drivers. It creates a software address for devices on the bus. (technically speaking the driver would deal with USB related stuff... but you usually don't write drivers in C#)

    The devices you linked to will usually be attached to your computer and show up in your device manager (if the drivers are loaded correctly).

    For the velleman product, the heck if I know what it'll show up as. It probably has custom drivers specific to it, or maybe uses some generic driver. I don't know. I've never witnessed the specific device.

    As for the USB to Parallel printer cable. Usually the driver emulates an LPT in your device manager. It'll be addressed like a real one, and for all intents and purposes will appear to all other software on your computer as a generic LPT port. So you'd program against it just like it were one, completely invisible of the fact its actually a USB to LPT.
     
    Joe-Censored likes this.
  3. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Well you wouldn't have an LED wired to a USB port. They're wayyyyy different.

    LED's expect some voltage to turn on, otherwise it's off. USB is a serial bus...

    So what you'd usually have is some device connected via USB that allowed you to control voltage pins on it by turning them on or off. That device... well, there's tons of them for sale out there:
    https://www.google.com/search?q=USB.....69i57j0l5.6199j0j7&sourceid=chrome&ie=UTF-8

    They're all controlled in their own unique way depending on the device you use and the driver it comes with.

    You could also build your own, if you have any electronic engineering skills. But I'm willing to bet you don't if you're hear asking about it.

    I will say... this type of subject is very independent of Unity. If you're googling about it... forego Unity all together. Unity uses C# with the mono runtime. So if you find any articles talking about controlling integrated circuits via C#/.net/mono, they'll work in Unity (well as long as you target desktop).
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    In regards to those drivers.

    It depends on how they're set up. And as I said it varies from device to device.

    If the driver emulates a serial port, like that USB to LPT cable you linked, well then you just communicate with the LPT port. You can google about communicating with serial/parallel ports in C#, there's lots of ways to go about it.

    If the device driver does something else. Well usually it'll have some sort of COM interface to communicate with it. In which case you should be googling about COM interop in C# (which I bet some LPT articles will also dive into, since that's a common way to write to LPT as well).

    I couldn't give you specifics though, since this is a HUGE range of topics. I don't know what product you're using, what your end goals are, all of that.

    ...

    How about instead, what is your end goal? What are you trying to accomplish?

    Are you like making a robot or something?

    I could maybe assist you in picking out hardware that is user friendly to work with, easy to get up and running, and you can integrate into Unity well.
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,791
    I have a question you may have a handy answer to. I am configuring an Escape Room with a bunch of magnetic triggers controlled by arduinos. Instead of running a laptop around and plugging into their USBs one by one I am thinking of linking all of them to a USB hub and addressing them from there via a Unity interface. Do you have a recommendation on hardware for this USB hub and perhaps what C# code framework I should get a handle on to accomplish this? Do the arduinos show up as devices in Unity? Or..what the heck should I be aware of as I sally forth into semi-uncharted waters here. Thanks in advance for any tip and pointers..mucho appreciated.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    So these questions really aren't Unity bound in any way. How they'll show up in Unity depends on how they show up on your computer.

    C# has basic ways to communicate with devices attached to your computer. Like I said in that post, depending the driver, it'll show up in various ways. A common thing you're going to find with arduino's usb-to-usb is that it emulates a serial port.

    In windows an emulated serial port just shows up as a COM device. In linux it may show up as a tty device (usually in format /dev/ttyS#). I'm not sure off hand with mac, probably similar to linux.

    At this point though... the "usb hub" really doesn't matter. It'll just appear as multiple devices on your computer. So instead of just COM1/ttyS1, you'll have a COM1,COM2,COM3/ttyS1,ttyS2,ttyS3.

    At this point you need a way to communicate with any of the emulated serial ports. The .net framework has a very basic class for this:
    SerialPort
    https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-3.1

    It also contains a static method for listing all the ports on your system (it 'should' in theory list your attached devices, as long as the drivers for them set them up as emulated serialports):
    https://docs.microsoft.com/en-us/do...ialport.getportnames?view=dotnet-plat-ext-3.1

    Mind you this really is just a list of the COM# devices found in 'Device Manager' in windows (if you're on windows). And it's really just read from the registry since that's where the OS stores that info when a COM device is installed/registered.

    You'll have to discern which of the named ports come back are your devices. This is specific to your machine and you'll likely want to use some sort of config file to store the information, if you hard-coded it then it'll only work on that machine. A config file will help make it more portable.

    Note that this is all because SerialPort is a very simple class. If you want something that can sniff the ports and suss out what's what... that'll require more robust libraries that you can write yourself, or find on the internet, I don't necessarily have any suggestions though.

    From there though you now have a SerialPort object and you get read/write with it. From there it's all down to how you coded your arduino.

    ...

    In the end... we're talking old-school stuff here. The way these things work are very very basic. The way serial and other com devices have worked for years is that they basically appear as "files" in your filesystem (this is why 'com' is actually a reserved name in windows, try creating a text file and name it just "com1" with no .txt, windows will say you're not allowed). Back in DOS days you just output text to that file using simple IO commands through the dos terminal.

    This goes for linux as well. All serial devices, hell all devices in general, show up as a file in /dev/*. In this case /dev/ttyS#. And again you just write data to the file, and instead of be written to disk, the OS intercepts and pushes it along the bus for that device.

    C# has basic IO functionality... it needs to to be considered a "turing-complete language". And things like the 'SerialPort' class are really just taking your command and passing it along to these really basic OS level command which you could have called manually on your own. Literally, if you were to open a stream to the device you could do the same thing. Or you could create a 'Process' object and feed in commands and do it the same way you would through terminal/command prompt.
     
    Last edited: May 6, 2020
    ippdev likes this.
  8. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,791
    @lordofduct Thank you sir for your comprehensive info. Exactly the kind of data I need to supplement what I already have figured out. This is on Windows 10. Got any reading I can do on the "process" commands in relation to arduino? When I get 'er done I will report back here on the experience for others to have a gander at if trying similar in their application or game. I am hoping to set up a human usable interface for the Escape Room to ping the boards to ensure they function, download the current arduino sketch per board and be able to upload it if it needs resetting or they replace a board, show the operator the trigger being set off as the client traverses the puzzle sequence. The electronics guy is a retired Master Chief Fire Control tech of the US Navy..so he is pretty savvy on the other end. Also..heh..setting it up in Unity gives me the juice in the near future to sell them on AR and VR additions which they can charge more money for.