Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.12 KB)

1
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

    
16
    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

    
27
    public Serial(String deviceName) {
28
        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
    protected boolean identfy() {
43
        this.portIdentifiers = CommPortIdentifier.getPortIdentifiers();
44
        this.portId = null;  // will be set if port found
45
        while (portIdentifiers.hasMoreElements()) {
46
            CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
47
            if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(this.portName)) {
48
                this.portId = pid;
49
                return true;
50
            }
51
        }
52
        return false;
53
    }
54

    
55
    /**
56
     * Destructor - close port
57
     */
58
    @Override()
59
    protected void finalize() throws Throwable {
60
        closePort();
61
    }
62

    
63
    public boolean acquirePort() {
64
        if (this.portId == null) {
65
            return false;
66
        }
67
        // Use port identifier for acquiring the port
68
        try {
69
            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
            return false;
75
        }
76

    
77
        return true;
78
    }
79

    
80
    public boolean openPort() {
81
        if (!acquirePort()) {
82
            return false;
83
        }
84
        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

    
90
                public void serialEvent(SerialPortEvent event) {
91
                    switch (event.getEventType()) {
92
                        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
    public void closePort() {
113
        if (this.port == null) {
114
            return;
115
        }
116
        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
    public boolean sendCommand(String lockToken, String string) {
130
        return sendCommand(lockToken, string.getBytes());
131
    }
132

    
133
    /**
134
     * Write shortcut
135
     * @param bytes
136
     * @return boolean
137
     */
138
    public boolean sendCommand(String lockToken, byte[] bytes) {
139
        // TODO: worker, ki odklene po X času brez dostopa
140
        if (!Management.locked || lockToken != Management.lockToken) {
141
            return false;
142
        }
143
        Management.lockAccess = new Date();
144

    
145
        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
    protected void onOutputBufferEmpty(SerialPortEvent event) {
161
        //TODO: implement
162
    }
163

    
164
    /**
165
     * Handle data available events.
166
     * @param event The data available event
167
     */
168
    protected void onDataAvailable(SerialPortEvent event) {
169
        //TODO: implement
170
    }
171
}