SpringBoot串⼝开发简单案例
标题 SpringBoot实现串⼝读取和发送数据
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /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.qqhxjmon</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,l配置⽂件
serialport:
baud-rate:115200
port-name: COM1
4,项⽬结构
5,项⽬中的代码(CodeConvertUtil和ProtocolUtil可以私发,因为代码较多)SerialBeanConfig
package;
import AnyDataReader;
import Bean;
import Configuration;
/**
* @Author: wangtao
* @CreateTime: 2021-01-27 08:56
* @Description: 将AnyDataReader放⼊Spring容器下进⾏管理
*/
@Configuration
public class SerialBeanConfig {
@Bean
AnyDataReader anyDataReader(){
return new AnyDataReader();
}
}
CustomDataProcessor
import SerialDataProcessor;
import CodeConvertUtil;
import ProtocolUtil;
import Slf4j;
import Component;
import 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){
<("转换异常");
}
}
}
CustomStringSerialDataParser
import StringSerialDataParser;
import Slf4j;
import Component;
import UnsupportedEncodingException;
/**
* @Author: wangtao
* @CreateTime: 2021-01-27 10:41
* @Description: ⾃定义串⼝解析类
*/
springboot结构@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){
<("格式转化错误");
}
return str;
}
}
SpringBootRxtxApplication
package;
import DefaultSerialDataListener;
import SerialContext;
import SpringApplication;
import SpringBootApplication;
@SpringBootApplication
public class SpringBootRxtxApplication {
public static void main(String[] args)throws Exception {
SpringApplication.run(SpringBootRxtxApplication.class, args);
SerialContext.setSerialPortEventListener(new DefaultSerialDataListener());
}
}
(本⼈研究串⼝代码也是研究了好⼏天,就当⾟苦费吧,购买之后,还有⾮SpringBoot版本的串⼝源码提供)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论