/Users/lyon/j4p/src/serialPorts/Utils.java
|
1 package serialPorts;
2
3 /**
4 * Created by
5 * User: lyon
6 * Date: Dec 29, 2003
7 * Time: 10:06:24 AM
8 *
9 */
10
11 import gnu.io.*;
12
13 import javax.comm.CommPortIdentifier;
14 import javax.comm.SerialPort;
15 import javax.comm.UnsupportedCommOperationException;
16 import javax.comm.PortInUseException;
17 import java.io.*;
18
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.util.Enumeration;
22 import java.util.Vector;
23
24 public class Utils {
25 public static void main(String[] args) {
26 System.out.println();
27 try {
28 Class.forName("javax.comm.RXTXCommDriver");
29 } catch (ClassNotFoundException e) {
30 e.printStackTrace();
31 }
32 }
33
34 private static void dial() throws IOException,
35 UnsupportedCommOperationException,
36 PortInUseException {
37 listPorts();
38 CommPortIdentifier cpid = Utils.getPortByName("/dev/cu.modem");
39 SerialPort sp = (SerialPort) cpid.open("Utils", 200);
40 PrintStream ps = new PrintStream(sp.getOutputStream());
41 BufferedReader br
42 = new BufferedReader(
43 new InputStreamReader(sp.getInputStream()));
44 setPort(sp);
45 String messageString = "atdt8770890";
46 ps.print(messageString + "\r\n");
47 checkModemResponse(br, messageString);
48 sp.close();
49 }
50
51
52 private static void checkModemResponse(BufferedReader br, String messageString) throws IOException {
53 if (!receive(br, messageString))
54 System.err.println("WARNING: Modem did not echo command.");
55
56
57 // The modem sends an extra blank line by way of a prompt.
58 // Here we read and discard it.
59 String junk = br.readLine();
60 if (junk.length() != 0) {
61 System.err.print("Warning unexpected response: ");
62 System.err.println(junk);
63 System.out.println("port closed");
64 }
65 }
66
67 private static void setPort(SerialPort sp) throws UnsupportedCommOperationException {
68 sp.setSerialPortParams(
69 9600,
70 SerialPort.DATABITS_8,
71 SerialPort.STOPBITS_2,
72 SerialPort.PARITY_NONE);
73 sp.setDTR(true);
74 sp.setRTS(true);
75 }
76
77 public static boolean receive(BufferedReader br, String whatToExpect) {
78 boolean returnedValue = false;
79 String response = "";
80
81 try {
82 response = br.readLine();
83
84 returnedValue = response.indexOf(whatToExpect) >= 0;
85 } catch (IOException err) {
86 System.out.println(
87 "error occurred while trying to "
88 + "receive data from the serial port "
89 + "through the reader object");
90 }
91
92 return returnedValue;
93 }
94
95 public static void sendString(OutputStream os, String s)
96 throws IOException {
97 byte b[] = s.getBytes();
98 os.write(b);
99 }
100
101 public static CommPortIdentifier getPortByName(String name) {
102 CommPortIdentifier cpid[] = getComPortIdentifiers();
103 for (int i = 0; i < cpid.length; i++) {
104 if (cpid[i].getName().equals(name))
105 return cpid[i];
106
107 }
108 return null;
109 }
110
111 public static CommPortIdentifier[] getComPortIdentifiers() {
112 Vector v = new Vector();
113 Enumeration pi = CommPortIdentifier.getPortIdentifiers();
114 while (pi.hasMoreElements())
115 v.addElement(pi.nextElement());
116 CommPortIdentifier cpid[] = new CommPortIdentifier[v.size()];
117 v.copyInto(cpid);
118 return cpid;
119 }
120
121 /* If you are having trouble under OSX, use
122 copy rxtx-2.1-6/MACOSX_IDE/CW/librxtxSerial.jnilib
123 to:
124 /System/Library/Frameworks/JavaVM.framework/Versions/1.4.1/Libraries
125
126 */
127 public static void listPorts() {
128 System.out.println("testPortIdentifiers:start");
129 CommPortIdentifier cpid[] = getComPortIdentifiers();
130 if (cpid.length == 0) {
131 System.out.println(
132 "no com ports detected!"
133 + " program terminated.");
134 return;
135 }
136 for (int i = 0; i < cpid.length; i++)
137 print(cpid[i]);
138 System.out.println("testPortIdentifiers:done");
139
140 }
141
142 public static void print(CommPortIdentifier cpid) {
143 System.out.print("port:" + cpid.getName() + "\t"
144 + "CurrentOwner:" + cpid.getCurrentOwner() + "\t"
145 + "isCurrentlyOwned=" + cpid.isCurrentlyOwned());
146 // isCurrentlyOwned
147 // indicates if another Java
148 // application owns the port.
149 if (cpid.getPortType() ==
150 CommPortIdentifier.PORT_SERIAL)
151 System.out.println(":PORT_SERIAL");
152 if (cpid.getPortType() ==
153 CommPortIdentifier.PORT_PARALLEL)
154 System.out.println(":PORT_PARALLEL");
155 }
156
157 private static void openAndClosePort(CommPortIdentifier cpid)
158 throws PortInUseException {
159 //CommPort port = cpid.open("TurnTable", 20);
160 System.out.println(" Opened successfully");
161 //port.close();
162 }
163
164
165 private static void testTurnTable()
166 throws
167 PortInUseException,
168 IOException,
169 UnsupportedCommOperationException {
170 CommPortIdentifier portId;
171 Utils tt = new Utils();
172 System.out.println("getPortIdentifiers...");
173 Enumeration portList = CommPortIdentifier.getPortIdentifiers();
174 System.out.println("getPortIdentifiers... done!");
175
176 while (portList.hasMoreElements()) {
177 portId = (CommPortIdentifier) portList.nextElement();
178 if
179 (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
180 if (portId.getName().equals("COM3")) {
181 processPort(portId);
182
183 }
184
185 }
186
187 }
188 }
189
190
191 private static void processPort(CommPortIdentifier portId)
192 throws PortInUseException,
193 IOException,
194 UnsupportedCommOperationException {
195 System.out.println("portId=" + portId.getName());
196 SerialPort sp =
197 openSerialPort(portId);
198
199 OutputStream os = sp.getOutputStream();
200 setUpSerialPort(sp);
201 sendMagicNumber(os);
202 sleepSec(10);
203 moveStepperMotor(os);
204 closeSerialPort(sp);
205 System.out.println("port closed");
206 System.in.read();
207 }
208
209 private static SerialPort openSerialPort(CommPortIdentifier portId)
210 throws PortInUseException {
211 return (SerialPort)
212 portId.open("SimpleWriteApp", 2000);
213
214 }
215
216 private static void setUpSerialPort(SerialPort sp)
217 throws UnsupportedCommOperationException {
218 sp.setSerialPortParams(9600, SerialPort.DATABITS_8,
219 SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
220 }
221
222 private static void sendMagicNumber(OutputStream os)
223 throws IOException {
224 byte[] mn = {27, 13, 27, 13, 27, 13, 0};
225 os.write(mn);
226 System.out.println("Send magic number..");
227 }
228
229 private static void moveStepperMotor(OutputStream os)
230 throws IOException {
231 byte[] st = {'M', 0, 0, 0, 0, 0, 0, 7, 0, 0, 13, 10};
232 os.write(st);
233 System.out.println("Move step.");
234 }
235
236 private static void closeSerialPort(SerialPort sp) {
237 //tt.step(1);
238 //sleepSec(2);
239 //tt.step(2);
240 //sleepSec(2);
241 //tt.step(3);
242 //sleepSec(2);
243
244 sp.close();
245 }
246
247
248 public void step(OutputStream os, int n) throws IOException {
249 System.out.println("Move " + n + " steps");
250 byte[] st = {'M', 0, 0, 0, 0, 0, 0, 7, 0, 0, 13, 10};
251 for (int i = 0; i < n; i++)
252 os.write(st);
253 }
254
255
256 public static void sleepSec(int sec) {
257
258 try {
259
260 Thread.sleep(sec * 1000);
261
262 } catch (InterruptedException e) {
263 }
264
265 }
266
267 }
268