⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protocol.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }        }        if (found == false) {            throw new ConnectionNotFoundException(deviceName +                    " port not found!");        }        handle = native_openByName(deviceName, baud,            bbc|stop|parity|rts|cts);        return this;    }    /**     * Check for the required permission.     *     * @param name name of resource to insert into the permission question     *     * @exception IOInterruptedException if another thread interrupts the     *   calling thread while this method is waiting to preempt the     *   display.     */    private void checkForPermission(String name)            throws InterruptedIOException {        name = "comm" + ":" + name;        try {            AccessController.                checkPermission(COMM_PERMISSION_NAME, name);        } catch (InterruptedSecurityException ise) {            throw new InterruptedIOException(                "Interrupted while trying to ask the user permission");        }    }    /**      * 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);	    /* If successful, update the local baud variable. */	    baud = baudrate;	} catch (IOException ioe) {	    // Ignore: this method throws no exceptions.	}	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 registered 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;            }        }    }    /**     * 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 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:     * bit 0: 0 - 1 stop bit, 1 - 2 stop bits      * bit 2-1: 00 - no parity, 01 - odd parity, 10 - even parity      * bit 4: 0 - no auto RTS, 1 - set auto RTS      * bit 5: 0 - no auto CTS, 1 - set auto CTS      * bit 7-6: 01 - 7 bits per symbol, 11 - 8 bits per symbol      *     * @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;    /**     * 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;   private native void finalize();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -