79383823

Date: 2025-01-24 09:28:21
Score: 12 🚩
Natty:
Report link
@Ashwani Garg
https://stackoverflow.com/a/50740694/23006962
    
I have the same problem as Ishan. 
As a client, I would like to read and write device values from a test server. From my first test server: 

https://sourceforge.net/projects/bacnetserver/ 

I got at least the answer that a device with its device ID after I added this option .withReuseAddress(true) to my IpNetwork. However, I get a BADTimeOut in this line:

DiscoveryUtils.getExtendedDeviceInformation(localDevice, device);

With my second test server BACsim from PolarSoft® Inc. and the same code I get the error message: java.net.BindException: Address already in use: Cannot bind. 

I am completely new to the Bacnet scene and was wondering why I need a LocalDevice as a client that only wants to read and write values from actual devices in the server.

Here is all my code:

IpNetwork network = new IpNetworkBuilder()
                .withLocalBindAddress("192.168.XX.X")
                .withBroadcast("192.168.56.X", 24)
                .withPort(47808)
                .withReuseAddress(true)
                .build();

        DefaultTransport transport = new DefaultTransport(network);
//        transport.setTimeout(1000);
//        transport.setSegTimeout(500);
        final LocalDevice localDevice = new LocalDevice(1, transport);
        System.out.println("Device: " + localDevice);
        
        localDevice.getEventHandler().addListener(new DeviceEventAdapter() {
            @Override
            public void iAmReceived(RemoteDevice device) {
                System.out.println("Discovered device " + device);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            try {

                               DiscoveryUtils.getExtendedDeviceInformation(localDevice, device);
                            } catch (BACnetException e) {
                                e.printStackTrace();
                            }
                            System.out.println(device.getName() + " " + device.getVendorName() + " " + device.getModelName() + " " + device.getAddress());

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

                            for (ObjectIdentifier id : value) {
                                List<ReadAccessSpecification> specs = new ArrayList<>();
                                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<>(specs));

                                ReadPropertyMultipleAck send = localDevice.send(device, 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(device, 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);
            }
        });

        try {
            localDevice.initialize();
        } catch (Exception e) {
            e.printStackTrace();
        }
        localDevice.sendGlobalBroadcast(new WhoIsRequest());

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

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        localDevice.terminate();


What am I doing wrong? I look forward to your answer! Many thanks in advance 
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): What am I doing wrong?
  • Blacklisted phrase (0.5): I need
  • Blacklisted phrase (1): I have the same problem
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (2): was wondering
  • RegEx Blacklisted phrase (3): thanks in advance
  • RegEx Blacklisted phrase (1): I get the error
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I have the same problem
  • Low reputation (1):
Posted by: Dominik