Skip to content

Commit 18ae0b8

Browse files
committed
Hardware: Improve scale implementation, use Common class AbstractScale
1 parent 7397683 commit 18ae0b8

File tree

9 files changed

+560
-640
lines changed

9 files changed

+560
-640
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
}

kriolos-opos-hardware/src/main/java/com/openbravo/pos/scale/DeviceScale.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public class DeviceScale {
3030

3131
private Scale m_scale;
3232

33-
/** Creates a new instance of DeviceScale
33+
/**
34+
*
3435
* @param parent
35-
* @param props */
36+
* @param props
37+
*/
3638
public DeviceScale(Component parent, AppProperties props) {
3739
StringParser sd = new StringParser(props.getProperty("machine.scale"));
3840
String sScaleType = sd.nextToken(':');

kriolos-opos-hardware/src/main/java/com/openbravo/pos/scale/ScaleAcomPC100.java

+22-73
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,13 @@
1818

1919
import gnu.io.*;
2020
import java.io.*;
21-
import java.util.TooManyListenersException;
2221
import java.util.logging.Level;
23-
import java.util.logging.Logger;
2422

2523
/**
2624
*
2725
* @author uniCenta + H Singh
2826
*/
29-
public class ScaleAcomPC100 implements Scale, SerialPortEventListener {
30-
31-
private final static Logger LOGGER = Logger.getLogger(ScaleAcomPC100.class.getName());
32-
private CommPortIdentifier m_PortIdPrinter;
33-
private SerialPort m_CommPortPrinter;
34-
35-
private final String m_sPortScale;
36-
private OutputStream m_out;
37-
private InputStream m_in;
27+
public class ScaleAcomPC100 extends AbstractSerialScale implements Scale, SerialPortEventListener {
3828

3929
private static final int SCALE_READY = 0;
4030
private static final int SCALE_READING = 1;
@@ -51,9 +41,7 @@ public class ScaleAcomPC100 implements Scale, SerialPortEventListener {
5141
* @param sPortPrinter
5242
*/
5343
public ScaleAcomPC100(String sPortPrinter) {
54-
m_sPortScale = sPortPrinter;
55-
m_out = null;
56-
m_in = null;
44+
super(sPortPrinter);
5745
m_iStatusScale = SCALE_READY;
5846
m_dWeightBuffer = 0.0;
5947
m_dWeightDecimals = 1.0;
@@ -64,31 +52,20 @@ public ScaleAcomPC100(String sPortPrinter) {
6452
* @return
6553
*/
6654
@Override
67-
public Double readWeight() {
55+
public Double readWeight() throws ScaleException {
6856

6957
synchronized (this) {
7058

7159
if (m_iStatusScale != SCALE_READY) {
72-
try {
73-
wait(200);
74-
} catch (InterruptedException e) {
75-
LOGGER.log(Level.WARNING, null, e);
76-
}
77-
if (m_iStatusScale != SCALE_READY) {
78-
m_iStatusScale = SCALE_READY;
79-
}
60+
waitFor(200);
8061
}
8162

8263
m_dWeightBuffer = 0.0;
8364
m_dWeightDecimals = 1.0;
8465
write(new byte[]{0x57, 0X0D}); // $
8566
flush();
8667

87-
try {
88-
wait(200);
89-
} catch (InterruptedException e) {
90-
LOGGER.log(Level.WARNING, null, e);
91-
}
68+
waitFor(200);
9269

9370
if (m_iStatusScale == SCALE_READY) {
9471
double dWeight = m_dWeightBuffer / m_dWeightDecimals;
@@ -104,38 +81,6 @@ public Double readWeight() {
10481
}
10582
}
10683

107-
private void flush() {
108-
try {
109-
m_out.flush();
110-
} catch (IOException e) {
111-
LOGGER.log(Level.WARNING, null, e);
112-
}
113-
}
114-
115-
private void write(byte[] data) {
116-
try {
117-
if (m_out == null) {
118-
m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPortScale);
119-
m_CommPortPrinter = (SerialPort) m_PortIdPrinter.open("PORTID", 2000);
120-
121-
m_out = m_CommPortPrinter.getOutputStream();
122-
m_in = m_CommPortPrinter.getInputStream();
123-
124-
m_CommPortPrinter.addEventListener(this);
125-
m_CommPortPrinter.notifyOnDataAvailable(true);
126-
127-
m_CommPortPrinter.setSerialPortParams(9600,
128-
SerialPort.DATABITS_7,
129-
SerialPort.STOPBITS_1,
130-
SerialPort.PARITY_EVEN);
131-
}
132-
m_out.write(data);
133-
} catch (NoSuchPortException | PortInUseException | UnsupportedCommOperationException
134-
| TooManyListenersException | IOException e) {
135-
LOGGER.log(Level.WARNING, null, e);
136-
}
137-
}
138-
13984
/**
14085
*
14186
* @param e
@@ -165,25 +110,29 @@ public void serialEvent(SerialPortEvent e) {
165110

166111
m_sScaleReading = m_sScaleReading + new String(readBuffer);
167112

168-
try {
169-
int start = m_sScaleReading.indexOf((char) 10);
170-
int end = m_sScaleReading.indexOf((char) 3);
113+
int start = m_sScaleReading.indexOf((char) 10);
114+
int end = m_sScaleReading.indexOf((char) 3);
171115

172-
if (start >= 0 && end >= 0) {
116+
if (start >= 0 && end >= 0) {
173117

174-
start = m_sScaleReading.indexOf((char) 10);
175-
end = m_sScaleReading.indexOf((char) 75);
176-
m_dWeightBuffer = Double.parseDouble(m_sScaleReading.substring(start + 1, end));
177-
m_sScaleReading = "";
178-
}
179-
} catch (IndexOutOfBoundsException ex) {
180-
LOGGER.log(Level.WARNING, "IndexOutOfBoundsException, message not complete yet.", ex);
118+
start = m_sScaleReading.indexOf((char) 10);
119+
end = m_sScaleReading.indexOf((char) 75);
120+
m_dWeightBuffer = Double.parseDouble(m_sScaleReading.substring(start + 1, end));
121+
m_sScaleReading = "";
181122
}
182123

183-
} catch (IOException eIO) {
184-
LOGGER.log(Level.WARNING, "IOException", eIO);
124+
} catch (IOException | IndexOutOfBoundsException ex) {
125+
LOGGER.log(Level.WARNING, "Exception on serialEvent", ex);
185126
}
186127
break;
187128
}
188129
}
130+
131+
@Override
132+
protected AbstractSerialScale.SerialPortParams getSerialPortParams() {
133+
return new AbstractSerialScale.SerialPortParams(9600,
134+
SerialPort.DATABITS_7,
135+
SerialPort.STOPBITS_1,
136+
SerialPort.PARITY_EVEN);
137+
}
189138
}

0 commit comments

Comments
 (0)