public interface MxNPort
open
close
setIoctlMode
setFlowCtrl
setBaud
setRTS
setDTR
flush
read
write
getBaud
getFlowCtrl
getIoctlMode
getLineStatus
getModemStatus
getOQueue
setBreak
Modifier and Type | Interface and Description |
---|---|
static class |
MxNPort.FlowCtrl
This class represents serial flow control settings.
|
static class |
MxNPort.IoctlMode
This class represents serial parameters such as baud rate, data bits, parity, and stop bits.
|
static class |
MxNPort.LineError
This class represents errors on line.
|
static class |
MxNPort.ModemStatus
This class represents modem status, such as CTS, DSR, DCD, and RI.
|
Modifier and Type | Field and Description |
---|---|
static int |
CTS_FLOW_ENABLE
Defines CTS hardware flow control
|
static int |
CTS_OFF
Defines CTS state OFF
|
static int |
CTS_ON
Defines CTS state ON
|
static int |
DATA_BITS_5
Defines serial communication with 5 data bits
|
static int |
DATA_BITS_6
Defines serial communication with 6 data bits
|
static int |
DATA_BITS_7
Defines serial communication with 7 data bits
|
static int |
DATA_BITS_8
Defines serial communication with 8 data bits
|
static int |
DCD_OFF
Defines DCD state OFF
|
static int |
DCD_ON
Defines DCD state ON
|
static int |
DSR_FLOW_ENABLE
Defines DSR hardware flow control
|
static int |
DSR_OFF
Defines DSR state OFF
|
static int |
DSR_ON
Defines DSR state ON
|
static int |
DTR_FLOW_ENABLE
Defines DTR hardware flow control
|
static int |
DTR_OFF
Defines DTS state OFF
|
static int |
DTR_ON
Defines DTS state ON
|
static int |
DTRDSR_FLOW_ENABLE
Defines DTR/DSR software flow control
|
static int |
DTRDSR_FLOW_MASK
Defines the bit mask of DTR/DSR hardware flow control
|
static int |
FLUSH_INPUT
Defines the flush to read buffer
|
static int |
FLUSH_OUTPUT
Defines the flush to write buffer
|
static int |
LINE_ERROR
Defines the error state for each field
|
static int |
LINE_NO_ERROR
Defines the non-error state for each field
|
static int |
PARITY_EVEN
Defines even parity checking
|
static int |
PARITY_MARK
Defines mark parity checking
|
static int |
PARITY_NONE
Defines no parity checking
|
static int |
PARITY_ODD
Defines odd parity checking
|
static int |
PARITY_SPACE
Defines space parity checking
|
static int |
RI_OFF
Defines RI state OFF
|
static int |
RI_ON
Defines RI state ON
|
static int |
RTS_FLOW_ENABLE
Defines RTS hardware flow control
|
static int |
RTS_OFF
Defines RTS state OFF
|
static int |
RTS_ON
Defines RTS state ON
|
static int |
RTSCTS_FLOW_ENABLE
Defines RTS/CTS hardware flow control
|
static int |
RTSCTS_FLOW_MASK
Defines bit mask of RTS/CTS hardware flow control
|
static int |
STOP_BITS_1
Defines 1 stop bit on serial communication
|
static int |
STOP_BITS_1_5
Defines 1.5 stop bits on serial communication
|
static int |
STOP_BITS_2
Defines 2 stop bits on serial communication
|
static int |
XOFF_FLOW_ENABLE
Defines software auto receive flow control
|
static int |
XON_FLOW_ENABLE
Defines software auto transmit flow control
|
static int |
XONXOFF_FLOW_ENABLE
Defines XON/XOFF software flow control
|
static int |
XONXOFF_FLOW_MASK
Defines the bit mask of RTS/CTS hardware flow control
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Disable NPort's serial com port so that it cannot receive/transmit data.
|
void |
flush(int mode)
Flushes any data in the NPort'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, such as overrun error,
parity error, framing error and break_error.
|
MxNPort.ModemStatus |
getModemStatus()
Gets the line status, such as CTS, DSR, DCD, or RI.
|
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 device.
|
java.lang.String |
getNPortModelName()
Returns the model name of a NPort device.
|
int |
getNPortPortIndex()
Returns the using serial port number of a NPort device.
|
int |
getOQueue()
Gets the length (in bytes) remaining in the NPort's output buffer.
|
void |
open()
Open NPort's serial 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)
Configure communication parameters, such as baud rate, parity, data bits, stop bits and flow control settings.
|
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.
|
static final int CTS_FLOW_ENABLE
static final int CTS_OFF
static final int CTS_ON
static final int DATA_BITS_5
static final int DATA_BITS_6
static final int DATA_BITS_7
static final int DATA_BITS_8
static final int DCD_OFF
static final int DCD_ON
static final int DSR_FLOW_ENABLE
static final int DSR_OFF
static final int DSR_ON
static final int DTR_FLOW_ENABLE
static final int DTR_OFF
static final int DTR_ON
static final int DTRDSR_FLOW_ENABLE
static final int DTRDSR_FLOW_MASK
static final int FLUSH_INPUT
static final int FLUSH_OUTPUT
static final int LINE_ERROR
static final int LINE_NO_ERROR
static final int PARITY_EVEN
static final int PARITY_MARK
static final int PARITY_NONE
static final int PARITY_ODD
static final int PARITY_SPACE
static final int RI_OFF
static final int RI_ON
static final int RTS_FLOW_ENABLE
static final int RTS_OFF
static final int RTS_ON
static final int RTSCTS_FLOW_ENABLE
static final int RTSCTS_FLOW_MASK
static final int STOP_BITS_1
static final int STOP_BITS_1_5
static final int STOP_BITS_2
static final int XOFF_FLOW_ENABLE
static final int XON_FLOW_ENABLE
static final int XONXOFF_FLOW_ENABLE
static final int XONXOFF_FLOW_MASK
void close() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
...
try{
//Close the NPort's serial com port
mCurrentNPort.close();
} catch (MxEcxeption e) {
//Close fail
}
MxException
- If an error occurs.void flush(int mode) throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Flush the data in the input/output buffer
mCurrentNPort.flush(MxNPort.FLUSH_INPUT | MxNPort.FLUSH_OUTPUT);
} catch (MxException e) {
// Flush fail
}
mode
- [in] is one or both of FLUSH_INPUT
and FLUSH_OUTPUT
.MxException
- If an error occurs.int getBaud() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Get the NPort current baud rate
int baud = mCurrentNPort.getBaud();
} catch (MxException e) {
// Get baud rate fail
}
MxException
- If an error occurs.MxNPort.FlowCtrl getFlowCtrl() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Get the NPort current flow control setting
MxNPort.FlowCtrl flow = mCurrentNPort.getFlowCtrl();
} catch (MxException e) {
// Get flow control setting fail
}
FlowCtrl
MxException
- If an error occurs.MxNPort.IoctlMode getIoctlMode() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Gets the NPort serial port's mode settings
MxNPort.IoctlMode mode = mCurrentNPort.getIoctlMode();
} catch (MxException e) {
// Get serial port's mode setting fail
}
IoctlMode
MxException
- If an error occurs.MxNPort.LineError getLineStatus() throws MxException
LineError
.
Returns and cleans the error during the transaction.
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Get if errors occurred when receiving data
MxNPort.LineError lineError = mCurrentNPort.getLineStatus();
} catch (MxException e) {
// Get if errors occurred when receiving data fail
}
LineError
MxException
- If an error occurs.MxNPort.ModemStatus getModemStatus() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Get NPort's line status
MxNPort.ModemStatus modem = mCurrentNPort.getModemStatus();
} catch (MxException e) {
// Get NPort's line status fail
}
ModemStatus
MxException
- If an error occurs.java.net.InetAddress getNPortIPAddress(int interface_index) throws MxException
Example:
Write characters to NPort's serial com port.
try{
InetAddress addr = getNPortIPAddress(1);
} catch (MxException e) {
// Get IP address fail
}
interface_index
- [in] The interface port index, only support first interface port now.MxException
- If an error occurs.byte[] getNPortMACAddress(int interface_number) throws MxException
Example:
Write characters to NPort's serial com port.
try{
byte[] mac = getNPortMACAddress(1);
} catch (MxException e) {
// Get MAC address fail
}
interface_number
- [in] The interface port index, only support first interface port now.MxException
- If an error occurs.int getNPortMaximumPort() throws MxException
Example:
Write characters to NPort's serial com port.
try{
int number = getNPortMaximumPort();
} catch (MxException e) {
// Get total serial port number fail
}
MxException
java.lang.String getNPortModelName() throws MxException
Example:
Write characters to NPort's serial com port.
try{
String model = getNPortModelName();
} catch (MxException e) {
// Get model name fail
}
MxException
int getNPortPortIndex() throws MxException
Example:
Write characters to NPort's serial com port.
try{
int number = getNPortPortIndex();
} catch (MxException e) {
// Get using serial port number fail
}
MxException
int getOQueue() throws MxException
Example:
...
try{
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Get the length of NPort's output buffer.
int oqueue_len = mCurrentNPort.getOQueue();
} catch (MxException e) {
// Get the length of NPort's output buffer. fail
}
MxException
- If an error occurs.void open() throws MxException
Baud rate | Data bits | Stop bits | Parity | Flow control | DTR state | RTS state |
38400 bps | 8 | 1 | None | None | ON | ON |
Example:
// Get a MxNPort list from current network
List<MxNPort> mNPortList = MxNPortService.getNPortInfoList();
if( mNPortList!=null ){
// Get the first NPort device
MxNPort mCurrentNPort = mNPortList.get(0);
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
}
MxException
- If an error occurs.int read(byte[] buffer) throws MxException
Example:
Read 10 characters form NPort's serial com port.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
char buf[10];
try{
len = read(buf);
} catch (MxException e) {
// Read fail
}
if(len < 0) {
// fail
}
buffer
- [in][out] The repository for the incoming data.MxException
- If an error occurs.int read(byte[] buffer, int length) throws MxException
Example:
Read 10 characters form NPort's serial com port.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
char buf[10];
try{
len = read(buf, 10);
} catch (MxException e) {
// Read fail
}
if(len < 0) {
// fail
}
buffer
- [in][out] The repository for the incoming data.length
- [in] The required data length. It must not exceed the size of the buffer.MxException
- If an error occurs.int read(byte[] buffer, int length, int timeout) throws MxException
Example:
Read 10 characters form NPort's serial com port whit timeout 5 seconds.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
char buf[10];
int timeout = 5000;
try{
len = read(buf, 10, timeout);
} catch (MxException e) {
// Read fail
}
if(len < 0) {
// fail
}
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.MxException
- If an error occurs.void setBaud(int speed) throws MxException
Example:
Set baud rate to 38400bps
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int speed = 38400;
try{
// Set baud rate 38400bps
mCurrentNPort.setBaud(speed);
} catch (MxException e) {
// Set baud rate fail
}
speed
- [in] The true baud rate, e.g. 200, 1200, 9600, 19200, etc.MxException
- If an error occurs.void setBreak(int time) throws MxException
Example:
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int time = 1000;
try{
// Keep sending break to NPort in 1 second
mCurrentNPort.setBreak(time);
} catch (MxException e) {
// Send break to NPort fail
}
time
- [in] Break time in milliseconds.MxException
- If an error occurs.void setDTR(int mode) throws MxException
Example:
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Set DTR enable
mCurrentNPort.setDTR(MxNPort.DTR_ON);
} catch (MxException e) {
// Set DTR enable fail
}
mode
- [in] for the DTR setting, which is either DTR_ON
or DTR_OFF
.MxException
- If an error occurs.void setFlowCtrl(MxNPort.FlowCtrl flow) throws MxException
RTSCTS_FLOW_ENABLE
and XONXOFF_FLOW_ENABLE
flow control settings.
Example:
Set baud rate to 38400bps
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
MxNPort.FlowCtrl flow = new MxNPort.FlowCtrl();
flow.RtsCts |= MxNPort.RTSCTS_FLOW_ENABLE;
flow.XonXoff |= MxNPort.XONXOFF_FLOW_ENABLE;
flow.DtrDsr &= 0;
try{
// Configure NPort's flow control
mCurrentNPort.setFlowCtrl(flow);
} catch (MxException e) {
// Configure NPort's flow control
}
flow
- [in] FlowCtrl
contains flow control settings.MxException
- If an error occurs.void setIoctlMode(MxNPort.IoctlMode mode) throws MxException
Example:
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
MxNPort.IoctlMode mode = new MxNPort.IoctlMode();
mode.baud = 38400;
mode.dataBits = MxNPort.DATA_BITS_8;
mode.mParity = MxNPort.PARITY_NONE;
mode.stopBits = MxNPort.STOP_BITS_1;
try{
// Configure NPort's communication parameters
mCurrentNPort.setIoctlMode(mode);
} catch (MxException e) {
// Configure NPort's communication parameters fail
}
mode
- [in] IoctlMode
contains serial settings.MxException
- If an error occurs.void setRTS(int mode) throws MxException
Example:
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
try{
// Set RTS enable
mCurrentNPort.setRTS(MxNPort.RTS_ON);
} catch (MxException e) {
// Set RTS enable fail
}
mode
- [in] for RTS settings; is either RTS_ON
or RTS_OFF
.MxException
- If an error occurs.int write(byte[] buffer) throws MxException
Example:
Write characters to NPort's serial com port.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
byte[] buf = {'H','e','l','l','o',' ','W','o','r','l','d'};
try{
len = write(buf);
} catch (MxException e) {
// write fail
}
if(len < 0) {
// fail
}
buffer
- [in] The buffer contains all data to send.MxException
- If an error occurs.int write(byte[] buffer, int length) throws MxException
Example:
Write characters to NPort's serial com port.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
byte[] buf = {'H','e','l','l','o',' ','W','o','r','l','d'};
try{
len = write(buf, buf.length);
} catch (MxException e) {
// write fail
}
if(len < 0) {
// fail
}
buffer
- [in] The buffer that contains data to send.length
- [in] The length of data to send.MxException
- If an error occurs.int write(byte[] buffer, int length, int timeout) throws MxException
Example:
Write characters to NPort's serial com port.
...
try{
// Open the NPort's serial com port
mCurrentNPort.open();
} catch (MxException e) {
// Open fail
}
int len;
int timeout = 1000;
byte[] buf = {'H','e','l','l','o',' ','W','o','r','l','d'};
try{
len = write(buf, buf.length, timeout);
} catch (MxException e) {
// write fail
}
if(len < 0) {
// fail
}
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.MxException
- If an error occurs.