/
SpecificDriverAdapter.java
198 lines (171 loc) · 7.22 KB
/
SpecificDriverAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jmri.jmrix.powerline.insteon2412s;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import jmri.jmrix.powerline.SerialPortController;
import jmri.jmrix.powerline.SerialTrafficController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import purejavacomm.CommPortIdentifier;
import purejavacomm.NoSuchPortException;
import purejavacomm.PortInUseException;
import purejavacomm.SerialPort;
import purejavacomm.UnsupportedCommOperationException;
/**
* Provide access to Powerline devices via a serial comm port.
* Derived from the Oaktree code.
*
* @author Bob Jacobsen Copyright (C) 2006, 2007, 2008
* @author Ken Cameron, (C) 2009, sensors from poll replies Converted to
* multiple connection
* @author kcameron Copyright (C) 2011
*/
public class SpecificDriverAdapter extends SerialPortController implements jmri.jmrix.SerialPortAdapter {
SerialPort activeSerialPort = null;
public SpecificDriverAdapter() {
super(new SpecificSystemConnectionMemo());
}
@Override
public String openPort(String portName, String appName) {
try {
// get and open the primary port
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
try {
activeSerialPort = (SerialPort) portID.open(appName, 2000); // name of program, msec to wait
} catch (PortInUseException p) {
return handlePortBusy(p, portName, log);
}
// try to set it for serial
try {
setSerialPort();
} catch (UnsupportedCommOperationException e) {
log.error("Cannot set serial parameters on port {}: {}", portName, e.getMessage());
return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
}
// set framing (end) character
try {
log.debug("Serial framing was observed as: {} {}", activeSerialPort.isReceiveFramingEnabled(),
activeSerialPort.getReceiveFramingByte());
} catch (Exception ef) {
log.debug("failed to set serial framing: ", ef);
}
// set timeout; framing should work before this anyway
try {
activeSerialPort.enableReceiveTimeout(10);
log.debug("Serial timeout was observed as: {} {}", activeSerialPort.getReceiveTimeout(),
activeSerialPort.isReceiveTimeoutEnabled());
} catch (Exception et) {
log.info("failed to set serial timeout: ", et);
}
// get and save stream
serialStream = activeSerialPort.getInputStream();
// purge contents, if any
purgeStream(serialStream);
// report status?
if (log.isInfoEnabled()) {
// report now
log.info(portName + " port opened at "
+ activeSerialPort.getBaudRate() + " baud with"
+ " DTR: " + activeSerialPort.isDTR()
+ " RTS: " + activeSerialPort.isRTS()
+ " DSR: " + activeSerialPort.isDSR()
+ " CTS: " + activeSerialPort.isCTS()
+ " CD: " + activeSerialPort.isCD()
);
}
if (log.isDebugEnabled()) {
// report additional status
log.debug(" port flow control shows {}", // NOI18N
(activeSerialPort.getFlowControlMode() == SerialPort.FLOWCONTROL_RTSCTS_OUT ? "hardware flow control" : "no flow control")); // NOI18N
// log events
setPortEventLogging(activeSerialPort);
}
opened = true;
} catch (NoSuchPortException p) {
return handlePortNotFound(p, portName, log);
} catch (IOException ex) {
log.error("Unexpected exception while opening port {}", portName, ex);
return "Unexpected error while opening port " + portName + ": " + ex;
}
return null; // normal operation
}
/**
* Set up all of the other objects to operate connected to this port.
*/
@Override
public void configure() {
SerialTrafficController tc = null;
// create a insteon2412s port controller
//adaptermemo = new jmri.jmrix.powerline.cm11.SpecificSystemConnectionMemo();
tc = new SpecificTrafficController(this.getSystemConnectionMemo());
// connect to the traffic controller
this.getSystemConnectionMemo().setTrafficController(tc);
tc.setAdapterMemo(this.getSystemConnectionMemo());
this.getSystemConnectionMemo().configureManagers();
tc.connectPort(this);
// Configure the form of serial address validation for this connection
this.getSystemConnectionMemo().setSerialAddress(new jmri.jmrix.powerline.SerialAddress(this.getSystemConnectionMemo()));
}
/**
* {@inheritDoc}
*/
@Override
public String[] validBaudRates() {
return Arrays.copyOf(validSpeeds, validSpeeds.length);
}
/**
* {@inheritDoc}
*/
@Override
public int[] validBaudNumbers() {
return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
}
// base class methods for the SerialPortController interface
@Override
public DataInputStream getInputStream() {
if (!opened) {
log.error("getInputStream called before load(), stream not available");
return null;
}
return new DataInputStream(serialStream);
}
@Override
public DataOutputStream getOutputStream() {
if (!opened) {
log.error("getOutputStream called before load(), stream not available");
}
try {
return new DataOutputStream(activeSerialPort.getOutputStream());
} catch (java.io.IOException e) {
log.error("getOutputStream exception: {}", e.getMessage());
}
return null;
}
@Override
public boolean status() {
return opened;
}
// private control members
private boolean opened = false;
InputStream serialStream = null;
/**
* Local method to do specific port configuration.
*
* @throws purejavacomm.UnsupportedCommOperationException for invalid connection/settings
*/
protected void setSerialPort() throws UnsupportedCommOperationException {
// find the baud rate value, configure comm options
int baud = 19200; // default, but also defaulted in the initial value of selectedSpeed
activeSerialPort.setSerialPortParams(baud, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// find and configure flow control
int flow = SerialPort.FLOWCONTROL_NONE; // default
configureLeadsAndFlowControl(activeSerialPort, flow);
}
protected String[] validSpeeds = new String[]{Bundle.getMessage("BaudAutomatic")};
protected int[] validSpeedValues = new int[]{19200};
protected String selectedSpeed = validSpeeds[0];
private final static Logger log = LoggerFactory.getLogger(SpecificDriverAdapter.class);
}