This can be both client and server
Inheritance: IDisposable
Exemple #1
0
 public BacnetAsyncResult(BacnetClient comm, BacnetAddress adr, byte invoke_id, byte[] transmit_buffer, int transmit_length, bool wait_for_transmit, int transmit_timeout)
 {
     m_transmit_timeout = transmit_timeout;
     m_adr = adr;
     m_wait_for_transmit = wait_for_transmit;
     m_transmit_buffer = transmit_buffer;
     m_transmit_length = transmit_length;
     AsyncWaitHandle = new System.Threading.ManualResetEvent(false);
     m_comm = comm;
     m_wait_invoke_id = invoke_id;
     m_comm.OnComplexAck += new BacnetClient.ComplexAckHandler(m_comm_OnComplexAck);
     m_comm.OnError += new BacnetClient.ErrorHandler(m_comm_OnError);
     m_comm.OnAbort += new BacnetClient.AbortHandler(m_comm_OnAbort);
     m_comm.OnSimpleAck += new BacnetClient.SimpleAckHandler(m_comm_OnSimpleAck);
     m_comm.OnSegment += new BacnetClient.SegmentHandler(m_comm_OnSegment);
 }
        /*****************************************************************************************************/
        static void handler_OnIam(BacnetClient sender, BacnetAddress adr, uint device_id, uint max_apdu, BacnetSegmentations segmentation, ushort vendor_id)
        {
            lock (DevicesList)
            {
                // Device already registred ?
                foreach (BacNode bn in DevicesList)
                    if (bn.getAdd(device_id) != null) return;   // Yes

                // Not already in the list
                DevicesList.Add(new BacNode(adr, device_id));   // add it
            }
        }
        public static void StartActivity()
        {
            // Bacnet on UDP/IP/Ethernet
            bacnet_client = new BacnetClient(new BacnetIpUdpProtocolTransport(0xBAC0, false), 3000, 3);
            // or Bacnet Mstp on COM4 à 38400 bps, own master id 8
            // m_bacnet_client = new BacnetClient(new BacnetMstpProtocolTransport("COM4", 38400, 8);
            // Or Bacnet Ethernet
            // bacnet_client = new BacnetClient(new BacnetEthernetProtocolTransport("Connexion au réseau local"));

            bacnet_client.Start();    // go

            // Send WhoIs in order to get back all the Iam responses :
            bacnet_client.OnIam += new BacnetClient.IamHandler(handler_OnIam);

            bacnet_client.WhoIs();
        }
Exemple #4
0
        private Task StartInternalAsync()
        {
            if (!Initialized)
            {
                _starTaskCompletionSource = new TaskCompletionSource<bool>();
                _bacnetClient = new BacnetClient(new BacnetMstpProtocolTransport(PortName, Baudrate, SourceAddress));

                _bacnetClient.Start(); // go

                // Send WhoIs in order to get back all the Iam responses :  
                _bacnetClient.OnIam += OnIam;
                _bacnetClient.WhoIs();
                Logger.Debug($"StartAsync(): Bacnet Client started / WhoIs sent. Waiting for IAm");
            }
            return _starTaskCompletionSource.Task;
        }
Exemple #5
0
        private void OnIam(BacnetClient sender, BacnetAddress adr, uint deviceId, uint maxApdu,
            BacnetSegmentations segmentation, ushort vendorId)
        {
            lock (_devicesList)
            {
                Logger.Debug($"OnIam(): DeviceId = {deviceId}");
                // Device already registred ?
                if (_devicesList.Any(bn => bn.GetAdd(deviceId) != null))
                {
                    return; // Yes
                }

                // Not already in the list
                _devicesList.Add(new BacNode(adr, deviceId)); // add it
                Initialized = true;
                if (deviceId == Settings.Default.BacnetMasterId)
                {
                    _starTaskCompletionSource.TrySetResult(true);
                }
            }
        }
Exemple #6
0
 private void m_comm_OnSimpleAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] data, int data_offset, int data_length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Exemple #7
0
        /// <summary>
        /// This is a simple handling that stores all segments in memory and assembles them when done
        /// </summary>
        private void PerformDefaultSegmentHandling(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, byte sequence_number, bool first, bool more_follows, byte[] buffer, int offset, int length)
        {
            if (first)
            {
                //clear any leftover segments
                m_segments.Clear();

                //copy buffer + encode new adpu header
                type &= ~BacnetPduTypes.SEGMENTED_MESSAGE;
                int adpu_header_len = 3;
                if ((type & BacnetPduTypes.PDU_TYPE_MASK) == BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST) adpu_header_len = 4;
                byte[] copy = new byte[length + adpu_header_len];
                Array.Copy(buffer, offset, copy, adpu_header_len, length);
                if ((type & BacnetPduTypes.PDU_TYPE_MASK) == BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST)
                    APDU.EncodeConfirmedServiceRequest(new EncodeBuffer(copy, 0), type, service, max_segments, max_adpu, invoke_id, 0, 0);
                else
                    APDU.EncodeComplexAck(new EncodeBuffer(copy, 0), type, service, invoke_id, 0, 0);
                m_segments.AddLast(copy);       //doesn't include BVLC or NPDU
            }
            else
            {
                //copy only content part
                byte[] copy = new byte[length];
                Array.Copy(buffer, offset, copy, 0, copy.Length);
                m_segments.AddLast(copy);
            }

            //process when finished
            if (!more_follows)
            {
                //assemble whole part
                byte[] apdu_buffer = AssembleSegments();
                m_segments.Clear();

                //process
                ProcessApdu(adr, type, apdu_buffer, 0, apdu_buffer.Length);
            }
        }
Exemple #8
0
 private void m_comm_OnSegment(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, byte sequence_number, bool first, bool more_follows, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = true;
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Exemple #9
0
 private void m_comm_OnError(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetErrorClasses error_class, BacnetErrorCodes error_code, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Error = new Exception("Error from device: " + error_class + " - " + error_code);
     }
 }
Exemple #10
0
 private void m_comm_OnComplexAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = false;
         m_result = new byte[length];
         if (length > 0) Array.Copy(buffer, offset, m_result, 0, length);
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();     //notify waiter even if segmented
     }
 }
Exemple #11
0
 private void m_comm_OnAbort(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, byte invoke_id, byte reason, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Error = new Exception("Abort from device: " + reason);
     }
 }
Exemple #12
0
 public void Dispose()
 {
     if (m_comm == null) return;
     m_comm.OnComplexAck -= m_comm_OnComplexAck;
     m_comm.OnError -= m_comm_OnError;
     m_comm.OnAbort -= m_comm_OnAbort;
     m_comm.OnSimpleAck -= m_comm_OnSimpleAck;
     m_comm.OnSegment -= m_comm_OnSegment;
     m_comm = null;
 }