﻿/*============================================================================
	FactoryIT I/F Sample Program

		DEVICE ID:		0
		Channel:		0 to 3ch
		Range:			-10 to 10V
=========================================================================== */
using System;
using System.Collections.Generic;
using System.Text;
using FIT;

namespace FitDai
{
    class FitDai
    {
        #region Address(Common)

        const uint FIT_IO = 0x300000;
        const uint FIT_IO_DEVICE_INFOR = 0x0000;
        const uint FIT_IO_DEVICE_CONFIG = 0x1000;
        const uint FIT_IO_INPUT = 0x4000;
        const uint FIT_IO_OUTPUT = 0x5000;

        const uint FIT_IO_DEVICE_SIZE = 0x0080;

        const uint FIT_PRODUCT_CATEGORY = 0x00;

        const uint FIT_MODULE_START = 0x10;
        const uint FIT_ERROR_STATUS = 0x11;

        #endregion

        #region Informarion(Common)

        const byte FIT_PRODUCT_DIGITAL = 0x01;
        const byte FIT_PRODUCT_ANALOG = 0x02;
        const byte FIT_PRODUCT_COUNTER = 0x03;

        const byte FIT_MODULE_START_OFF = 0x00;
        const byte FIT_MODULE_START_ON = 0x01;

        #endregion

        #region Address(AIO)

        const byte FIT_AIO_AI_BIT = 0x12;
        const byte FIT_AIO_AI_RANGE = 0x13;
        const byte FIT_AIO_AI_MODE = 0x14;
        const byte FIT_AIO_AO_BIT = 0x1A;
        const byte FIT_AIO_AO_RANGE = 0x1B;

        #endregion

        #region Information(AIO)

        const byte FIT_AIO_RANGE_PM10 = 0;
        const byte FIT_AIO_RANGE_PM5 = 1;
        const byte FIT_AIO_RANGEP10 = 50;
        const byte FIT_AIO_RANGE_P5 = 51;
        const byte FIT_AIO_RANGE_P20MA = 100;

        #endregion

        static void Main(string[] args)
        {
            #region Sample

            byte[] FIT_SAMPLE_IP_ADDRESS = { 192, 168, 0, 169 };
            const ushort FIT_SAMPLE_PORT = 0x5007;
            const byte FIT_SAMPLE_DEVICE_ID = 0;

            #endregion

            #region 各変数宣言

            uint dwVaBase;
            uint dwVaOffset;
            ushort hHandle;
            ushort wStatus;
            byte[] byCategory = new byte[100];
            byte[] byModuleStart = new byte[100];
            byte[] byRange = new byte[100];
            byte[] byData = new byte[0x80];
            byte[] byErrorStatus = new byte[100];
            byte byChCount;

            #endregion

            #region Open

            hHandle = Fit.FIT_Open(FIT_SAMPLE_IP_ADDRESS, FIT_SAMPLE_PORT, null);
            if (hHandle == 0)
            {
                Console.WriteLine("Error! FIT_Open = " + string.Format("{0:X4}", hHandle) + "(H)");
                return;
            }

            dwVaOffset = FIT_IO_DEVICE_SIZE * FIT_SAMPLE_DEVICE_ID;     //Offset Address

            #endregion

            #region Read 'Category'

            dwVaBase = FIT_IO + FIT_IO_DEVICE_CONFIG;
            wStatus = Fit.FIT_Read(hHandle, dwVaBase + dwVaOffset + FIT_PRODUCT_CATEGORY, 1, byCategory);

            if (wStatus != 0)
            {
                Console.WriteLine("Error! FIT_Read = " + string.Format("{0:X4}", wStatus) + "(H)");
                return;
            }
            if (byCategory[0] != FIT_PRODUCT_ANALOG)
            {
                Console.WriteLine("Error! Category = " + string.Format("{0:X2}", byCategory[0]) + "(H)");
                return;
            }

            #endregion

            #region Write 'D/A Range'

            byRange[0] = FIT_AIO_RANGE_PM10;
            wStatus = Fit.FIT_Write(hHandle, dwVaBase + dwVaOffset + FIT_AIO_AO_RANGE, 1, byRange);
            if (wStatus != 0)
            {
                Console.WriteLine("Error! FIT_Write = " + string.Format("{0:X4}", wStatus) + "(H)");
            }

            #endregion

            #region Write 'Module Start'

            byModuleStart[0] = FIT_MODULE_START_ON;
            wStatus = Fit.FIT_Write(hHandle, dwVaBase + dwVaOffset + FIT_MODULE_START, 1, byModuleStart);
            if (wStatus != 0)
            {
                Console.WriteLine("Error! FIT_Write = " + string.Format("{0:X4}", wStatus) + "(H)");
            }

            #endregion

            #region Write 'D/A Data'

            for (byChCount = 0; byChCount < 4; byChCount++)
            {
                //Output Data:10V
                byData[byChCount * 2 + 1] = 0x0F;
                byData[byChCount * 2] = 0xFF;
                Console.WriteLine("D/A CH" + byChCount + " DATA:" + string.Format("{0:X2}", byData[byChCount * 2 + 1]) + string.Format("{0:X2}", byData[byChCount * 2]));
            }

            dwVaBase = FIT_IO + FIT_IO_OUTPUT;
            wStatus = Fit.FIT_Write(hHandle, dwVaBase + dwVaOffset, 2 * 4, byData);
            if (wStatus != 0)
            {
                Console.WriteLine("Error! FIT_Write = " + string.Format("{0:X4}", wStatus) + "(H)");
            }

            #endregion

            #region Read 'Error Status'

            dwVaBase = FIT_IO + FIT_IO_DEVICE_INFOR;
            wStatus = Fit.FIT_Read(hHandle, dwVaBase + dwVaOffset + FIT_ERROR_STATUS, 1, byErrorStatus);
            if (wStatus != 0)
            {
                Console.WriteLine("Error! FIT_Read = " + string.Format("{0:X4}", wStatus) + "(H)");
            }
            if (byErrorStatus[0] != 0x00)
            {
                Console.WriteLine("Error! Error Status = " + string.Format("{0:X2}", byErrorStatus[0]) + "(H)");
            }
            #endregion

            #region Close

            Fit.FIT_Close(hHandle);

            #endregion
        }
    }
}
