lowlevel.py :  » Development » Python-Twain-Module » twain_1.0.3 » demo » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Python Twain Module 
Python Twain Module » twain_1.0.3 » demo » lowlevel.py

# demonstration of low-level access to TWAIN API
# prints out the list of available data sources

import twain, struct, string

## The identity structure is as follows (see twain.h)
##
## typedef struct {
##   TW_UINT16  MajorNum;  /* Major revision number of the software. */
##   TW_UINT16  MinorNum;  /* Incremental revision number of the software. */
##   TW_UINT16  Language;  /* e.g. TWLG_SWISSFRENCH */
##   TW_UINT16  Country;   /* e.g. TWCY_SWITZERLAND */
##   TW_STR32   Info;      /* e.g. "1.0b3 Beta release" */
##} TW_VERSION, FAR * pTW_VERSION;
##
##  2 + 2 + 2 + 2 + 34 bytes = 42
##
## typedef struct {
##    TW_UINT32  Id;              /* Unique number.  In Windows, application hWnd      */
##    TW_VERSION Version;         /* Identifies the piece of code              */
##    TW_UINT16  ProtocolMajor;   /* Application and DS must set to TWON_PROTOCOLMAJOR */
##    TW_UINT16  ProtocolMinor;   /* Application and DS must set to TWON_PROTOCOLMINOR */
##    TW_UINT32  SupportedGroups; /* Bit field OR combination of DG_ constants */
##    TW_STR32   Manufacturer;    /* Manufacturer name, e.g. "Hewlett-Packard" */
##    TW_STR32   ProductFamily;   /* Product family name, e.g. "ScanJet"       */
##    TW_STR32   ProductName;     /* Product name, e.g. "ScanJet Plus"         */
## } TW_IDENTITY, FAR * pTW_IDENTITY;
##
## 4 + 42 + 2 + 2 + 4 + 34 + 34 + 34 bytes = 156
##
##  Note: TW_STR32 = char x[34];

##  This is the struct module string which represents the TW_IDENTITY
##  structure. Note that the alignment is non-default. The long (SupportedGroups)
##  is aligned to a two byte boundary, and so I have to extract it using
##  a '4s', rather than a 'L'.
fmtString = "L42sHH4s34s34s34s"
slen= struct.calcsize(fmtString)

def UnpackIdentity(id):
  return struct.unpack(fmtString, id)

def StripNull(s):
  offset = string.find(s, '\0')
  if s != -1: s= s[:offset]
  return s

sm = twain.SourceManager(0L) 

identity = struct.pack("%ds" % slen, "")

rv = sm.DSM_Entry(twain.DG_CONTROL, twain.DAT_IDENTITY, twain.MSG_GETFIRST, identity)
while 1:
  if rv != twain.TWRC_SUCCESS: break
  #ProductName = UnpackIdentity(identity)[5]
  ProductName = identity[122:]
  ProductName = StripNull(ProductName)
  print ProductName
  rv = sm.DSM_Entry(twain.DG_CONTROL, twain.DAT_IDENTITY, 
    twain.MSG_GETNEXT, identity)

del sm
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.