View Javadoc
1   package net.sf.mbus4j;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
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   * @author aploese
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     // Serialization for saveDataSource
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         //dataBits = 
140         in.readInt();
141         //flowControl = 
142         in.readInt();
143         //stopBits = 
144         in.readInt();
145         //parity = 
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      * @return the portName
157      */
158     public String getPortName() {
159         return portName;
160     }
161 
162     /**
163      * @param portName the portName to set
164      */
165     public void setPortName(String portName) {
166         this.portName = portName;
167     }
168 
169     /**
170      * @return the dataBits
171      */
172     public DataBits getDataBits() {
173         return DATA_BITS;
174     }
175 
176     /**
177      * @return the flowControl
178      */
179     public Set<FlowControl> getFlowControl() {
180         return FLOW_CONTROL;
181     }
182 
183     /**
184      * @return the stopBits
185      */
186     public StopBits getStopBits() {
187         return STOP_BITS;
188     }
189 
190     /**
191      * @return the parity
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 }