Monitor USB for camera connection - c#

I have a USB microscope camera and I would like to open the Windows 10 camera app automatically if the camera gets connected to a USB port. After checking that this isn't possible with Windows 10 at the settings level, I have decided to write a short C# program that monitors the USB ports for a camera device connection (and runs the camera display app).
As to the monitoring I haven't got a slightest clue where to begin. Needless to say that the monitoring program should be lean with respect to blocking ressources, because it would be running all the time. Can I simply obtain an event notification of USB connection? Do I have to expect problems with permissions?
And of course, suggestions for alternative approaches are welcome, if any. Note that I do not want to capture and display video from the camera myself, unless it is easier to do it that way instead of opening the existing camera app.

assuming the microscope is using the web-camera interface I would look for guides about how to enumerate and connect to web-cameras. I have found a few alternatives:
Media capture API
Aforge
Versatile WebCam
I had most success with versatile webcam, but if you just want to enumerate devices any of them should do. Aforge is probably the easiest to use.
You can then use a timer to check for devices, and do something if the microscope is connected.
If the microscope is not a webcamera you will need to refer to the camera documentation on how to enumerate devices.

Related

How can I take a HD signal from a device with component-out and display it in a window in a C# application?

I've been tasked with getting the video from the component out ports of a video device into a window in our C# application.
Think taking the component output from a TiVo or DVD player (not HDMI so no HDCP to worry about) and displaying it in a window.
We are only looking for basic real-time streaming. No recording, no snapshots, no video overlays... just showing the source in a window with sound. Nothing else.
I have searched high and low for a few months now and haven't found any devices that will let us do this.
I have found several capture devices such as Elgato's Game Capture HD but (I was told) they aren't a true capture device and therefore don't work with things like DirectShow, so you can't use them with third-party software, only their own.
I have also found cheap Component-to-USB capture devices on Amazon, but they say they don't support HD.
Note: We don't want something like SlingBox which a) has too much lag because of its buffer, and b) you can't integrate it with your own application anyway.
So can anyone give me some pointers on where to go? Been searching for a few months now and getting nowhere fast and now time is starting to be come critical and I literally still have zero leads here.
The only decent HD capture device I have found is from BlackMagicDesign, I use this on my MediaCenter to record HD from My settop box, so maybe one of thier products may help you.
http://www.blackmagicdesign.com/products/intensity/

How to continue playing a capture device when the device is replugged in?

OK,I'm now developing a winform app that can capture stream from a video capture device using DirectShow.NET.
And the problem is when I plug the usb capture device out. the image freezes,and after trying to stop , my code hungs on IMediaControl.Stop() method. Thus i can't continue playing. So is there a way to stop the playing progress when device is plugged out? or how to replay after the device replugs in?Thanks.
There is no well-defined behavior for scenario with unplugged device. Eventually this should result in certain error code, however a delay with timeout might be involved. The behavior is dependent on actual implementation on the underlying filter backed by hardware driver.

Polling USB Game controller device C#

