Each twain data source has a unique name (that is chosen by the device's driver, when that driver is installed).
In order to be able to download images from a twain device, you must have a REXX GUI window open. The reason for this is because, at various times, the twain device causes "signals" to be sent to your window. These are handled in the message loop of your main script. Furthermore, you must enable twain support by calling GuiWindowDefaults and specifying the 'TWAIN' option.
There is also something called a Twain Data Source Manager on your computer. This is simply some software that helps your script interact with the various data sources. You must open the Data Source Manager with a call to TwainOpenDSM and pass a handle to your GUI window above.
REXX Twain automatically uses the Data Source Manager on your behalf when you call the REXX Twain functions. For example, the Data Source Manager can be told to display a graphical listbox containing the names of all of the data sources on the computer, so that the enduser can click on one to choose it. REXX Twain has a function you can call named TwainSelectSource. TwainSelectSource tells the Data Source Manager to display that listbox, gets the user's choice, and stores the name of that data source as the device all other REXX Twain functions operate upon.
To begin transferring images from one of the data sources, you call TwainAcquire. This starts the process of downloading each image from the data source that the user picked out via TwainSelectSource. (If your script has not called TwainSelectSource to let the user pick out a data source, then TwainAcquire defaults to using the first twain device on the system).
Sometimes it takes a twain device a long time to prepare an image to be downloaded. For example, a scanner needs to physically scan the page before any image is ready for your script to download. So TwainAcquire merely tells the device that you wish to begin downloading images. At this point, no download has yet happened.
When the twain device finally has an image ready to be downloaded, then it will cause GuiGetMsg to return. The GuiObject variable will be set to TWAIN. (This implies that, if you're using Twain, you should not create your own object with the name TWAIN). The GuiSignal variable will be set to XFER to inform you that the twain device is ready for you to download the next image. To download that image, you call TwainGetImage. You pass the name of some subroutine in your script that will be called by TwainGetImage, and will be passed a handle to the downloaded image. At this point, you typically will display and/or store the image (and REXX Twain has functions to help you do both). Your subroutine will return a 1 if you wish REXX Twain to free that handle, a 0 if you do not want the handle freed (in which case, you are responsible for freeing it yourself later), or no return if you wish the acquire process to be aborted.
After TwainGetImage returns, the twain device may have another image to be downloaded. (For example, a digital camera will typically have many images in it). In this case, the twain device will cause GuiGetMsg to once again return. GuiObject will be TWAIN and GuiSignal will be XFER. You will repeat the above process (of calling TwainGetImage) to download the next image.
When there are no more images to be downloaded, the twain device will cause GuiGetMsg to return once more. GuiObject will be TWAIN, but GuiSignal will be DONE to inform you that there are no more images to download and the acquire process is done.