Project

General

Profile

Statistics
| Revision:

root / trunk / src / java / org / lidar / Serial.java

History | View | Annotate | Download (5.12 KB)

1 9 andrej.cim
package org.lidar;
2
3
import java.io.InputStream;
4
import java.io.OutputStream;
5
import javax.comm.*;
6
import java.util.*;
7
import org.apache.commons.configuration.XMLConfiguration;
8
import org.lidar.api.Management;
9
10
/**
11
 * Serial port communication
12
 * @author Andrej Cimpersek
13
 */
14
public class Serial {
15 11 andrej.cim
16 9 andrej.cim
    private boolean isOpened = false;
17
    private String deviceName;
18
    private String portName;
19
    private Enumeration portIdentifiers;
20
    private CommPortIdentifier portId;
21
    private SerialPort port;
22
    protected InputStream inputStream;
23
    protected OutputStream outputStream;
24
    private int baudRate, dataBits, stopBits, parity;
25
    private XMLConfiguration configuration;
26 11 andrej.cim
27
    public Serial(String deviceName) {
28 9 andrej.cim
        configuration = Config.getConfiguration();
29
        this.portName = configuration.getString(String.format("devices.%s.port", deviceName));
30
        this.baudRate = configuration.getInt(String.format("devices.%s.baudRate", deviceName));
31
        this.dataBits = configuration.getInt(String.format("devices.%s.dataBits", deviceName));
32
        this.stopBits = configuration.getInt(String.format("devices.%s.stopBits", deviceName));
33
        this.parity = configuration.getInt(String.format("devices.%s.parity", deviceName));
34
    }
35
36
    /**
37
     * Check each port identifier if
38
     *   (a) it indicates a serial (not a parallel) port, and
39
     *   (b) matches the desired name.
40
     * @return boolean
41
     */
42 11 andrej.cim
    protected boolean identfy() {
43 9 andrej.cim
        this.portIdentifiers = CommPortIdentifier.getPortIdentifiers();
44
        this.portId = null;  // will be set if port found
45 11 andrej.cim
        while (portIdentifiers.hasMoreElements()) {
46
            CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
47
            if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(this.portName)) {
48 9 andrej.cim
                this.portId = pid;
49
                return true;
50
            }
51
        }
52
        return false;
53
    }
54
55
    /**
56
     * Destructor - close port
57
     */
58
    @Override()
59 11 andrej.cim
    protected void finalize() throws Throwable {
60 9 andrej.cim
        closePort();
61
    }
62
63 11 andrej.cim
    public boolean acquirePort() {
64
        if (this.portId == null) {
65
            return false;
66
        }
67 9 andrej.cim
        // Use port identifier for acquiring the port
68
        try {
69 11 andrej.cim
            this.port = (SerialPort) this.portId.open(
70
                    this.deviceName, // Name of the application asking for the port
71
                    10000 // Wait max. 10 sec. to acquire port
72
                    );
73
        } catch (PortInUseException e) {
74 9 andrej.cim
            return false;
75
        }
76
77
        return true;
78
    }
79
80 11 andrej.cim
    public boolean openPort() {
81
        if (!acquirePort()) {
82
            return false;
83
        }
84 9 andrej.cim
        closePort();
85
        try {
86
            this.port.setSerialPortParams(this.baudRate, this.dataBits, this.stopBits, this.parity);
87
            this.port.removeEventListener();
88
            this.port.addEventListener(new SerialPortEventListener() {
89 11 andrej.cim
90
                public void serialEvent(SerialPortEvent event) {
91
                    switch (event.getEventType()) {
92 9 andrej.cim
                        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
93
                            onOutputBufferEmpty(event);
94
                            break;
95
96
                        case SerialPortEvent.DATA_AVAILABLE:
97
                            onDataAvailable(event);
98
                            break;
99
                    }
100
                }
101
            });
102
            this.inputStream = this.port.getInputStream();
103
            this.outputStream = this.port.getOutputStream();
104
            this.isOpened = true;
105
        } catch (Exception e) {
106
            this.isOpened = false;
107
        } finally {
108
            return this.isOpened;
109
        }
110
    }
111
112 11 andrej.cim
    public void closePort() {
113
        if (this.port == null) {
114
            return;
115
        }
116 9 andrej.cim
        try {
117
            this.inputStream.close();
118
            this.outputStream.close();
119
            this.port.close();
120
        } catch (Exception e) {
121
        } finally {
122
            this.isOpened = false;
123
        }
124
    }
125
126
    /**
127
     * Write shortcut for string
128
     */
129 11 andrej.cim
    public boolean sendCommand(String lockToken, String string) {
130 9 andrej.cim
        return sendCommand(lockToken, string.getBytes());
131
    }
132
133
    /**
134
     * Write shortcut
135
     * @param bytes
136
     * @return boolean
137
     */
138 11 andrej.cim
    public boolean sendCommand(String lockToken, byte[] bytes) {
139 9 andrej.cim
        // TODO: worker, ki odklene po X času brez dostopa
140 11 andrej.cim
        if (!Management.locked || lockToken != Management.lockToken) {
141
            return false;
142
        }
143 9 andrej.cim
        Management.lockAccess = new Date();
144 11 andrej.cim
145 9 andrej.cim
        try {
146
            outputStream.write(bytes);
147
        } catch (Exception e) {
148
            return false;
149
        }
150
151
        return true;
152
    }
153
154
    /**
155
     * Handle output buffer empty events.
156
     * NOTE: The reception of this event is optional and not
157
     *       guaranteed by the API specification.
158
     * @param event The output buffer empty event
159
     */
160 11 andrej.cim
    protected void onOutputBufferEmpty(SerialPortEvent event) {
161 9 andrej.cim
        //TODO: implement
162
    }
163
164
    /**
165
     * Handle data available events.
166
     * @param event The data available event
167
     */
168 11 andrej.cim
    protected void onDataAvailable(SerialPortEvent event) {
169 9 andrej.cim
        //TODO: implement
170
    }
171
}