为了总结我的问题,我从jserialcomm库的串行端口获取了一些数据。这些数据是不断流动的数据,有时数据可能不完整或过剩。我想从这些数据中获取一个干净的数据,并通过spring boot将其作为web服务发送到tcp端口,但不幸的是,我得到了一个不规则的数组。当我再次触发它的抛出 ConcurrentModificationException
. 我无法按需要控制此数组并将清晰数据发送到: http://localhost:8090/value
.
结果:
packetlistenerclass类:
public class PacketListener implements SerialPortDataListener {
Logger logger = Logger.getLogger(PacketListener.class.getName());
public String str;
public List<String> collector = new ArrayList<>();
int counter = 0;
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent serialPortEvent) {
while (counter <10) {
byte[] newData = serialPortEvent.getReceivedData();
logger.log(Level.INFO, "received Data : {0}", new String(newData));
collector.add(new String(newData));
counter++;
}
}
}
端口服务类:
@Service
public class PortService implements PortManager {
Logger logger = Logger.getLogger(PortService.class.getName());
Entities entities;
@Override
public void portReader() {
SerialPort comPort = SerialPort.getCommPorts()[0];
setProperties(comPort);
comPort.openPort();
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING,0,0);
PacketListener packetListener = new PacketListener();
comPort.addDataListener(packetListener);
}
@Override
public SerialPort setProperties(SerialPort serialPort) {
entities = new Entities();
serialPort.setBaudRate(entities.getBaudRate());
serialPort.setParity(Parity.NONE.getValue());
serialPort.setNumDataBits(entities.getDataBits());
serialPort.setNumStopBits(entities.getStopBits());
return serialPort;
}
}
portcontroller类:
@Controller
public class PortController {
@Autowired
PortManager portService;
PacketListener packetListener = new PacketListener();
@GetMapping(value = "/value")
@ResponseBody
public List<String> getValue(){
portService.portReader();
return packetListener.collector;
}
}
我想控制一切 packetListener.collector
数组或它可以是任何字符串值,如果我可以确定它的正确性了。
暂无答案!
目前还没有任何答案,快来回答吧!