// Openbravo POS is a point of sales application designed for touch screens.
// Copyright (C) 2007-2009 Openbravo, S.L.
// http://www.openbravo.com/product/pos
//
// This file is part of Openbravo POS.
//
// Openbravo POS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Openbravo POS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Openbravo POS. If not, see <http://www.gnu.org/licenses/>.
package com.nordpos.device.scale;
import gnu.io.*;
import java.io.*;
import java.util.TooManyListenersException;
public class ScaleComm implements DeviceScale, SerialPortEventListener {
private String m_sPortScale;
private Integer m_iPortSpeed;
private Integer m_iPortBits;
private Integer m_iPortStopBits;
private Integer m_iPortParity;
private CommPortIdentifier m_PortIdPrinter;
private SerialPort m_CommPortPrinter;
private OutputStream m_out;
private InputStream m_in;
private static final int SCALE_READY = 0;
private static final int SCALE_READING = 1;
private double m_dWeightBuffer;
private int m_iStatusScale;
/** Creates a new instance of ScaleComm */
public ScaleComm(String sPortPrinter, Integer iPortSpeed, Integer iPortBits, Integer iPortStopBits, Integer iPortParity) {
m_sPortScale = sPortPrinter;
m_iPortSpeed = iPortSpeed;
m_iPortBits = iPortBits;
m_iPortStopBits = iPortStopBits;
m_iPortParity = iPortParity;
m_out = null;
m_in = null;
m_iStatusScale = SCALE_READY;
m_dWeightBuffer = 0.0;
}
public Double readWeight() {
synchronized(this) {
if (m_iStatusScale != SCALE_READY) {
try {
wait(1000);
} catch (InterruptedException e) {
}
if (m_iStatusScale != SCALE_READY) {
// bascula tonta.
m_iStatusScale = SCALE_READY;
}
}
// Ya estamos en SCALE_READY
m_dWeightBuffer = 0.0;
write(new byte[] {0x05});
flush();
// Esperamos un ratito
try {
wait(1000);
} catch (InterruptedException e) {
}
if (m_iStatusScale == SCALE_READY) {
// a value as been readed.
double dWeight = m_dWeightBuffer / 1000.0;
m_dWeightBuffer = 0.0;
return new Double(dWeight);
} else {
m_iStatusScale = SCALE_READY;
m_dWeightBuffer = 0.0;
return new Double(0.0);
}
}
}
private void flush() {
try {
m_out.flush();
} catch (IOException e) {
}
}
private void write(byte[] data) {
try {
if (m_out == null) {
m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPortScale); // Tomamos el puerto
m_CommPortPrinter = (SerialPort) m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto
m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura
m_in = m_CommPortPrinter.getInputStream();
m_CommPortPrinter.addEventListener(this);
m_CommPortPrinter.notifyOnDataAvailable(true);
// m_CommPortPrinter.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD); // Configuramos el puerto
m_CommPortPrinter.setSerialPortParams(m_iPortSpeed, m_iPortBits, m_iPortStopBits, m_iPortParity);
}
m_out.write(data);
} catch (NoSuchPortException e) {
e.printStackTrace();
} catch (PortInUseException e) {
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (TooManyListenersException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent e) {
// Determine type of event.
switch (e.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
while (m_in.available() > 0) {
int b = m_in.read();
if (b == 0x001E) { // RS ASCII
// Fin de lectura
synchronized (this) {
m_iStatusScale = SCALE_READY;
notifyAll();
}
} else if (b > 0x002F && b < 0x003A){
synchronized(this) {
if (m_iStatusScale == SCALE_READY) {
m_dWeightBuffer = 0.0; // se supone que esto debe estar ya garantizado
m_iStatusScale = SCALE_READING;
}
m_dWeightBuffer = m_dWeightBuffer * 10.0 + b - 0x0030;
}
} else {
// caracteres invalidos, reseteamos.
m_dWeightBuffer = 0.0; // se supone que esto debe estar ya garantizado
m_iStatusScale = SCALE_READY;
}
}
} catch (IOException eIO) {}
break;
}
}
}