📄 serialservice.java
字号:
package com.dalsemi.onewire.adapter;import java.io.*;import java.util.*;import javax.comm.*;//import javax.comm.*;public class SerialService implements SerialPortEventListener{ private static final boolean DEBUG = false; /** The serial port name of this object (e.g. COM1, /dev/ttyS0) */ private final String comPortName; /** The serial port object for setting serial port parameters */ private SerialPort serialPort = null; /** The input stream, for reading data from the serial port */ private InputStream serialInputStream = null; /** The output stream, for writing data to the serial port */ private OutputStream serialOutputStream = null; /** The hash code of the thread that currently owns this serial port */ private int currentThreadHash = 0; /** temporary array, used for converting characters to bytes */ private byte[] tempArray = new byte[128]; /** used to end the Object.wait loop in readWithTimeout method */ private transient boolean dataAvailable = false; /** Vector of thread hash codes that have done an open but no close */ private final Vector users = new Vector(4); /** Flag to indicate byte banging on read */ private final boolean byteBang; /** Vector of serial port ID strings (i.e. "COM1", "COM2", etc) */ private static final Vector vPortIDs = new Vector(2); /** static list of threadIDs to the services they are using */ private static Hashtable knownServices = new Hashtable(); /** static list of all unique SerialService classes */ private static Hashtable uniqueServices = new Hashtable(); /** * Cleans up the resources used by the thread argument. If another * thread starts communicating with this port, and then goes away, * there is no way to relinquish the port without stopping the * process. This method allows other threads to clean up. * * @param thread that may have used a <code>USerialAdapter</code> */ public static void CleanUpByThread(Thread t) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.CleanUpByThread(Thread)"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// try { SerialService temp = (SerialService) knownServices.get(t); if (temp==null) return; synchronized(temp) { if (t.hashCode() == temp.currentThreadHash) { //then we need to release the lock... temp.currentThreadHash = 0; } } temp.closePortByThreadID(t); } catch(Exception e) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("Exception cleaning: "+e.toString()); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } } /** * do not use default constructor * user getSerialService(String) instead. */ private SerialService() { this.comPortName = null; this.byteBang = false; } /** * this constructor only for use in the static method: * getSerialService(String) */ protected SerialService(String strComPort) { this.comPortName = strComPort; // check to see if need to byte-bang the reads String prop = com.dalsemi.onewire.OneWireAccessProvider.getProperty( "onewire.serial.bytebangread"); if (prop != null) { if (prop.indexOf("true") != -1) byteBang = true; else byteBang = false; } else { byteBang = false; } } public static SerialService getSerialService(String strComPort) { synchronized(uniqueServices) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println( "SerialService.getSerialService called: strComPort=" + strComPort); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// String strLowerCaseComPort = strComPort.toLowerCase(); Object o = uniqueServices.get(strLowerCaseComPort); if(o!=null) { return (SerialService)o; } else { SerialService sps = new SerialService(strComPort); uniqueServices.put(strLowerCaseComPort, sps); return sps; } } } /** * SerialPortEventListener method. This just calls the notify * method on this object, so that all blocking methods are kicked * awake whenever a serialEvent occurs. */ public void serialEvent(SerialPortEvent spe) { dataAvailable = true; if(DEBUG) { switch(spe.getEventType()) { case SerialPortEvent.BI: System.out.println("SerialPortEvent: Break interrupt."); break; case SerialPortEvent.CD: System.out.println("SerialPortEvent: Carrier detect."); break; case SerialPortEvent.CTS: System.out.println("SerialPortEvent: Clear to send."); break; case SerialPortEvent.DATA_AVAILABLE: System.out.println("SerialPortEvent: Data available at the serial port."); break; case SerialPortEvent.DSR: System.out.println("SerialPortEvent: Data set ready."); break; case SerialPortEvent.FE: System.out.println("SerialPortEvent: Framing error."); break; case SerialPortEvent.OE: System.out.println("SerialPortEvent: Overrun error."); break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println("SerialPortEvent: Output buffer is empty."); break; case SerialPortEvent.PE: System.out.println("SerialPortEvent: Parity error."); break; case SerialPortEvent.RI: System.out.println("SerialPortEvent: Ring indicator."); break; } System.out.println("SerialService.SerialEvent: oldValue=" + spe.getOldValue()); System.out.println("SerialService.SerialEvent: newValue=" + spe.getNewValue()); } //try //{ // serialInputStream.notifyAll(); //} //catch(Exception e) //{ // e.printStackTrace(); //} } public synchronized void openPort() throws IOException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.openPort() called"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// openPort(null); } public synchronized void openPort(SerialPortEventListener spel) throws IOException { // record this thread as an owner if (users.indexOf(Thread.currentThread()) == -1) users.addElement(Thread.currentThread()); if(isPortOpen()) return; CommPortIdentifier port_id; try { port_id = CommPortIdentifier.getPortIdentifier(comPortName); } catch(NoSuchPortException nspe) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.openPort: No such port (" + comPortName + "). " + nspe); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// throw new IOException("No such port (" + comPortName + "). " + nspe); } // check if the port is currently used if (port_id.isCurrentlyOwned()) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.openPort: Port In Use (" + comPortName + ")"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// throw new IOException("Port In Use (" + comPortName + ")"); } // try to aquire the port try { // get the port object serialPort = (SerialPort)port_id.open("Dallas Semiconductor", 2000); //serialPort.setInputBufferSize(4096); //serialPort.setOutputBufferSize(4096); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("SerialService.openPort: getInputBufferSize = " + serialPort.getInputBufferSize()); System.out.println("SerialService.openPort: getOutputBufferSize = " + serialPort.getOutputBufferSize()); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(spel!=null) serialPort.addEventListener(spel); else serialPort.addEventListener(this); serialPort.notifyOnOutputEmpty(true); serialPort.notifyOnDataAvailable(true); // flow i/o serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); serialInputStream = serialPort.getInputStream(); serialOutputStream = serialPort.getOutputStream(); // bug workaround serialOutputStream.write(0); // settings serialPort.disableReceiveFraming(); serialPort.disableReceiveThreshold(); serialPort.enableReceiveTimeout(1); // set baud rate serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort.setDTR(true); serialPort.setRTS(true); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println( "SerialService.openPort: Port Openend (" + comPortName + ")"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } catch(Exception e) { // close the port if we have an object if (serialPort != null) serialPort.close(); serialPort = null; throw new IOException( "Could not open port (" + comPortName + ") :" + e); } } public synchronized void setNotifyOnDataAvailable(boolean notify) { serialPort.notifyOnDataAvailable(notify); } public static Enumeration getSerialPortIdentifiers() { synchronized(vPortIDs) { if(vPortIDs.size()==0) { Enumeration e = CommPortIdentifier.getPortIdentifiers(); while(e.hasMoreElements()) { CommPortIdentifier portID = (CommPortIdentifier)e.nextElement(); if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL) vPortIDs.addElement(portID.getName()); } } return vPortIDs.elements(); } } public synchronized String getPortName() { return comPortName; } public synchronized boolean isPortOpen() { return serialPort!=null; } public synchronized boolean isDTR() { return serialPort.isDTR(); } public synchronized void setDTR(boolean newDTR) { serialPort.setDTR(newDTR); } public synchronized boolean isRTS() { return serialPort.isRTS(); } public synchronized void setRTS(boolean newRTS) { serialPort.setRTS(newRTS); } /** * Send a break on this serial port * * @param duration - break duration in ms */ public synchronized void sendBreak (int duration) { serialPort.sendBreak(duration); } public synchronized int getBaudRate() { return serialPort.getBaudRate(); } public synchronized void setBaudRate(int baudRate) throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); try { // set baud rate serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.setBaudRate: baudRate=" + baudRate); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } catch(UnsupportedCommOperationException uncoe) { throw new IOException("Failed to set baud rate: " + uncoe); } } /** * Close this serial port. * * @throws IOException - if port is in use by another application */ public synchronized void closePort() { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.closePort"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// closePortByThreadID(Thread.currentThread()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -