using Casic.Birmm.RbFreqStandMeasure.Tools; using Casic.Birmm.RbFreqStandMeasure.VISA.Port; using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; using System; using System.Collections.Generic; using System.Reflection; namespace Casic.Birmm.RbFreqStandMeasure.R_DevService.Service.Impl { class TCPServiceImpl:GPIBService { public string[] getId() { string[] counterId; List<string> strList = new List<string>(); try { counterId = PortUltility.FindAddresses(PortType.LAN); foreach (string counter in counterId) if (!counter.Contains("53230")) strList.Add(counter); } catch (Exception ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "tcp getId : " + ex.Message); counterId = null; } return strList.ToArray(); } 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; } public bool isOpen() { try { _portOperatorBaseTcp.WriteLine("*IDN?"); string idn = _portOperatorBaseTcp.ReadLine(); if (idn != null || idn != "") return true; else return false; } catch (Exception ex) { return false; } /*if (_portOperatorBaseTcp == null || !_portOperatorBaseTcp.IsPortOpen) return false; else return true;*/ } 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 = 1000; 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 { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType,"TCPRead:"+DateTime.Now); content = _portOperatorBaseTcp.ReadLine(); } catch (Exception ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "TCPRead: " + ex.Message); content = ""; } return content; } } }