PC based automation doesn’t need to be difficult. Some application user interfaces are terribly complicated and take forever to learn. Often, even the simple interfaces prove difficult because they make some features impossible to use so that other features are simple.
I see this all the time in products. The Mac vs PC battle (which is really Apple vs Microsoft) is a perfect example. Both have essentially equivalent hardware power, but they take very different approaches to the user interface. Microsoft gives you access to every option under the sun, while Apple only gives you access to the options that 80% of people use regularly.
Microsoft products generally take longer to understand, but once you get it, you can do whatever you want. Apple gets you off the ground quickly, but if you are a power user, you will tend to be frustrated by a system that holds you back.
The Expert I/O application programmer’s interface (API) is a good mix between these two extremes. It is simple to learn and at the same time doesn’t hold you back.
Right from the start, your life is made easy by allowing you to use your favorite programming language. The only stipulation is that it must support the use of dynamic link libraries (DLL’s) and it’s hard to find a language that doesn’t. You can use Visual Basic, C, C++, Delphi, Pascal, etc. You can even use scripting languages such as PHP and Python. Wouldn’t that be cool, a remote accessible web application built with PHP to control a PC based automation system.
Here is a simple example of using C to set a digital output high.
// Open the Expert I/O API.
// Scan the USB and gather descriptors.
eioGetDescriptors( &DescriptorGroup );
// Claim the interface.
eioClaimInterface( DescriptorGroup.Descriptors, &EioHandle );
// Set digital output voltage high to 3.3V.
eioDioSetPull( EioHandle, 2, eioDIO_PULL_3_3 );
// Set bit 0 of port 2 high.
eioDioSet( EioHandle, 2, 1 );
// Release the interface.
eioReleaseInterface( EioHandle );
// Close the Expert I/O API.
Lines 1 through 8 are initialization and only need to be executed once. The initialization starts with calling [cci lang=”c”]eioOpen()[/cci]. This allows the API to configure its internal settings and ready itself for communicating on the USB.
The next step is to call [cci_c]eioGetDescriptors()[/cci_c] to scan each USB bus and locate all Expert I/O devices. It returns a [cci_c]DescriptorGroup[/cci_c] which contains an array of [cci_c]Descriptors[/cci_c]. Each descriptor in [cci_c]Descriptors[/cci_c] describes an Expert I/O found connected to the USB.
Descriptors contain a model ID and a serial number. By inspecting the descriptors, you can locate the specific Expert I/O you wish to access. For the purpose of this example, we simply choose the first device found on the bus. On line 8, we use the first descriptor to obtain a handle to the device.
A devices handle is exactly as it sounds. It is a “handle” to grab hold of the Expert I/O to control it. The handle is used every time you perform an operation on the device.
Line 8 serves another purpose, it claims the Expert I/O’s interface for exclusive use by our application. This is important, because the last thing you want is 2 applications trying to control the same Expert I/O simultaneously. Applications can share access to an Expert I/O, but no more than one application at a time can claim it’s interface.
Now we are ready to start controlling the Expert I/O. Line 11 configures a digital output (port 2) to 3.3V logic high ([cci_c]eioDIO_PULL_3_3[/cci_c]). Notice how we use the handle to identify the Expert I/O.
Line 14 is the line that causes the digital output to change states. It sets bit 0 of port 2 high. Each port consists of 8 pins and each pin is associated with a bit in an 8-bit value. The 1 in this function call puts a 1 in the bit 0 position of the port (binary 1 is 00000001) and thereby forces its associated pin to logic high.
At this point, you can include code to control any other ports on the Expert I/O as you see fit.
When finished accessing the Expert I/O and ready to relinquish control of its interface, call [cci_c]eioReleaseInterface()[/cci_c] (line 17 in our example). With the interface released, it is now accessible to other applications that wish to claim it.
The last thing to do is to notify the API that we are finished using it. This is done on line 20 with a call to [cci_c]eioClose()[/cci_c]. Closing the API allows it to release any memory that it has allocated and to shut down in a structured manor.
This is only a small example, but once it is in place and working, expanding it to control other I/O ports is very easy. There is a set of simple interface functions for each I/O type (motor, analog in, analog out, etc.). These are explained in complete detail in the software user manual.
If you have questions or comments, please post them below.
Struggling with extending the USB connection? Be sure to check out TIED IN KNOTS – A Must-Have Guide to Untangling USB Extension Options for PC Based Automation.