Home > Mobile >  How to enable part of a code, specifically another event handler, in C# after clicking a button
How to enable part of a code, specifically another event handler, in C# after clicking a button

Time:11-09

I have a scenario where I am connecting to server over TCP/IP protocol. This needs to be done after I click the "Connect to TCP/IP server" button on my Windows form. Upon clicking this button the status of the connection, either successful or failed, is displayed. This part I managed quite easily. What I need is that when the connection is successful, only then the program/code needs to start listening to the incomming messages from server and procede futher. I tested if I can get server messages without the button (i.e., when the form initializes it automatically connect to the server) and it worked fine. You will see in the code below that I am using reference commands to a third party software on the same PC which handles the incomming messages from the server. The reason for the third party software is simply beacuse I am using VisualStudio and C# to extend the capabilities of the software which is allowed by this software in their documentation. I will indeed be writing some of the variables to this third party software. This I am also fine with doing. Hence, the only problem I have is to only enable the listening of the incomming meassage from the server when the connect button is pressed and the connection is successful.

Any help will be much appreciated.

Here is my code so far, but it does not work: `

namespace Form1
{
    public partial class Main_Form: Form
    {
        private CogJob Job;
        private CogJobManager Job_manager;
        private CogJobIndependent Job_independent;

        String Received_string;
        bool State_of_connection = false;
        ICogIOTCPIP TCP_IP_server_device;
        
        public Main_HMI()
        {
            InitializeComponent();

            //Load application
            Job_manager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"Project"));

            // Initialize job variables
            Job = Job_manager.Job("Application");
            Job_independent = Job.OwnedIndependent;

            // Flush all job queues
            Job.ImageQueueFlush();
            Job_manager.UserQueueFlush();
            Job_manager.FailureQueueFlush();
            Job_independent.RealTimeQueueFlush();

            if (State_of_connection == true)
            {
                TCP_IP_server_device.MessageReceived  = new     CogIOStreamMessageEventHandler(TCP_IP_server_device_MessageReceived);
            }
            
        }
        
        // Decode the message received from the TCP/IP server device
        private void TCP_IP_server_device_MessageReceived(object sender, CogIOStreamMessageEventArgs eventArgs)
        {
            Received_string = eventArgs.DecodedMessage.Substring(2, 9);
        }

        // Savely shutdown application job manager when the form is closed;
        private void Main_HMI_FormClosing(object sender, FormClosingEventArgs e)
        {
            Job_manager.Shutdown();
        }

        private void Connect_to_TCP_IP_server_button_Click(object sender, EventArgs e)
        {
            Job_manager.IOEnable = true;
            TCP_IP_server_device = Job_manager.StreamInput("PLC", 2000, true);

            if (TCP_IP_server_device == null)
            {
                MessageBox.Show("Connection failed", "TCP/IP server device connection status");
            }

            else
            {
                MessageBox.Show("Connection successful", "TCP/IP server device connection status");
                State_of_connection = true;
            }
        }
    }
}

`

CodePudding user response:

The solution is quite easy:

namespace Form1;

public partial class Main_Form: Form
{
    private CogJob Job;
    private CogJobManager Job_manager;
    private CogJobIndependent Job_independent;

    String Received_string;
    ICogIOTCPIP TCP_IP_server_device;
    
    public Main_HMI()
    {
        InitializeComponent();

        //Load application
        Job_manager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"Project"));

        // Initialize job variables
        Job = Job_manager.Job("Application");
        Job_independent = Job.OwnedIndependent;

        // Flush all job queues
        Job.ImageQueueFlush();
        Job_manager.UserQueueFlush();
        Job_manager.FailureQueueFlush();
        Job_independent.RealTimeQueueFlush();
    }
    
    // Decode the message received from the TCP/IP server device
    private void TCP_IP_server_device_MessageReceived(object sender, CogIOStreamMessageEventArgs eventArgs)
    {
        Received_string = eventArgs.DecodedMessage.Substring(2, 9);
    }

    // Savely shutdown application job manager when the form is closed;
    private void Main_HMI_FormClosing(object sender, FormClosingEventArgs e)
    {
        Job_manager.Shutdown();
    }

    private void Connect_to_TCP_IP_server_button_Click(object sender, EventArgs e)
    {
        Job_manager.IOEnable = true;
        TCP_IP_server_device = Job_manager.StreamInput("PLC", 2000, true);

        if (TCP_IP_server_device == null)
        {
            MessageBox.Show("Connection failed", "TCP/IP server device connection status");
        }
        else
        {
            MessageBox.Show("Connection successful", "TCP/IP server device connection status");
            TCP_IP_server_device.MessageReceived  = new     CogIOStreamMessageEventHandler(TCP_IP_server_device_MessageReceived);
        }
    }
}

Just enable event handling on successful connection.

  • Related