I learned recently how PS2 game controllers send continuous updates to the PS2, with data identifying which buttons have been pressed, etc.
Having an adapter that allows my PS2 game controller to connect to a USB port, I started looking into writing a driver to allow it to control the mouse (similar to Joy2Mouse (http://atzitznet.no-ip.org/Joy2Mouse3/) ), as an exercise in understanding how it works.
I am having trouble even grasping the basics of how to access and asynchronously poll a USB device for input, and was wondering if anyone had any example code (C#) for accessing the USB device and polling its input, or could explain how it is done?
From what I can gather, I will need to use some windows drivers for accessing the USB device, but I have no experience of this, and so don't know how to do this.
Thanks for any help you can give.
Those PS2 adapters interface with the controller for you, and provide a joystick interface for you.
There are a few ways of accessing a joystick. An older method (but still works) is by using Managed DirectX. http://www.codeproject.com/KB/directx/joystick.aspx
Note that Managed DirectX is no longer released. You might try SlimDX (http://slimdx.org/features.php)

Use USB to activate MOSFET/Relay

I am working on a personal project involving sending simple signals from my computer to a circuit via USB. Basically I am using the USB signal as the gate signal for a MOSFET which will in turn activate a relay to turn on/off various AC peripherals. For example if I want to turn on a light bulb for 5 seconds every minute I would be sending a 1 down the first wire for the first 5 seconds of every minute.
This is my problem: I have no idea how to manually send a 0/1 down a specific wire on a USB cable, or even interact with a USB port at all :(
So I guess there are multiple parts to this question, is it possible to interact directly with the bits being sent via a USB port? If so how would I do this? I am familiar with C++ and C#, so I really hope that you can do it in one of those...
Thanks!
edit Hmm so it looks like the USB port actually only has one 5V pin so direct USB interaction wont work. Going to take a look at a parallel adapter and get back on it.
USB is a bad fit for anything that doesn't have a USB interface at the other end of the wire. If you don't want to get into building your own USB device, I'd suggest buying a USB to serial adapter, which gives you two directly-controllable output lines (the flow control lines), or a USB to parallel adapter, which gives you more than 8 lines.
Chris Johnson's answer has a link to instructions for Windows serial port programming. You'll want to look at section 7 -- the SETDTR, CLRDTR, SETRTS, and CLRRTS are your flow control line toggles (for the DTR and RTS lines, respectively).
As far as hardware goes, a "1" (SET) value on a flow control line is +3 to +15 volts on the line, and a "0" is -3 to -15. Actual voltages can vary between devices; measure it to be sure. (EDITED; I got the 1 and 0 mixed up. The control lines use the opposite convention from the data lines.)
Here are Wikipedia pages for voltage characteristics and pinouts.
EDIT: Having done some more research on USB-to-parallel adapters, I don't think they will give the needed level of control. For best results, you'll need a PCI or PCMCIA parallel card, or a parallel port built into the motherboard.
I'm not a Windows programmer, but this library might be useful for controlling the parallel port's lines from Windows.
The easiest thing to do for this application is to use serial port emulation, either with a USB-Serial cable, or with a USB-Serial converter chip (e.g. the FTDI FT232) in your hardware device.
Either way, this allows you to interact with your USB device as you would a serial port (see, e.g. here for how to do this in C++ in Windows)
Much like Chris suggested, you can get a USB slave device from FTDI. $27 at digikey will get you a small board with all the fine wiring already done.
However, for your purposes the bit-banging mode described on page 39 of the datasheet would be much better than the UART mode (UARTs generate pulses at several kilohertz, you want to have the voltage stay at the level you set it to).
Hopefully your MOSFET will turn on with a 3V signal since the FTDI will put out approximately 3.1-3.2V for a high output.
Also, make sure you use the latest drivers from FTDI... a couple years ago they had drivers (WHQL-certified even) that caused frequent BSOD, and I've often found that driver CDs that come with hardware are several years out of date.
I built something very similar to what you are doing (I was running a car window motor from a usb device, used a mossfet H bridge (the HDR1X8 on the diagram) to drive the motor.). You need a USB to I/O device this is what I used (I got a U421, they fit perfectly over the center line of a breadboard, the 401 works well with breadboards too if you don't have a split one.
They give you a dll and you just link in to it with your code. its just as simple as making a call to WriteA and WriteB for writing out to your mosfet device. Be warned logic level lines are not meant to drive current so you will need to hook this up to a transistor if you expect any kind of medium to large current flow. See App1 in the application notes of the menu on the usbmicro site to see the proper way to hook it up.
Use a USB prototype board. It usually comes with a software SDK.
Check out these links from my web site:
1.You can use a usb to serial converter. All you need to know is here
2.You can use programming to access the usb directly but for this one you have to have a driver already installed for you device. Details are here

how do i take picture from a digital camera attached to my computer throught USB cable using vb.net or asp.net or C#?

I am writing picture editing windows forms application using vb.net/c#. i have a client requirement to capture the photo from digital still camera attached to computer.
how can i capture a photo from USB connected digital still camera device in my windows application ?
If you use the Windows Image Acquisition Library, you'll see events there for capturing camera new picture events. I had a similar requirement and wrote a test rig; we went down to the local camera store and tried every camera they had. The only cameras we could find that supported this functionality were the Nikon D-series cameras.
We found that with most cameras, you can't even take a picture when they are plugged in. When you plug them in to the USB port, most cameras will switch into a mode where the only thing they'll do is transfer data. The quick way to find out if a camera will work at all is to plug it into a PC, then try to snap a picture. If it lets you do that you have a chance. It also needs to support PTP.
I assume you want to activate the action of taking a picture from the computer which the camera is attached to. If that is the case then the first thing I would do is search for an API for that particular camera model. I don't believe there is a standard protocol/framework for interacting with digital cameras besides accessing the memory card within the camera.
This is depend on the interface the camera exporting. If this is standard mass storage interface you just use standard file interface, i.e you will see the camera as removable disk and can use standard Create/Read/Write/File operation.
Many new cameras have ptp (Picture transport protocol) interface. So you will need using Windows Image Acquisition API.
You might find useful following Link. If i understand correctly this is a sample code for exactly what are you looking for. Google is your friend :)
Another piece of info: many cameras will support both mass storage and ptp interfaces and it will be selectable by camera user interface. In case of automatic mode camera probably will switch to ptp interface.
Usually the camera is displayed as a removable drive when attached.
So for a Winforms application just let the user select the drive and the picture you want to upload. You can do any processing once you have the FileStream of the picture.
In ASP.net you are going to need a FileUpload Control where again the user can select the drive and picture to upload. Processing this time would be via MemoryStream on the HttpRequest.Files object.
Hope that helps.
This depends on your camera.
Many cameras will simply mount as USB mass storage devices. If this is the case, then you can just copy the file from the visible file system like you would any other file on an external disk.
If the camera doesn't make its contents available in this way, you'll need to look at the camera driver documentation to see how they recommend interacting with it.
It will depend on the brand of camera. Here is a link to start with for Canon.

Categories