//   GPP Series Serial Sample Program for VC# 2008 or Higher
//   Please Open project (GPP_Access.csproj), Not Open Solution
//   ソリューションファイルでなくプロジェクトファイル(GPP_Access.csproj)を開いてください。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PSW_Access
{
    public partial class PSW_Access : Form
 
    {
        public int Opened;

        // Open Window
        public PSW_Access()
        {
            InitializeComponent();

            Opened = 0;
            for (int i = 1; i < 30; i++)
                cmbAddress.Items.Add(i);
            cmbAddress.SelectedIndex = 0;

            // Set form
            btnOpen_Close.Text = "OPEN";
            btnLoad.Enabled = false;
            btnMonitor.Enabled = false;
            btnValueSet.Enabled = false;
            btnVoltSet.Enabled = false;
            cmbAddress.Enabled = true;
        }

        // Form Close(x Button)
        private void Form1_FormClosing(object sender,FormClosingEventArgs e)
        {
            // Device Close
            if (cmbAddress.Enabled == false)
            {
                serialPort1.WriteLine("ALLOUTOFF\n");
                serialPort1.WriteLine("LOCAL\n");
                System.Threading.Thread.Sleep(200);
            }
            if (Opened != 0)
            {
                serialPort1.Close();
            }
        }
        // Open/Close Button
        private void btnOpen_Close_Click(object sender, EventArgs e)
        {
            if(btnOpen_Close.Text == "OPEN"){
                try
                {
                    Opened = 1;
                    Cursor.Current = Cursors.WaitCursor;
                    // Device Open and Init
                    serialPort1.PortName = "COM" + (cmbAddress.SelectedIndex + 1).ToString();
                    serialPort1.BaudRate = 115200;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;
                    serialPort1.Parity = System.IO.Ports.Parity.None;
                    serialPort1.Handshake = System.IO.Ports.Handshake.None;
                    serialPort1.Open();
                    serialPort1.DiscardInBuffer();

                    serialPort1.WriteLine("*cls\n");
                    serialPort1.WriteLine("ALLOUTOFF\n");
                    lblUnit.Text = "A";
                    btnLoad.Text = "OUT ON";

                    // Set form
                    btnOpen_Close.Text = "CLOSE";
                    btnLoad.Enabled = true;
                    btnMonitor.Enabled = true;
                    btnValueSet.Enabled = true;
                    btnVoltSet.Enabled = true;
                    cmbAddress.Enabled = false;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
            else{
                try
                {
                    // Device Close
                    serialPort1.WriteLine("ALLOUTOFF\n");
                    serialPort1.WriteLine("LOCAL\n");

                    System.Threading.Thread.Sleep(200);
                    serialPort1.Close();

                    // Set form
                    btnOpen_Close.Text = "OPEN";
                    btnLoad.Enabled = false ;
                    btnMonitor.Enabled = false ;
                    btnValueSet.Enabled = false ;
                    btnVoltSet.Enabled = false;
                    cmbAddress.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }
        

        // Load Button
        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (btnLoad.Text == "OUT ON")
            {
                // OFF -> ON
                serialPort1.WriteLine(":OUTP1 1\n");
                btnLoad.Text = "OUT OFF";
            }
            else
            {
                // ON -> OFF
                serialPort1.WriteLine(":OUTP1 0\n");
                btnLoad.Text = "OUT ON";
            }
        }

        // Set Button
        private void btnValueSet_Click(object sender, EventArgs e)
        {
            string SendMsg ;

            SendMsg = "ISET1:" + ReplaceCommonEscapeSequences(txtValue.Text);
            try
            {
                serialPort1.WriteLine(SendMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }

        // Monitor Button
        private void btnMonitor_Click(object sender, EventArgs e)
        {
            string txtVoltage, txtCurrent, txtPower;
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                serialPort1.WriteLine(":MEAS1:ALL?\n");
                System.Threading.Thread.Sleep(100);
                txtMeasure.Text = serialPort1.ReadExisting() ;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // Set Volt Button
        private void btnVoltSet_Click(object sender, EventArgs e)
        {
            string SendMsg;

            SendMsg = "VSET1:" + ReplaceCommonEscapeSequences(txtVoltage.Text);
            try
            {
                serialPort1.WriteLine(SendMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string ReplaceCommonEscapeSequences(string s)
        {
            return s.Replace("\\n", "\n").Replace("\\r", "\r");
        }

        private string InsertCommonEscapeSequences(string s)
        {
            return s.Replace("\n", "\\n").Replace("\r", "\\r");
        }
    }
}
