1 package edu.berkeley.cs.obits;
7 public class AtmelSerial {
9 public static SerialPort detectObitsPort() throws Exception {
10 Enumeration e = CommPortIdentifier.getPortIdentifiers();
11 while(e.hasMoreElements()) {
12 CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
13 System.err.println("trying " + cpi.getName());
15 return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
17 public static void main(String[] s) throws Exception {
18 final SerialPort sp = detectObitsPort();
19 sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
20 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
21 final OutputStream out = sp.getOutputStream();
22 final InputStream in = sp.getInputStream();
23 while(in.available() > 0) in.read();
28 DataInputStream dis = new DataInputStream(in);
29 System.err.println("waiting...");
30 if (dis.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
31 if (dis.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
32 if (dis.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
33 if (dis.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
34 if (dis.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
35 if (dis.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
36 System.err.println("ready.");
42 System.err.println(sp.isDTR() + " " + sp.isDSR() + " " + sp.isRTS() + " " + sp.isCTS());
45 } catch (Exception e) {
54 if (i2==-1) { System.err.println("input closed"); System.exit(-1); }
55 System.out.print((char)i2);
58 } catch (Exception e) {
65 long begin = System.currentTimeMillis();
66 DataOutputStream dos = new DataOutputStream(out);
67 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
68 for(String str = br.readLine(); str != null; str = br.readLine()) {
69 long foo = Long.parseLong(str, 16);
71 dos.writeByte((int)(foo >> 24));
72 dos.writeByte((int)(foo >> 16));
73 dos.writeByte((int)(foo >> 8));
74 dos.writeByte((int)(foo >> 0));
76 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
79 long end = System.currentTimeMillis();
80 System.err.println("finished in " + ((end-begin)/1000) + "s");
82 } catch (Exception e) { e.printStackTrace(); }