BACnet客户端未使用BACnet4j连接到BACnet接口

时间:2017-07-28 08:03:30

标签: java networking bacnet bacnet4j

我是BACnet和其他自动化协议的新手。我们将编写一个BACnet客户端,预计将连接到BACnet接口以获取对象,并进一步我们将在我们的微服务层中摄取这些对象。我们的服务器基础设施包含LumInsight Desktop,它将数据推送到BACnet接口。我们指的是一个示例代码,但在运行它时,我遇到了问题:

> inside main....com.serotonin.bacnet4j.transport.DefaultTransport@7b7221a0
inside main....3996: BACnet device
Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
    at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
    at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
    at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
    at java.net.DatagramSocket.bind(Unknown Source)
    at java.net.DatagramSocket.<init>(Unknown Source)
    at com.serotonin.bacnet4j.npdu.ip.IpNetwork.initialize(IpNetwork.java:215)
    at com.serotonin.bacnet4j.transport.DefaultTransport.initialize(DefaultTransport.java:183)
    at com.serotonin.bacnet4j.LocalDevice.initialize(LocalDevice.java:228)
    at com.bacnet.Main.main(Main.java:110)


    package com.bacnet;

    import com.serotonin.bacnet4j.LocalDevice;
    import com.serotonin.bacnet4j.RemoteDevice;
    import com.serotonin.bacnet4j.ServiceFuture;
    import com.serotonin.bacnet4j.event.DeviceEventAdapter;
    import com.serotonin.bacnet4j.exception.BACnetException;
    import com.serotonin.bacnet4j.exception.ErrorAPDUException;
    import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
    import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyAck;
    import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyMultipleAck;
    import com.serotonin.bacnet4j.service.confirmed.*;
    import com.serotonin.bacnet4j.service.unconfirmed.WhoIsRequest;
    import com.serotonin.bacnet4j.transport.DefaultTransport;
    import com.serotonin.bacnet4j.transport.Transport;
    import com.serotonin.bacnet4j.type.constructed.ReadAccessResult;
    import com.serotonin.bacnet4j.type.constructed.ReadAccessSpecification;
    import com.serotonin.bacnet4j.type.constructed.SequenceOf;
    import com.serotonin.bacnet4j.type.enumerated.ObjectType;
    import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
    import com.serotonin.bacnet4j.type.enumerated.Segmentation;
    import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
    import com.serotonin.bacnet4j.type.primitive.Real;
    import com.serotonin.bacnet4j.util.DiscoveryUtils;
    import com.serotonin.bacnet4j.RemoteObject;

    import java.util.ArrayList;
    import java.util.List;

    public class Main {
        public static void main(String[] args) throws Exception {

            IpNetwork network = new IpNetwork("10.44.55.5", IpNetwork.DEFAULT_PORT);

            Transport transport = new DefaultTransport(network);
            System.out.println("inside main...." + transport);
            transport.setTimeout(500);
            transport.setSegTimeout(150);
            final LocalDevice localDevice = new LocalDevice(3996, transport);
            System.out.println("inside main...." + localDevice);
            localDevice.getEventHandler().addListener(new DeviceEventAdapter() {
                @Override
                public void iAmReceived(RemoteDevice device) {
                    System.out.println("inside I am received...");
                    System.out.println("Discovered device " + device);
                    localDevice.addRemoteDevice(device);

                    final RemoteDevice remoteDevice = localDevice
                            .getRemoteDevice(device.getAddress());

                    remoteDevice
                            .setSegmentationSupported(Segmentation.segmentedBoth);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                try {
                                    DiscoveryUtils.getExtendedDeviceInformation(
                                            localDevice, remoteDevice);
                                } catch (BACnetException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(remoteDevice.getName() + " "
                                        + remoteDevice.getVendorName() + " "
                                        + remoteDevice.getModelName() + " "
                                        + remoteDevice.getAddress() + " "
                                        + remoteDevice.getProtocolRevision() + " "
                                        + remoteDevice.getProtocolVersion());

                                ReadPropertyAck ack = localDevice.send(
                                        remoteDevice,
                                        new ReadPropertyRequest(remoteDevice
                                                .getObjectIdentifier(),
                                                PropertyIdentifier.objectList))
                                        .get();
                                SequenceOf<ObjectIdentifier> value = ack.getValue();

                                for (ObjectIdentifier id : value) {

                                    List<ReadAccessSpecification> specs = new ArrayList<ReadAccessSpecification>();
                                    specs.add(new ReadAccessSpecification(id,
                                            PropertyIdentifier.presentValue));
                                    specs.add(new ReadAccessSpecification(id,
                                            PropertyIdentifier.units));
                                    specs.add(new ReadAccessSpecification(id,
                                            PropertyIdentifier.objectName));
                                    specs.add(new ReadAccessSpecification(id,
                                            PropertyIdentifier.description));
                                    specs.add(new ReadAccessSpecification(id,
                                            PropertyIdentifier.objectType));
                                    ReadPropertyMultipleRequest multipleRequest = new ReadPropertyMultipleRequest(
                                            new SequenceOf<ReadAccessSpecification>(
                                                    specs));

                                    ReadPropertyMultipleAck send = localDevice
                                            .send(remoteDevice, multipleRequest)
                                            .get();
                                    SequenceOf<ReadAccessResult> readAccessResults = send
                                            .getListOfReadAccessResults();

                                    System.out.print(id.getInstanceNumber() + " "
                                            + id.getObjectType() + ", ");
                                    for (ReadAccessResult result : readAccessResults) {
                                        for (ReadAccessResult.Result r : result
                                                .getListOfResults()) {
                                            System.out.print(r.getReadResult()
                                                    + ", ");
                                        }
                                    }
                                    System.out.println();
                                }

                                ObjectIdentifier mode = new ObjectIdentifier(
                                        ObjectType.analogValue, 11);

                                ServiceFuture send = localDevice.send(remoteDevice,
                                        new WritePropertyRequest(mode,
                                                PropertyIdentifier.presentValue,
                                                null, new Real(2), null));
                                System.out.println(send.getClass());
                                System.out.println(send.get().getClass());

                            } catch (ErrorAPDUException e) {
                                System.out.println("Could not read value "
                                        + e.getApdu().getError() + " " + e);
                            } catch (BACnetException e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();
                }

                @Override
                public void iHaveReceived(RemoteDevice device, RemoteObject object) {
                    System.out.println("Value reported " + device + " " + object);
                }
            });

            localDevice.initialize();
            localDevice.sendGlobalBroadcast(new WhoIsRequest());

            List<RemoteDevice> remoteDevices = localDevice.getRemoteDevices();
            for (RemoteDevice device : remoteDevices) {
                System.out.println("Remote dev " + device);
            }

            System.in.read();
            localDevice.terminate();
        }

    }

3 个答案:

答案 0 :(得分:0)

错误是自解释的&#34;地址已在使用&#34;,请检查是否有任何其他客户端正在运行并使用端口47808(默认端口)。

答案 1 :(得分:0)

请在日食中终止所有当前正在运行的任务后重新运行程序。或者使用netstat -a来检查哪个应用程序正在获取47808。

最简单的解决方案是将本地默认端口更改为47809或1024以上的其他端口。

如果您还有任何问题。请回复,我使用BACnet4j创建了几个客户端。

答案 2 :(得分:0)

理论上,如果您将套接字设置为“非排他”并且为了“重用”,则只能使用相同的(套接字)端口号,然后再“绑定”到该端口,但是可能没有试图共享它,因为只有1个侦听器/“客户端”可能会从该UDP端口接收响应。

无论如何,始终考虑在另一个端口上发送(“客户端”)请求;并在标准端口号-47808 / 0xBAC0上唯一侦听(“服务器”响应)(如果可以)(-假设它尚未被积极使用/您不能放弃端口号的现有用法来代替此/您的新用法/需求。

对于Windows,您可以使用“命令提示符”中的“ netstat -ao” cmd-line工具查看持有/使用端口号的应用程序的“ PID”(进程ID),然后您可以在Windows“任务管理器”中显示PID列,以查看PID映射到的应用。