Power cycling a USB device? - c#

I'm working on troubleshooting a device that only works once it has been unplugged and plugged back into its USB port on the PC.
The application that talks to the device is written in C#. So I need a programmatic way in C# or perhaps batch files, etc. to reset a USB device.
Thus far I've tried using the devcon.exe tool, but that doesn't seem to do the same job that physically removing and adding it back does.
Is it possible to use Win32_USBController to control a USB device's power through the port somehow? MSDN Link
Perhaps in a similar fashion, I found the CIM_Controller class, which has Reset() function. MSDN Link
The tools for resetting a USB port in the Windows Driver Framework/Kit don't appear to be readily accessible via a library or otherwise without first creating an actual driver unless I'm mistaken.
Thanks

I would recommend that you navigate to the directory and find the device. Right click on it and in the short cut menu click on eject. The application and the OS should handle the device. It is a bad idea to "unplug" a USB device, especially flash drives without ejecting them. You could end up with open file errors.
If the problem is not ejecting the device then the issue is probably with the application. If the application is opening a file on the device and the device has to be unplugged in order to function properly then the application may not be closing the open file properly...
Just saying
You might look at the SO link: How to programmatically unplug & replug an arbitrary USB device?
and read through that...
it sounds to me like your scanner is completing a task then shutting it's post down. it remains plugged into the PC but the PC does not 'see' it. go to the directory and see if it is still in the directory...
unplugging once the device shuts down the port does nothing. replugging reconnects the electronics and the PC becomes aware of the device which completes one more task. then shuts down its port again.
Dr t

Related

Using MIDI in UWP

