Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if the scanner is plugged in (C#, .NET TWAIN)

Tags:

c#

twain

I'm using the .NET TWAIN code from http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=1007385#xx1007385xx in my application. When I try to scan an image when the scanner is not plugged in, the application freezes.

How can I check if the device is plugged in, using the TWAIN driver?

like image 786
SelvirK Avatar asked Sep 19 '08 07:09

SelvirK


People also ask

Why is my scanner not connecting to my computer?

One simple reason your computer may not detect the scanner is a loose connection. Check the USB and AC adapter cords and all connections to make sure they're tight and secure. Examine the cables themselves for signs of damage that may prevent them from working properly.

What to do if the scanner is not working?

First check your connection to the scanner and computer: Make sure your scanner's AC adapter is securely connected to the scanner and a working electrical outlet. Make sure the interface cable is securely connected to the scanner and your computer, and that the cable is not damaged or crimped.


1 Answers

Maybe I'm taking the question too literally, but using the TWAIN API, it is not possible to check if a device is plugged in i.e. connected and powered on. The TWAIN standard does define a capability for this purpose called CAP_DEVICEONLINE, but this feature is so poorly conceived and so few drivers implement it correctly that it is useless in practice.

The closest you can get is this: Open the device (MSG_OPENDS): Almost all drivers will check for device-ready when they are opened, and will display an error dialog to the user. There is no TWAIN mechanism for suppressing or detecting this dialog Some drivers will allow the user to correct the problem and continue, in which case you (your app) will never know there was a problem. Some drivers will allow the user to cancel, in which case the MSG_OPENDS operation will fail, probably returning TWRC_CANCEL but maybe TWRC_FAILURE

A few TWAIN drivers will open without error even though the device is off-line. Such a driver may return FALSE to a query of CAP_DEVICEONLINE. Such a driver will probably do the device-online check when you enable the device with MSG_ENABLEDS, and then if the device is not on-line, you get the error dialog to the user, and so on as above.

Aside and IMPO: WIA is 'more modern' but also much less comprehensive for scanning than TWAIN, and in my experience unusable for multipage scanning from a document feeder. WIA's designers and maintainers seem not to understand or care about scanners other than low-end consumer flatbeds. It's good for cameras.

like image 154
Spike0xff Avatar answered Oct 02 '22 11:10

Spike0xff