Newer
Older
RbFreqStand / RbFreqStandMeasure / R_DevService / Service / Impl / GPIBServiceImpl.cs
yxw on 9 Apr 2021 3 KB 添加LAN通信
using Casic.Birmm.RbFreqStandMeasure.Tools;
using Casic.Birmm.RbFreqStandMeasure.VISA.Port;
using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity;
using System;
using System.Reflection;

namespace Casic.Birmm.RbFreqStandMeasure.R_DevService.Service.Impl
{
    public class GPIBServiceImpl:GPIBService
    {
        public string[] getId()
        {
            string[] counterId;
            try
            {
                counterId = PortUltility.FindAddresses(PortType.GPIB);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "getCounterId : " + ex.Message);
                counterId = null;
            }
            return counterId;
        }

        private PortOperatorBase _portOperatorBase;
        
        public int open(string id)
        {
            int result = -1;
            if (NewPortInstance("GPIB",id))
            {
                try
                {
                    _portOperatorBase.Open();
                    result = 0;
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "open:" + ex.Message);
                    result = -1;
                }
            }

            return result;
        }

        private bool NewPortInstance(string type,string sensitivityId)
        {
            bool hasAddress = false;
            bool hasException = false;

            try
            {
                if (type == "GPIB")
                    _portOperatorBase = new GPIBPortOperator(sensitivityId);
                else if (type == "LAN")
                    _portOperatorBase = new LANPortOperator("TCPIP0::"+ sensitivityId + "::INSTR");

                hasAddress = true;
            }
            catch (Exception ex)
            {
                hasException = true;
            }

            if (!hasException) _portOperatorBase.Timeout = 2000;
            return hasAddress;
        }

        public int close()
        {
            int result = -1;
            try
            {
                _portOperatorBase.Close();

                result = 0;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "getCounterId : " + ex.Message);
                result = -1;
            }

            return result;
        }

        public int write(string content)
        {
            int result = -1;
            try
            {
                _portOperatorBase.WriteLine(content);

                result = 0;
            }
            catch (Exception ex)
            {

                result = -1;
            }
            return result;
        }

        public string read()
        {
            string content = "";
            try
            {
                content = _portOperatorBase.ReadLine();

            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "getCounterId : " + ex.Message);
                content = "";
            }
            return content;
        }
        
        public int openLAN(string ip)
        {
            int result = -1;
            if (NewPortInstance("LAN", ip))
            {
                try
                {
                    _portOperatorBase.Open();
                    result = 0;
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "open:" + ex.Message);
                    result = -1;
                }
            }

            return result;
        }

        /*
         * GPIB  频率:freq value
         * 周期:pulse:period value
         * output 0/1
         * 
         * TCPIP 频率:MEAS:freq?
         * 周期:MEAS:period?
         * */
    }
}