📄 serialservice.java
字号:
public synchronized void flush() throws IOException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.flush"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(!isPortOpen()) throw new IOException("Port Not Open"); serialOutputStream.flush(); while(serialInputStream.available()>0) serialInputStream.read(); } // ------------------------------------------------------------------------ // BeginExclusive/EndExclusive Mutex Methods // ------------------------------------------------------------------------ /** * Gets exclusive use of the 1-Wire to communicate with an iButton or * 1-Wire Device. * This method should be used for critical sections of code where a * sequence of commands must not be interrupted by communication of * threads with other iButtons, and it is permissible to sustain * a delay in the special case that another thread has already been * granted exclusive access and this access has not yet been * relinquished. <p> * * @param blocking <code>true</code> if want to block waiting * for an excluse access to the adapter * @return <code>true</code> if blocking was false and a * exclusive session with the adapter was aquired * * @throws IOException */ public boolean beginExclusive (boolean blocking) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.beginExclusive(bool)"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if (blocking) { while (!beginExclusive()) { try{Thread.sleep(50);}catch(Exception e){} } return true; } else return beginExclusive(); } /** * Relinquishes exclusive control of the 1-Wire Network. * This command dynamically marks the end of a critical section and * should be used when exclusive control is no longer needed. */ public synchronized void endExclusive () { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.endExclusive"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // if own then release if (currentThreadHash == Thread.currentThread().hashCode()) { currentThreadHash = 0; } } /** * Check if this thread has exclusive control of the port. */ public synchronized boolean haveExclusive () { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.haveExclusive"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// return (currentThreadHash == Thread.currentThread().hashCode()); } /** * Gets exclusive use of the 1-Wire to communicate with an iButton or * 1-Wire Device. * This method should be used for critical sections of code where a * sequence of commands must not be interrupted by communication of * threads with other iButtons, and it is permissible to sustain * a delay in the special case that another thread has already been * granted exclusive access and this access has not yet been * relinquished. This is private and non blocking<p> * * @return <code>true</code> a exclusive session with the adapter was * aquired * * @throws IOException */ private synchronized boolean beginExclusive () { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.beginExclusive()"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if (currentThreadHash == 0) { // not owned so take currentThreadHash = Thread.currentThread().hashCode(); knownServices.put(Thread.currentThread(), this); return true; } else if (currentThreadHash == Thread.currentThread().hashCode()) { // already own return true; } else { // want port but don't own return false; } } /** * Allows clean up port by thread */ private synchronized void closePortByThreadID(Thread t) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.closePortByThreadID(Thread), Thread=" + t); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // remove this thread as an owner users.removeElement(t); // if this is the last owner then close the port if (users.isEmpty()) { // if don't own a port then just return if (!isPortOpen()) return; // close the port serialPort.close(); serialPort = null; serialInputStream = null; serialOutputStream = null; } } // ------------------------------------------------------------------------ // Standard InputStream methods // ------------------------------------------------------------------------ public synchronized int available() throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); return serialInputStream.available(); } public synchronized int read() throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); return serialInputStream.read(); } public synchronized int read(byte[] buffer) throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); return read(buffer, 0, buffer.length); } public synchronized int read(byte[] buffer, int offset, int length) throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); return serialInputStream.read(buffer, offset, length); } public synchronized int readWithTimeout(byte[] buffer, int offset, int length) throws IOException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.readWithTimeout(): length=" + length); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(!isPortOpen()) throw new IOException("Port Not Open"); // set max_timeout to be very long long max_timeout = System.currentTimeMillis() + length*20 + 800; int count = 0; // check which mode of reading if (byteBang) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.readWithTimeout(): byte-banging read"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// int new_byte; do { new_byte = serialInputStream.read(); if (new_byte != -1) { buffer[count+offset] = (byte)new_byte; count++; } else { // check for timeout if (System.currentTimeMillis() > max_timeout) break; // no bytes available yet so yield Thread.yield(); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if (DEBUG) System.out.print("y"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } } while (length > count); } else { do { int get_num = serialInputStream.available(); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// //if(DEBUG) // System.out.println("SerialService.readWithTimeout(): get_num=" + get_num + ", ms left=" + (max_timeout - System.currentTimeMillis())); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if (get_num > 0) { // check for block bigger then buffer if ((get_num + count) > length) get_num = length - count; // read the block count += serialInputStream.read(buffer, count+offset, get_num); } else { // check for timeout if (System.currentTimeMillis() > max_timeout ) length = 0; Thread.yield(); } } while (length > count); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("SerialService.readWithTimeout: read " + count + " bytes"); System.out.println("SerialService.readWithTimeout: " + com.dalsemi.onewire.utils.Convert.toHexString(buffer, offset, count)); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // return the number of characters found return count; } public synchronized char[] readWithTimeout(int length) throws IOException { byte[] buffer = new byte[length]; int count = readWithTimeout(buffer, 0, length); if (length != count) throw new IOException( "readWithTimeout, timeout waiting for return bytes (wanted " + length + ", got " + count + ")"); char[] returnBuffer = new char[length]; for(int i=0; i<length; i++) returnBuffer[i] = (char)(buffer[i] & 0x00FF); return returnBuffer; } // ------------------------------------------------------------------------ // Standard OutputStream methods // ------------------------------------------------------------------------ public synchronized void write(int data) throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("SerialService.write: write 1 byte"); System.out.println("SerialService.write: " + com.dalsemi.onewire.utils.Convert.toHexString((byte)data)); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// try { serialOutputStream.write(data); serialOutputStream.flush(); } catch (IOException e) { // drain IOExceptions that are 'Interrrupted' on Linux // convert the rest to IOExceptions if (!((System.getProperty("os.name").indexOf("Linux") != -1) && (e.toString().indexOf("Interrupted") != -1))) throw new IOException("write(char): " + e); } } public synchronized void write(byte[] data, int offset, int length) throws IOException { if(!isPortOpen()) throw new IOException("Port Not Open"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("SerialService.write: write " + length + " bytes"); System.out.println("SerialService.write: " + com.dalsemi.onewire.utils.Convert.toHexString(data, offset, length)); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// try { serialOutputStream.write(data, offset, length); serialOutputStream.flush(); } catch (IOException e) { // drain IOExceptions that are 'Interrrupted' on Linux // convert the rest to IOExceptions if (!((System.getProperty("os.name").indexOf("Linux") != -1) && (e.toString().indexOf("Interrupted") != -1))) throw new IOException("write(char): " + e); } } public synchronized void write(byte[] data) throws IOException { write(data, 0, data.length); } public synchronized void write(String data) throws IOException { byte[] dataBytes = data.getBytes(); write(dataBytes, 0, dataBytes.length); } public synchronized void write(char data) throws IOException { write((int)data); } public synchronized void write(char[] data) throws IOException { write(data, 0, data.length); } public synchronized void write(char[] data, int offset, int length) throws IOException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("SerialService.write: write " + length + " chars"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(length>tempArray.length) tempArray = new byte[length]; for(int i=0; i<length; i++) tempArray[i] = (byte)data[i]; write(tempArray, 0, length); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -