Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1532 | - | 1 | package dongfang.mkt.io; |
2 | |||
3 | import gnu.io.CommPort; |
||
4 | import gnu.io.CommPortIdentifier; |
||
5 | import gnu.io.NoSuchPortException; |
||
6 | import gnu.io.SerialPort; |
||
7 | |||
8 | import java.io.IOException; |
||
9 | import java.io.InputStream; |
||
10 | import java.io.OutputStream; |
||
11 | |||
12 | public class RXTXSerialPort implements MKCommPort { |
||
13 | private SerialPort serialPort; |
||
14 | |||
15 | public void init(String id) throws IOException { |
||
16 | // CommPortIdentifier.getPortIdentifier("COM1"); |
||
17 | // Just take the 1st available port. |
||
18 | CommPortIdentifier portIdentifier = null; |
||
19 | |||
20 | try { |
||
21 | if (id == null) { |
||
22 | portIdentifier = (CommPortIdentifier) CommPortIdentifier |
||
23 | .getPortIdentifiers().nextElement(); |
||
24 | if (portIdentifier == null) { |
||
25 | throw new IOException( |
||
26 | "No serial port found (in search for any serial port)."); |
||
27 | } |
||
28 | } else |
||
29 | portIdentifier = CommPortIdentifier.getPortIdentifier(id); |
||
30 | } catch (NoSuchPortException ex) { |
||
31 | throw new IOException(ex.getMessage()); |
||
32 | } |
||
33 | |||
34 | System.out.println("Using " + portIdentifier.getName()); |
||
35 | |||
36 | if (portIdentifier.isCurrentlyOwned()) { |
||
37 | System.out.println("Error: Port is currently in use"); |
||
38 | return; |
||
39 | } else { |
||
40 | try { |
||
41 | CommPort commPort = portIdentifier.open(this.getClass() |
||
42 | .getName(), 2001); |
||
43 | if (commPort instanceof SerialPort) { |
||
44 | serialPort = (SerialPort) commPort; |
||
45 | serialPort.setSerialPortParams(57600, |
||
46 | SerialPort.DATABITS_8, SerialPort.STOPBITS_1, |
||
47 | SerialPort.PARITY_NONE); |
||
48 | } |
||
49 | } catch (Exception ex) { |
||
50 | throw new IOException(ex.getMessage()); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public InputStream getInputStream() throws IOException { |
||
56 | return serialPort.getInputStream(); |
||
57 | } |
||
58 | |||
59 | public OutputStream getOutputStream() throws IOException { |
||
60 | return serialPort.getOutputStream(); |
||
61 | } |
||
62 | } |