1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <iostream>
18 #include <iomanip>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <USBDeviceManager.hpp>
23 #include <USB_DIO_Family.hpp>
24
25
26 using namespace AIOUSB;
27 using namespace std;
28
29 int main( int argc, char *argv[] ) {
30 const string VERSION = "1.6, 25 December 2009";
31
32
33
34
35
36 const bool DEMO_EEPROM_WRITE = false;
37
38 USBDeviceManager deviceManager;
39 try {
40 deviceManager.open();
41 cout
42 << "USB-IIRO-16 sample program version: " << VERSION << "\n"
43 << " AIOUSB C++ library version: " << deviceManager.VERSION_NUMBER << ", " << deviceManager.VERSION_DATE << "\n"
44 << " AIOUSB library version: " << deviceManager.getAIOUSBVersion() << ", " << deviceManager.getAIOUSBVersionDate() << "\n"
45 << " This program demonstrates controlling a USB-IIRO-16 device on\n"
46 << " the USB bus. For simplicity, it uses the first such device found\n"
47 << " on the bus.\n";
48 deviceManager.printDevices();
49 USBDeviceArray devices = deviceManager.getDeviceByProductID( USB_IIRO_16 );
50 if( devices.size() > 0 ) {
51 USB_DIO_Family &device = *( USB_DIO_Family * ) devices.at( 0 );
52 device.reset()
53 .setCommTimeout( 1000 );
54
55
56
57
58 if( DEMO_EEPROM_WRITE ) {
59 cout << "Writing to EEPROM ... " << flush;
60 const int offset = 0x100;
61 string message = "USB-IIRO-16 sample program";
62 UCharArray prevData = device.customEEPROMRead( offset, message.length() );
63 UCharArray data( message.length() );
64 for( int index = 0; index < ( int ) message.length(); index++ )
65 data.at( index ) = message.at( index );
66 device.setCommTimeout( 10000 )
67 .customEEPROMWrite( offset, data );
68 UCharArray readData = device.customEEPROMRead( offset, message.length() );
69 string readMessage( readData.size(), ' ' );
70 for( int index = 0; index < ( int ) readData.size(); index++ )
71 readMessage.at( index ) = readData.at( index );
72 device.customEEPROMWrite( offset, prevData )
73 .setCommTimeout( 1000 );
74 cout
75 << ( ( readMessage.compare( message ) == 0 )
76 ? "successful"
77 : "failed: data read back did not compare" )
78 << endl;
79 }
80
81
82
83
84 cout
85 << "EEPROM contents:\n"
86 << '['
87 << flush << hex << setw( 2 ) << setfill( '0' );
88 UCharArray data = device.customEEPROMRead( 0, device.CUSTOM_EEPROM_SIZE );
89 for( int index = 0; index < ( int ) data.size() - 1; index++ )
90 cout << ( unsigned ) data.at( index ) << ", ";
91 cout << ( unsigned ) data.at( data.size() - 1 ) << "]\n";
92
93
94
95
96 cout << "Configuring digital I/O ... " << flush;
97 const int numPorts = device.dio().getNumPorts();
98 const int numOutputPorts = numPorts / 2;
99 const int numChannels = device.dio().getNumChannels();
100 const int numOutputChannels = numChannels / 2;
101 const int numInputChannels = numChannels - numOutputChannels;
102 BoolArray outputs( numPorts );
103 for( int port = 0; port < numOutputPorts; port++ )
104 outputs.at( port ) = true;
105 for( int port = numOutputPorts; port < numPorts; port++ )
106 outputs.at( port ) = false;
107 BoolArray values( numOutputChannels );
108 values.assign( values.size(), true );
109 device.dio().configure( false, outputs, values );
110 cout << "successful" << endl;
111
112
113
114
115 cout
116 << "In the following demonstrations, you should hear the relays click\n"
117 << "and see the LED on the device blink.\n";
118 cout << " CLosing relays:" << flush << dec;
119 for( int channel = 0; channel < numOutputChannels; channel++ ) {
120 cout << " " << channel << flush;
121 device.dio().write( channel, false );
122 usleep( 100000 );
123 }
124 cout << endl << " Opening relays:" << flush;
125 for( int channel = 0; channel < numOutputChannels; channel++ ) {
126 cout << " " << channel << flush;
127 device.dio().write( channel, true );
128 usleep( 100000 );
129 }
130 cout << endl;
131
132
133
134
135 cout << "Closing all relays ... " << flush;
136 values.assign( values.size(), false );
137 device.dio().write( 0, values );
138 cout << "successful" << endl;
139 sleep( 2 );
140 cout << "Opening all relays ... " << flush;
141 values.assign( values.size(), true );
142 device.dio().write( 0, values );
143 cout << "successful" << endl;
144
145
146
147
148 sleep( 1 );
149 cout << "CLosing every other relay:";
150 for( int channel = 0; channel < numOutputChannels; channel += 2 ) {
151 cout << " " << channel << flush;
152 device.dio().write( channel, false );
153 usleep( 100000 );
154 }
155 cout << endl << "Reading back relays:" << endl;
156 for( int channel = 0; channel < numOutputChannels; channel++ ) {
157 cout << " Channel " << channel << " = " << device.dio().read( channel ) << endl;
158 }
159 cout << "Reading isolated inputs:" << endl;
160 for( int channel = 0; channel < numInputChannels; channel++ ) {
161 cout << " Channel " << ( numOutputChannels + channel ) << " = " << device.dio().read( numOutputChannels + channel ) << endl;
162 }
163
164
165
166
167 values.assign( values.size(), true );
168 device.dio().write( 0, values );
169 } else
170 cout << "No USB-IIRO-16 devices found on USB bus" << endl;
171 deviceManager.close();
172 } catch( exception &ex ) {
173 cerr << "Error \'" << ex.what() << "\' occurred while manipulating device" << endl;
174 if( deviceManager.isOpen() )
175 deviceManager.close();
176 }
177 }
178
179