📄 protocol.java
字号:
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); /* If successful, update the local baud variable. */ baud = baudrate; } catch (IOException ioe) { // NYI - could not set baudrate as requested. } return temp; } /** * Close the native serial port. * * @exception IOException if an I/O error occurs. */ protected void disconnect() throws IOException { try { 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; for (;;) { try { bytesRead = native_readBytes(handle, b, off, len); } finally { if (iStreams == 0) { throw new InterruptedIOException("Stream closed"); } } if (bytesRead == -1) { eof = true; return -1; } if (bytesRead != 0 || !blocking) { return bytesRead; } /* Wait a while for I/O to become ready */ GeneralBase.iowait(); } } /** * 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); } 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); } /* * 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; /** * Native finalizer. */ private native void finalize();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -