作者 钟来

Merge remote-tracking branch 'origin/master'

@@ -10,6 +10,8 @@ import com.zhonglai.luhui.smart.feeder.dto.commd.FeederCommdDto; @@ -10,6 +10,8 @@ import com.zhonglai.luhui.smart.feeder.dto.commd.FeederCommdDto;
10 import org.slf4j.Logger; 10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;
12 12
  13 +import java.io.IOException;
  14 +import java.io.InputStream;
13 import java.util.concurrent.BlockingQueue; 15 import java.util.concurrent.BlockingQueue;
14 import java.util.concurrent.LinkedBlockingQueue; 16 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.TimeUnit; 17 import java.util.concurrent.TimeUnit;
@@ -69,16 +71,31 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat @@ -69,16 +71,31 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat
69 @Override 71 @Override
70 public void serialEvent(SerialPortEvent event) { 72 public void serialEvent(SerialPortEvent event) {
71 if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) { 73 if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
72 - try {  
73 - // 读取串口数据  
74 - byte[] newData = new byte[serialPort.bytesAvailable()];  
75 - int numRead = serialPort.readBytes(newData, newData.length); 74 +// try {
  75 +// // 读取串口数据
  76 +// byte[] newData = new byte[serialPort.bytesAvailable()];
  77 +// int numRead = serialPort.readBytes(newData, newData.length);
  78 +//
  79 +// logger.info("串口返回{}字节数据:{}",numRead, ByteUtil.toHexString(newData));
  80 +// FeederCommdDto commdDto = new FeederCommdDto(newData);
  81 +// dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
  82 +// } catch (Exception e) {
  83 +// logger.error("读取串口数据时出错: " , e);
  84 +// }
  85 + String data = "";
  86 + if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE){
  87 + return;//判断事件的类型
  88 + }
  89 + SerialPort port = event.getSerialPort();
76 90
77 - logger.info("串口返回{}字节数据:{}",numRead, ByteUtil.toHexString(newData));  
78 - FeederCommdDto commdDto = new FeederCommdDto(newData); 91 + try {
  92 + Thread.sleep(500);
  93 + byte[] bytes = readFromPort(port);
  94 + logger.info("串口返回数据:"+ByteUtil.toHexString(bytes));
  95 + FeederCommdDto commdDto = new FeederCommdDto(bytes);
79 dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据 96 dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
80 } catch (Exception e) { 97 } catch (Exception e) {
81 - logger.error("读取串口数据时出错: " , e); 98 + logger.error("返回数据处理异常",e);
82 } 99 }
83 } 100 }
84 } 101 }
@@ -87,6 +104,7 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat @@ -87,6 +104,7 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat
87 public void disconnect() { 104 public void disconnect() {
88 if (isConnected) { 105 if (isConnected) {
89 try { 106 try {
  107 +
90 serialPort.removeDataListener(); 108 serialPort.removeDataListener();
91 serialPort.closePort(); 109 serialPort.closePort();
92 isConnected = false; 110 isConnected = false;
@@ -159,4 +177,38 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat @@ -159,4 +177,38 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat
159 byte[] bytes = ByteUtil.hexStringToByte(hexStr.replace(" ","").trim().toUpperCase()); 177 byte[] bytes = ByteUtil.hexStringToByte(hexStr.replace(" ","").trim().toUpperCase());
160 return sendByte(bytes); 178 return sendByte(bytes);
161 } 179 }
  180 +
  181 + private static byte[] readFromPort(SerialPort serialPort) throws Exception {
  182 +
  183 + InputStream in = null;
  184 + byte[] bytes = null;
  185 +
  186 + try {
  187 + if (serialPort != null) {
  188 + in = serialPort.getInputStream();
  189 + } else {
  190 + return null;
  191 + }
  192 + int bufflenth = in.available(); // 获取buffer里的数据长度
  193 + while (bufflenth > 0) {
  194 + bytes = new byte[bufflenth]; // 初始化byte数组为buffer中数据的长度
  195 + in.read(bytes);
  196 + bufflenth = in.available();
  197 + }
  198 + } catch (Exception e) {
  199 + throw e;
  200 + } finally {
  201 + try {
  202 + if (in != null) {
  203 + in.close();
  204 + }
  205 + } catch (IOException e) {
  206 + throw e;
  207 + }
  208 +
  209 + }
  210 +
  211 + return bytes;
  212 +
  213 + }
162 } 214 }