root / trunk / code / projects / diagnostic_station / server / SerialComm.java @ 1606
History | View | Annotate | Download (6.08 KB)
1 | 1606 | erhart | import gnu.io.CommPort; |
---|---|---|---|
2 | import gnu.io.CommPortIdentifier; |
||
3 | import gnu.io.SerialPort; |
||
4 | |||
5 | import java.io.IOException; |
||
6 | import java.io.InputStream; |
||
7 | import java.io.OutputStream; |
||
8 | |||
9 | public class SerialComm |
||
10 | { |
||
11 | /*
|
||
12 | * The following define the default settings for connections
|
||
13 | */
|
||
14 | public static final String PORT_DEFAULT = "COM15"; |
||
15 | public static final int BAUDRATE_DEFAULT = 115200; |
||
16 | public static final int PARITY_DEFAULT = SerialPort.PARITY_NONE; |
||
17 | public static final int DATABITS_DEFAULT = SerialPort.DATABITS_8; |
||
18 | // At this time, the station does use 2 stopbits
|
||
19 | public static final int STOPBITS_DEFAULT = SerialPort.STOPBITS_2; |
||
20 | |||
21 | /*
|
||
22 | * Possible parities
|
||
23 | */
|
||
24 | public static final int PARITY_EVEN = SerialPort.PARITY_EVEN; |
||
25 | public static final int PARITY_MARK = SerialPort.PARITY_MARK; |
||
26 | public static final int PARITY_NONE = SerialPort.PARITY_NONE; |
||
27 | public static final int PARITY_ODD = SerialPort.PARITY_ODD; |
||
28 | public static final int PARITY_SPACE = SerialPort.PARITY_SPACE; |
||
29 | |||
30 | /*
|
||
31 | * Possible numbers of databits
|
||
32 | */
|
||
33 | public static final int DATABITS_5 = SerialPort.DATABITS_5; |
||
34 | public static final int DATABITS_6 = SerialPort.DATABITS_6; |
||
35 | public static final int DATABITS_7 = SerialPort.DATABITS_7; |
||
36 | public static final int DATABITS_8 = SerialPort.DATABITS_8; |
||
37 | |||
38 | /*
|
||
39 | * Possible numbers of stopbits
|
||
40 | */
|
||
41 | public static final int STOPBITS_1 = SerialPort.STOPBITS_1; |
||
42 | public static final int STOPBITS_2 = SerialPort.STOPBITS_2; |
||
43 | // 1.5 stopbits
|
||
44 | public static final int STOPBITS_1_5 = SerialPort.STOPBITS_1_5; |
||
45 | |||
46 | /*
|
||
47 | * The following global variables define the settings for connections
|
||
48 | * These may be changed using the different constructors
|
||
49 | */
|
||
50 | private String port = PORT_DEFAULT; |
||
51 | private int baudRate = BAUDRATE_DEFAULT; |
||
52 | private int parity = PARITY_DEFAULT; |
||
53 | private int dataBits = DATABITS_DEFAULT; |
||
54 | private int stopBits = STOPBITS_DEFAULT; |
||
55 | private int timeout = 10000; |
||
56 | |||
57 | /*
|
||
58 | * These input and output streams read from and write to serial
|
||
59 | * These are initialized by the connect method
|
||
60 | */
|
||
61 | private InputStream in; |
||
62 | private OutputStream out; |
||
63 | |||
64 | /*
|
||
65 | * We need to store the gui so that we can write to its console whenever
|
||
66 | * we read or write from serial
|
||
67 | */
|
||
68 | private ServerGUI gui;
|
||
69 | |||
70 | /*
|
||
71 | * All settings at defaults (see above for default settings)
|
||
72 | */
|
||
73 | public SerialComm(ServerGUI gui) throws IOException{ |
||
74 | super();
|
||
75 | this.gui = gui;
|
||
76 | connect(); |
||
77 | } |
||
78 | |||
79 | /*
|
||
80 | * For example, port could be "COM4"
|
||
81 | * All other settings at defaults (see above for default settings)
|
||
82 | */
|
||
83 | public SerialComm(ServerGUI gui, String port) throws IOException{ |
||
84 | super();
|
||
85 | this.gui = gui;
|
||
86 | this.port = port;
|
||
87 | connect(); |
||
88 | } |
||
89 | |||
90 | /*
|
||
91 | * All settings set as specified in the arguments
|
||
92 | */
|
||
93 | public SerialComm(ServerGUI gui, String port, int baudRate, int dataBits, |
||
94 | int stopBits, int parity) throws IOException{ |
||
95 | super();
|
||
96 | this.gui = gui;
|
||
97 | this.port = port;
|
||
98 | this.baudRate = baudRate;
|
||
99 | this.parity = parity;
|
||
100 | this.dataBits = dataBits;
|
||
101 | this.stopBits = stopBits;
|
||
102 | connect(); |
||
103 | } |
||
104 | |||
105 | public void puts(String s) throws IOException{ |
||
106 | System.out.print("> " + s); |
||
107 | gui.addTextToConsole("> " + s);
|
||
108 | out.write((s).getBytes()); |
||
109 | } |
||
110 | |||
111 | /*
|
||
112 | * Reads until a newline is found
|
||
113 | * This operation is blocking
|
||
114 | */
|
||
115 | public String gets() throws IOException{ |
||
116 | int c_read = 0; |
||
117 | String read = ""; |
||
118 | do{
|
||
119 | c_read = in.read(); |
||
120 | if(c_read != -1) |
||
121 | read += (char)c_read;
|
||
122 | } |
||
123 | while(c_read != (int)'\n' && c_read != (int)'\r'); |
||
124 | System.out.print("< " + read); |
||
125 | gui.addTextToConsole("< " + read);
|
||
126 | return read;
|
||
127 | } |
||
128 | |||
129 | /*
|
||
130 | * If no data is available to be read, returns null
|
||
131 | * If data is available, reads until a newline is found
|
||
132 | * This operation is blocking until a newline is read
|
||
133 | */
|
||
134 | public String gets_nb() throws IOException{ |
||
135 | int c_read = 0; |
||
136 | String read = ""; |
||
137 | if(in.available() < 0) |
||
138 | return null; |
||
139 | do{
|
||
140 | c_read = in.read(); |
||
141 | if(c_read != -1) |
||
142 | read += (char)c_read;
|
||
143 | } |
||
144 | while(c_read != (int)'\n' && c_read != (int)'\r'); |
||
145 | System.out.print("< " + read); |
||
146 | gui.addTextToConsole("< " + read);
|
||
147 | return read;
|
||
148 | } |
||
149 | |||
150 | /*
|
||
151 | * Returns the next character, or the character '\0' if there is no
|
||
152 | * character to read
|
||
153 | * This operation is non-blocking
|
||
154 | */
|
||
155 | public char getc_nb() throws IOException{ |
||
156 | int c_read = 0; |
||
157 | char read = '\0'; |
||
158 | if(in.available() <= 0) |
||
159 | return read;
|
||
160 | c_read = in.read(); |
||
161 | if(c_read != -1) |
||
162 | read = (char)c_read;
|
||
163 | System.out.print("< " + read); |
||
164 | gui.addTextToConsole("< " + read);
|
||
165 | return read;
|
||
166 | } |
||
167 | |||
168 | /*
|
||
169 | * Connects to a serial port using the settings specified in the
|
||
170 | * global variables
|
||
171 | * Must be called before using any of the get or put methods
|
||
172 | * (called by the constructor)
|
||
173 | * See global variables port, baudRate, parity, dataBits, stopBits, timeout
|
||
174 | */
|
||
175 | private void connect() throws IOException{ |
||
176 | try{
|
||
177 | CommPortIdentifier portIdentifier = |
||
178 | CommPortIdentifier.getPortIdentifier(port); |
||
179 | if ( portIdentifier.isCurrentlyOwned()){
|
||
180 | System.out.println("Error: Port is currently in use\n"); |
||
181 | } |
||
182 | else{
|
||
183 | CommPort commPort = portIdentifier.open(this.getClass().getName(),
|
||
184 | timeout); |
||
185 | |||
186 | if ( commPort instanceof SerialPort ){ |
||
187 | SerialPort serialPort = (SerialPort) commPort; |
||
188 | serialPort.setSerialPortParams(baudRate, dataBits, stopBits, |
||
189 | parity); |
||
190 | |||
191 | in = serialPort.getInputStream(); |
||
192 | out = serialPort.getOutputStream(); |
||
193 | } |
||
194 | else{
|
||
195 | System.out.println("Error: " + port + |
||
196 | " is not a serial port\n");
|
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | catch(Exception e){ |
||
201 | throw new IOException(e.getMessage()); |
||
202 | } |
||
203 | } |
||
204 | } |