即:当有数据时通知 serialPort.notifyOnDataAvailable(true); // 设置串口的一些

时间:2022-02-28 06:48:33

直接上参考代码,,对照直接,需要用到梗阻行列队伍,否则会呈现流读取不完整的情况,看简介的如上网站:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AmsApplication.class)
@WebAppConfiguration
@Transactional
@Component
public class SerialPortInitializer extends Thread implements ApplicationListener<ContextRefreshedEvent>, SerialPortEventListener {
// 监听器,我的理解是独立斥地一个线程监听串口数据
private CommPortIdentifier portId; // 串口通信打点类
private Enumeration<?> portList; // 有效连接上的端口的枚举
private InputStream inputStream; // 从串口来的输入流
private OutputStream outputStream;// 向串口输出的流
private SerialPort serialPort; // 串口的引用
// 堵塞行列队伍用来存放读到的数据
private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();

private Logger log = LoggerFactory.getLogger(getClass());

@Value("${ams.coredata.SerialPort:false}")
private boolean initialized;

@Autowired
private CorePublicAccountService corePublicAccountService;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("ams.coredata.SerialPort:" + initialized);
if (this.initialized) {
runSerialPort();
this.initialized = false;
}
}

private void runSerialPort() {
int i = this.startComPort();
if (i == 1) {
// 启动线程来措置惩罚惩罚收到的数据
this.start();
try {
String st = "哈哈----你好";
System.out.println("发出字节数:" + st.getBytes("gbk").length);
outputStream.write(st.getBytes("gbk"), 0, st.getBytes("gbk").length);
} catch (IOException e) {
e.printStackTrace();
}
} else {
return;
}
}

/**
*
* 通过措施打开COM4串口,设置监听器以及相关的参数
*
* @return 返回1 暗示端口打开告成,返回 0暗示端口打开掉败
*/
public int startComPort() {
// 通过串口通信打点类获恰当前连接上的串口列表
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {

// 获取相应串口东西
portId = (CommPortIdentifier) portList.nextElement();

System.out.println("设备类型:--->" + portId.getPortType());
System.out.println("设备名称:---->" + portId.getName());

log.info("设备类型:--->" + portId.getPortType());
log.info("设备名称:---->" + portId.getName());

// 判断端口类型是否为串口
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// 判断如果COM3串口存在,就打开该串口
try {
// 打开串口名字为COM3,延迟为2毫秒
serialPort = (SerialPort) portId.open(portId.getName(), 2000);

} catch (PortInUseException e) {
e.printStackTrace();
return 0;
}
// 设置当前串口的输入输出流
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
// 给当前串口添加一个监听器
try {

serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
return 0;
}
// 设置监听器生效,即:当有数据时通知
serialPort.notifyOnDataAvailable(true);

// 设置串口的一些读写参数
try {
// 比特率、数据位、遏制位、奇偶校验位
// serialPort.setInputBufferSize(MAX_PRIORITY);
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
return 0;
}

return 1;
}
}
return 0;
}

@Override
public void run() {
try {
System.out.println("--------------任务措置惩罚惩罚线程运行了--------------");
while (true) {
// 如果堵塞行列队伍中存在数据就将其输出
if (msgQueue.size() > 0) {
System.out.println(msgQueue.take());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void serialEvent(SerialPortEvent event) {
AccountSyncFieldsDTO accountSyncFieldsDTO = new AccountSyncFieldsDTO();
CorePublicAccountDTO corePublicAccountDTO = new CorePublicAccountDTO();

switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据

byte[] readBuffer = new byte[1024];
int numBytes = -1;
ByteArrayOutputStream result = new ByteArrayOutputStream();

try {
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
if (numBytes >= 0) {
msgQueue.add(new Date() + "真实收到的数据为:-----"
+ new String(readBuffer));
result.write(readBuffer, 0, numBytes);
readBuffer = new byte[1024];
} else {
msgQueue.add("额------没有读到数据");
}

}

String out = new String(result.toByteArray());
System.out.println("zip:" + out);
String unCompressStr = GZIPUtils.unCompress(out);
System.out.println("unzip:" + unCompressStr);
JSONObject jsonObject = JSONObject.parseObject(unCompressStr);

accountSyncFieldsDTO= (AccountSyncFieldsDTO)JSONObject.toJavaObject(jsonObject,AccountSyncFieldsDTO.class);
BeanUtils.copyProperties(accountSyncFieldsDTO, corePublicAccountDTO);
setProperties(accountSyncFieldsDTO, corePublicAccountDTO);

corePublicAccountService.save(Long.parseLong(corePublicAccountDTO.getCreatedBy()),
corePublicAccountDTO);
System.out.println("生存告成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

package com.idea.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;