YIHE-TOOL
简述
目前它只是一个简单的工具,包含:
1、视频流工具,包含新增、删除、修改、查询等操作。
2、ip检测是否在线
3、读取bit位码
4、支持PLC之间通讯
JAR引入
<dependency>
<groupId>yihe-tool</groupId>
<artifactId>yihe-tool</artifactId>
<version>1.0.5</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/yihe-tool-1.0.5.jar</systemPath>
</dependency>
依赖引入
<!-- 一般项目中都有这两个 如果没有再去添加依赖 -->
<dependencies>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.14.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
使用指南
//初始化
MediaMtxConfig mediaMtxConfig = new MediaMtxConfig("http://10.10.19.112:9997", "http://10.10.19.112:8889");
//获取集合
System.out.println(mediaMtxConfig.readList().getBody());
// 获取单个视频流
System.out.println(mediaMtxConfig.readPath("rtsp01"));
// 添加264视频
System.out.println(mediaMtxConfig.addPathH264("rtsp02", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102"));
// 添加265视频
System.out.println(mediaMtxConfig.addPathH265("rtsp03", "rtsp://admin:hnks7777@10.92.10.6:554/Streaming/Channels/102"));
// 修改264视频
System.out.println(mediaMtxConfig.upPathH264("rtsp02", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102").getStatus());
// 修改265视频
System.out.println(mediaMtxConfig.upPathH265("rtsp03", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102").getStatus());
// 删除视频流
System.out.println(mediaMtxConfig.delPath("rtsp02").getStatus());
// 读取 bit 下标为0开始 第二位bit 为1==true 0==false
System.out.println(BitUtils.readBit(15, 1));
// 获取ip是否在线
System.out.println(IpPingUtils.isHostReachable("10.10.19.112"));
Springboot 使用
1、创建 application-hk.yml文件
mtx:
#流API
media-mtx-url: http://10.10.19.112:9997
#流地址
media-mtx-stream_url: http://10.10.19.112:8889
2、创建 MediaMtx.java 实体
@Configuration
@ConfigurationProperties(prefix = "mtx")
@Data
public class Mtx {
private String mediaMtxUrl;
private String mediaMtxStreamUrl;
}
3、配置 MediaMtxConfig
@Configuration
public class MtxConfig {
@Autowired
Mtx mtx;
@Bean(name = "mtxConfig")
public MediaMtxConfig mtxConfig() {
return new MediaMtxConfig(mtx.getMediaMtxUrl(), mtx.getMediaMtxStreamUrl());
}
}
4、使用方式1
@Api(value = "测试", tags = {"测试"})
@RestController
@RequestMapping("/hk/api/v1")
public class HkApiController {
@Autowired
private MediaMtxConfig mtxConfig;
@ApiOperation("测试")
@GetMapping(value = "/get")
public AjaxResult getOrganization() {
String s = mtxConfig.readPath("rtsp1133");
System.out.println(s);
return AjaxResult.success();
}
}
5、使用方式2
@Api(value = "测试", tags = {"测试"})
@RestController
@RequestMapping("/hk/api/v1")
public class HkApiController {
private final MediaMtxConfig mtxConfig;
public HkApiController(@Qualifier("mtx1") MediaMtxConfig mtxConfig) {
this.mtxConfig = mtxConfig;
}
@ApiOperation("测试")
@GetMapping(value = "/get")
public AjaxResult getOrganization() {
String s = mtxConfig.readPath("rtsp1133");
System.out.println(s);
return AjaxResult.success();
}
}
vue 使用
<iframe src="http://10.10.19.112:8889/rtsp01" scrolling="no" frameborder="0"></iframe>
PLC通讯相关
前言
- 支持单数据读写,多数据读写,大数据量自动分包读写
- 支持序列化批量多地址且地址不连续的读写
- 支持读写DB区,I区,Q区,M区,V区
- 支持读写西门子S1500,S1200,S400,S300,S200Smart,西门子机床828D
- 支持PLC自动重连
知识点
知识点1:访问数据类型与JAVA数据类型和PLC数据类型对应关系
访问数据类型 | 数据类型名称 | 数据大小[位] | 数据大小[字节] | JAVA数据类型 | PLC数据类型 | 示例 |
---|---|---|---|---|---|---|
boolean | 布尔类型 | 1 | 1/8 | Boolean | BOOL | true |
byte | 字节类型 | 8 | 1 | Byte | BYTE | 0x11 |
uint16 | 无符号2字节整型 | 16 | 2 | Integer | WORD/UINT | 65535 |
int16 | 有符号2字节整型 | 16 | 2 | Short | WORD/INT | -32760 |
uint32 | 无符号4字节整型 | 32 | 4 | Long | DWORD/UDINT | 70000 |
int32 | 有符号4字节整型 | 32 | 4 | Integer | DWORD/DINT | -70000 |
float32 | 4字节浮点型 | 32 | 4 | Float | REAL | 3.14 |
float64 | 8字节浮点型 | 64 | 8 | Double | LREAL | 3.14 |
string | 字符型 | 8 | 1 | String | String | ABC |
time | 时间/耗时 | 32 | 4 | Long | Time | 100ms |
date | 日期 | 16 | 2 | LocalDate | Date | 2023-04-03 |
timeOfDay | 一天中的时间 | 32 | 4 | LocalTime | TimeOfDay | 10:22:11 |
dtl | 日期+时间 | 96 | 12 | LocalDateTime | DTL | 2023-04-03 10:22:11 |
Springboot S7 使用
1、创建 application-hk.yml 文件
plc:
x: 192.168.10.180
d: 192.168.10.200
enabled: true
2、读取PLC基础信息
@Configuration
@ConfigurationProperties(prefix = "plc")
@Data
public class HkPlcProperties {
private String x;
private Boolean enabled;
private String d;
}
3、创建配置类
@Configuration
public class S7Config {
@Autowired
private HkPlcProperties plcProperties;
@Bean(name = "s7PLC1")
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public S7PLC s7PLC1() {
return new S7PLC(EPlcType.S1500, plcProperties.getX());
}
@Bean(name = "s7Serializer1")
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public S7Serializer s7Serializer1(@Qualifier("s7PLC1") S7PLC s7PLC1) {
return new S7Serializer(s7PLC1);
}
@Bean(name = "s7PLC2")
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public S7PLC s7PLC2() {
return new S7PLC(EPlcType.S1500, plcProperties.getD());
}
@Bean(name = "s7Serializer2")
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public S7Serializer s7Serializer2(@Qualifier("s7PLC2") S7PLC s7PLC2) {
return new S7Serializer(s7PLC2);
}
}
4、使用
@Component
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public class S7ComponentSelector {
private final S7PLC s7PLC1;
private final S7PLC s7PLC2;
private final S7Serializer s7Serializer1;
private final S7Serializer s7Serializer2;
@Autowired
HkPlcProperties plcProperties;
@Autowired
public S7ComponentSelector(@Qualifier("s7PLC1") S7PLC s7PLC1,
@Qualifier("s7PLC2") S7PLC s7PLC2,
@Qualifier("s7Serializer1") S7Serializer s7Serializer1,
@Qualifier("s7Serializer2") S7Serializer s7Serializer2) {
this.s7PLC1 = s7PLC1;
this.s7PLC2 = s7PLC2;
this.s7Serializer1 = s7Serializer1;
this.s7Serializer2 = s7Serializer2;
}
// 根据地址选择PLC
public S7PLC selectPLC(String address) {
if (address.equals(plcProperties.getX())) {
return s7PLC1;
} else {
return s7PLC2;
}
}
// 根据地址选择序列化器
public S7Serializer selectSerializer(String address) {
if (address.equals(plcProperties.getX())) {
return s7Serializer1;
} else {
return s7Serializer2;
}
}
}
Springboot ModbusTcp 使用
1、ModbusTcp 配置 注意 这只是其中一种写法 可以根据自己需求修改
import lombok.Getter;
@Configuration
public class ModbusTcpConfig {
@Getter
private final HashMap<String, Object> plcMap = new HashMap<>();
private final String plc_one = "192.168.1.88";
private final String plc_two = "10.10.12.253";
@Bean
public HashMap<String, Object> ModbusTcpPLC() throws RuntimeException {
if (StringUtils.isNotEmpty(plc_one) && StringUtils.isNotEmpty(plc_two)) {
ModbusTcp hPlc1 = new ModbusTcp(plc_one);
ModbusTcp hPlc2 = new ModbusTcp(plc_two);
plcMap.put("plc_one", hPlc1);
plcMap.put("plc_two", hPlc2);
return plcMap;
} else {
throw new RuntimeException("Plc is null");
}
}
}
2、使用案例 也可以 自己进行实体封装或者Map封装
@RestController
@RequestMapping("/api/get/plc")
public class HkPlcCom {
private static final Logger logger = LoggerFactory.getLogger(HkPlcCom.class);
@Autowired
private ModbusTcpConfig modbusTcpConfig;
@PostMapping("/info")
public AjaxResult info() {
try {
ModbusTcp plc = (ModbusTcp) modbusTcpConfig.getPlcMap().get("plc_one");
int 大车位置 = plc.readInt16(1, 0);
return AjaxResult.success(大车位置);
} catch (SocketRuntimeException e) {
logger.error("Socket runtime exception occurred: {}", e.getMessage());
return AjaxResult.error(e.getMessage());
} catch (Exception e) {
logger.error("An unexpected error occurred: {}", e.getMessage());
return AjaxResult.error(e.getMessage());
}
}
}