📄 v24.java
字号:
/* * Copyright (c) 2000 jPOS.org. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the jPOS project * (http://www.jpos.org/)". Alternately, this acknowledgment may * appear in the software itself, if and wherever such third-party * acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse * or promote products derived from this software without prior * written permission. For written permission, please contact * license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", * nor may "jPOS" appear in their name, without prior written * permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project. For more * information please see <http://www.jpos.org/>. */package org.jpos.util;import javax.comm.CommPortIdentifier;import javax.comm.PortInUseException;import javax.comm.SerialPort;import javax.comm.SerialPortEvent;import javax.comm.SerialPortEventListener;import javax.comm.UnsupportedCommOperationException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.TooManyListenersException;import org.jpos.iso.ISOUtil;/** * handy Serial port functions * * @author apr@cs.com.uy * @version $Id: V24.java,v 1.14 2003/10/13 11:04:20 apr Exp $ * @see <a href="http://www.frii.com/~jarvi/rxtx/">CommAPI</a> */public class V24 implements SerialPortEventListener, LogSource { Logger logger; String realm; InputStream is; OutputStream os; SerialPort port; CommPortIdentifier portId; long lostCD; boolean watchCD, autoFlushRX; public static final int MAX_STRING_SIZE = 64*1024; /** * @param portId * @param logger * @param realm * @throws IOException * @throws PortInUseException */ public V24 (CommPortIdentifier portId, Logger logger, String realm) throws IOException, PortInUseException { super(); setLogger (logger, realm); this.portId = portId; this.autoFlushRX = false; initPort(); } /** * @param portName (i.e. /dev/ttyS1) * @param logger * @param realm * @throws IOException * @throws PortInUseException */ public V24 (String portName, Logger logger, String realm) throws IOException, PortInUseException { super(); setLogger (logger, realm); portId = getPortIdentifier(portName); initPort(); } /** * removeEventListener, close Port */ public void close() { port.removeEventListener(); port.setDTR (false); port.close(); } /** * port is opened by default within V24 constructors<br> * the close()/open() scheme is used to work around some * extrange behaveour on some CommAPI implementations */ public void open() throws IOException, PortInUseException { initPort(); } /** * @return already opened port */ public SerialPort getSerialPort() { return port; } /** * @param watchCD true to monitor */ public void setWatchCD (boolean watchCD) { this.watchCD = watchCD; } /** * @return OutputStream associated with this port */ public OutputStream getOutputStream() { return os; } /** * @return InputStream associated with this port */ public InputStream getInputStream() { return is; } /** * @param ev SerialPortEvent * SerialPortEventListener requirements */ public void serialEvent (SerialPortEvent ev) { int evType = ev.getEventType(); switch (evType) { case SerialPortEvent.DSR: if (!ev.getNewValue()) synchronized (this) { this.notify(); } break; case SerialPortEvent.CD: checkCD (ev.getNewValue()); if (!ev.getNewValue()) synchronized (this) { this.notify(); } break; case SerialPortEvent.DATA_AVAILABLE: if (autoFlushRX) try { flushAndLog(); } catch (IOException e) { } else { synchronized (this) { this.notify(); } } break; } Thread.yield(); } /** * @param baud Baud Rate * @param data number of Data bits * @param stop number of Stop bits * @param flow Flow Control mask * @see javax.comm.SerialPort * @throws UnsupportedCommOperationException */ public synchronized void setSpeed (int baud, int data, int stop, int parity, int flow) throws UnsupportedCommOperationException { port.setSerialPortParams(baud, data, stop, parity); port.setFlowControlMode (flow); } /** * @param logger current logger * @param realm logger realm * @see LogSource */ public void setLogger (Logger logger, String realm) { this.logger = logger; this.realm = realm; } /** * @return current log realm * @see LogSource */ public String getRealm () { return realm; } /** * @return current Logger */ public Logger getLogger() { return logger; } private String elapsed (long start) { long el = System.currentTimeMillis() - start; if (el < 5000) return el + "ms"; else return (el/1000) + "s"; } private void checkCD(boolean on) { if (!on) { lostCD = System.currentTimeMillis(); Logger.log ( new LogEvent (this,"badnews",portId.getName()+" lost CD ") ); } else { if (lostCD != 0) { Logger.log ( new LogEvent (this, "goodnews", portId.getName() + " recovered CD after " + elapsed (lostCD)) ); } lostCD = 0; synchronized (this) { this.notify(); } } } /** * @return connection status */ public boolean isConnected() { // return port.isDSR() && port.isCD(); return port.isCD(); // return port.isDSR(); } /** * flush receiver and dump discarded characters thru Logger * @throws IOException */ public void flushAndLog () throws IOException { StringBuffer buf = new StringBuffer(); while (is.available() > 0) { while (is.available() > 0) { char c = (char) is.read(); if (buf.length() < 1000) // paranoia check - ignore garbage buf.append (c); else throw new IOException ("excessive garbage"); } try { // avoid multiple log events. Wait for possibly // more characters to arrive Thread.sleep (250); } catch (InterruptedException e) { } } Logger.log ( new LogEvent (this, "flush", ISOUtil.dumpString( buf.toString().getBytes() ) ) ); } /** * flush receiver * @throws IOException */ public void flushReceiver () throws IOException { while (is.available() > 0) is.read(); } /** * @param b content to be sent (do not perform flush after sending) * @throws IOException */ public synchronized void send (byte[] b) throws IOException { os.write (b);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -