This commit is contained in:
Класс
2023-06-17 15:41:26 +03:00
parent 68b6bcbcab
commit 38f69f291a
24 changed files with 617 additions and 152 deletions
+14
View File
@@ -0,0 +1,14 @@
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'com.github.kochedykov', name: 'jlibmodbus', version: '1.2.9.0'
}
+25
View File
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":mbrtu" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="modbus_master_rtu" external.system.module.version="1.0" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" exported="" name="Gradle: com.github.kochedykov:jlibmodbus:1.2.9.0" level="project" />
<orderEntry type="library" exported="" name="Gradle: com.github.purejavacomm:purejavacomm:1.0.2.RELEASE" level="project" />
<orderEntry type="library" exported="" name="Gradle: org.scream3r:jssc:2.8.0" level="project" />
<orderEntry type="library" exported="" name="Gradle: org.rxtx:rxtx:2.1.7" level="project" />
<orderEntry type="library" exported="" name="Gradle: net.java.dev.jna:jna:4.2.2" level="project" />
</component>
</module>
+69
View File
@@ -0,0 +1,69 @@
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.ModbusSlave;
import com.intelligt.modbus.jlibmodbus.ModbusSlaveFactory;
import com.intelligt.modbus.jlibmodbus.data.DataHolder;
import com.intelligt.modbus.jlibmodbus.data.ModbusHoldingRegisters;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.serial.*;
import jssc.SerialPortList;
import java.io.Console;
public class MainSlave {
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(arg[arg.length - 1]);
sp.setBaudRate(SerialPort.BaudRate.BAUD_RATE_9600);
sp.setDataBits(8);
sp.setParity(SerialPort.Parity.NONE);
sp.setStopBits(1);
SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC());
ModbusSlave m = ModbusSlaveFactory.createModbusSlaveRTU(sp);
DataHolder dataHolder = new DataHolder();
ModbusHoldingRegisters registers = new ModbusHoldingRegisters(10);
registers.set(0, 101);
registers.set(1, 105);
registers.set(2, 660);
dataHolder.setHoldingRegisters(registers);
m.setDataHolder(dataHolder);
try {
m.listen();
int symbol = 0;
while (symbol >= 0) {
symbol = System.in.read();
if (symbol != 10)
registers.set(0, symbol);
}
} catch (ModbusIOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
}
+72
View File
@@ -0,0 +1,72 @@
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.serial.*;
import jssc.SerialPortList;
import java.util.logging.Logger;
public class main {
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(arg[arg.length - 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 = 3; //Количество условных портов
try {
int[] registerValues = m.readHoldingRegisters(slaveId, offset, quantity); // Настройка тип регситра(в данном случае установлены регистры хранения)
//Вывод
for (int value : registerValues) {
System.out.println("Адресс: " + offset + ", Значение: " + value);
offset++;
}
} catch (ModbusIOException mioE) {
Modbus.log().throwing("???", "???", mioE);
} catch (Exception e) {
Modbus.log().throwing("???", "???", e);
} finally {
try {
m.disconnect();
} catch (ModbusIOException mioE) {
Modbus.log().throwing("???", "???", mioE);
}
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
}