0

我是 BACnet 和其他自动化协议的新手。我们将编写一个 BACnet 客户端,该客户端预计将连接到 BACnet 接口以获取对象,然后我们将在微服务层中摄取这些对象。我们的服务器基础架构包含 LumInsight将数据推送到 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();
        }

    }
4

3 回答 3

0

理论上,您只能使用相同的(套接字)端口#如果您已将套接字设置为“非独占”并用于“重用” - 在您“绑定”到它之前,那么尝试可能没有意义共享它,因为只有 1 个侦听器/“客户端”可能会从该 UDP 端口接收响应。

始终考虑在另一个端口上发送(“客户端”)请求;并在标准端口# - 47808/0xBAC0 上唯一地监听(“服务器”响应),如果可以的话(-假设它还没有被积极使用/你不能放弃端口的现有使用来代替这个/您的新用途/需求)。

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

于 2019-02-09T10:33:10.530 回答
0

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

最简单的解决方案将您的本地默认端口更改为 47809 或高于 1024 的任何其他端口。

万一你还有什么问题。请回复评论,我已经使用 BACnet4j 创建了几个客户端。

于 2018-06-07T12:01:50.297 回答
0

该错误是不言自明的“地址已在使用中”,请检查是否有任何其他客户端正在运行并使用端口 47808(默认端口)。

于 2017-10-03T10:15:44.987 回答