using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NationalInstruments.Visa;
using System.IO;

namespace MainForm
{
    public partial class Form1 : Form
    {
        public MessageBasedSession mbSession;
        public ResourceManager rmSession = new ResourceManager();
        MainForm.NIVisaCmd vs = new MainForm.NIVisaCmd();
        static int lTimeout = 20000; // Timeout value in milliseconds
        static string sInstrumentAddress = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            vs.TimeOut = lTimeout;
            btnGenerateWave.Enabled = false;
            btnLoadScript.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                mbSession = (MessageBasedSession)rmSession.Open(tbAddress.Text); 
                mbSession.TimeoutMilliseconds = lTimeout;
                vs.ObjMBS = mbSession;
                tbAnswer.Text = vs.SendTo(tbAddress.Text, "*IDN?");
                if (tbAnswer.Text.Contains("ACTIVE TECHNOLOGIES,AFG"))
                {
                    btnGenerateWave.Enabled = true;
                    btnLoadScript.Enabled = true;
                    sInstrumentAddress = tbAddress.Text;
                }
            }
            catch (InvalidCastException)
            {
                MessageBox.Show("Resource selected must be a message-based session");
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

        }

        private void btnLoadScript_Click(object sender, EventArgs e)
        {
         

            // Load a SCPI script
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.Title = "Please select the SCPI script";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Execute the script
                if (File.Exists(openFileDialog1.FileName))
                {
                    using (StreamReader reader = new StreamReader(openFileDialog1.FileName, true))
                    {
                        int cnt = 1;
                        string ln = reader.ReadLine();
                        while (ln != null)
                        {
                            if (ln.Length > 0)
                            {
                                Console.WriteLine("{0}> {1}", cnt, ln);

                                if (!ln.EndsWith("\n"))
                                    ln += '\n';

                                tbAnswer.Text = vs.SendTo(sInstrumentAddress, ln);
                            }
                            ln = reader.ReadLine();
                            cnt++;
                        }
                    }
                }
            }
        }

        private void btnGenerateWave_Click(object sender, EventArgs e)
        {

            // Reset to default
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "*RST");
            // Set the channel 1 parameters
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce1:FUNCtion:SHAPe SINusoid");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce1:FREQuency 100MHZ");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce1:VOLTage:AMPLitude 1");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce1:VOLTage:OFFSet 0");
            // Set the channel 2 parameters
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce2:FUNCtion:SHAPe SQUare");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce2:FREQuency 5MHZ");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce2:VOLTage:HIGH 2");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "SOURce2:VOLTage:LOW 0");
            //Turn on the output channels
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "OUTPut1:STATe ON");
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "OUTPut2:STATe ON");
            //Start the generation
            tbAnswer.Text = vs.SendTo(sInstrumentAddress, "AFGControl:START");

        }

        private void tbAddress_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
