Added timeouts for read operations and SerialPortTimeoutException class
This commit is contained in:
parent
c5cb719c4c
commit
f24c629662
|
@ -1,5 +1,5 @@
|
||||||
/* jSSC (Java Simple Serial Connector) - serial port communication library.
|
/* jSSC (Java Simple Serial Connector) - serial port communication library.
|
||||||
* © Alexey Sokolov (scream3r), 2010-2011.
|
* © Alexey Sokolov (scream3r), 2010-2013.
|
||||||
*
|
*
|
||||||
* This file is part of jSSC.
|
* This file is part of jSSC.
|
||||||
*
|
*
|
||||||
|
@ -417,7 +417,7 @@ public class SerialPort {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read Hex string from port (example: FF OA FF). Separator by default is a space
|
* Read Hex string from port (example: FF 0A FF). Separator by default is a space
|
||||||
*
|
*
|
||||||
* @param byteCount count of bytes for reading
|
* @param byteCount count of bytes for reading
|
||||||
*
|
*
|
||||||
|
@ -433,7 +433,7 @@ public class SerialPort {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read Hex string from port with setted separator (example if separator is "::": FF::OA::FF)
|
* Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
|
||||||
*
|
*
|
||||||
* @param byteCount count of bytes for reading
|
* @param byteCount count of bytes for reading
|
||||||
*
|
*
|
||||||
|
@ -509,6 +509,141 @@ public class SerialPort {
|
||||||
return intBuffer;
|
return intBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitBytesWithTimeout(String methodName, int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("waitBytesWithTimeout()");
|
||||||
|
boolean timeIsOut = true;
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
while((System.currentTimeMillis() - startTime) < timeout){
|
||||||
|
if(getInputBufferBytesCount() >= byteCount){
|
||||||
|
timeIsOut = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(0, 100);//Need to sleep some time to prevent high CPU loading
|
||||||
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
|
//Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(timeIsOut){
|
||||||
|
throw new SerialPortTimeoutException(portName, methodName, timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read byte array from port
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return byte array with "byteCount" length
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public byte[] readBytes(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readBytes()");
|
||||||
|
waitBytesWithTimeout("readBytes()", byteCount, timeout);
|
||||||
|
return readBytes(byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read string from port
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return byte array with "byteCount" length converted to String
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public String readString(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readString()");
|
||||||
|
waitBytesWithTimeout("readString()", byteCount, timeout);
|
||||||
|
return readString(byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read Hex string from port (example: FF 0A FF). Separator by default is a space
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return byte array with "byteCount" length converted to Hexadecimal String
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public String readHexString(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readHexString()");
|
||||||
|
waitBytesWithTimeout("readHexString()", byteCount, timeout);
|
||||||
|
return readHexString(byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return byte array with "byteCount" length converted to Hexadecimal String
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public String readHexString(int byteCount, String separator, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readHexString()");
|
||||||
|
waitBytesWithTimeout("readHexString()", byteCount, timeout);
|
||||||
|
return readHexString(byteCount, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read Hex String array from port
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return String array with "byteCount" length and Hexadecimal String values
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public String[] readHexStringArray(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readHexStringArray()");
|
||||||
|
waitBytesWithTimeout("readHexStringArray()", byteCount, timeout);
|
||||||
|
return readHexStringArray(byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read int array from port
|
||||||
|
*
|
||||||
|
* @param byteCount count of bytes for reading
|
||||||
|
* @param timeout timeout in milliseconds
|
||||||
|
*
|
||||||
|
* @return int array with values in range from 0 to 255
|
||||||
|
*
|
||||||
|
* @throws SerialPortException
|
||||||
|
* @throws SerialPortTimeoutException
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public int[] readIntArray(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException {
|
||||||
|
checkPortOpened("readIntArray()");
|
||||||
|
waitBytesWithTimeout("readIntArray()", byteCount, timeout);
|
||||||
|
return readIntArray(byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read all available bytes from port like a byte array
|
* Read all available bytes from port like a byte array
|
||||||
*
|
*
|
||||||
|
@ -609,7 +744,7 @@ public class SerialPort {
|
||||||
* @since 0.8
|
* @since 0.8
|
||||||
*/
|
*/
|
||||||
public int[] readIntArray() throws SerialPortException {
|
public int[] readIntArray() throws SerialPortException {
|
||||||
checkPortOpened("readHex()");
|
checkPortOpened("readIntArray()");
|
||||||
int byteCount = getInputBufferBytesCount();
|
int byteCount = getInputBufferBytesCount();
|
||||||
if(byteCount <= 0){
|
if(byteCount <= 0){
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
/* jSSC (Java Simple Serial Connector) - serial port communication library.
|
||||||
|
* © Alexey Sokolov (scream3r), 2010-2013.
|
||||||
|
*
|
||||||
|
* This file is part of jSSC.
|
||||||
|
*
|
||||||
|
* jSSC is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* jSSC is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with jSSC. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* If you use jSSC in public project you can inform me about this by e-mail,
|
||||||
|
* of course if you want it.
|
||||||
|
*
|
||||||
|
* e-mail: scream3r.org@gmail.com
|
||||||
|
* web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/
|
||||||
|
*/
|
||||||
|
package jssc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author scream3r
|
||||||
|
*/
|
||||||
|
public class SerialPortTimeoutException extends Exception {
|
||||||
|
|
||||||
|
private String portName;
|
||||||
|
private String methodName;
|
||||||
|
private int timeoutValue;
|
||||||
|
|
||||||
|
public SerialPortTimeoutException(String portName, String methodName, int timeoutValue) {
|
||||||
|
super("Port name - " + portName + "; Method name - " + methodName + "; Serial port operation timeout (" + timeoutValue + " ms).");
|
||||||
|
this.portName = portName;
|
||||||
|
this.methodName = methodName;
|
||||||
|
this.timeoutValue = timeoutValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getting port name during operation with which the exception was called
|
||||||
|
*/
|
||||||
|
public String getPortName(){
|
||||||
|
return portName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getting method name during execution of which the exception was called
|
||||||
|
*/
|
||||||
|
public String getMethodName(){
|
||||||
|
return methodName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getting timeout value in millisecond
|
||||||
|
*/
|
||||||
|
public int getTimeoutValue(){
|
||||||
|
return timeoutValue;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue