com.moxa.mxnportapi

## Class NPortDriver

- java.lang.Object
- - com.moxa.mxnportapi.CommonNPortDriver
  - - com.moxa.mxnportapi.NPortDriver

- All Implemented Interfaces:
    MxNPort, java.util.Comparator<java.lang.Integer>

      public class NPortDriver
      extends CommonNPortDriver

  Created by bird on 7/21/2017.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `NPortDriver.BreakTask`
    `class` | `NPortDriver.RecvThread`

    - ### Nested classes/interfaces inherited from interface com.moxa.mxnportapi.MxNPort

      `MxNPort.FlowCtrl, MxNPort.IoctlMode, MxNPort.LineError, MxNPort.ModemStatus`

  - ### Field Summary

    - ### Fields inherited from interface com.moxa.mxnportapi.MxNPort

      `CTS_FLOW_ENABLE, CTS_OFF, CTS_ON, DATA_BITS_5, DATA_BITS_6, DATA_BITS_7, DATA_BITS_8, DCD_OFF, DCD_ON, DSR_FLOW_ENABLE, DSR_OFF, DSR_ON, DTR_FLOW_ENABLE, DTR_OFF, DTR_ON, DTRDSR_FLOW_ENABLE, DTRDSR_FLOW_MASK, FLUSH_INPUT, FLUSH_OUTPUT, LINE_ERROR, LINE_NO_ERROR, PARITY_EVEN, PARITY_MARK, PARITY_NONE, PARITY_ODD, PARITY_SPACE, RI_OFF, RI_ON, RTS_FLOW_ENABLE, RTS_OFF, RTS_ON, RTSCTS_FLOW_ENABLE, RTSCTS_FLOW_MASK, STOP_BITS_1, STOP_BITS_1_5, STOP_BITS_2, XOFF_FLOW_ENABLE, XON_FLOW_ENABLE, XONXOFF_FLOW_ENABLE, XONXOFF_FLOW_MASK`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Closes the NPort's serial port through the real com mode.
    `void` | `flush(int mode)` Flushes any data in the driver's input/output buffer.
    `int` | `getBaud()` Get the serial port baud rate setting.
    `MxNPort.FlowCtrl` | `getFlowCtrl()` Get the serial port's hardware and software flow control settings.
    `MxNPort.IoctlMode` | `getIoctlMode()` Gets the serial port's mode settings, such as data bits, parity, and stop bits.
    `MxNPort.LineError` | `getLineStatus()` Checks if errors occurred when receiving data; returns and cleans the error during the transaction.
    `MxNPort.ModemStatus` | `getModemStatus()` Gets the line status, such as CTS, DSR,or DCD.
    `java.net.InetAddress` | `getNPortIPAddress(int interface_index)` Returns the IP address of a NPort devices.
    `byte[]` | `getNPortMACAddress(int interface_number)` Returns the MAC address of a NPort devices.
    `int` | `getNPortMaximumPort()` Returns the total serial port number of a NPort devices.
    `java.lang.String` | `getNPortModelName()` Returns the model name of a NPort devices.
    `int` | `getNPortPortIndex()` Returns the using port number of a NPort devices.
    `int` | `getOQueue()` Gets the length (in bytes) remaining in the output buffer.
    `void` | `open()` Open a COM port for data transmitting/receiving.
    `int` | `read(byte[] buffer)` Reads data from the port; returns the data if the buffer is full.
    `int` | `read(byte[] buffer, int length)` Reads data from a port; returns the data if the given length of data has been received.
    `int` | `read(byte[] buffer, int length, int timeout)` Reads data from the port; returns the data if the given length of data has been received.
    `void` | `setBaud(int speed)` Sets the baud rate using the actual speed value.
    `void` | `setBreak(int time)` Sends out a break signal.
    `void` | `setDTR(int mode)` Sets the DTR state for a serial port.
    `void` | `setFlowCtrl(MxNPort.FlowCtrl flow)` Sets the hardware and/or software flow control.
    `void` | `setIoctlMode(MxNPort.IoctlMode mode)` Configures communication parameters, such as baud rate, parity, data bits and stop bits.
    `void` | `setRTS(int mode)` Sets the RTS state for a serial port.
    `int` | `write(byte[] buffer)` Puts a block of data to the driver's output buffer.
    `int` | `write(byte[] buffer, int length)` Puts a block of data to the driver's output buffer.
    `int` | `write(byte[] buffer, int length, int timeout)` Puts a block of data to the driver's output buffer.

    - ### Methods inherited from class com.moxa.mxnportapi.CommonNPortDriver

      `compare`

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.Comparator

      `comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- - ### Method Detail

    - #### close

          public void close()
                     throws MxException

      Closes the NPort's serial port through the real com mode.
      Throws:
        `MxException` - If an error occurs.

    - #### flush

          public void flush(int mode)
                     throws MxException

      Flushes any data in the driver's input/output buffer.
      Parameters:
        `mode` - [in] is one or both of `FLUSH_INPUT` and `FLUSH_OUTPUT`.
      Throws:
        `MxException` - If an error occurs.

    - #### getBaud

          public int getBaud()
                      throws MxException

      Get the serial port baud rate setting. The return value is the actual baud rate. For example, a return value of 9600 means 9600 bps whereas 200 means 200bps.
      Returns:
        The actual baud rate.
      Throws:
        `MxException` - If an error occurs.

    - #### getFlowCtrl

          public MxNPort.FlowCtrl getFlowCtrl()
                                       throws MxException

      Get the serial port's hardware and software flow control settings.
      Returns:
        `FlowCtrl`
      Throws:
        `MxException` - If an error occurs.

    - #### getIoctlMode

          public MxNPort.IoctlMode getIoctlMode()
                                         throws MxException

      Gets the serial port's mode settings, such as data bits, parity, and stop bits.
      Returns:
        `IoctlMode`
      Throws:
        `MxException` - If an error occurs.

    - #### getLineStatus

          public MxNPort.LineError getLineStatus()
                                          throws MxException

      Checks if errors occurred when receiving data; returns and cleans the error during the transaction.
      Returns:
        `LineError`
      Throws:
        `MxException` - If an error occurs.

    - #### getModemStatus

          public MxNPort.ModemStatus getModemStatus()
                                             throws MxException

      Gets the line status, such as CTS, DSR,or DCD.
      Returns:
        `ModemStatus`
      Throws:
        `MxException` - If an error occurs.

    - #### getNPortIPAddress

          public java.net.InetAddress getNPortIPAddress(int interface_index)
                                                 throws MxException

      Returns the IP address of a NPort devices.
      Parameters:
        `interface_index` - [in] The interface port index, only support first interface port now.
      Returns:
        The IP address of the device.
      Throws:
        `MxException` - If an error occurs.

    - #### getNPortMACAddress

          public byte[] getNPortMACAddress(int interface_number)
                                    throws MxException

      Returns the MAC address of a NPort devices.
      Parameters:
        `interface_number` - [in] The interface port index, only support first interface port now.
      Returns:
        The MAC address of the device.
      Throws:
        `MxException` - If an error occurs.

    - #### getNPortMaximumPort

          public int getNPortMaximumPort()
                                  throws MxException

      Returns the total serial port number of a NPort devices.
      Returns:
        The serial port number of the device.
      Throws:
        `MxException`

    - #### getNPortModelName

          public java.lang.String getNPortModelName()
                                             throws MxException

      Returns the model name of a NPort devices.
      Returns:
        The model name of the device.
      Throws:
        `MxException`

    - #### getNPortPortIndex

          public int getNPortPortIndex()
                                throws MxException

      Returns the using port number of a NPort devices.
      Returns:
        The serial port number of the device.
      Throws:
        `MxException`

    - #### getOQueue

          public int getOQueue()
                        throws MxException

      Gets the length (in bytes) remaining in the output buffer.
      Returns:
        The length of output buffer.
      Throws:
        `MxException` - If an error occurs.

    - #### open

          public void open()
                    throws MxException

      Open a COM port for data transmitting/receiving. After calling open(), the initial status of the COM port is:

      Baud rate | Data bits | Stop bits | Parity | Flow control | DTR state | RTS state
      38400 bps | 8 | 1 | None | None | ON | ON

      Throws:
        `MxException` - If an error occurs.

    - #### read

          public int read(byte[] buffer)
                   throws MxException

      Reads data from the port; returns the data if the buffer is full.
      Parameters:
        `buffer` - [in][out] The repository for the incoming data.
      Returns:
        The actual read data in the buffer.
      Throws:
        `MxException` - If an error occurs.

    - #### read

          public int read(byte[] buffer,
                          int length)
                   throws MxException

      Reads data from a port; returns the data if the given length of data has been received.
      Parameters:
        `buffer` - [in][out] The repository for the incoming data.
        `length` - [in] The required data length. It must not exceed the size of the buffer.
      Returns:
        The actual read data in the buffer.
      Throws:
        `MxException` - If an error occurs.

    - #### read

          public int read(byte[] buffer,
                          int length,
                          int timeout)
                   throws MxException

      Reads data from the port; returns the data if the given length of data has been received. Otherwise, if a timeout has occurred, the received data will be stored in the buffer and the length of received data will be returned.
      Parameters:
        `buffer` - [in][out] The repository for the incoming data.
        `length` - [in] The required data length. It must not exceed the size of the buffer.
        `timeout` - [in] The timeout value for receiving data.
      Returns:
        The actual read data in the buffer.
      Throws:
        `MxException` - If an error occurs.

    - #### setBaud

          public void setBaud(int speed)
                       throws MxException

      Sets the baud rate using the actual speed value.
      Parameters:
        `speed` - [in] The true baud rate, e.g. 200, 1200, 9600, 19200, etc.
      Throws:
        `MxException` - If an error occurs.

    - #### setBreak

          public void setBreak(int time)
                        throws MxException

      Sends out a break signal. This function will block access for the given length of time.
      Parameters:
        `time` - [in] Break time in milliseconds.
      Throws:
        `MxException` - If an error occurs.

    - #### setDTR

          public void setDTR(int mode)
                      throws MxException

      Sets the DTR state for a serial port.
      Parameters:
        `mode` - [in] for the DTR setting, which is either `DTR_ON` or `DTR_OFF`.
      Throws:
        `MxException` - If an error occurs.

    - #### setFlowCtrl

          public void setFlowCtrl(MxNPort.FlowCtrl flow)
                           throws MxException

      Sets the hardware and/or software flow control. The UPort 1100 series supports `RTSCTS_FLOW_ENABLE` and `MxNPort.XONXOFF_FLOW_ENABLE` flow control settings. The UPort 1200/1400/1600 series support `RTSCTS_FLOW_ENABLE`, `MxNPort.XONXOFF_FLOW_ENABLE`, and `DTRDSR_FLOW_ENABLE`.
      Parameters:
        `flow@throws` - MxException If an error occurs.
      Throws:
        `MxException` - If an error occurs.

    - #### setIoctlMode

          public void setIoctlMode(MxNPort.IoctlMode mode)
                            throws MxException

      Configures communication parameters, such as baud rate, parity, data bits and stop bits.
      Parameters:
        `mode` - [in] `IoctlMode` contains serial settings.
      Throws:
        `MxException` - If an error occurs.

    - #### setRTS

          public void setRTS(int mode)
                      throws MxException

      Sets the RTS state for a serial port.
      Parameters:
        `mode` - [in] for RTS settings; is either `RTS_ON` or `RTS_OFF`.
      Throws:
        `MxException` - If an error occurs.

    - #### write

          public int write(byte[] buffer)
                    throws MxException

      Puts a block of data to the driver's output buffer. The actual length of data written depends on the amount of free space in the driver's output buffer. The write() returns when all data in the buffer has been sent.
      Specified by:
        `write` in interface `MxNPort`
      Specified by:
        `write` in class `CommonNPortDriver`
      Parameters:
        `buffer` - [in] The buffer contains all data to send.
      Returns:
        The actual data written to the device.
      Throws:
        `MxException` - If an error occurs.

    - #### write

          public int write(byte[] buffer,
                           int length)
                    throws MxException

      Puts a block of data to the driver's output buffer. The actual length of data written depends on the amount of free space in the driver's output buffer. The write() returns when the given length of data in the buffer has been sent.
      Parameters:
        `buffer` - [in] The buffer that contains data to send.
        `length` - [in] The length of data to send.
      Returns:
        The actual data written to the device.
      Throws:
        `MxException` - If an error occurs.

    - #### write

          public int write(byte[] buffer,
                           int length,
                           int timeout)
                    throws MxException

      Puts a block of data to the driver's output buffer. The actual length of data written depends on the amount of free space in the driver's output buffer. The write() returns when the given length of data in the buffer has been sent. Otherwise, if a timeout occurs the write() returns the transmitted length of data. The caller should handle transmitting the other data.
      Parameters:
        `buffer` - [in] The buffer that contains data to send.
        `length` - [in] The length of data to send.
        `timeout` - [in] The timeout value to receive data.
      Returns:
        The actual data written to the device.
      Throws:
        `MxException` - If an error occurs.