Revision 11
| Serial.java | ||
|---|---|---|
| 12 | 12 |
* @author Andrej Cimpersek |
| 13 | 13 |
*/ |
| 14 | 14 |
public class Serial {
|
| 15 |
|
|
| 15 | 16 |
private boolean isOpened = false; |
| 16 | 17 |
private String deviceName; |
| 17 | 18 |
private String portName; |
| ... | ... | |
| 22 | 23 |
protected OutputStream outputStream; |
| 23 | 24 |
private int baudRate, dataBits, stopBits, parity; |
| 24 | 25 |
private XMLConfiguration configuration; |
| 25 |
|
|
| 26 |
public Serial(String deviceName){
|
|
| 26 |
|
|
| 27 |
public Serial(String deviceName) {
|
|
| 27 | 28 |
configuration = Config.getConfiguration(); |
| 28 | 29 |
this.portName = configuration.getString(String.format("devices.%s.port", deviceName));
|
| 29 | 30 |
this.baudRate = configuration.getInt(String.format("devices.%s.baudRate", deviceName));
|
| ... | ... | |
| 38 | 39 |
* (b) matches the desired name. |
| 39 | 40 |
* @return boolean |
| 40 | 41 |
*/ |
| 41 |
protected boolean identfy(){
|
|
| 42 |
protected boolean identfy() {
|
|
| 42 | 43 |
this.portIdentifiers = CommPortIdentifier.getPortIdentifiers(); |
| 43 | 44 |
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)){
|
|
| 45 |
while (portIdentifiers.hasMoreElements()) {
|
|
| 46 |
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); |
|
| 47 |
if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(this.portName)) {
|
|
| 48 | 48 |
this.portId = pid; |
| 49 | 49 |
return true; |
| 50 | 50 |
} |
| ... | ... | |
| 56 | 56 |
* Destructor - close port |
| 57 | 57 |
*/ |
| 58 | 58 |
@Override() |
| 59 |
protected void finalize() throws Throwable{
|
|
| 59 |
protected void finalize() throws Throwable {
|
|
| 60 | 60 |
closePort(); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 |
public boolean acquirePort(){
|
|
| 64 |
if(this.portId == null) return false; |
|
| 63 |
public boolean acquirePort() {
|
|
| 64 |
if (this.portId == null) {
|
|
| 65 |
return false; |
|
| 66 |
} |
|
| 65 | 67 |
// Use port identifier for acquiring the port |
| 66 | 68 |
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) {
|
|
| 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) {
|
|
| 72 | 74 |
return false; |
| 73 | 75 |
} |
| 74 | 76 |
|
| 75 | 77 |
return true; |
| 76 | 78 |
} |
| 77 | 79 |
|
| 78 |
public boolean openPort(){
|
|
| 79 |
if(!acquirePort()) return false; |
|
| 80 |
public boolean openPort() {
|
|
| 81 |
if (!acquirePort()) {
|
|
| 82 |
return false; |
|
| 83 |
} |
|
| 80 | 84 |
closePort(); |
| 81 | 85 |
try {
|
| 82 | 86 |
this.port.setSerialPortParams(this.baudRate, this.dataBits, this.stopBits, this.parity); |
| 83 | 87 |
this.port.removeEventListener(); |
| 84 | 88 |
this.port.addEventListener(new SerialPortEventListener() {
|
| 85 |
public void serialEvent(SerialPortEvent event){
|
|
| 86 |
switch(event.getEventType()) {
|
|
| 89 |
|
|
| 90 |
public void serialEvent(SerialPortEvent event) {
|
|
| 91 |
switch (event.getEventType()) {
|
|
| 87 | 92 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: |
| 88 | 93 |
onOutputBufferEmpty(event); |
| 89 | 94 |
break; |
| ... | ... | |
| 104 | 109 |
} |
| 105 | 110 |
} |
| 106 | 111 |
|
| 107 |
public void closePort(){
|
|
| 108 |
if(this.port == null) return; |
|
| 112 |
public void closePort() {
|
|
| 113 |
if (this.port == null) {
|
|
| 114 |
return; |
|
| 115 |
} |
|
| 109 | 116 |
try {
|
| 110 | 117 |
this.inputStream.close(); |
| 111 | 118 |
this.outputStream.close(); |
| ... | ... | |
| 119 | 126 |
/** |
| 120 | 127 |
* Write shortcut for string |
| 121 | 128 |
*/ |
| 122 |
public boolean sendCommand(String lockToken, String string){
|
|
| 129 |
public boolean sendCommand(String lockToken, String string) {
|
|
| 123 | 130 |
return sendCommand(lockToken, string.getBytes()); |
| 124 | 131 |
} |
| 125 | 132 |
|
| ... | ... | |
| 128 | 135 |
* @param bytes |
| 129 | 136 |
* @return boolean |
| 130 | 137 |
*/ |
| 131 |
public boolean sendCommand(String lockToken, byte[] bytes){
|
|
| 138 |
public boolean sendCommand(String lockToken, byte[] bytes) {
|
|
| 132 | 139 |
// TODO: worker, ki odklene po X času brez dostopa |
| 133 |
if(!Management.locked || lockToken != Management.lockToken) return false; |
|
| 140 |
if (!Management.locked || lockToken != Management.lockToken) {
|
|
| 141 |
return false; |
|
| 142 |
} |
|
| 134 | 143 |
Management.lockAccess = new Date(); |
| 135 |
|
|
| 144 |
|
|
| 136 | 145 |
try {
|
| 137 | 146 |
outputStream.write(bytes); |
| 138 | 147 |
} catch (Exception e) {
|
| ... | ... | |
| 148 | 157 |
* guaranteed by the API specification. |
| 149 | 158 |
* @param event The output buffer empty event |
| 150 | 159 |
*/ |
| 151 |
protected void onOutputBufferEmpty(SerialPortEvent event){
|
|
| 160 |
protected void onOutputBufferEmpty(SerialPortEvent event) {
|
|
| 152 | 161 |
//TODO: implement |
| 153 | 162 |
} |
| 154 | 163 |
|
| ... | ... | |
| 156 | 165 |
* Handle data available events. |
| 157 | 166 |
* @param event The data available event |
| 158 | 167 |
*/ |
| 159 |
protected void onDataAvailable(SerialPortEvent event){
|
|
| 168 |
protected void onDataAvailable(SerialPortEvent event) {
|
|
| 160 | 169 |
//TODO: implement |
| 161 | 170 |
} |
| 162 | 171 |
} |
Also available in: Unified diff