📄 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/>. *//* * $Log: V24.java,v $ * Revision 1.9 2001/05/11 00:04:40 apr * Workaround - we don't check for DSR on, * isDSR fails on several installations (see RXTX). * * Revision 1.8 2000/11/02 12:09:17 apr * Added license to every source file * * Revision 1.7 2000/04/26 12:33:09 apr * javadoc warnings ... * * Revision 1.6 2000/04/16 23:53:03 apr * LogProducer renamed to LogSource * * Revision 1.5 2000/03/20 19:24:13 apr * Testing ISOGetty ... minor bugfixes/timings in answer()/hangup()/reset() * * Revision 1.4 2000/03/14 00:01:12 apr * isConnected(): remove debugging code * * Revision 1.3 2000/03/01 14:44:45 apr * Changed package name to org.jpos * * Revision 1.2 2000/01/17 18:26:06 apr * Supervise every 100 executions * * Revision 1.1 2000/01/11 01:25:02 apr * moved non ISO-8583 related classes from jpos.iso to jpos.util package * (AntiHog LeasedLineModem LogEvent LogListener LogSource * Loggeable Logger Modem RotateLogListener SimpleAntiHog SimpleDialupModem * SimpleLogListener SimpleLogSource SystemMonitor V24) * * Revision 1.4 1999/12/17 14:58:30 apr * RXTX dataavailable workaround * * Revision 1.3 1999/12/15 16:07:36 apr * Protection against negative timeouts on readUntil * * Revision 1.2 1999/12/15 15:09:07 apr * debugging to readUntil ... * * Revision 1.1 1999/11/24 18:10:49 apr * VISA 1 support helper class * */package org.jpos.util;import java.io.*;import java.util.*;import javax.comm.*;import org.jpos.iso.ISOUtil;/** * handy Serial port functions * * @author apr@cs.com.uy * @version $Id: V24.java,v 1.9 2001/05/11 00:04:40 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); } 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() ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -