1 package org.lsst.ccs.drivers.ftdi;
2
3 import java.io.PrintStream;
4 import jline.console.ConsoleReader;
5 import org.kohsuke.args4j.CmdLineException;
6 import org.kohsuke.args4j.CmdLineParser;
7 import org.kohsuke.args4j.Option;
8
9
10
11
12
13
14
15
16
17
18 public class TalkFtdi {
19
20 private final static PrintStream out = System.out;
21 private final Thread readW = new Thread(new Reader());
22 private Ftdi ftd = new Ftdi();
23 private int nRead = 0, nByte = 0;
24 private boolean open;
25
26
27
28
29
30
31
32
33
34 private class Reader implements Runnable {
35
36 byte[] data = new byte[1024];
37
38 @Override
39 public void run()
40 {
41 while (true) {
42 int leng;
43 try {
44 leng = ftd.read(data, 0, 1);
45 int nread = ftd.getQueueStatus();
46 if (nread > data.length - leng) {
47 nread = data.length - leng;
48 }
49 if (!open) break;
50 leng += ftd.read(data, leng, nread);
51 }
52 catch (FtdiException e) {
53 if (open) {
54 out.println(e);
55 }
56 break;
57 }
58 out.write(data, 0, leng);
59 nRead++;
60 nByte += leng;
61 }
62 }
63
64 }
65
66
67
68
69
70
71
72
73
74 private static class Options {
75
76 @Option(name="-n", usage="node name")
77 private String node;
78
79 @Option(name="-s", usage="serial number")
80 private String serial;
81
82 @Option(name="-i", usage="device index")
83 private int index = 0;
84
85 @Option(name="-b", usage="baud rate")
86 private int baud = 115200;
87
88 }
89
90
91
92
93
94
95
96
97
98 public static void main(String[] args) throws Exception
99 {
100 Options optns = new Options();
101 CmdLineParser parser = new CmdLineParser(optns);
102 try {
103 parser.parseArgument(args);
104 }
105 catch (CmdLineException e) {
106 out.println(e.getMessage());
107 return;
108 }
109 (new TalkFtdi()).run(optns);
110 System.exit(0);
111 }
112
113
114
115
116
117
118
119
120
121
122
123 private void run(Options optns) throws Exception
124 {
125 ftd.open(optns.node, optns.index, optns.serial);
126 ftd.setBaudrate(optns.baud);
127 ftd.setDataCharacteristics(Ftdi.DATABITS_8, Ftdi.STOPBITS_1,
128 Ftdi.PARITY_NONE);
129 open = true;
130
131 readW.setDaemon(true);
132 readW.start();
133 ConsoleReader readC = new ConsoleReader();
134
135 while (true) {
136 String line = readC.readLine("");
137 if (line == null) break;
138 if (line.equals("")) {
139 out.format("#reads: %s; #bytes: %s\n", nRead, nByte);
140 }
141 else {
142 ftd.write(line.getBytes());
143 }
144 }
145
146 open = false;
147 ftd.close();
148 }
149
150 }