ela-compil / BACnet

BACnet protocol library for .NET :satellite:
https://www.nuget.org/packages/bacnet/
MIT License
217 stars 96 forks source link

MSTP bacnetAdress #70

Closed Marwen09 closed 3 years ago

Marwen09 commented 3 years ago

Hello I tried to create a bacnetAdress with MSTP protocol but there are bacnetAdress only for IP and Ethernet , any idea? public BacnetAddress(BacnetAddressTypes addressType, string address = null, ushort network = 0) : this(addressType, network) { if (address == null) return;

        switch (type)
        {
            case BacnetAddressTypes.IP:
                adr = new byte[6];
                var addressParts = address.Split(':');
                var addressBytes = IPAddress.Parse(addressParts[0]).GetAddressBytes();
                Array.Copy(addressBytes, adr, addressBytes.Length);

                var portBytes = BitConverter.GetBytes(addressParts.Length > 1
                    ? ushort.Parse(addressParts[1])
                    : (ushort)0xBAC0);

                if (BitConverter.IsLittleEndian)
                    portBytes = portBytes.Reverse().ToArray();

                Array.Copy(portBytes, 0, adr, addressBytes.Length, portBytes.Length);
                break;

            case BacnetAddressTypes.Ethernet:
                adr = PhysicalAddress.Parse(address).GetAddressBytes();
                break;

            default:
                throw new NotSupportedException("String format is not supported for address type " + type);
        }
    }
gralin commented 3 years ago

Hi. For some reason the original author didn't include option of providing MSTP address using this constructor. I suggest you use the other one like this:

var address = new BacnetAddress(BacnetAddressTypes.MSTP, network: 0, address: new byte[] { 1 });