spring-boot-starter-rxtx
spring boot 对串口的读取的快速办法
运用办法
- 一般項目请运用 common-rxtx
-
引进依托
<dependency> <groupId>cn.qqhxj.common</groupId> <artifactId>spring-boot-starter-rxtx</artifactId> <version>1.3.1-RELEASE</version> </dependency>
-
串口装备
serialport.baud-rate=115200 serialport.port-name=COM10
-
串口数据读取器装备(可选)
默许装备了 数据开始为
{
, 数据完毕为}
的数据解析器(VariableLengthSerialReader
)
系统还供给了以下四种数据读取器。-
AnyDataReader
读取一切的数据 -
ConstLengthSerialReader
读取定长的数据 -
VariableLengthSerialReader
读取有前后标识字符的数据 -
LiveControlSerialReader
读取有开始位、数据长度的数据
我们还能够依照自己的协议结束新的数据解析器
- 需求结束
SerialReader
接口 - 不要忘记要加入到spring的IOC容器中,才调对数据做处理哦
-
-
数据解析器装备(可选)
默许装备了 字符串的数据解析器(将数据读取器读取的数据直接转为字符串)
你们能够自己装备需求的解析器 示例如下 - 装备数据处理器
没有进行任何的默许装备
需求的请自行装备
- 要结束`SerialDataProcessor<T>` 接口在 `public void processor(T t)`办法中对数据做处理
- 要将该处理器加入到spring的IOC容器中。
装备办法如下所示
@Component
public class XXXProcessor implements SerialDataProcessor<String> {
@Override
public void processor(String s) {
System.out.println(s);
}
}
-
建议
@SpringBootApplication
public class RxtxDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RxtxDemoApplication.class, args);
while (true) {
;
}
}
}
发表评论