1// Literatur:
2// Manuals vom Hersteller FTDI
3
4// Library hinzufuegen: ftd2xx.lib
5// Header-Datei inkludieren: ftd2xx.h
6// DLLs in Ausgabe-Ordner kopieren: ftbusui.dll
7// ftcserco.dll
8// ftd2xx.dll
9// ftlang.dll
10// ftserui2.dll
11
12/* Beispiel einer Ausgabe-Datei:
13 date time value
14 2012.11.21 13:06:24 0xf0
15 2012.11.21 13:06:28 0xf1
16 2012.11.21 13:06:28 0xf0
17*/
18
19#include <iostream>
20#include <fstream>
21#include <iomanip>
22#include <conio.h>
23#include <string>
24#include <time.h>
25#include <windows.h>
26#include "ftd2xx.h"
27
28using namespace std;
29
30#define D0 0x01 // TXD (orange wire)
31#define D1 0x02 // RXD (yellow wire)
32#define D2 0x04 // RTS (green wire)
33#define D3 0x08 // RTS (brown wire)
34
35#define RESET_MODE 0x00
36#define ASYNC_BIT_BANG 0x01
37#define SYNC_BIT_BANG 0x04
38
39
40int main(int argc, char **argv)
41{
42 FT_HANDLE handle;
43 FT_STATUS status;
44 DWORD nrDevices;
45 DWORD bytesWritten;
46 DWORD bytesRead;
47 int devNr = 0;
48 unsigned char latency = 0;
49 unsigned char output = 0;
50 unsigned char input = 0;
51 int tmp = 0;
52 time_t rawtime;
53 struct tm* timeinfo;
54
55
56 try
57 {
58 if ((argc == 2) && (strcmp(argv[1], "-list") == 0))
59 {
60 status = FT_ListDevices(&nrDevices, NULL, FT_LIST_NUMBER_ONLY);
61 if(status != FT_OK)
62 throw("Can't list connected devices!");
63
64 cout << "Number devices connected: " << nrDevices << endl;
65 }
66 else if ((argc == 3) && (strcmp(argv[1], "-write") == 0))
67 {
68 devNr = atoi(argv[2]);
69 status = FT_Open(devNr, &handle);
70 if(status != FT_OK)
71 throw("Can't open device!");
72
73 // 0...input, 1...output
74 FT_SetBitMode(handle, D0 | D1 | D2 | D3, ASYNC_BIT_BANG);
75 if(status != FT_OK)
76 throw("Can't set bit-mode!");
77
78 // 9600 * 16
79 FT_SetBaudRate(handle, FT_BAUD_9600);
80 if(status != FT_OK)
81 throw("Can't set baud rate!");
82
83 cout << "End write mode with 0xff!" << endl;
84
85 do
86 {
87 cout << "New value (x x x x D3 D2 D1 D0): ";
88 cin >> hex >> tmp;
89
90 if (tmp != 0xFF)
91 {
92 output = (unsigned char)tmp & 0x0F;
93 FT_Write(handle, &output, 1, &bytesWritten);
94 if(status != FT_OK)
95 throw("Can't write to device!");
96
97 Sleep(100);
98 }
99 }
100 while (tmp != 0xFF);
101
102 FT_Close(handle);
103 if(status != FT_OK)
104 throw("Can't close device!");
105 }
106 else if ((argc == 4) && (strcmp(argv[1], "-read") == 0))
107 {
108 // open output file
109 ofstream out(argv[3]);
110 if(!out.is_open())
111 throw("Can't open file!");
112
113 // open FTDI device
114 devNr = atoi(argv[2]);
115 status = FT_Open(devNr, &handle);
116 if(status != FT_OK)
117 throw("Can't open device!");
118
119 // 0...input, 1...output
120 FT_SetBitMode(handle, 0, ASYNC_BIT_BANG);
121 if(status != FT_OK)
122 throw("Can't set bit-mode!");
123
124 // 9600 * 16
125 FT_SetBaudRate(handle, FT_BAUD_9600);
126 if(status != FT_OK)
127 throw("Can't set baud rate!");
128
129 cout << "End read mode by unplugging the device!" << endl;
130 out << "date\ttime\tvalue" << endl;
131
132 do
133 {
134 // clear device buffers
135 status = FT_Purge(handle, FT_PURGE_RX | FT_PURGE_TX);
136 if(status != FT_OK)
137 {
138 out.close();
139 cout << "File was written." << endl;
140
141 throw("Can't clear device buffers!");
142 }
143
144 // read all inputs
145 status = FT_Read(handle, &input, 1, &bytesRead);
146 if(status != FT_OK)
147 {
148 out.close();
149 cout << "File was written." << endl;
150
151 throw("Can't read from device!");
152 }
153
154 // if something changed
155 if (tmp != input)
156 {
157 time(&rawtime);
158 timeinfo = localtime(&rawtime);
159 out << setfill('0');
160 out << setw(4);
161 out << timeinfo->tm_year + 1900 << ".";
162 out << setw(2);
163 out << timeinfo->tm_mon << ".";
164 out << setw(2);
165 out << timeinfo->tm_mday << "\t";
166 out << setw(2);
167 out << timeinfo->tm_hour << ":";
168 out << setw(2);
169 out << timeinfo->tm_min << ":";
170 out << setw(2);
171 out << timeinfo->tm_sec;
172 out << hex << "\t0x" << (int)input << dec << endl;
173 }
174 tmp = input;
175 }
176 while(true);
177
178 FT_Close(handle);
179 if(status != FT_OK)
180 throw("Can't close device!");
181
182 out.close();
183 }
184 else
185 {
186 cout << endl;
187 cout << " " << argv[0] << " -list" << endl;
188 cout << " ... lists some information" << endl;
189 cout << endl;
190 cout << " " << argv[0] << " -write 0" << endl;
191 cout << " ... opens device 0 in output mode" << endl;
192 cout << endl;
193 cout << " " << argv[0] << " -read 0 samples.txt" << endl;
194 cout << " ... opens device 0 in input mode" << endl;
195 cout << " and stores read data in samples.txt" << endl;
196 cout << endl;
197 }
198 }
199 catch(char const *error)
200 {
201 cout << "Error: " << error << endl;
202 }
203 catch(...)
204 {
205 cout << "Undefined Error!" << endl;
206 }
207
208 cout << "Press any key..." << endl;
209 getch();
210 return 0;
211}