Skip to content

Example of mstp serial adapter #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions mstp-serial/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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>

<parent>
<groupId>org.code-house.bacnet4j</groupId>
<artifactId>bacnet4j-wrapper</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<artifactId>mstp-serial</artifactId>
<packaging>bundle</packaging>

<name>Code-House :: Bacnet4J Wrapper :: MSTP Serial adapter</name>
<description>Universal serial adapter for MSTP client.</description>

<properties>
<osgi.import>
org.code_house.bacnet4j.wrapper.mstp,
*
</osgi.import>
<osgi.export>
org.code_house.bacnet4j.wrapper.mstp
</osgi.export>
</properties>

<dependencies>
<dependency>
<groupId>org.code-house.bacnet4j</groupId>
<artifactId>mstp</artifactId>
</dependency>
<dependency>
<groupId>org.opensmarthouse</groupId>
<artifactId>purejavacomm</artifactId>
</dependency>
<dependency>
<groupId>com.infiniteautomation</groupId>
<artifactId>bacnet4j</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* (C) Copyright 2023 Code-House and others.
*
* bacnet4j-wrapper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0.txt
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.code_house.bacnet4j.wrapper.mstp.serial;

import com.serotonin.bacnet4j.npdu.mstp.MasterNode;
import com.serotonin.bacnet4j.npdu.mstp.MstpNetwork;
import com.serotonin.bacnet4j.util.sero.SerialPortWrapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.code_house.bacnet4j.wrapper.mstp.MstpNetworkBuilder;
import purejavacomm.CommPort;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;

public class MstpSerialNetworkBuilder extends MstpNetworkBuilder {

private final SerialPortManager serialPortManager;

public MstpSerialNetworkBuilder(SerialPortManager serialPortManager) {
this.serialPortManager = serialPortManager;
}

@Override
public MstpNetwork build() throws Exception {
CommPortIdentifier identifier = serialPortManager.getIdentifier(getSerialPort());

MasterNode node = new MasterNode(new ManagedSerialPort(identifier, getBaud(), getDataBits(), getStopBits(), getParity()),
(byte)this.getStation(), 2);
node.setMaxInfoFrames(5);
node.setUsageTimeout(100);
return new MstpNetwork(node, 0);
}

static class ManagedSerialPort extends SerialPortWrapper {

private final CommPortIdentifier identifier;
private final int baud;
private final int dataBits;
private final int stopBits;
private final int parity;
private SerialPort port;

ManagedSerialPort(CommPortIdentifier port, int baud, int dataBits, int stopBits, int parity) {
this.identifier = port;
this.baud = baud;
this.dataBits = dataBits;
this.stopBits = stopBits;
this.parity = parity;
}

@Override
public void close() throws Exception {
if (port != null) {
port.close();
}
}

@Override
public void open() throws Exception {
CommPort port = identifier.open("bacnet4j-wrapper", 2000);
if (!(port instanceof SerialPort)) {
throw new IllegalStateException("Given port is not a serial port");
}

this.port = (SerialPort) port;
this.port.setSerialPortParams(baud, dataBits, stopBits, parity);
}

@Override
public InputStream getInputStream() {
if (port != null) {
try {
return port.getInputStream();
} catch (IOException e) {
throw new RuntimeException("Could not open input stream for port " + identifier.getName(), e);
}
}
return null;
}

@Override
public OutputStream getOutputStream() {
if (port != null) {
try {
return port.getOutputStream();
} catch (IOException e) {
throw new RuntimeException("Could not open output stream for port " + identifier.getName(), e);
}
}
return null;
}

@Override
public String getCommPortId() {
return identifier.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* (C) Copyright 2023 Code-House and others.
*
* bacnet4j-wrapper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0.txt
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.code_house.bacnet4j.wrapper.mstp.serial;

import purejavacomm.CommPortIdentifier;
import purejavacomm.NoSuchPortException;

public class SerialPortManager {

public CommPortIdentifier getIdentifier(String serialPort) throws NoSuchPortException {
return CommPortIdentifier.getPortIdentifier(serialPort);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* (C) Copyright 2023 Code-House and others.
*
* bacnet4j-wrapper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* https://www.gnu.org/licenses/gpl-3.0.txt
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.code_house.bacnet4j.wrapper.mstp.serial;

import org.code_house.bacnet4j.wrapper.api.Device;
import org.code_house.bacnet4j.wrapper.api.DeviceDiscoveryListener;
import org.code_house.bacnet4j.wrapper.mstp.BacNetMstpClient;
import org.code_house.bacnet4j.wrapper.mstp.MstpNetworkBuilder;

/**
* Test of mstp client with serial adapter based on purejavacomm.
*/
class MstpSerialNetworkBuilderMain {

public static void main(String[] args) throws Exception {
BacNetMstpClient client = new BacNetMstpClient(
new MstpSerialNetworkBuilder(new SerialPortManager())
.withSerialPort("/dev/ttyUSB0")
.withBaud(9600)
.withDataBits((short) 8)
.withStopBits((short) 1)
.build(),
1339
);
client.listenForDevices(new DeviceDiscoveryListener() {
@Override
public void deviceDiscovered(Device device) {
System.out.println("Device discovered " + device);
}
});

client.start();
System.in.read();
client.stop();;
}

}
7 changes: 1 addition & 6 deletions mstp/pom.xml
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@

<properties>
<osgi.import>
jssc;resolution:=optional,
!jssc,
*
</osgi.import>
<osgi.export>
@@ -30,11 +30,6 @@
<groupId>org.code-house.bacnet4j</groupId>
<artifactId>api</artifactId>
</dependency>
<dependency>
<groupId>org.scream3r</groupId>
<artifactId>jssc</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@
<module>api</module>
<module>ip</module>
<module>mstp</module>
<module>mstp-serial</module>
<module>assembly</module>
</modules>

@@ -61,6 +62,7 @@
<bacnet4j.version>6.0.0</bacnet4j.version>
<sero-warp.version>1.0.0</sero-warp.version>
<sero-scheduler.version>1.1.0</sero-scheduler.version>
<purejavacomm.version>1.0.5</purejavacomm.version>
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
@@ -137,6 +139,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.opensmarthouse</groupId>
<artifactId>purejavacomm</artifactId>
<version>${purejavacomm.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>