正在显示
1 个修改的文件
包含
58 行增加
和
7 行删除
| @@ -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 | } |
| @@ -159,4 +176,38 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat | @@ -159,4 +176,38 @@ public class SerialPortService implements com.fazecast.jSerialComm.SerialPortDat | ||
| 159 | byte[] bytes = ByteUtil.hexStringToByte(hexStr.replace(" ","").trim().toUpperCase()); | 176 | byte[] bytes = ByteUtil.hexStringToByte(hexStr.replace(" ","").trim().toUpperCase()); |
| 160 | return sendByte(bytes); | 177 | return sendByte(bytes); |
| 161 | } | 178 | } |
| 179 | + | ||
| 180 | + private static byte[] readFromPort(SerialPort serialPort) throws Exception { | ||
| 181 | + | ||
| 182 | + InputStream in = null; | ||
| 183 | + byte[] bytes = null; | ||
| 184 | + | ||
| 185 | + try { | ||
| 186 | + if (serialPort != null) { | ||
| 187 | + in = serialPort.getInputStream(); | ||
| 188 | + } else { | ||
| 189 | + return null; | ||
| 190 | + } | ||
| 191 | + int bufflenth = in.available(); // 获取buffer里的数据长度 | ||
| 192 | + while (bufflenth > 0) { | ||
| 193 | + bytes = new byte[bufflenth]; // 初始化byte数组为buffer中数据的长度 | ||
| 194 | + in.read(bytes); | ||
| 195 | + bufflenth = in.available(); | ||
| 196 | + } | ||
| 197 | + } catch (Exception e) { | ||
| 198 | + throw e; | ||
| 199 | + } finally { | ||
| 200 | + try { | ||
| 201 | + if (in != null) { | ||
| 202 | + in.close(); | ||
| 203 | + } | ||
| 204 | + } catch (IOException e) { | ||
| 205 | + throw e; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + return bytes; | ||
| 211 | + | ||
| 212 | + } | ||
| 162 | } | 213 | } |
-
请 注册 或 登录 后发表评论