Automation and Events

deniliadenilia Member Posts: 23
hi all

I wonder is it possible to make a Navision client sit and listen for event from external module without a loop? I have a DLL that gets the data from the serial communication, process it, and set ready for use.
Right now, I have Navision in infinite loop waiting for the data. What I need is some sort of event trigger from Navision and i'm not sure how to do so.

Thank you

Comments

  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    Your automation variable has a property "WithEvents"
  • deniliadenilia Member Posts: 23
    Ok, I found that property. i started to search the forums to find some examples how it's implemented. But I cannot see my triggers like some people say.

    This is a stripped DLL code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO.Ports;
    using System.Runtime.InteropServices;
    
    namespace Navision
    {
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface iSerial
        {
            [DispIdAttribute(1)]
            void OpenSerialPort();
            void CloseSerialPort();
            string GetString();
            
    
        }
        [ClassInterface(ClassInterfaceType.None)]
        public class Serial : iSerial
        {
           
            private SerialPort sp;
            private string SerialString;
            public Serial()
            {
            }
            public void OpenSerialport()
            {
                try
                {
                    sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
                    sp.DataReceived += new SerialDataReceivedEventHandler(this.sp_DataReceived);
                    sp.ReceivedBytesThreshold = 1;
                    sp.Handshake = Handshake.None;
                    sp.ReadTimeout = 200;
                    sp.Open();
                }
                catch   {  }     
            }
            private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                if (e.EventType == SerialData.Chars)
                {
                   
                    SetString = Data;             
    
                }           
            }
            public string SetString
            {
                get
                { return SerialString; }
                set 
                { SerialString = value; }
    
            }
            public string GetString()
            {
                string tmp = SetString;
                return tmp;
            }
           
            public void StopSerialGun()
            {
                sp.Close();            
            }        
        }
    }
    

    What am I doing wrong? and Where in C/AL I can see that WithEvents trigger?
  • lubostlubost Member Posts: 627
    You have to generate event in your DLL.
    In your code is nothing to fire any event which can send a data to Navision. By the way, I think that safer way is to save the received data into a file and create timer loop in Navision to check for that file(s).
  • deniliadenilia Member Posts: 23
    Well, it needs to be real time, so i cannot use the file. however I did revise the code, and now i can see the triggers in Navision. But it does not fire the event. I can see that the event function is called, but nothing in Navision
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO.Ports;
    using System.Runtime.InteropServices;
    
    
    namespace NAV_Serial_Port
    {
        
        [ComVisible(false)] 
        public delegate void GetSerialtring();
    
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
        public interface SerialEvents
        {
            [DispIdAttribute(1)]
            event GetSerialString MyEvent;        
            void FireEvent();
            
        } 
    
        
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface iSerial
        {
            [DispIdAttribute(2)]
            void OpenSerialPort();
            void StopSerialport();        
            string GetString(); 
    
        }
        
        [ClassInterface(ClassInterfaceType.None)]
        [ComSourceInterfaces(typeof(SerialEvents))]
        public class Serial : iSerial    {
           
            private SerialPort sp;
            private string SerialString;
            public event GetSerialString MyEvent;
           
            public Serial()
            {
            }
            
            public void FireEvent()
            {
                if (MyEvent != null)
                    MyEvent();
                
            }
            public void CallEvent()
            {
                Serial s = new Serial();
                s.MyEvent += new GetSerialString(f);
                s.FireEvent();
                s.MyEvent -= new GetSerialString(f);           
    
            }
            public void f()
            {                      
            }
    
            
            public void OpenSerialPort()
            {
                try
                {
                    sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
                    sp.DataReceived += new SerialDataReceivedEventHandler(this.sp_DataReceived);
                    sp.ReceivedBytesThreshold = 1;
                    sp.Handshake = Handshake.None;
                    sp.ReadTimeout = 200;
                    sp.Open();
                }
                catch
                {               
                }    
    
            }
            public void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                if (e.EventType == SerialData.Chars)
                {
                    
                     SetString = Data;
                      callEvent();                  
    
                }                
                    
            }
            
            public string SetString
            {
                get
                { return GunString; }
                set 
                { GunString = value; }
    
            }
            public string GetString()
            {
                string tmp = SetString;
                return tmp;
            }
           
            public void StopSerialport()
            {
                try
                {
                    sp.Close();
                    sp.Dispose();
                }
                catch {}
            }
    
            
        }
    }
    
  • deniliadenilia Member Posts: 23
    I figured it out. Thanks
  • absolutelyfreewebabsolutelyfreeweb Member Posts: 104
    what was wrong?
  • kenethkeneth Member Posts: 3
    I am facing the same problem. How did you solve it, Denilia?
Sign In or Register to comment.