|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Paulo Borges |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package com.openbravo.pos.scale; |
| 18 | + |
| 19 | +import gnu.io.CommPortIdentifier; |
| 20 | +import gnu.io.NoSuchPortException; |
| 21 | +import gnu.io.PortInUseException; |
| 22 | +import gnu.io.SerialPort; |
| 23 | +import gnu.io.SerialPortEvent; |
| 24 | +import gnu.io.SerialPortEventListener; |
| 25 | +import gnu.io.UnsupportedCommOperationException; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.io.OutputStream; |
| 29 | +import java.util.TooManyListenersException; |
| 30 | +import java.util.logging.Level; |
| 31 | +import java.util.logging.Logger; |
| 32 | + |
| 33 | +/** |
| 34 | + * |
| 35 | + * @author poolb |
| 36 | + */ |
| 37 | +public abstract class AbstractSerialScale implements Scale, SerialPortEventListener { |
| 38 | + |
| 39 | + protected final static Logger LOGGER = Logger.getLogger(AbstractSerialScale.class.getName()); |
| 40 | + |
| 41 | + private CommPortIdentifier m_PortIdPrinter; |
| 42 | + private SerialPort m_CommPortPrinter; |
| 43 | + |
| 44 | + private final String m_sPortScale; |
| 45 | + protected OutputStream m_out; |
| 46 | + protected InputStream m_in; |
| 47 | + |
| 48 | + public AbstractSerialScale(String sPortPrinter) { |
| 49 | + m_sPortScale = sPortPrinter; |
| 50 | + m_out = null; |
| 51 | + m_in = null; |
| 52 | + } |
| 53 | + |
| 54 | + private void open() throws ScaleException { |
| 55 | + |
| 56 | + try { |
| 57 | + m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPortScale); // Get serial port ID |
| 58 | + m_CommPortPrinter = (SerialPort) m_PortIdPrinter.open("PORTID", 2000); // Open Serial |
| 59 | + |
| 60 | + m_out = m_CommPortPrinter.getOutputStream(); |
| 61 | + m_in = m_CommPortPrinter.getInputStream(); |
| 62 | + |
| 63 | + m_CommPortPrinter.addEventListener(this); |
| 64 | + m_CommPortPrinter.notifyOnDataAvailable(true); |
| 65 | + |
| 66 | + SerialPortParams serialPortParams = getSerialPortParams(); |
| 67 | + |
| 68 | + m_CommPortPrinter.setSerialPortParams(serialPortParams.getBaudRate(), |
| 69 | + serialPortParams.getDataBits(), |
| 70 | + serialPortParams.getStopBits(), |
| 71 | + serialPortParams.getParity()); // Configuramos el puerto |
| 72 | + } catch (NoSuchPortException | TooManyListenersException | UnsupportedCommOperationException | PortInUseException | IOException ex) { |
| 73 | + LOGGER.log(Level.SEVERE, "Exception open serial port: " + this.m_sPortScale, ex); |
| 74 | + throw new ScaleException("Exception open serial port: " + this.m_sPortScale, ex); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void close() { |
| 79 | + if (m_in != null) { |
| 80 | + try { |
| 81 | + m_in.close(); |
| 82 | + } catch (IOException ex) { |
| 83 | + // |
| 84 | + } |
| 85 | + m_in = null; |
| 86 | + } |
| 87 | + |
| 88 | + if (m_out != null) { |
| 89 | + try { |
| 90 | + m_out.close(); |
| 91 | + } catch (IOException ex) { |
| 92 | + // |
| 93 | + } |
| 94 | + m_out = null; |
| 95 | + } |
| 96 | + |
| 97 | + if (m_CommPortPrinter != null) { |
| 98 | + m_CommPortPrinter.close(); |
| 99 | + m_CommPortPrinter = null; |
| 100 | + } |
| 101 | + |
| 102 | + if (m_PortIdPrinter != null) { |
| 103 | + m_PortIdPrinter = null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + protected void requireConnection() throws ScaleException { |
| 108 | + if (m_in == null) { |
| 109 | + close(); |
| 110 | + open(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + protected void flush() { |
| 115 | + try { |
| 116 | + if (this.m_out != null) { |
| 117 | + this.m_out.flush(); |
| 118 | + } |
| 119 | + } catch (IOException e) { |
| 120 | + LOGGER.log(Level.SEVERE, "Exception while flush: ", e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected String getSerialPort() { |
| 125 | + return this.m_sPortScale; |
| 126 | + } |
| 127 | + |
| 128 | + protected void write(byte[] data) throws ScaleException { |
| 129 | + try { |
| 130 | + requireConnection(); |
| 131 | + m_out.write(data); |
| 132 | + } catch (IOException e) { |
| 133 | + LOGGER.log(Level.WARNING, null, e); |
| 134 | + throw new ScaleException("Cannot write to: " + this.m_sPortScale, e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected void waitFor(int timeToWait) { |
| 139 | + try { |
| 140 | + wait(timeToWait); |
| 141 | + } catch (InterruptedException ex) { |
| 142 | + LOGGER.log(Level.SEVERE, "Exception calling wait: ", ex); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public abstract Double readWeight() throws ScaleException; |
| 148 | + |
| 149 | + @Override |
| 150 | + public abstract void serialEvent(SerialPortEvent ev); |
| 151 | + |
| 152 | + protected abstract SerialPortParams getSerialPortParams(); |
| 153 | + |
| 154 | + public static class SerialPortParams { |
| 155 | + |
| 156 | + private int baudRate; |
| 157 | + private int dataBits; |
| 158 | + private int stopBits; |
| 159 | + private int parity; |
| 160 | + |
| 161 | + public SerialPortParams(int baudRate, int dataBits, int stopBits, int parity) { |
| 162 | + this.baudRate = baudRate; |
| 163 | + this.dataBits = dataBits; |
| 164 | + this.stopBits = stopBits; |
| 165 | + this.parity = parity; |
| 166 | + } |
| 167 | + |
| 168 | + public int getBaudRate() { |
| 169 | + return baudRate; |
| 170 | + } |
| 171 | + |
| 172 | + public void setBaudRate(int baudRate) { |
| 173 | + this.baudRate = baudRate; |
| 174 | + } |
| 175 | + |
| 176 | + public int getDataBits() { |
| 177 | + return dataBits; |
| 178 | + } |
| 179 | + |
| 180 | + public void setDataBits(int dataBits) { |
| 181 | + this.dataBits = dataBits; |
| 182 | + } |
| 183 | + |
| 184 | + public int getStopBits() { |
| 185 | + return stopBits; |
| 186 | + } |
| 187 | + |
| 188 | + public void setStopBits(int stopBits) { |
| 189 | + this.stopBits = stopBits; |
| 190 | + } |
| 191 | + |
| 192 | + public int getParity() { |
| 193 | + return parity; |
| 194 | + } |
| 195 | + |
| 196 | + public void setParity(int parity) { |
| 197 | + this.parity = parity; |
| 198 | + } |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments