1 package net.sf.mbus4j;
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 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.util.Objects;
34 import java.util.Set;
35 import net.sf.atmodem4j.spsw.Baudrate;
36 import net.sf.atmodem4j.spsw.DataBits;
37 import net.sf.atmodem4j.spsw.FlowControl;
38 import net.sf.atmodem4j.spsw.logging.LoggingSerialPortSocket;
39 import net.sf.atmodem4j.spsw.Parity;
40 import net.sf.atmodem4j.spsw.SerialPortSocket;
41 import net.sf.atmodem4j.spsw.StopBits;
42 import net.sf.json.JSONObject;
43 import net.sf.mbus4j.json.JsonSerializeType;
44
45
46
47
48
49 public class SerialPortConnection extends Connection {
50
51 static final String SERIAL_CONNECTION = "serialConnection";
52
53 public static final int DEFAULT_RESPONSE_TIMEOUT_OFFSET = 50;
54 public static final Set<FlowControl> FLOW_CONTROL = FlowControl.getFC_NONE();
55 public static final DataBits DATA_BITS = DataBits.DB_8;
56 public static final StopBits STOP_BITS = StopBits.SB_1;
57 public static final Parity PARITY = Parity.EVEN;
58
59 private String portName;
60 private SerialPortSocket sPort;
61
62 public SerialPortConnection() {
63 super(DEFAULT_BAUDRATE, DEFAULT_RESPONSE_TIMEOUT_OFFSET);
64 }
65
66 public SerialPortConnection(String portName) {
67 super(DEFAULT_BAUDRATE, DEFAULT_RESPONSE_TIMEOUT_OFFSET);
68 this.portName = portName;
69 }
70
71 public SerialPortConnection(String portName, int bitPerSecond, int responseTimeoutOffset) {
72 super(bitPerSecond, responseTimeoutOffset);
73 this.portName = portName;
74 }
75
76 @Override
77 public void open() throws IOException {
78 try {
79 setConnState(State.OPENING);
80 sPort = SerialPortSocket.FACTORY.createSerialPortSocket(portName);
81 if (getLoggingStream() != null) {
82 sPort = new LoggingSerialPortSocket(sPort, getLoggingStream());
83 }
84 sPort.openRaw(Baudrate.fromValue(getBitPerSecond()), DATA_BITS, STOP_BITS, PARITY, FlowControl.getFC_NONE());
85 is = sPort.getInputStream();
86 os = sPort.getOutputStream();
87
88 setConnState(State.OPEN);
89 } catch (UnsatisfiedLinkError | NoClassDefFoundError ex) {
90 throw new IOException(ex);
91 }
92 }
93
94 @Override
95 public void close() throws IOException {
96 setConnState(State.CLOSING);
97 try {
98 sPort.close();
99 } finally {
100 setConnState(State.CLOSED);
101 }
102 }
103
104 @Override
105 public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
106 JSONObject result = super.toJSON(jsonSerializeType);
107 result.accumulate("serialPort", portName);
108 return result;
109 }
110
111 @Override
112 public void fromJSON(JSONObject json) {
113 super.fromJSON(json);
114 portName = json.getString("serialPort");
115 }
116 private static final long serialVersionUID = -1;
117 private static final int SERIAL_VERSION = 2;
118
119
120 private void writeObject(ObjectOutputStream out) throws IOException {
121 out.writeInt(SERIAL_VERSION);
122 out.writeUTF(portName);
123 }
124
125 private void readObject(ObjectInputStream in) throws IOException {
126 int ver = in.readInt();
127 switch (ver) {
128 case 1:
129 readObjectVer1(in);
130 break;
131 case 2:
132 readObjectVer2(in);
133 break;
134 }
135 }
136
137 private void readObjectVer1(ObjectInputStream in) throws IOException {
138 portName = in.readUTF();
139
140 in.readInt();
141
142 in.readInt();
143
144 in.readInt();
145
146 in.readInt();
147 in.readInt();
148 in.readInt();
149 }
150
151 private void readObjectVer2(ObjectInputStream in) throws IOException {
152 portName = in.readUTF();
153 }
154
155
156
157
158 public String getPortName() {
159 return portName;
160 }
161
162
163
164
165 public void setPortName(String portName) {
166 this.portName = portName;
167 }
168
169
170
171
172 public DataBits getDataBits() {
173 return DATA_BITS;
174 }
175
176
177
178
179 public Set<FlowControl> getFlowControl() {
180 return FLOW_CONTROL;
181 }
182
183
184
185
186 public StopBits getStopBits() {
187 return STOP_BITS;
188 }
189
190
191
192
193 public Parity getParity() {
194 return PARITY;
195 }
196
197 @Override
198 public String getJsonFieldName() {
199 return SERIAL_CONNECTION;
200 }
201
202 @Override
203 public String getName() {
204 return portName;
205 }
206
207 @Override
208 public int hashCode() {
209 int hash = 5;
210 hash = 59 * hash + Objects.hashCode(this.portName);
211 return hash;
212 }
213
214 @Override
215 public boolean equals(Object obj) {
216 if (this == obj) {
217 return true;
218 }
219 if (obj == null) {
220 return false;
221 }
222 if (getClass() != obj.getClass()) {
223 return false;
224 }
225 final SerialPortConnection other = (SerialPortConnection) obj;
226 if (!Objects.equals(this.portName, other.portName)) {
227 return false;
228 }
229 return true;
230 }
231
232 @Override
233 public String toString() {
234 return "SerialPortConnection{" + "portName=" + portName + '}';
235 }
236
237
238
239 }