从串口读数据
/**
* 从串口读取数据
*
* @param serialPort 当前已建立连接的SerialPort对象
* @return 读取到的数据
*/
public static byte[] readFromPort(SerialPort serialPort, int len) {
{
LogUtils.log("从串口读取数据");
long maxTime = 4000;
int count = 0;
int readLen = 0;
long beginTime = System.currentTimeMillis();
if (isSafe(serialPort)) {
byte[] bytes = {};
InputStream in = serialPort.getInputStream();
try {
Thread.sleep(200);
byte[] readBuffer = new byte[1];
while (true) {
// readLen = serialPort.readBytes(readBuffer, readBuffer.length);
if (in.available() > 0){
readLen = in.read(readBuffer);
}else{
readLen = 0;
readBuffer = new byte[1];
if (System.currentTimeMillis() - beginTime > maxTime) {
LogUtils.error(TAG, "读取超时1");
bytes = null;
break;
}
}
// LogUtils.error(TAG,"len = " + len + " readLen = " + readLen + " count = " + count + " in.available() = " + in.available() +
// " readBuffer = " + BaseTypeUtils.bytesToHexString(readBuffer));
if (readLen != 0) {
if (count == 0) {
if (readBuffer[0] == (byte) 0xA0) {
bytes = BaseTypeUtils.byteMerger(bytes, readBuffer);
count++;
}
} else if (count > 0) {
if (readBuffer[0] == (byte)0xD0) {
bytes = BaseTypeUtils.byteMerger(bytes, readBuffer);
// LogUtils.error(TAG,"开始验证结尾 bytes.length = " + bytes.length + " content = " + BaseTypeUtils.bytesToHexString(bytes));
if (BaseTypeUtils.checkChkSum(bytes, bytes.length - 2)) {
count = 0;
LogUtils.info(TAG, "读取成功");
break;
} else {
count++;
if (count > len) {
LogUtils.info(TAG, "字节太多,超出范围");
bytes = new byte[0];
readBuffer = new byte[1];
count = 0;
}
}
} else {
count++;
bytes = BaseTypeUtils.byteMerger(bytes, readBuffer);
}
}
} else {
if (System.currentTimeMillis() - beginTime > maxTime) {
LogUtils.error(TAG, "读取超时2");
bytes = null;
count = 0;
break;
}
}
}
} catch (Exception ex) {
closePort(serialPort);
LogUtils.error(TAG, ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LogUtils.error(TAG,e);
}
}
}
LogUtils.log("从串口读取数据 结束");
//LogUtils.error(TAG, "串口返回的内容 hex = " + BaseTypeUtils.bytesToHexString(bytes));
return bytes;
} else {
LogUtils.log("从串口读取数据 结束 null");
return null;
}
/*if (isSafe(serialPort)) {
try {
//等待一会,要不然可能读取不到
Thread.sleep(10);
byte[] bytes = {};
byte[] readBuffer = new byte[1024];
int len;
byte[] tempBytes;
while ((len = serialPort.readBytes(readBuffer, readBuffer.length)) != 0) {
tempBytes = new byte[len];
System.arraycopy(readBuffer, 0, tempBytes, 0, len);
bytes = BaseTypeUtils.byteMerger(bytes, tempBytes);
// LogUtils.info(TAG, " len = " + len);
}
LogUtils.info(TAG, "读取的内容 hex = " + BaseTypeUtils.bytesToHexString(bytes));
return bytes;
} catch (InterruptedException e) {
LogUtils.error(TAG, e);
return null;
}
} else {
LogUtils.info(TAG, "读取的内容 = null");
return null;
}*/
/*if (isSafe(serialPort)) {
InputStream in = serialPort.getInputStream(); //use an input stream
byte[] bytes = {};
try {
Thread.sleep(10);
byte[] readBuffer = new byte[1];
while (in.available() > 0) { //As long as there is data coming in, read it
in.read(readBuffer);
bytes = ArrayUtils.concat(bytes, readBuffer);
}
in.close();
} catch (Exception ex) {
closePort(serialPort);
LogUtils.error(TAG, ex);
} finally {
}
LogUtils.error(TAG, "读取的内容 hex = " + BaseTypeUtils.bytesToHexString(bytes));
return bytes;
} else {
return null;
}*/
/*if (isSafe(serialPort)) {
try {
//等待一会,要不然可能读取不到
Thread.sleep(50);
System.out.println("Available: " + serialPort.bytesAvailable());
byte[] bytes = new byte[2048];
int numRead = serialPort.readBytes(bytes, bytes.length);
System.out.println("Read " + numRead + " bytes.");
byte[] res = new byte[numRead];
System.arraycopy(bytes, 0, res, 0, numRead);
LogUtils.error(TAG, "读取的内容 hex = " + BaseTypeUtils.bytesToHexString(res));
return res;
} catch (InterruptedException e) {
LogUtils.error(TAG, e);
return null;
}
} else {
LogUtils.info(TAG, "读取的内容 = null");
return null;
}*/
}
}