Package com.metratec.lib.connection
Class UsbConnection
- java.lang.Object
-
- com.metratec.lib.connection.ICommConnection
-
- com.metratec.lib.connection.SerialConnection
-
- com.metratec.lib.connection.UsbConnection
-
public class UsbConnection extends SerialConnection
A connection to a usb device- Author:
- man
-
-
Field Summary
-
Fields inherited from class com.metratec.lib.connection.SerialConnection
DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8, FLOWCONTROL_NONE, FLOWCONTROL_RTSCTS_IN, FLOWCONTROL_RTSCTS_OUT, FLOWCONTROL_XONXOFF_IN, FLOWCONTROL_XONXOFF_OUT, PARITY_EVEN, PARITY_MARK, PARITY_NONE, PARITY_ODD, PARITY_SPACE, STOPBITS_1, STOPBITS_1_5, STOPBITS_2
-
Fields inherited from class com.metratec.lib.connection.ICommConnection
CONNECTION_LOST, DEVICE_IN_USE, ETHERNET_TIMEOUT, ETHERNET_UNKNOWN_HOST, NO_DEVICES_FOUND, NO_LIBRARY_FOUND, NOT_AVAILABLE, NOT_INITIALISED, RECV_TIMEOUT, SERIAL_NO_ACCESS, SERIAL_NOT_INITIALISED, SERIAL_PARAMETER_NOT_SET, SERIAL_PORT_NOT_EXIST, SET_CONFIGURATION, UNHANDLED_ERROR, USB_SET_BAUDRATE, USB_SET_DATA_CHARACTERISTICS, USB_SET_FLOWCONTROL, USB_SET_TIMEOUTS, USER_ERRORCODE_01, USER_ERRORCODE_02, USER_ERRORCODE_03, USER_ERRORCODE_04, USER_ERRORCODE_05, WRONG_PARAMETER
-
-
Constructor Summary
Constructors Constructor Description UsbConnection(java.lang.String usbDeviceSerialNumber)
Construct a new USBConnection objectUsbConnection(java.lang.String usbDeviceSerialNumber, int baudrate)
Construct a new USBConnection objectUsbConnection(java.lang.String usbDeviceSerialNumber, int baudrate, int dataBit, int stopBit, int parity, int flowControl)
Construct a new USBConnection objectUsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice)
Construct a new USBConnection objectUsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice, int baudrate)
Construct a new USBConnection objectUsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice, int baudrate, int dataBit, int stopBit, int parity, int flowControl)
Construct a new USBConnection object
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addVIDPID(int vid, int pid)
Add VID/PID pair to the list of supported USB devices.void
connect()
This method opens a connection.int
dataAvailable()
void
disconnect()
Closes the communication interfaceint
getConnectionTimeout()
java.util.Hashtable<java.lang.String,java.lang.Object>
getInfo()
Gets Device Informationsjava.io.InputStream
getInputStream()
jd2xx.JD2XX
getJD2XX()
java.io.OutputStream
getOutputStream()
int
getRecvTimeout()
int
getSendTimeout()
java.lang.String
getSerialNumber()
Get the USB device's serial number.static java.util.ArrayList<jd2xx.JD2XX.DeviceInfo>
getUSBDevices()
static java.util.LinkedList<java.lang.String>
getUSBDevicesList()
boolean
isConnected()
java.lang.StringBuilder
receive(int... terminators)
Receives data from the connected device until one of the terminator signs is found.int
recv()
Receives a single byte.void
recv(byte[] b, int off, int len)
Receives until buffer is filled or timeout occurrs.void
send(byte[] senddata)
Sends data to the connected devicevoid
setConnectionTimeout(int time)
Sets the waiting time for the connectionvoid
setRecvTimeout(int timeout)
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still validvoid
setSendTimeout(int timeout)
Set the send timeoutvoid
setSerialNumber(java.lang.String serialNumber)
Set the USB device's serial number.void
setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings)
Sets connections settingsjava.lang.String
toString()
-
Methods inherited from class com.metratec.lib.connection.SerialConnection
getBaudrate, getDataBit, getFlowControl, getParity, getStopBit, setBaudrate, setDataBit, setFlowControl, setParity, setStopBit
-
Methods inherited from class com.metratec.lib.connection.ICommConnection
recv, recv, send
-
-
-
-
Constructor Detail
-
UsbConnection
public UsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice)
Construct a new USBConnection object- Parameters:
usbDevice
- DeviceInfo
-
UsbConnection
public UsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice, int baudrate)
Construct a new USBConnection object- Parameters:
usbDevice
- DeviceInfobaudrate
- baud rate
-
UsbConnection
public UsbConnection(jd2xx.JD2XX.DeviceInfo usbDevice, int baudrate, int dataBit, int stopBit, int parity, int flowControl)
Construct a new USBConnection object- Parameters:
usbDevice
- DeviceInfobaudrate
- baud rate of the connected device, can read with theSerialConnection.getBaudrate()
method.dataBit
- Number of data bits, can read with theSerialConnection.getDataBit()
method.stopBit
- Value of StopBits, can read with theSerialConnection.getStopBit()
method. A value larger 2 means 1.5 stopbits.parity
- Parity-Bit (0 for no, 1 for ODD, 2 for EVEN, 3 for MARK, 4 for SPACE), can read with theSerialConnection.getParity()
method.flowControl
- Flow Control (0 for no, ..), can read with theSerialConnection.getFlowControl()
method.
-
UsbConnection
public UsbConnection(java.lang.String usbDeviceSerialNumber)
Construct a new USBConnection object- Parameters:
usbDeviceSerialNumber
- usb serial number
-
UsbConnection
public UsbConnection(java.lang.String usbDeviceSerialNumber, int baudrate)
Construct a new USBConnection object- Parameters:
usbDeviceSerialNumber
- usb serial numberbaudrate
- baud rate of the connected device, can read with theSerialConnection.getBaudrate()
method.
-
UsbConnection
public UsbConnection(java.lang.String usbDeviceSerialNumber, int baudrate, int dataBit, int stopBit, int parity, int flowControl)
Construct a new USBConnection object- Parameters:
usbDeviceSerialNumber
- usb serial numberbaudrate
- baud rate of the connected device, can read with theSerialConnection.getBaudrate()
method.dataBit
- Number of data bits, can read with theSerialConnection.getDataBit()
method.stopBit
- Value of StopBits, can read with theSerialConnection.getStopBit()
method. A value larger 2 means 1.5 stopbits.parity
- Parity-Bit (0 for no, 1 for ODD, 2 for EVEN, 3 for MARK, 4 for SPACE), can read with theSerialConnection.getParity()
method.flowControl
- Flow Control (0 for no, ..), can read with theSerialConnection.getFlowControl()
method.
-
-
Method Detail
-
addVIDPID
public static void addVIDPID(int vid, int pid)
Add VID/PID pair to the list of supported USB devices.Depending on the operating system, all FTDI devices might be usable by default. On some systems like Linux and OS X however, only the default FTDI VID/PIDs and all additional non-standard VID/PIDs used by metraTec devices are supported by default. Therefore in order to write platform-independent code, you should call this method for every non-standard and non-metraTec VID/PID you intend to use (e.g. HAMEG power supply PIDs).
- Parameters:
vid
- Vendor IDpid
- Product ID
-
getUSBDevices
public static java.util.ArrayList<jd2xx.JD2XX.DeviceInfo> getUSBDevices() throws CommConnectionException
- Returns:
- ArrayList with Device Infos
- Throws:
CommConnectionException
- possible Errorcodes:- NO_LIBRARY_FOUND
- UNHANDLED_ERROR
-
getUSBDevicesList
public static java.util.LinkedList<java.lang.String> getUSBDevicesList() throws CommConnectionException
- Returns:
- LinkedList(String) with available devices
- Throws:
CommConnectionException
- possible Errorcodes:- NO_LIBRARY_FOUND
- UNHANDLED_ERROR
-
disconnect
public void disconnect() throws CommConnectionException
Description copied from class:ICommConnection
Closes the communication interface- Specified by:
disconnect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- UNHANDLED_ERROR
- for USB Connection:
- UNHANDLED_ERROR
- for TCP Connection:
-
connect
public void connect() throws CommConnectionException
Description copied from class:ICommConnection
This method opens a connection. Parameters are passed through the constructor- Specified by:
connect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- ETHERNET_UNKNOWN_HOST
- ETHERNET_TIMEOUT
- WRONG_PARAMETER
- for USB Connection:
- DEVICE_IN_USE
- NO_LIBRARY_FOUND
- NO_DEVICES_FOUND
- USB_SET_BAUDRATE
- USB_SET_DATA_CHARACTERISTICS
- USB_SET_FLOWCONTROL
- USB_SET_TIMEOUTS
- for RS232 Connection:
- NO_LIBRARY_FOUND
- SERIAL_PORT_NOT_EXIST
- SERIAL_PARAMETER_NOT_SET
- DEVICE_IN_USE
- SERIAL_NO_ACCESS
- SERIAL_NO_INITIALISE
- for TCP Connection:
-
getInputStream
public java.io.InputStream getInputStream()
- Specified by:
getInputStream
in classICommConnection
- Returns:
- the
InputStream
of this connection
-
getOutputStream
public java.io.OutputStream getOutputStream()
- Specified by:
getOutputStream
in classICommConnection
- Returns:
- the
OutputStream
of this connection
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classICommConnection
- Returns:
- true if connected else false
-
recv
public int recv() throws CommConnectionException
Description copied from class:ICommConnection
Receives a single byte.- Specified by:
recv
in classICommConnection
- Returns:
- byte or -1 if no Data available (timeout)
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
send
public void send(byte[] senddata) throws CommConnectionException
Description copied from class:ICommConnection
Sends data to the connected device- Specified by:
send
in classICommConnection
- Parameters:
senddata
- data/command send to the connected device- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- WRONG_PARAMETER
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
dataAvailable
public int dataAvailable() throws CommConnectionException
- Specified by:
dataAvailable
in classICommConnection
- Returns:
- number of available signs
- Throws:
CommConnectionException
- possible Errorcodes:- for all connections
- CONNECTION_LOST
- NOT_INITIALISE
- for all connections
-
getInfo
public java.util.Hashtable<java.lang.String,java.lang.Object> getInfo()
Description copied from class:ICommConnection
Gets Device Informations- Specified by:
getInfo
in classICommConnection
- Returns:
- Hashtable with the information
-
setSettings
public void setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings)
Description copied from class:ICommConnection
Sets connections settings- Specified by:
setSettings
in classICommConnection
- Parameters:
settings
- Hashtable with the special settings
-
getRecvTimeout
public int getRecvTimeout()
- Specified by:
getRecvTimeout
in classICommConnection
- Returns:
- the receive timeout
-
setRecvTimeout
public void setRecvTimeout(int timeout) throws CommConnectionException
Description copied from class:ICommConnection
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still valid- Specified by:
setRecvTimeout
in classICommConnection
- Parameters:
timeout
- time in milliseconds- Throws:
CommConnectionException
- possible Errorcodes:- for all connections
- SET_CONFIGURATION
- for all connections
-
getSendTimeout
public int getSendTimeout()
- Returns:
- the send timeout
-
setSendTimeout
public void setSendTimeout(int timeout) throws CommConnectionException
Set the send timeout- Parameters:
timeout
- send timeout- Throws:
CommConnectionException
- if an error occurs
-
setConnectionTimeout
public void setConnectionTimeout(int time)
Description copied from class:ICommConnection
Sets the waiting time for the connection- Specified by:
setConnectionTimeout
in classICommConnection
- Parameters:
time
- connection timeout
-
getConnectionTimeout
public int getConnectionTimeout()
- Specified by:
getConnectionTimeout
in classICommConnection
- Returns:
- the waiting time for the connection
-
getSerialNumber
public java.lang.String getSerialNumber()
Get the USB device's serial number.- Returns:
- the serial number.
-
setSerialNumber
public void setSerialNumber(java.lang.String serialNumber)
Set the USB device's serial number.- Parameters:
serialNumber
- the serial number.
-
getJD2XX
public jd2xx.JD2XX getJD2XX()
- Returns:
- the jd2xx
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
recv
public void recv(byte[] b, int off, int len) throws CommConnectionException
Description copied from class:ICommConnection
Receives until buffer is filled or timeout occurrs.- Overrides:
recv
in classICommConnection
- Parameters:
b
- Byte array to filloff
- Offset into array (it is filled beginning with the offset)len
- Number of bytes to read.- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
receive
public java.lang.StringBuilder receive(int... terminators) throws CommConnectionException
Description copied from class:ICommConnection
Receives data from the connected device until one of the terminator signs is found. The result is returned as a StringBuilder for performance reasons.- Overrides:
receive
in classICommConnection
- Parameters:
terminators
- A list of terminator signs. Note that this may be a list of parameters or an array.- Returns:
- a StringBuilder object, including the termination sign
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
-