KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

USB Detection, .net core

Thu May 13, 2021 3:31 pm

I'm writing a project in .NET Core, and now have the need to detect our devices (I have the vendorID:DeviceID) on the USB bus, and also detect device insert/removal

If I KNOW the device is there, opening it as a Serial Port is easy, but the detection is the tough part. Anyone have ideas?

User avatar
topguy
Posts: 7147
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: USB Detection, .net core

Thu May 13, 2021 4:47 pm

Linux has a lot of information about devices in the "/proc" and "/sys" filesystems. ( ref: https://www.kernel.org/doc/Documentatio ... b_info.txt )

You could also create a udev rule that send a signal to your program if/when a device is inserted.

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Thu May 13, 2021 5:10 pm

Here is the question however

Looking at the example here
https://www.kernel.org/doc/Documentatio ... b_info.txt

I'll have something similar to: (only with FTDI as the vendor, with our product ID)

T: Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#= 4 Spd=12 MxCh= 0
D: Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=0565 ProdID=0001 Rev= 1.08
S: Manufacturer=Peracom Networks, Inc.
S: Product=Peracom USB to Serial Converter
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
I: If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
E: Ad=81(I) Atr=02(Bulk) MxPS= 64 Ivl= 16ms
E: Ad=01(O) Atr=02(Bulk) MxPS= 16 Ivl= 16ms
E: Ad=82(I) Atr=03(Int.) MxPS= 8 Ivl= 8ms

But .Net core really wants to open them as a serial port - So when I plug in I see it com up as a Com port (say port 5) - how do I map this over?

User avatar
topguy
Posts: 7147
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: USB Detection, .net core

Thu May 13, 2021 10:30 pm

"comX" ports only exist on Windows.
On Linux the device-name will very likely be "/dev/ttyUSB0" ( in some cases "/dev/ttyACM0" )

There is a potential issue with your ProductId not being recognized by the FDTI/USBSerial driver in the Linux kernel.
You will find that out when you have a chance to test it.

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Fri May 14, 2021 1:19 pm

I said "Com" as a shorthand, but how do I know if
/dev/ttyUSB0
or
/dev/ttyUSB2
or
/dev/ttyUSB3 is my device?

User avatar
scruss
Posts: 4100
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB Detection, .net core

Fri May 14, 2021 2:50 pm

Take a look in /dev/serial/by-id/ and if your device really is a serial port, there should be a device called usb-vendorID_DeviceID*

If it's not recognized as a serial port, there won't be anything there. You'd need to set up a custom udev rule to do a particular action (load a module, perhaps) when a device is inserted. This would be outside .net core, as it's a Linux thing.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Fri May 14, 2021 3:41 pm

Thanks, I'll spend a bunch of time looking. In the System.io.ports, when I enumerate the ports, we can see it, but there doesn't seem to be a way to get the vendor:product from the port object - it just sees them as "com ports"

Sigh

Is there a particular library you know of that does any of this?

User avatar
topguy
Posts: 7147
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: USB Detection, .net core

Fri May 14, 2021 5:43 pm

KG2V wrote:
Fri May 14, 2021 1:19 pm
I said "Com" as a shorthand, but how do I know if
/dev/ttyUSB0
or
/dev/ttyUSB2
or
/dev/ttyUSB3 is my device?
Do you plan to have multiple "ttyUSB" devices ?

This is where I use "udev" rules, I create rules that make a symbolic link from ttyUSBX to a device name of your choice.
This is a linux feature so its something you add to the system so its not really something you can do in your program.

Scrusss "by-id" suggestion is a good one too.

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Fri May 14, 2021 6:11 pm

Here is the issue - we sell sensors - right now, all I2C, but we do sell a I2C USB carrier board

Our GOAL is that J Random User, who is using the product as an appliance, buys a new sensor from us, plugs it in, we want to auto detect that it is there (sort of like windows), and automatically come up in the software

So say they have a temperature probe, and they plug in a second temperature, or a Dissolved Oxygen, or pH or (we make a bunch of types), we want to auto detect it is there, and just start reading it and displaying it. We do almost this with I2C right now (except I don't re-detect till restart - might add a button/periodic rescan, but have not). The issue now is if the person adds a second probe of a type, before they do, they have to change the default I2C address of one of them

Basically, we are trying to get to the point people can plug and play the devices

User avatar
topguy
Posts: 7147
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: USB Detection, .net core

Fri May 14, 2021 9:28 pm

If you are serious about how you distribute your software, you would package it into a proper .deb package.
Considering it is a .NET application I assume it is a pre-requisite to install Mono, and a deb package can make sure that pre-requisites are installed.
You can also add udev-rules and scripts to the package that can work in the system and help you achieve the PnP features you are looking for.

Loose thinking:
An udev rule can trigger on a device "add" and "remove" and you can run quick scripts.
For example you can add a line to a file when a device is added, and you can remove that line when the device is removed.
And potentially your software can watch that file for changes.

User avatar
scruss
Posts: 4100
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: USB Detection, .net core

Fri May 14, 2021 11:39 pm

KG2V wrote:
Fri May 14, 2021 3:41 pm
In the System.io.ports, when I enumerate the ports, ...
But did you look in the /dev/serial/by-id/ filesystem, like I said? Everything's a file on Linux (even when it's not really) and that's how you query things.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Mon May 17, 2021 11:58 am

Not yet. Opening a port might be interesting without it - but that is on my list - Friday was nuts, and today promises not to be much better (Family medical stuff)

KG2V
Posts: 9
Joined: Fri Jan 08, 2021 4:25 pm

Re: USB Detection, .net core

Mon May 17, 2021 1:33 pm

topguy wrote:
Fri May 14, 2021 9:28 pm

Considering it is a .NET application I assume it is a pre-requisite to install Mono,
Nope, no Mono - the whole thing is accessed via a web page. The final app is a ASP/Blazor app UI wise, the USB stuff will be in a .net standard sensor library

BTW, I see the devices in /dev/serial/by-id I'm wondering if I can use one of the .net pot watcher classes. Now to try opening and interacting in my code....

Return to “Other programming languages”