Newer
Older
RbFreqStand / RbFreqStandMeasure / R_DevService / Service / Impl / TCPServiceImpl.cs
yxw on 30 Apr 2021 3 KB 修改单位
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
{
    class TCPServiceImpl:GPIBService
    {
        public string[] getId()
        {
            string[] counterId;
            try
            {
                counterId = PortUltility.FindAddresses(PortType.LAN);
                
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "tcp getId : " + ex.Message);
                counterId = null;
            }
            return counterId;
        }
        
        private PortOperatorBase _portOperatorBaseTcp;

        public int open(string ip)
        {
            int result = -1;
            if (NewPortInstance(ip))
            {
                try
                {
                    _portOperatorBaseTcp.Open();

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

            return result;
        }

        private bool NewPortInstance(string id)
        {
            bool hasAddress = false;
            bool hasException = false;

            try
            {
                _portOperatorBaseTcp = new LANPortOperator(id);//TCPIP0::192.168.0.134::inst0::INSTR//"TCPIP0::" + sensitivityId + "::INSTR"

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

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

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

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

            return result;
        }

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

               
                result = 0;
            }
            catch (Exception ex)
            {

                result = -1;
            }
            return result;
        }

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

            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "getCounterId : " + ex.Message);
                content = "";
            }
            return content;
        }

    }
}