root / trunk / src / java / org / lidar / Serial.java @ 10
History | View | Annotate | Download (5.01 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 |
private boolean isOpened = false; |
| 16 |
private String deviceName; |
| 17 |
private String portName; |
| 18 |
private Enumeration portIdentifiers; |
| 19 |
private CommPortIdentifier portId;
|
| 20 |
private SerialPort port;
|
| 21 |
protected InputStream inputStream; |
| 22 |
protected OutputStream outputStream; |
| 23 |
private int baudRate, dataBits, stopBits, parity; |
| 24 |
private XMLConfiguration configuration;
|
| 25 |
|
| 26 |
public Serial(String deviceName){ |
| 27 |
configuration = Config.getConfiguration(); |
| 28 |
this.portName = configuration.getString(String.format("devices.%s.port", deviceName)); |
| 29 |
this.baudRate = configuration.getInt(String.format("devices.%s.baudRate", deviceName)); |
| 30 |
this.dataBits = configuration.getInt(String.format("devices.%s.dataBits", deviceName)); |
| 31 |
this.stopBits = configuration.getInt(String.format("devices.%s.stopBits", deviceName)); |
| 32 |
this.parity = configuration.getInt(String.format("devices.%s.parity", deviceName)); |
| 33 |
} |
| 34 |
|
| 35 |
/**
|
| 36 |
* Check each port identifier if
|
| 37 |
* (a) it indicates a serial (not a parallel) port, and
|
| 38 |
* (b) matches the desired name.
|
| 39 |
* @return boolean
|
| 40 |
*/
|
| 41 |
protected boolean identfy(){ |
| 42 |
this.portIdentifiers = CommPortIdentifier.getPortIdentifiers();
|
| 43 |
this.portId = null; // will be set if port found |
| 44 |
while (portIdentifiers.hasMoreElements())
|
| 45 |
{
|
| 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) return false; |
| 65 |
// Use port identifier for acquiring the port
|
| 66 |
try {
|
| 67 |
this.port = (SerialPort)this.portId.open( |
| 68 |
this.deviceName, // Name of the application asking for the port |
| 69 |
10000 // Wait max. 10 sec. to acquire port |
| 70 |
); |
| 71 |
} catch(PortInUseException e) {
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
public boolean openPort(){ |
| 79 |
if(!acquirePort()) return false; |
| 80 |
closePort(); |
| 81 |
try {
|
| 82 |
this.port.setSerialPortParams(this.baudRate, this.dataBits, this.stopBits, this.parity); |
| 83 |
this.port.removeEventListener();
|
| 84 |
this.port.addEventListener(new SerialPortEventListener() { |
| 85 |
public void serialEvent(SerialPortEvent event){ |
| 86 |
switch(event.getEventType()) {
|
| 87 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
|
| 88 |
onOutputBufferEmpty(event); |
| 89 |
break;
|
| 90 |
|
| 91 |
case SerialPortEvent.DATA_AVAILABLE:
|
| 92 |
onDataAvailable(event); |
| 93 |
break;
|
| 94 |
} |
| 95 |
} |
| 96 |
}); |
| 97 |
this.inputStream = this.port.getInputStream(); |
| 98 |
this.outputStream = this.port.getOutputStream(); |
| 99 |
this.isOpened = true; |
| 100 |
} catch (Exception e) { |
| 101 |
this.isOpened = false; |
| 102 |
} finally {
|
| 103 |
return this.isOpened; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public void closePort(){ |
| 108 |
if(this.port == null) return; |
| 109 |
try {
|
| 110 |
this.inputStream.close();
|
| 111 |
this.outputStream.close();
|
| 112 |
this.port.close();
|
| 113 |
} catch (Exception e) { |
| 114 |
} finally {
|
| 115 |
this.isOpened = false; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/**
|
| 120 |
* Write shortcut for string
|
| 121 |
*/
|
| 122 |
public boolean sendCommand(String lockToken, String string){ |
| 123 |
return sendCommand(lockToken, string.getBytes());
|
| 124 |
} |
| 125 |
|
| 126 |
/**
|
| 127 |
* Write shortcut
|
| 128 |
* @param bytes
|
| 129 |
* @return boolean
|
| 130 |
*/
|
| 131 |
public boolean sendCommand(String lockToken, byte[] bytes){ |
| 132 |
// TODO: worker, ki odklene po X času brez dostopa
|
| 133 |
if(!Management.locked || lockToken != Management.lockToken) return false; |
| 134 |
Management.lockAccess = new Date(); |
| 135 |
|
| 136 |
try {
|
| 137 |
outputStream.write(bytes); |
| 138 |
} catch (Exception e) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/**
|
| 146 |
* Handle output buffer empty events.
|
| 147 |
* NOTE: The reception of this event is optional and not
|
| 148 |
* guaranteed by the API specification.
|
| 149 |
* @param event The output buffer empty event
|
| 150 |
*/
|
| 151 |
protected void onOutputBufferEmpty(SerialPortEvent event){ |
| 152 |
//TODO: implement
|
| 153 |
} |
| 154 |
|
| 155 |
/**
|
| 156 |
* Handle data available events.
|
| 157 |
* @param event The data available event
|
| 158 |
*/
|
| 159 |
protected void onDataAvailable(SerialPortEvent event){ |
| 160 |
//TODO: implement
|
| 161 |
} |
| 162 |
} |