//
// @file ModbusRtuButtonForm.cs
//
// A simple C# example using FieldTalk to control a coil with
// two form buttons using Modbus RTU.
//
// @if NOTICE
//
// Copyright (c) proconX Pty Ltd. All rights reserved.
//
// The following source file constitutes example program code and is
// intended merely to illustrate useful programming techniques.  The user
// is responsible for applying the code correctly.
//
// THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// @endif
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FieldTalk.Modbus.Master;

namespace ModbusRtuButton
{
    public partial class Form1 : Form
    {
        // Create an instance of the Modbus RTU protocol
        private MbusRtuMasterProtocol modbusProtocol = new MbusRtuMasterProtocol();

        public Form1()
        {
            InitializeComponent();
        }

        private void PollTimer_Tick(object sender, EventArgs e)
        {
            int result = 0;
            bool[] coilArr = new bool[2];

            // Reset coil number 100 on Modbus Slave with ID 1
            result = modbusProtocol.readCoils(1, 100, coilArr);
            if (result == BusProtocolErrors.FTALK_SUCCESS)
            {
                if (coilArr[0])
                {
                    CoilStateLabel.Text = "ON";
                }
                else
                {
                    CoilStateLabel.Text = "OFF";
                }
            }
            else
            {
                CoilStateLabel.Text = "ERROR";
            }
            // Indicate result on status line
            StatusLine.Text = "readCoil: " + BusProtocolErrors.getBusProtocolErrorText(result);
        }

        private void OpenButton_Click(object sender, EventArgs e)
        {
            int result = 0;

            // Open the protocol and serial port connection
            // Note: The \\.\ prefix is required to be able to open COM ports >= 10
            result = modbusProtocol.openProtocol("\\\\.\\" + PortComboBox.Text, Int32.Parse(BaudrateComboBox.Text), 8, 1, ParityComboBox.SelectedIndex);
            // Change time-out to 300 ms
            modbusProtocol.setTimeout(300);

            // Indicate result on status line
            StatusLine.Text = "openProtocol: " + BusProtocolErrors.getBusProtocolErrorText(result);
            // Enable or disable button controls depending on successful open operation
            if (result == BusProtocolErrors.FTALK_SUCCESS)
            {
                OpenButton.Enabled = false;
                CloseButton.Enabled = true;
                SetButton.Enabled = true;
                ResetButton.Enabled = true;
                PollTimer.Enabled = true;
            }
        }

        private void SetButton_Click(object sender, EventArgs e)
        {
            int result = 0;

            // Energize coil number 100 on Modbus Slave with ID 1
            result = modbusProtocol.writeCoil(1, 100, true);
            // Indicate result on status line
            StatusLine.Text = "writeCoil: " + BusProtocolErrors.getBusProtocolErrorText(result);
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            int result = 0;

            // Reset coil number 100 on Modbus Slave with ID 1
            result = modbusProtocol.writeCoil(1, 100, false);
            // Indicate result on status line
            StatusLine.Text = "writeCoil: " + BusProtocolErrors.getBusProtocolErrorText(result);
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            // Close protocol and serial port
            modbusProtocol.closeProtocol();
            // Indicate result on status line
            StatusLine.Text = "closeProtocol";
            // Disable button controls
            OpenButton.Enabled = true;
            CloseButton.Enabled = false;
            SetButton.Enabled = false;
            ResetButton.Enabled = false;
            PollTimer.Enabled = false;
            CoilStateLabel.Text = "???";
        }

        private void ModbusRtuButtonForm_Load(object sender, EventArgs e)
        {
            PortComboBox.SelectedIndex = 0;
            BaudrateComboBox.SelectedIndex = 3;
            ParityComboBox.SelectedIndex = 2;
        }

    }
}