Другая версия master на другой библиотеке
Используется библиотека jlibmodbus. pom.xml добавлен для того, что бы эта библиотека работала. Подключена библиотека jssc для того, чтобы выводить список портов(нужно добавлять вручную). Сборка на Maven. SDK проекта: openjdk-20. Для проверки использовалась программа Virtual Serial Port Driver. С помощбю нее создаем пару COM-портов Slave'ом служила программа Modbus Slave.
This commit is contained in:
parent
52c755cbd4
commit
68b6bcbcab
75
Modbus_RTU_Master_jlibmodbus/MasterRTU.java
Normal file
75
Modbus_RTU_Master_jlibmodbus/MasterRTU.java
Normal file
@ -0,0 +1,75 @@
|
||||
package org.example;
|
||||
|
||||
import com.intelligt.modbus.jlibmodbus.Modbus;
|
||||
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
|
||||
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
|
||||
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
|
||||
import com.intelligt.modbus.jlibmodbus.serial.*;
|
||||
import jssc.SerialPortList;
|
||||
|
||||
public class MasterRTU {
|
||||
|
||||
static public void main(String[] arg) {
|
||||
SerialParameters sp = new SerialParameters();
|
||||
Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
|
||||
|
||||
try {
|
||||
//Вывод доступных портов
|
||||
String[] dev_list = SerialPortList.getPortNames();
|
||||
|
||||
System.out.println("Доступные порты:");
|
||||
|
||||
for (int n = 0; n < dev_list.length; n++) {
|
||||
System.out.println(dev_list[n]);
|
||||
}
|
||||
|
||||
if (dev_list.length > 0) {
|
||||
//Настройка
|
||||
sp.setDevice(dev_list[1]);
|
||||
sp.setBaudRate(SerialPort.BaudRate.BAUD_RATE_9600);
|
||||
sp.setDataBits(8);
|
||||
sp.setParity(SerialPort.Parity.NONE);
|
||||
sp.setStopBits(1);
|
||||
|
||||
//Создание и подключение через jssc
|
||||
SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC());
|
||||
ModbusMaster m = ModbusMasterFactory.createModbusMasterRTU(sp);
|
||||
m.connect();
|
||||
|
||||
int slaveId = 1; // id устройства slave
|
||||
int offset = 0; // Начальный адресс
|
||||
int quantity = 10; //Количество условных портов
|
||||
|
||||
try {
|
||||
int[] registerValues = m.readHoldingRegisters(slaveId, offset, quantity); // Настройка тип регситра(в данном случае установлены регистры хранения)
|
||||
|
||||
//Вывод
|
||||
for (int value : registerValues) {
|
||||
System.out.println("Адресс: " + offset + ", Значение: " + value);
|
||||
|
||||
if (offset == 0 && value == 101) {
|
||||
System.out.println("Hello");
|
||||
}
|
||||
|
||||
offset++;
|
||||
}
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
m.disconnect();
|
||||
} catch (ModbusIOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
BIN
Modbus_RTU_Master_jlibmodbus/jssc-2.9.4.jar
Normal file
BIN
Modbus_RTU_Master_jlibmodbus/jssc-2.9.4.jar
Normal file
Binary file not shown.
31
Modbus_RTU_Master_jlibmodbus/pom.xml
Normal file
31
Modbus_RTU_Master_jlibmodbus/pom.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>ModbusMasterJlibModbus</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.intelligt.modbus</groupId>
|
||||
<artifactId>jlibmodbus</artifactId>
|
||||
<version>1.2.9.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.0-alpha0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user