📄 protocol.java
字号:
if (deviceName != null) { checkPermission(deviceName); /* 6231661: before checking if port is already open, check if no open Streams (ensureNoStreamsOpen). This is done to throw the correct exception: IOException when open Streams exist */ ensureNoStreamsOpen(); /* 6227981: before opening, check to see if the port is already opened" */ if (openedPorts.contains(deviceName)) { throw new IOException("Connection already open"); } handle = native_openByName(deviceName, baud, bbc|stop|parity|rts|cts); } else { checkPermission("comm:" + portNumber); handle = native_openByNumber(portNumber, baud, bbc|stop|parity|rts|cts); } /* 6227970: if open fails throw an IOException */ if (handle < 0) { throw new IOException("Could not open connection"); } openedPorts.addElement(deviceName); thisDeviceName = new String(deviceName); registerCleanup(); } /** * Gets the baudrate for the serial port connection. * @return the baudrate of the connection * @see #setBaudRate */ public int getBaudRate() { return baud; } /** * Sets the baudrate for the serial port connection. * If the requested <code>baudrate</code> is not supported * on the platform, then the system MAY use an alternate valid setting. * The alternate value can be accessed using the * <code>getBaudRate</code> method. * @param baudrate the baudrate for the connection * @return the previous baudrate of the connection * @see #getBaudRate */ public int setBaudRate(int baudrate) { int temp = baud; /* * If the baudrate is not supported, select one * that is allowed. */ if (baudrate < 299) { baudrate = 110; } else if (baudrate < 599) { baudrate = 300; } else if (baudrate < 1199) { baudrate = 600; } else if (baudrate < 2399) { baudrate = 1200; } else if (baudrate < 4799) { baudrate = 2400; } else if (baudrate < 9599) { baudrate = 4800; } else if (baudrate < 14399) { baudrate = 9600; } else if (baudrate < 19199) { baudrate = 14400; } else if (baudrate < 38399) { baudrate = 19200; } else if (baudrate < 55999) { baudrate = 38400; } else if (baudrate < 57599) { baudrate = 56000; } else if (baudrate < 115199) { baudrate = 57600; } else if (baudrate < 127999) { baudrate = 115200; } else if (baudrate < 255999) { baudrate = 128000; } else { baudrate = 256000; } try { /* Set the new baudrate. */ ///* native_configurePort(handle, baudrate, ///* bbc|stop|parity|rts|cts); native_configurePort(handle, baudrate, bbc|stop|parity|rts|cts); /* If successful, update the local baud variable. */ baud = baudrate; } catch (IOException ioe) { // NYI - could not set baudrate as requested. } return temp; } /** * Override close the GCF connection * * * @exception IOException if an I/O error occurs when closing the * connection. */ public void close() throws IOException { if ((thisDeviceName != null) && (openedPorts.contains(thisDeviceName))) { openedPorts.remove(thisDeviceName); } super.close(); } /** * Close the native serial port. * * @exception IOException if an I/O error occurs. */ protected void disconnect() throws IOException { try { ///* native_close(handle); native_close(handle); } finally { /* Reset handle to prevent resgistered cleanup close. */ handle = -1; } } /** * Reads up to <code>len</code> bytes of data from the input stream into * an array of bytes, blocks until at least one byte is available, * if blocking is turned on. * Sets the <code>eof</code> field of the connection when the native read * returns -1. * <p> * Polling the native code is done here to avoid the need for * asynchronous native methods to be written. Not all implementations * work this way (they block in the native code) but the same * Java code works for both. * * @param b the buffer into which the data is read * @param off the start offset in array <code>b</code> * at which the data is written * @param len the maximum number of bytes to read * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached * @exception IOException if an I/O error occurs */ protected int nonBufferedRead(byte b[], int off, int len) throws IOException { int bytesRead = 0; try { if (b == null) { int chunk = 256; b = new byte[chunk]; int end = off + len; int tmp = chunk; for (; off < end && tmp == chunk; off += chunk) { if (off + chunk > end) { chunk = end - off; } tmp = native_readBytes(handle, b, 0, chunk); if (tmp > 0) { bytesRead += tmp; } } if (tmp < 0) { eof = true; } } else { bytesRead = native_readBytes(handle, b, off, len); } } finally { if (iStreams == 0) { throw new InterruptedIOException("Stream closed"); } } if (bytesRead == -1) { eof = true; } /// GeneralBase.iowait(); return(bytesRead); } /** * Reads up to <code>len</code> bytes of data from the input stream into * an array of bytes, but does not block if no bytes available. * Sets the <code>eof</code> flag if the end of stream is reached. * <p> * This is implemented so the <code>available</code> method of * <code>BufferedConnectionBaseAdapter</code> will return more than * zero if the buffer is empty. * * @param b the buffer into which the data is read * @param off the start offset in array <code>b</code> * at which the data is written * @param len the maximum number of bytes to read * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached * @exception IOException if an I/O error occurs */ protected int readBytesNonBlocking(byte b[], int off, int len) throws IOException { int bytesRead; try { // the native read does not block ///* bytesRead = native_readBytes(handle, b, off, len); bytesRead = native_readBytes(handle, b, off, len); } finally { if (iStreams == 0) { throw new InterruptedIOException("Stream closed"); } } if (bytesRead == -1) { eof = true; } return bytesRead; } /** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this output stream. * <p> * Polling the native code is in the stream object handed out by * our parent helper class. This done to avoid the need for * asynchronous native methods to be written. Not all implementations * work this way (they block in the native code) but the same * Java code works for both. * * @param b the data * @param off the start offset in the data * @param len the number of bytes to write * * @return number of bytes written * @exception IOException if an I/O error occurs. In particular, * an <code>IOException</code> is thrown if the output * stream is closed. */ public int writeBytes(byte b[], int off, int len) throws IOException { ///* return native_writeBytes(handle, b, off, len); return native_writeBytes(handle, b, off, len); } /* * Real primitive methods */ /** * Open a serial port by logical number. * * @param port logical number of the port 0 being the first * @param baud baud rate to set the port at * @param flags options for the serial port * * @return handle to a native serial port * * @exception IOException if an I/O error occurs. */ private static native int native_openByNumber(int port, int baud, int flags) throws IOException; /** * Open a serial port by system dependent device name. * * @param name device name of the port * @param baud baud rate to set the port at * @param flags options for the serial port * * @return handle to a native serial port * * @exception IOException if an I/O error occurs. */ private static native int native_openByName(String name, int baud, int flags) throws IOException; /** * Configure a serial port optional parameters. * * @param port device port returned from open * @param baud baud rate to set the port at * @param flags options for the serial port * * @exception IOException if an I/O error occurs */ private static native void native_configurePort(int port, int baud, int flags) throws IOException; /** * Close a serial port. * * @param hPort handle to a native serial port * * @exception IOException if an I/O error occurs */ private static native void native_close(int hPort) throws IOException; /** Register this object's native cleanup function. */ private native void registerCleanup(); /** * Read from a serial port without blocking. * * @param hPort handle to a native serial port * @param b I/O buffer * @param off starting offset for data * @param len length of data * * @return number of bytes read * * @exception IOException if an I/O error occurs */ private static native int native_readBytes(int hPort, byte b[], int off, int len) throws IOException; /** * Write to a serial port without blocking. * * @param hPort handle to a native serial port * @param b I/O buffer * @param off starting offset for data * @param len length of data * * @return number of bytes that were written * * @exception IOException if an I/O error occurs. */ private static native int native_writeBytes(int hPort, byte b[], int off, int len) throws IOException;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -