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

白昼怎懂夜的黑 提交于 2019-12-09 06:07:32

问题


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?


回答1:


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.




回答2:


I started of with the same source code that you downloaded from CodeProject, but moved most of the code in MainFrame.cs that initiates the scanning to a Scanner class. In order to check for scan errors I call the following method in stead of calling Twain.Acquire directly:

enum AcquireResult
{
    OK = 0,
    InitFailed = 1,
    DeviceIDFailed = 2,
    CapabilityFailed = 3,
    UserInterfaceError = 4
}
private void StartScan()
{
    if (!_msgFilter)
    {
        _parent.Enabled = false;
        _msgFilter = true;
        Application.AddMessageFilter(this);
    }
    AcquireResult ar = _twain.Acquire();
    if (ar != AcquireResult.OK)
    {
        EndingScan();
        switch (ar)
        {
            case AcquireResult.CapabilityFailed:
                throw new Exception("Scanner capability setup failed");
            case AcquireResult.DeviceIDFailed:
                throw new Exception("Unable to determine device identity");
            case AcquireResult.InitFailed:
                throw new Exception("Scanner initialisation failed");
            case AcquireResult.UserInterfaceError:
                throw new Exception("Error with the Twain user interface");
            default:
                throw new Exception("Document scanning failed");
        }
    }
}

I usually initiate the scan event on a seperate thread in order for the app not to freeze while scanning is in progress.




回答3:


just add this code on your TwainCommand (cmd)

case TwainCommand.Null:
    {
     EndingScan();
     tw.CloseSrc();
             Msgbox("There is no device or the scannning has been cancelled.");
     break;
    }

this will appear if the systems detect no device or the scanning has been cancelled.




回答4:


You can check in the registry. In:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6bdd1fc6-810f-11d0-bec7-08002be2092f} each scanner that's ever been detected is enumerated there in the subkeys.

Starting with 0000, go through and check if the CreateFileName value is blank or has data.

If it has data, it's a connected scanner, if it's blank, it's not connected.




回答5:


i try do this but dont work good with TWAIN mybe try WIA

mybe try this:

on buton run scanner

timer1.Interval = 30000;

switch (cmd)
{
case TwainCommand.TransferReady:

{
..........
}

default:

{
timer1.Start();
break;
}

on event timer tick

{
EndingScan();
tw.CloseSrc();
}


来源:https://stackoverflow.com/questions/100284/how-do-i-check-if-the-scanner-is-plugged-in-c-net-twain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!