标题 SpringBoot实现串口读取和发送数据
1,根据本地运行环境,下载对应的RXTX文件(下载地址:http://fizzed.com/oss/rxtx-for-java)
2,本地构建SpringBoot项目,pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zkjd.bd</groupId>
<artifactId>spring-boot-rxtx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-rxtx</name>
<description>SpringBootRxtx</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>cn.qqhxj.common</groupId>
<artifactId>spring-boot-starter-rxtx</artifactId>
<version>1.3.1-RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3,application.yml配置文件
serialport:
baud-rate: 115200
port-name: COM1
4,项目结构
5,项目中的代码(CodeConvertUtil和ProtocolUtil可以私发,因为代码较多)
- SerialBeanConfig
package com.zkjd.bd.springbootrxtx.config;
import cn.qqhxj.common.rxtx.reader.AnyDataReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: wangtao
* @CreateTime: 2021-01-27 08:56
* @Description: 将AnyDataReader放入Spring容器下进行管理
*/
@Configuration
public class SerialBeanConfig {
@Bean
AnyDataReader anyDataReader(){
return new AnyDataReader();
}
}
- CustomDataProcessor
package com.zkjd.bd.springbootrxtx.processor;
import cn.qqhxj.common.rxtx.processor.SerialDataProcessor;
import com.zkjd.bd.springbootrxtx.util.CodeConvertUtil;
import com.zkjd.bd.springbootrxtx.util.ProtocolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
* @Author: wangtao
* @CreateTime: 2021-01-26 20:02
* @Description: 自定义串口数据处理类,该数据为解析后返回的数据
*/
@Component
@Slf4j
public class CustomDataProcessor implements SerialDataProcessor<String> {
@Override
public void process(String s) {
try {
//进行报文分割
String[] ds = ProtocolUtil.splitInstruct(s);
//用户地址(设备卡号),去首位0
String sender = ds[1].trim().replaceAll("^(0+)", "");
//通信电文内容
String content = ds[4].trim();
//混合传输
if(content.startsWith("A4")){
content = CodeConvertUtil.hexStr2StrByGbk(content.substring(2));
}
System.out.println("报文内容===========>"+s);
} catch ( UnsupportedEncodingException e) {
log.error("转换异常");
}
}
}
- CustomStringSerialDataParser
package com.zkjd.bd.springbootrxtx.parser;
import cn.qqhxj.common.rxtx.parse.StringSerialDataParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
* @Author: wangtao
* @CreateTime: 2021-01-27 10:41
* @Description: 自定义串口解析类
*/
@Slf4j
@Component
public class CustomStringSerialDataParser extends StringSerialDataParser {
@Override
public String parse(byte[] bytes) {
String str = "";
try {
str = new String(bytes,"GBK");
log.info("解析后的数据为===>"+str);
} catch (UnsupportedEncodingException e){
log.error("格式转化错误");
}
return str;
}
}
- SpringBootRxtxApplication
package com.zkjd.bd.springbootrxtx;
import cn.qqhxj.common.rxtx.DefaultSerialDataListener;
import cn.qqhxj.common.rxtx.SerialContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRxtxApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootRxtxApplication.class, args);
SerialContext.setSerialPortEventListener(new DefaultSerialDataListener());
}
}
(本人研究串口代码也是研究了好几天,就当辛苦费吧,购买之后,还有非SpringBoot版本的串口源码提供)下载案例全部源码
注:Java使用rxtx进行多串口开发问题,可以参考:使用rxtx的纯Java实现库purejavacomm
针对jdk11+ 使用串口有两种方式:
方式一:
(1)jdk11没有对应的jre,可以命令生成jre(使用管理员cmd窗口)
bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre
将dll文件放入对应文件目录(<JAVA_HOME>\jre\bin)
(2)jdk11+生成的jre是没有ext文件目录的,可以将RXTXcomm.jar放入项目中,并添加到本地依赖(要把项目的jdk版本修改为jdk11+)
(3)启动项目并测试发送串口数据
方式二:
使用其他串口开发依赖 链接:nrjavaserial