When using the UWP MIDI API with a physical MIDI device, the app hangs on exit and cannot be forcefully terminated neither from command line nor from the Task Manager.
This happens for all UWP apps that use MIDI, including the official example from MSDN, without modifications.
I have raised an issue on GitHub about this, but it does not seem to be a new problem. MIDI is supposedly broken since 2014.
Is there a way to circumvent this problem in UWP through other means? I'm writing in C#, but I am fluent in C++ and could use lower-level APIs, if they existed. I only need to get MIDI input from a selected MIDI device (keyboard).
I also tried using the winmm.dll as suggested in the related post (Getting signals from a MIDI port in C#), but this does not work for me. The midiInGetNumDevs() method returns 0 when used from a UWP app.
I am using M-Audio Fast Track USB sound card (connected via USB) and an M-Audio 88es Keystation MIDI Keyboard (connected via USB). I haven't had problems in desktop applications.
I have discovered a partial workaround where I move the deallocation code to the App Suspending event handler instead of having it in a destructor. However, the problem still arises when a device is unplugged during app's lifetime. If the MIDI device is plugged in while the app is running, the app closes without problems (with the workaround). If the MIDI device is plugged in before the app is run and remains plugged in while the app is running, the app closes without problems (with the workaround). Only if the device is plugged out while the app is running will the app hang on close, regardless of whether the deallocation code is done in the destructor or in the App Suspending handler.
I’m unable to reproduce this issue. I believe it could be related to the audio card’s driver.
Using the sample you mentioned, https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MIDI
• App did not hang when I closed it.
• I was able to remove the cable during app lifecycle, without issue. I was still able to send and receive messages after reconnecting.
• I was able to plug in the device mid-lifecycle, without issue. I was still able to send and receive messages.
Although it's currently working from desktop, it’s required to be “universal” for it to work well in UWP. Is the audio card driver universal?
In our developing drivers doc, we see a note to Use the InfVerif tool to verify that your driver package's INF file is universal. https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/infverif.
In the app developer documentation, I see a note to test the device metadata. https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/step-4--test-device-metadata
Additional documentation related to your question:
Building UWP device apps
https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/the-workflow
Test the device metadata for your UWP device app
https://learn.microsoft.com/en-us/windows-hardware/drivers/devapps/step-4--test-device-metadata
Getting Started with Universal Windows drivers
https://learn.microsoft.com/en-us/windows-hardware/drivers/develop/getting-started-with-universal-drivers
MIDI sample
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MIDI
Releasing resources in app suspend
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/suspend-an-app#release-resources
Use Dispose to suppress finalization
NOTE: depending on a finalizer will keep resources in memory longer than necessary. They won’t be released until a full GC is performed or when the process exits.
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

Audio stream from phone to pc via serial port in C#

I'm looking for a way to stream audio from a phone directly to the pc. I'm not talking about music or something but every sound of the phone (voice, ringing etc). Is this possible over an USB cable and the Serial Port of the PC without using third party tools or libraries? I'm currently developing in C#.
No! Do not connect your phone line to the microphone port. The high voltage on the line (60-120V when ringing) may damage your PC.
What you need is a PHONE-to-LINE in adaptor (or sometimes PHONE-TO-MIC adapter) found in electronic stores. Here is an example from AMAZON:
http://www.amazon.com/TRX-20-Direct-Connect-Telephone-ADAPTER/dp/B000KL0XA6/ref=sr_1_1?ie=UTF8&qid=1459033666&sr=8-1&keywords=phone+recording+device (I have no ties or experience with this particular one).
Then, your plug it in to your PC's LINE-IN or MIC (see instructions of the device). From this point on you should be able to record the audio from a call using any sound recording application.
Please keep in mind that PHONE recording may be illegal in some states, if the parties involved are not informed.
If your objective is to detect if a telephone number exists, your method may be intrusive, since it will attempt a call.
In any case, a voice modem could be a better choice, assuming you can find one. Windows used to have a voice modem API & Stack, which should enable you to dial a phone number, wait for the answer or no answer, and then, capture the audio from the remote end. Under most conditions it should detect the ring, busy, or number not available.
The open source project Asterisk (asterisk.org) has "heavy duty machinery" to do this job, albeit it requires specialised programming. There is even an answering machine detection: Asterisk Answering machine detection (AMD) always detects receiver as MACHINE

Reading logcat on android from c#

I am looking into making a c# program that will read in the logcat output from an android device and read it in to the c# program.
Initially it should do this while the phone is connected and it shouldn't require a specific app on the phone to be installed for the c# program to be able to retrieve the logcat output. Also the phone shouldn't require root access.
Is this something that is possible, I can't find anything on Google that says its possible but thought I'd ask on here in case someone has some useful information.
Thanks for any help you can provide
The most practical answer is to execute the shell command 'adb logcat' from your C# program and capture its output.
The only requirement for the device is that USB debugging be enabled in the settings menu.
The host PC will require that the android developer tools and appropriate USB driver for the device be installed. This can, unfortunately involve a substantial amount of hassle, especially finding the right drivers for windows hosts.
More complicated approaches would be to duplicate the functionality of the adb program (it is open source) and/or USB driver in your program, or to install an app on the device with the read logs permission which sends them to you - or even run an ssh server under the app userid so you can connect in and obtain them.

How to detect USB Wifi plug into computer via C#?

I want to write a simple program that detect USB Wifi plugged computer and unplug it. I am finding code or library help me to do that (C# or Delphi). Tell me if you know :)
Or
Code or lib that list all USB devices on computer ?
Thanks !
I can only answer part of the question. You can "eject" USB devices using this API:
CM_Request_Device_Eject (MSDN link)
I'm using it to eject USB thumb drives after I copy stuff onto them in a backup application. I'm pretty sure there are ways of enumerating available USB devices using one of the many CM_XXX routines, but I was only interested in storage devices connected via USB, so I can't comment on WiFi devices.
An other sugestion: Maybe you can solve your problem at an other layer, maybe you can detect the addition of a new network adapter and disable that. Not all USB devices can be ejected, it's probably a driver-related issue. But I guess, if you can eject it from the notification area icon, you can eject it from code.
may have a look at http://sourceforge.net/projects/libusbdotnet/ (never used it by myself)

C# Automatic deployment/execution from a usb device

is there any way that a process on my usb will be launched automatically when the usb is connected to the computer?
Is there any way to deploy a file automatically from the usb, each time the usb is connected to the computer?
thanks.
The user would have to have "auto-play" enabled and then you can setup an autorun file to accomplish this, but it is highly dependent on the users configuration.
It should be noted that this is also a huge security concern for many people. Auto-run usb sticks are one of the ways that malware is automatically distributed.
Not trying to tell you your requirements, but I would consider if there is a better way to do this.
Take a look at Autorun.
I have an old mobile-phone that emulates a cd-drive with a autorun.inf on it. That autorun started an app that let me manage my mobile (calendar, callist, sms) from the computer.
This probably only works if the user has Autorun enabled. It's the first thing I disable.

Categories

Resources