/** * * NORD POS is a fork of Openbravo POS. * * Copyright (C) 2009-2013 Nord Trading Ltd. <http://www.nordpos.com> * * This file is part of NORD POS. * * NORD 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. * * NORD 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 * NORD POS. If not, see <http://www.gnu.org/licenses/>. */ package com.nordpos.device.writter; import gnu.io.*; // RXTX comm library import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Andrey Svininykh <svininykh@gmail.com> * @version NORD POS 3.0 */ public class WritterRXTX extends Writter { private static final Logger logger = Logger.getLogger(WritterRXTX.class.getName()); private CommPortIdentifier m_PortIdPrinter; private CommPort m_CommPortPrinter; private final String m_sPortPrinter; private final Integer m_iPortSpeed; private final Integer m_iPortBits; private final Integer m_iPortStopBits; private final Integer m_iPortParity; private OutputStream m_out; public WritterRXTX(String sPortPrinter, Integer iPortSpeed, Integer iPortBits, Integer iPortStopBits, Integer iPortParity) { m_sPortPrinter = sPortPrinter; m_iPortSpeed = iPortSpeed; m_iPortBits = iPortBits; m_iPortStopBits = iPortStopBits; m_iPortParity = iPortParity; m_out = null; } @Override protected void internalWrite(byte[] data) { try { if (m_out == null) { m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPortPrinter); // Tomamos el puerto m_CommPortPrinter = m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_SERIAL) { ((SerialPort) m_CommPortPrinter).setSerialPortParams(m_iPortSpeed, m_iPortBits, m_iPortStopBits, m_iPortParity); // Configuramos el puerto } } m_out.write(data); } catch (NoSuchPortException | PortInUseException | UnsupportedCommOperationException | IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); } } @Override protected void internalFlush() { try { if (m_out != null) { m_out.flush(); } } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); } } @Override protected void internalClose() { try { if (m_out != null) { m_out.flush(); m_out.close(); m_out = null; m_CommPortPrinter = null; m_PortIdPrinter = null; } } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); } } }