using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace SampAPVSC
{
	/// <summary>
	/// Form1 ̊Tv̐łB
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		public System.Windows.Forms.Button Command10;
		public System.Windows.Forms.Button Command9;
		public System.Windows.Forms.Button Command8;
		public System.Windows.Forms.Button Command7;
		public System.Windows.Forms.Button Command6;
		public System.Windows.Forms.Button Command5;
		public System.Windows.Forms.Button Command4;
		public System.Windows.Forms.Button Command3;
		public System.Windows.Forms.Button Command2;
		public System.Windows.Forms.Button Command1;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

		public const short FCSRAM_USER_START = 0x00;	// [Ü̊JnAhX(݃GA)
		public const int FCSRAM_USER_END = 0x1FFFF;		// [Ü̏IAhX
		public const int FCSRAM_SYSTEM_START = 0x20000;	// VXëJnAhX(݃GA)
		public const int FCSRAM_SYSTEM_END = 0x3FFFF;	// VXëIAhX
		public const int FCSRAM_FREE_START = 0x40000;	// 󂫗̈JnAhX
		public const int FCSRAM_FREE_END = 0x7FDFF;		// 󂫗̈IAhX
		public const short FCSRAM_RECORD_SIZE = 0x20;	// 1R[h̃f[^TCY
	
	
		// [U`^
		public struct FCRAS_STATUS
		{
			public byte Status1;		// A[
			public byte Status2;		// WDT, 荞݃ClCu, P^C}
			public byte Status3;		// fW^o̓f[^
			public byte Status4;		// WDTZbgl (High)
			public byte Status5;		// WDTZbgl (Low)
			public byte Status6;		// fW^o̓f[^
		}

		public struct FCRAS_STATUSX009
		{
			public byte Status1;		// A[
			public byte Status2;		// WDT, 荞݃ClCu, P^C}
			public byte Status3;		// fW^o̓f[^
			public byte Status4;		// WDTZbgl (High)
			public byte Status5;		// WDTZbgl (Low)
			public byte Status6;		// fW^o̓f[^
			public byte Status7;		// xZTE~[O{[hA[̓j^
			public byte Status8;		// A[̓j^2
			public byte Status9;		// [A[j^
		}
	
	
        // RASc[CũI[v
        [DllImport("FcSrasapiP.dll", EntryPoint = "fcsoftrasOpenP")]
		private static extern int fcsoftrasOpen();

        // RASc[CũN[Y
        [DllImport("FcSrasapiP.dll", EntryPoint = "fcsoftrasCloseP")]
		private static extern int fcsoftrasClose();

		// [UAP Window̓o^
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasRegisterAlarmApWindowP@4")]
		private static extern int fcrasRegisterAlarmApWindow(int hWnd);
	
		// [UAP Window̓o^
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasUnregisterAlarmApWindowP@0")]
		private static extern int fcrasUnregisterAlarmApWindow();
	
		// WDT̃ZbgX^[g
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasResetAndStartWDTP@4")]
		private static extern int fcrasResetAndStartWDT(int dwWdtTimerValue);
	
		// WDT̃Xgbv
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasStopWDTP@0")]
		private static extern int fcrasStopWDT();
	
		// RAS{[h̃Xe[^X1`6̎擾
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasGetStatusP@4")]
		private static extern int fcrasGetStatus(ref FCRAS_STATUS FCRAS_STATUS);
	
		// Digital Inf[^l̎擾
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasDigitalInP@4")]
		private static extern int fcrasDigitalIn(ref byte byDigitalInData);
	
		// Digital Outf[^̏o
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasDigitalOutP@4")]
		private static extern int fcrasDigitalOut(byte byDigitalOutData);
	
		// VZOf[^f[^̏o
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcras7SegmentOutP@4")]
		private static extern int fcras7SegmentOut(byte by7SegmentData);
	
		// RAS{[h̑SXe[^X̎擾
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasGetStatusX009P@4")]
		private static extern int fcrasGetStatusX009(ref FCRAS_STATUSX009 lpbyRasStatus);
	
		// Ox~bgݒ
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasExtLimitOutP@4")]
		private static extern int fcrasExtLimitOut(byte byExtLimitData);
	
		// x~bgݒ
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasIntLimitOutP@4")]
		private static extern int fcrasIntLimitOut(byte byIntLimitData);
	
		// Ox~bg擾
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasGetExtLimitP@4")]
		private static extern int fcrasGetExtLimit(ref byte byExtLimitData);
	
		// x~bg擾
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasGetIntLimitP@4")]
		private static extern int fcrasGetIntLimit(ref byte byIntLimitData);
	
		// A[([Ü)̃Cg
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMWriteP@4")]
		private static extern int fcrasSRAMWrite(ref byte lpByWriteData);

		// A[([Ü)̃[h
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMReadP@12")]
		private static extern int fcrasSRAMRead(int dwOffset, ref byte lpbyDate, ref byte lpbyReadData);
	
		//@[Ü̃Jg|C^ǂݏo
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMGetUserCurrentP@4")]
		private static extern int fcrasSRAMGetUserCurrent(ref int lpdwOffset);

		//@VXë̃Jg|C^ǂݏo
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMGetSystemCurrentP@4")]
		private static extern int fcrasSRAMGetSystemCurrent(ref int lpdwOffset);

		// A[(󂫗̈)̃Cg
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMReadPhyP@12")]
		private static extern int fcrasSRAMReadPhy(int Offset, ref byte SramData, int Size);
	
		// A[(󂫗̈)̃[h
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMWritePhyP@12")]
		private static extern int fcrasSRAMWritePhy(int Offset, ref byte SramData, int Size);

		// A[̏
        [DllImport("FcSrasapiP.dll", EntryPoint = "_fcrasSRAMInitP@4")]
		private static extern int fcrasSRAMInit(short wMode);

		byte defaultExtLimit;		// Ox~bgl̕ۑp
		byte defaultIntLimit;		// x~bgl̕ۑp

		public Form1()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows tH[ fUCiŐꂽR[h 
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
            this.Command10 = new System.Windows.Forms.Button();
            this.Command9 = new System.Windows.Forms.Button();
            this.Command8 = new System.Windows.Forms.Button();
            this.Command7 = new System.Windows.Forms.Button();
            this.Command6 = new System.Windows.Forms.Button();
            this.Command5 = new System.Windows.Forms.Button();
            this.Command4 = new System.Windows.Forms.Button();
            this.Command3 = new System.Windows.Forms.Button();
            this.Command2 = new System.Windows.Forms.Button();
            this.Command1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // Command10
            // 
            this.Command10.BackColor = System.Drawing.SystemColors.Control;
            this.Command10.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command10.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command10.Location = new System.Drawing.Point(24, 136);
            this.Command10.Name = "Command10";
            this.Command10.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command10.Size = new System.Drawing.Size(113, 33);
            this.Command10.TabIndex = 9;
            this.Command10.Text = "Set highlimit internal temp.(35)";
            this.Command10.UseVisualStyleBackColor = false;
            this.Command10.Click += new System.EventHandler(this.Command10_Click);
            // 
            // Command9
            // 
            this.Command9.BackColor = System.Drawing.SystemColors.Control;
            this.Command9.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command9.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command9.Location = new System.Drawing.Point(168, 136);
            this.Command9.Name = "Command9";
            this.Command9.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command9.Size = new System.Drawing.Size(113, 33);
            this.Command9.TabIndex = 8;
            this.Command9.Text = "Set highlimit internal temp.(60)";
            this.Command9.UseVisualStyleBackColor = false;
            this.Command9.Click += new System.EventHandler(this.Command9_Click);
            // 
            // Command8
            // 
            this.Command8.BackColor = System.Drawing.SystemColors.Control;
            this.Command8.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command8.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command8.Location = new System.Drawing.Point(168, 176);
            this.Command8.Name = "Command8";
            this.Command8.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command8.Size = new System.Drawing.Size(113, 33);
            this.Command8.TabIndex = 7;
            this.Command8.Text = "Digital Input";
            this.Command8.UseVisualStyleBackColor = false;
            this.Command8.Click += new System.EventHandler(this.Command8_Click);
            // 
            // Command7
            // 
            this.Command7.BackColor = System.Drawing.SystemColors.Control;
            this.Command7.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command7.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command7.Location = new System.Drawing.Point(24, 176);
            this.Command7.Name = "Command7";
            this.Command7.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command7.Size = new System.Drawing.Size(113, 33);
            this.Command7.TabIndex = 6;
            this.Command7.Text = "Status Read";
            this.Command7.UseVisualStyleBackColor = false;
            this.Command7.Click += new System.EventHandler(this.Command7_Click);
            // 
            // Command6
            // 
            this.Command6.BackColor = System.Drawing.SystemColors.Control;
            this.Command6.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command6.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command6.Location = new System.Drawing.Point(168, 96);
            this.Command6.Name = "Command6";
            this.Command6.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command6.Size = new System.Drawing.Size(113, 33);
            this.Command6.TabIndex = 5;
            this.Command6.Text = "Set highlimit external temp.(60)";
            this.Command6.UseVisualStyleBackColor = false;
            this.Command6.Click += new System.EventHandler(this.Command6_Click);
            // 
            // Command5
            // 
            this.Command5.BackColor = System.Drawing.SystemColors.Control;
            this.Command5.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command5.Location = new System.Drawing.Point(24, 96);
            this.Command5.Name = "Command5";
            this.Command5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command5.Size = new System.Drawing.Size(113, 33);
            this.Command5.TabIndex = 4;
            this.Command5.Text = "Set highlimit external temp.(35)";
            this.Command5.UseVisualStyleBackColor = false;
            this.Command5.Click += new System.EventHandler(this.Command5_Click);
            // 
            // Command4
            // 
            this.Command4.BackColor = System.Drawing.SystemColors.Control;
            this.Command4.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command4.Location = new System.Drawing.Point(168, 56);
            this.Command4.Name = "Command4";
            this.Command4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command4.Size = new System.Drawing.Size(113, 33);
            this.Command4.TabIndex = 3;
            this.Command4.Text = "Digital Output(00H)";
            this.Command4.UseVisualStyleBackColor = false;
            this.Command4.Click += new System.EventHandler(this.Command4_Click);
            // 
            // Command3
            // 
            this.Command3.BackColor = System.Drawing.SystemColors.Control;
            this.Command3.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command3.Location = new System.Drawing.Point(24, 56);
            this.Command3.Name = "Command3";
            this.Command3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command3.Size = new System.Drawing.Size(113, 33);
            this.Command3.TabIndex = 2;
            this.Command3.Text = "Digital Output(5AH)";
            this.Command3.UseVisualStyleBackColor = false;
            this.Command3.Click += new System.EventHandler(this.Command3_Click);
            // 
            // Command2
            // 
            this.Command2.BackColor = System.Drawing.SystemColors.Control;
            this.Command2.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command2.Location = new System.Drawing.Point(168, 16);
            this.Command2.Name = "Command2";
            this.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command2.Size = new System.Drawing.Size(113, 33);
            this.Command2.TabIndex = 1;
            this.Command2.Text = "WDT Stop";
            this.Command2.UseVisualStyleBackColor = false;
            this.Command2.Click += new System.EventHandler(this.Command2_Click);
            // 
            // Command1
            // 
            this.Command1.BackColor = System.Drawing.SystemColors.Control;
            this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command1.Location = new System.Drawing.Point(24, 16);
            this.Command1.Name = "Command1";
            this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command1.Size = new System.Drawing.Size(113, 33);
            this.Command1.TabIndex = 0;
            this.Command1.Text = "WDT Start(10sec)";
            this.Command1.UseVisualStyleBackColor = false;
            this.Command1.Click += new System.EventHandler(this.Command1_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
            this.ClientSize = new System.Drawing.Size(307, 214);
            this.Controls.Add(this.Command10);
            this.Controls.Add(this.Command9);
            this.Controls.Add(this.Command8);
            this.Controls.Add(this.Command7);
            this.Controls.Add(this.Command6);
            this.Controls.Add(this.Command5);
            this.Controls.Add(this.Command4);
            this.Controls.Add(this.Command3);
            this.Controls.Add(this.Command2);
            this.Controls.Add(this.Command1);
            this.Location = new System.Drawing.Point(251, 195);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "Sample Application(VCS)";
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
			{
				return;
			}

            fcsoftrasOpen();

            Application.Run(new Form1());

            fcsoftrasClose();
        }

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// NɊOx^x̃~bg擾sB
			fcrasGetExtLimit(ref defaultExtLimit);
			fcrasGetIntLimit(ref defaultIntLimit);
		}

		private void Form1_Closed(object sender, System.EventArgs e)
		{
			// IɊOx^x̃~bglN̒lɖ߂B
			fcrasExtLimitOut(defaultExtLimit);
			fcrasIntLimitOut(defaultIntLimit);
		}

		private void Command1_Click(object sender, System.EventArgs e)
		{
			uint dwWdtTimerValue;
			// WDT̃ZbgX^[g
			int lRetVal1;
			dwWdtTimerValue = 10000;
			lRetVal1 = fcrasResetAndStartWDT((int)dwWdtTimerValue);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal1), "WDT Start (10sec)",  MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command2_Click(object sender, System.EventArgs e)
		{
			// WDT̃Xgbv
			int lRetVal2;
			lRetVal2 = fcrasStopWDT();
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal2), "WDT Stop", MessageBoxButtons.OK, MessageBoxIcon.Information);

		}

		private void Command3_Click(object sender, System.EventArgs e)
		{
			byte byDigitalOutData;
			// fW^f[^̏o
			int lRetVal3;
			byDigitalOutData = 0x5A;
			lRetVal3 = fcrasDigitalOut(byDigitalOutData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal3), "Digital Output (5AH)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command4_Click(object sender, System.EventArgs e)
		{
			byte byDigitalOutData;
			// fW^f[^̃NA
			int lRetVal4;
			byDigitalOutData = 0x0;
			lRetVal4 = fcrasDigitalOut(byDigitalOutData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal4), "Digital Output(00H)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command5_Click(object sender, System.EventArgs e)
		{
			byte byExtLimitData;
			// Ox̋elݒ
			int lRetVal5;
			byExtLimitData = 70;
			lRetVal5 = fcrasExtLimitOut(byExtLimitData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal5), "Set external temp.(+35)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command6_Click(object sender, System.EventArgs e)
		{
			byte byExtLimitData;
			// Ox̋elݒ
			int lRetVal6;
			byExtLimitData = 120;
			lRetVal6 = fcrasExtLimitOut(byExtLimitData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal6), "Set external temp.(+60)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command7_Click(object sender, System.EventArgs e)
		{
			FCRAS_STATUSX009 lpbyRasStatusX009 = new FCRAS_STATUSX009();
			// RAS{[h̑SXe[^X̎擾
			int lRetVal7;
			lRetVal7 = fcrasGetStatusX009(ref lpbyRasStatusX009);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal7) + "\r\n" +  
				"Status1 = " + Convert.ToString(lpbyRasStatusX009.Status1) + "(" + Convert.ToString(lpbyRasStatusX009.Status1,16) + "h)" + "\r\n" +
                "Status2 = " + Convert.ToString(lpbyRasStatusX009.Status2) + "(" + Convert.ToString(lpbyRasStatusX009.Status2, 16) + "h)" + "\r\n" +
                "Status3 = " + Convert.ToString(lpbyRasStatusX009.Status3) + "(" + Convert.ToString(lpbyRasStatusX009.Status3, 16) + "h)" + "\r\n" +
                "Status4 = " + Convert.ToString(lpbyRasStatusX009.Status4) + "(" + Convert.ToString(lpbyRasStatusX009.Status4, 16) + "h)" + "\r\n" +
                "Status5 = " + Convert.ToString(lpbyRasStatusX009.Status5) + "(" + Convert.ToString(lpbyRasStatusX009.Status5, 16) + "h)" + "\r\n" +
                "Status6 = " + Convert.ToString(lpbyRasStatusX009.Status6) + "(" + Convert.ToString(lpbyRasStatusX009.Status6, 16) + "h)" + "\r\n" +
                "Status7 = " + Convert.ToString(lpbyRasStatusX009.Status7) + "(" + Convert.ToString(lpbyRasStatusX009.Status7, 16) + "h)" + "\r\n" +
                "Status8 = " + Convert.ToString(lpbyRasStatusX009.Status8) + "(" + Convert.ToString(lpbyRasStatusX009.Status8, 16) + "h)" + "\r\n" +
                "Status9 = " + Convert.ToString(lpbyRasStatusX009.Status9) + "(" + Convert.ToString(lpbyRasStatusX009.Status9, 16) + "h)",
				"Status Read", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command8_Click(object sender, System.EventArgs e)
		{
			byte byDigitalInData = 0;
			// fW^̓f[^l̎擾
			int lRetVal8;
			lRetVal8 = fcrasDigitalIn(ref byDigitalInData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal8) + "\r\n" + "Input Valuse = " + byDigitalInData, "Digtal Input", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command9_Click(object sender, System.EventArgs e)
		{
			byte byIntLimitData;
			// x̋elݒ
			int lRetVal9;
			byIntLimitData = 120;
			lRetVal9 = fcrasIntLimitOut(byIntLimitData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal9), "Set internal temp.(+60)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void Command10_Click(object sender, System.EventArgs e)
		{
			byte byIntLimitData;
			// x̋elݒ
			int lRetVal10;
			byIntLimitData = 70;
			lRetVal10 = fcrasIntLimitOut(byIntLimitData);
			MessageBox.Show("Return Value = " + Convert.ToString(lRetVal10), "Set internal temp.(+35)", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
	}
}
