diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/VISA/Port/PortOperator.cs b/RbFreqStandMeasure/VISA/Port/PortOperator.cs new file mode 100644 index 0000000..292588e --- /dev/null +++ b/RbFreqStandMeasure/VISA/Port/PortOperator.cs @@ -0,0 +1,210 @@ +using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Port +{ + class RS232PortOperator : PortOperatorBase, IPortType + { + public int BaudRate { private set; get; } + + public SerialParity Parity { private set; get; } + + public SerialStopBitsMode StopBits { private set; get; } + + public int DataBits { private set; get; } + + public PortType PortType { get => PortType.RS232; } + + public SerialFlowControlModes FlowControl { set; get; } = SerialFlowControlModes.None; + + SerialSession serialSession; + + public RS232PortOperator(string address, int baudRate, SerialParity parity, SerialStopBitsMode stopBits, int dataBits) : base(new SerialSession(address), address) + { + if (!address.ToUpper().Contains("ASRL")) throw new ArgumentException($"该地址不含ASRL字样"); + BaudRate = baudRate; + Parity = parity; + StopBits = stopBits; + if (dataBits < 5 || dataBits > 8) throw new NotSupportedException($"不支持数据位为:{dataBits.ToString()}"); + DataBits = dataBits; + serialSession = (SerialSession)Session; + } + + public override void Open() + { + base.Open(); + serialSession.BaudRate = BaudRate; + switch (Parity) + { + case SerialParity.None: + serialSession.Parity = SerialParity.None; break; + case SerialParity.Odd: + serialSession.Parity = SerialParity.Odd; break; + case SerialParity.Even: + serialSession.Parity = SerialParity.Even; break; + case SerialParity.Mark: + serialSession.Parity = SerialParity.Mark; break; + case SerialParity.Space: + serialSession.Parity = SerialParity.Space; break; + } + switch (StopBits) + { + case SerialStopBitsMode.One: + serialSession.StopBits = SerialStopBitsMode.One; break; + case SerialStopBitsMode.OneAndOneHalf: + serialSession.StopBits = SerialStopBitsMode.OneAndOneHalf; break; + case SerialStopBitsMode.Two: + serialSession.StopBits = SerialStopBitsMode.Two; break; + } + serialSession.DataBits = (short)DataBits; + switch (FlowControl) + { + case SerialFlowControlModes.None: + serialSession.FlowControl = SerialFlowControlModes.None; break; + case SerialFlowControlModes.XOnXOff: + serialSession.FlowControl = SerialFlowControlModes.XOnXOff; break; + case SerialFlowControlModes.RtsCts: + serialSession.FlowControl = SerialFlowControlModes.RtsCts; break; + case SerialFlowControlModes.DtrDsr: + serialSession.FlowControl = SerialFlowControlModes.DtrDsr; break; + } + } + } + + class USBPortOperator : PortOperatorBase, IPortType + { + public USBPortOperator(string address) : base(new UsbSession(address), address) + { + if (!address.ToUpper().Contains("USB")) + throw new ArgumentException($"该地址不含USB字样"); + } + public PortType PortType { get => PortType.USB; } + } + + class GPIBPortOperator : PortOperatorBase, IPortType + { + public GPIBPortOperator(string address) : base(new GpibSession(address), address) + { + if (!address.ToUpper().Contains("GPIB")) + throw new ArgumentException($"该地址不含GPIB字样"); + } + public PortType PortType { get => PortType.GPIB; } + } + + class LANPortOperator : PortOperatorBase, IPortType + { + public LANPortOperator(string address) : base(new TcpipSession(address), address) + { + if (!address.ToUpper().Contains("TCPIP")) + throw new ArgumentException($"该地址不含TCPIP字样"); + } + public PortType PortType { get => PortType.LAN; } + } + + class PortEventArgs : EventArgs + { + public string Address { private set; get; } + public bool Cancel { set; get; } + public PortEventArgs(string address) + { + Address = address; + } + } + + abstract class PortOperatorBase : IPortOperator + { + public string Address { set; get; } + + public PortOperatorBase(IMessageBasedSession session) + { + Session = session; + } + public PortOperatorBase(IMessageBasedSession session, string address) : this(session) + { + Address = address; + } + + public int Timeout { set; get; } = 2000; + + public event EventHandler PortOpenning; + + public event EventHandler PortClosing; + + protected virtual void OnPortOpenning(PortEventArgs e) + { + PortOpenning?.Invoke(this, e); + } + + protected virtual void OnPortClosing(PortEventArgs e) + { + PortClosing?.Invoke(this, e); + } + + public bool IsPortOpen { private set; get; } = false; + + protected IMessageBasedSession Session { private set; get; } + + public virtual void Open() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortOpenning(e); + if (!e.Cancel) + { + Session.TimeoutMilliseconds = Timeout; + this.IsPortOpen = true; + } + } + + public virtual void Close() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortClosing(e); + if (!e.Cancel) + { + Session.Dispose(); + this.IsPortOpen = false; + } + } + + public virtual void Write(string command) + { + Session.RawIO.Write(command); + } + + public virtual void WriteLine(string command) + { + Write($"{command}\n"); + } + + //public const int READ_BUFFER_COUNT = 1024; + + public virtual string Read() + { + return Encoding.UTF8.GetString(Session.RawIO.Read()); + } + + public virtual string ReadLine() + { + string result = Read(); + return result.EndsWith("\n") ? result.TrimEnd(new char[] { '\n' }) : result; + } + } + + interface IPortOperator + { + void Open(); + void Close(); + void Write(string command); + string Read(); + } + + interface IPortType + { + PortType PortType { get; } + } +} diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/VISA/Port/PortOperator.cs b/RbFreqStandMeasure/VISA/Port/PortOperator.cs new file mode 100644 index 0000000..292588e --- /dev/null +++ b/RbFreqStandMeasure/VISA/Port/PortOperator.cs @@ -0,0 +1,210 @@ +using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Port +{ + class RS232PortOperator : PortOperatorBase, IPortType + { + public int BaudRate { private set; get; } + + public SerialParity Parity { private set; get; } + + public SerialStopBitsMode StopBits { private set; get; } + + public int DataBits { private set; get; } + + public PortType PortType { get => PortType.RS232; } + + public SerialFlowControlModes FlowControl { set; get; } = SerialFlowControlModes.None; + + SerialSession serialSession; + + public RS232PortOperator(string address, int baudRate, SerialParity parity, SerialStopBitsMode stopBits, int dataBits) : base(new SerialSession(address), address) + { + if (!address.ToUpper().Contains("ASRL")) throw new ArgumentException($"该地址不含ASRL字样"); + BaudRate = baudRate; + Parity = parity; + StopBits = stopBits; + if (dataBits < 5 || dataBits > 8) throw new NotSupportedException($"不支持数据位为:{dataBits.ToString()}"); + DataBits = dataBits; + serialSession = (SerialSession)Session; + } + + public override void Open() + { + base.Open(); + serialSession.BaudRate = BaudRate; + switch (Parity) + { + case SerialParity.None: + serialSession.Parity = SerialParity.None; break; + case SerialParity.Odd: + serialSession.Parity = SerialParity.Odd; break; + case SerialParity.Even: + serialSession.Parity = SerialParity.Even; break; + case SerialParity.Mark: + serialSession.Parity = SerialParity.Mark; break; + case SerialParity.Space: + serialSession.Parity = SerialParity.Space; break; + } + switch (StopBits) + { + case SerialStopBitsMode.One: + serialSession.StopBits = SerialStopBitsMode.One; break; + case SerialStopBitsMode.OneAndOneHalf: + serialSession.StopBits = SerialStopBitsMode.OneAndOneHalf; break; + case SerialStopBitsMode.Two: + serialSession.StopBits = SerialStopBitsMode.Two; break; + } + serialSession.DataBits = (short)DataBits; + switch (FlowControl) + { + case SerialFlowControlModes.None: + serialSession.FlowControl = SerialFlowControlModes.None; break; + case SerialFlowControlModes.XOnXOff: + serialSession.FlowControl = SerialFlowControlModes.XOnXOff; break; + case SerialFlowControlModes.RtsCts: + serialSession.FlowControl = SerialFlowControlModes.RtsCts; break; + case SerialFlowControlModes.DtrDsr: + serialSession.FlowControl = SerialFlowControlModes.DtrDsr; break; + } + } + } + + class USBPortOperator : PortOperatorBase, IPortType + { + public USBPortOperator(string address) : base(new UsbSession(address), address) + { + if (!address.ToUpper().Contains("USB")) + throw new ArgumentException($"该地址不含USB字样"); + } + public PortType PortType { get => PortType.USB; } + } + + class GPIBPortOperator : PortOperatorBase, IPortType + { + public GPIBPortOperator(string address) : base(new GpibSession(address), address) + { + if (!address.ToUpper().Contains("GPIB")) + throw new ArgumentException($"该地址不含GPIB字样"); + } + public PortType PortType { get => PortType.GPIB; } + } + + class LANPortOperator : PortOperatorBase, IPortType + { + public LANPortOperator(string address) : base(new TcpipSession(address), address) + { + if (!address.ToUpper().Contains("TCPIP")) + throw new ArgumentException($"该地址不含TCPIP字样"); + } + public PortType PortType { get => PortType.LAN; } + } + + class PortEventArgs : EventArgs + { + public string Address { private set; get; } + public bool Cancel { set; get; } + public PortEventArgs(string address) + { + Address = address; + } + } + + abstract class PortOperatorBase : IPortOperator + { + public string Address { set; get; } + + public PortOperatorBase(IMessageBasedSession session) + { + Session = session; + } + public PortOperatorBase(IMessageBasedSession session, string address) : this(session) + { + Address = address; + } + + public int Timeout { set; get; } = 2000; + + public event EventHandler PortOpenning; + + public event EventHandler PortClosing; + + protected virtual void OnPortOpenning(PortEventArgs e) + { + PortOpenning?.Invoke(this, e); + } + + protected virtual void OnPortClosing(PortEventArgs e) + { + PortClosing?.Invoke(this, e); + } + + public bool IsPortOpen { private set; get; } = false; + + protected IMessageBasedSession Session { private set; get; } + + public virtual void Open() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortOpenning(e); + if (!e.Cancel) + { + Session.TimeoutMilliseconds = Timeout; + this.IsPortOpen = true; + } + } + + public virtual void Close() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortClosing(e); + if (!e.Cancel) + { + Session.Dispose(); + this.IsPortOpen = false; + } + } + + public virtual void Write(string command) + { + Session.RawIO.Write(command); + } + + public virtual void WriteLine(string command) + { + Write($"{command}\n"); + } + + //public const int READ_BUFFER_COUNT = 1024; + + public virtual string Read() + { + return Encoding.UTF8.GetString(Session.RawIO.Read()); + } + + public virtual string ReadLine() + { + string result = Read(); + return result.EndsWith("\n") ? result.TrimEnd(new char[] { '\n' }) : result; + } + } + + interface IPortOperator + { + void Open(); + void Close(); + void Write(string command); + string Read(); + } + + interface IPortType + { + PortType PortType { get; } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs new file mode 100644 index 0000000..acde3e0 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs @@ -0,0 +1,89 @@ +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + enum PortType + { + RS232, USB, GPIB, LAN, None + } + + class PortUltility + { + private static string ToStringFromPortType(PortType portType) + { + switch (portType) + { + case PortType.USB: return "USB"; + case PortType.GPIB: return "GPIB"; + case PortType.LAN: return "TCPIP"; + case PortType.None: return ""; + case PortType.RS232: + default: return "ASRL"; + } + } + + public static string[] FindAddresses(PortType portType) + { + IEnumerable result = new List(); + try + { + result = GlobalResourceManager.Find($"{ToStringFromPortType(portType)}?*INSTR"); + } + catch (Exception ex) + { + if (!(ex is NativeVisaException)) + { + if (ex.InnerException != null) throw ex.InnerException; + else throw ex; + } + } + + return result.ToArray().Where(n => !n.Contains("//")).ToArray(); + } + + public static string[] FindRS232Type(string[] addresses) + { + List list = new List(); + for (int i = 0; i < addresses.Length; i++) + { + try + { + GlobalResourceManager.TryParse(addresses[i], out ParseResult result); + list.Add(result.AliasIfExists); + } + catch + { + //list.Add("None"); + } + } + return list.ToArray(); + } + + public static bool OpenIPAddress(string address, out string fullAddress) + { + bool result = false; + string addressTemp = $"TCPIP0::{address}::INSTR"; + fullAddress = addressTemp; + try + { + using (TcpipSession session = new TcpipSession(addressTemp)) + { + result = true; + } + } + catch { } + return result; + } + + public static string[] FindAddresses() + { + return FindAddresses(PortType.None); + } + } + +} diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/VISA/Port/PortOperator.cs b/RbFreqStandMeasure/VISA/Port/PortOperator.cs new file mode 100644 index 0000000..292588e --- /dev/null +++ b/RbFreqStandMeasure/VISA/Port/PortOperator.cs @@ -0,0 +1,210 @@ +using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Port +{ + class RS232PortOperator : PortOperatorBase, IPortType + { + public int BaudRate { private set; get; } + + public SerialParity Parity { private set; get; } + + public SerialStopBitsMode StopBits { private set; get; } + + public int DataBits { private set; get; } + + public PortType PortType { get => PortType.RS232; } + + public SerialFlowControlModes FlowControl { set; get; } = SerialFlowControlModes.None; + + SerialSession serialSession; + + public RS232PortOperator(string address, int baudRate, SerialParity parity, SerialStopBitsMode stopBits, int dataBits) : base(new SerialSession(address), address) + { + if (!address.ToUpper().Contains("ASRL")) throw new ArgumentException($"该地址不含ASRL字样"); + BaudRate = baudRate; + Parity = parity; + StopBits = stopBits; + if (dataBits < 5 || dataBits > 8) throw new NotSupportedException($"不支持数据位为:{dataBits.ToString()}"); + DataBits = dataBits; + serialSession = (SerialSession)Session; + } + + public override void Open() + { + base.Open(); + serialSession.BaudRate = BaudRate; + switch (Parity) + { + case SerialParity.None: + serialSession.Parity = SerialParity.None; break; + case SerialParity.Odd: + serialSession.Parity = SerialParity.Odd; break; + case SerialParity.Even: + serialSession.Parity = SerialParity.Even; break; + case SerialParity.Mark: + serialSession.Parity = SerialParity.Mark; break; + case SerialParity.Space: + serialSession.Parity = SerialParity.Space; break; + } + switch (StopBits) + { + case SerialStopBitsMode.One: + serialSession.StopBits = SerialStopBitsMode.One; break; + case SerialStopBitsMode.OneAndOneHalf: + serialSession.StopBits = SerialStopBitsMode.OneAndOneHalf; break; + case SerialStopBitsMode.Two: + serialSession.StopBits = SerialStopBitsMode.Two; break; + } + serialSession.DataBits = (short)DataBits; + switch (FlowControl) + { + case SerialFlowControlModes.None: + serialSession.FlowControl = SerialFlowControlModes.None; break; + case SerialFlowControlModes.XOnXOff: + serialSession.FlowControl = SerialFlowControlModes.XOnXOff; break; + case SerialFlowControlModes.RtsCts: + serialSession.FlowControl = SerialFlowControlModes.RtsCts; break; + case SerialFlowControlModes.DtrDsr: + serialSession.FlowControl = SerialFlowControlModes.DtrDsr; break; + } + } + } + + class USBPortOperator : PortOperatorBase, IPortType + { + public USBPortOperator(string address) : base(new UsbSession(address), address) + { + if (!address.ToUpper().Contains("USB")) + throw new ArgumentException($"该地址不含USB字样"); + } + public PortType PortType { get => PortType.USB; } + } + + class GPIBPortOperator : PortOperatorBase, IPortType + { + public GPIBPortOperator(string address) : base(new GpibSession(address), address) + { + if (!address.ToUpper().Contains("GPIB")) + throw new ArgumentException($"该地址不含GPIB字样"); + } + public PortType PortType { get => PortType.GPIB; } + } + + class LANPortOperator : PortOperatorBase, IPortType + { + public LANPortOperator(string address) : base(new TcpipSession(address), address) + { + if (!address.ToUpper().Contains("TCPIP")) + throw new ArgumentException($"该地址不含TCPIP字样"); + } + public PortType PortType { get => PortType.LAN; } + } + + class PortEventArgs : EventArgs + { + public string Address { private set; get; } + public bool Cancel { set; get; } + public PortEventArgs(string address) + { + Address = address; + } + } + + abstract class PortOperatorBase : IPortOperator + { + public string Address { set; get; } + + public PortOperatorBase(IMessageBasedSession session) + { + Session = session; + } + public PortOperatorBase(IMessageBasedSession session, string address) : this(session) + { + Address = address; + } + + public int Timeout { set; get; } = 2000; + + public event EventHandler PortOpenning; + + public event EventHandler PortClosing; + + protected virtual void OnPortOpenning(PortEventArgs e) + { + PortOpenning?.Invoke(this, e); + } + + protected virtual void OnPortClosing(PortEventArgs e) + { + PortClosing?.Invoke(this, e); + } + + public bool IsPortOpen { private set; get; } = false; + + protected IMessageBasedSession Session { private set; get; } + + public virtual void Open() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortOpenning(e); + if (!e.Cancel) + { + Session.TimeoutMilliseconds = Timeout; + this.IsPortOpen = true; + } + } + + public virtual void Close() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortClosing(e); + if (!e.Cancel) + { + Session.Dispose(); + this.IsPortOpen = false; + } + } + + public virtual void Write(string command) + { + Session.RawIO.Write(command); + } + + public virtual void WriteLine(string command) + { + Write($"{command}\n"); + } + + //public const int READ_BUFFER_COUNT = 1024; + + public virtual string Read() + { + return Encoding.UTF8.GetString(Session.RawIO.Read()); + } + + public virtual string ReadLine() + { + string result = Read(); + return result.EndsWith("\n") ? result.TrimEnd(new char[] { '\n' }) : result; + } + } + + interface IPortOperator + { + void Open(); + void Close(); + void Write(string command); + string Read(); + } + + interface IPortType + { + PortType PortType { get; } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs new file mode 100644 index 0000000..acde3e0 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs @@ -0,0 +1,89 @@ +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + enum PortType + { + RS232, USB, GPIB, LAN, None + } + + class PortUltility + { + private static string ToStringFromPortType(PortType portType) + { + switch (portType) + { + case PortType.USB: return "USB"; + case PortType.GPIB: return "GPIB"; + case PortType.LAN: return "TCPIP"; + case PortType.None: return ""; + case PortType.RS232: + default: return "ASRL"; + } + } + + public static string[] FindAddresses(PortType portType) + { + IEnumerable result = new List(); + try + { + result = GlobalResourceManager.Find($"{ToStringFromPortType(portType)}?*INSTR"); + } + catch (Exception ex) + { + if (!(ex is NativeVisaException)) + { + if (ex.InnerException != null) throw ex.InnerException; + else throw ex; + } + } + + return result.ToArray().Where(n => !n.Contains("//")).ToArray(); + } + + public static string[] FindRS232Type(string[] addresses) + { + List list = new List(); + for (int i = 0; i < addresses.Length; i++) + { + try + { + GlobalResourceManager.TryParse(addresses[i], out ParseResult result); + list.Add(result.AliasIfExists); + } + catch + { + //list.Add("None"); + } + } + return list.ToArray(); + } + + public static bool OpenIPAddress(string address, out string fullAddress) + { + bool result = false; + string addressTemp = $"TCPIP0::{address}::INSTR"; + fullAddress = addressTemp; + try + { + using (TcpipSession session = new TcpipSession(addressTemp)) + { + result = true; + } + } + catch { } + return result; + } + + public static string[] FindAddresses() + { + return FindAddresses(PortType.None); + } + } + +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs new file mode 100644 index 0000000..50cfab2 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + public static class RegexUlitity + { + public static bool IsMatch(this string input, string pattern) + { + return Regex.IsMatch(input, pattern); + } + } +} diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/VISA/Port/PortOperator.cs b/RbFreqStandMeasure/VISA/Port/PortOperator.cs new file mode 100644 index 0000000..292588e --- /dev/null +++ b/RbFreqStandMeasure/VISA/Port/PortOperator.cs @@ -0,0 +1,210 @@ +using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Port +{ + class RS232PortOperator : PortOperatorBase, IPortType + { + public int BaudRate { private set; get; } + + public SerialParity Parity { private set; get; } + + public SerialStopBitsMode StopBits { private set; get; } + + public int DataBits { private set; get; } + + public PortType PortType { get => PortType.RS232; } + + public SerialFlowControlModes FlowControl { set; get; } = SerialFlowControlModes.None; + + SerialSession serialSession; + + public RS232PortOperator(string address, int baudRate, SerialParity parity, SerialStopBitsMode stopBits, int dataBits) : base(new SerialSession(address), address) + { + if (!address.ToUpper().Contains("ASRL")) throw new ArgumentException($"该地址不含ASRL字样"); + BaudRate = baudRate; + Parity = parity; + StopBits = stopBits; + if (dataBits < 5 || dataBits > 8) throw new NotSupportedException($"不支持数据位为:{dataBits.ToString()}"); + DataBits = dataBits; + serialSession = (SerialSession)Session; + } + + public override void Open() + { + base.Open(); + serialSession.BaudRate = BaudRate; + switch (Parity) + { + case SerialParity.None: + serialSession.Parity = SerialParity.None; break; + case SerialParity.Odd: + serialSession.Parity = SerialParity.Odd; break; + case SerialParity.Even: + serialSession.Parity = SerialParity.Even; break; + case SerialParity.Mark: + serialSession.Parity = SerialParity.Mark; break; + case SerialParity.Space: + serialSession.Parity = SerialParity.Space; break; + } + switch (StopBits) + { + case SerialStopBitsMode.One: + serialSession.StopBits = SerialStopBitsMode.One; break; + case SerialStopBitsMode.OneAndOneHalf: + serialSession.StopBits = SerialStopBitsMode.OneAndOneHalf; break; + case SerialStopBitsMode.Two: + serialSession.StopBits = SerialStopBitsMode.Two; break; + } + serialSession.DataBits = (short)DataBits; + switch (FlowControl) + { + case SerialFlowControlModes.None: + serialSession.FlowControl = SerialFlowControlModes.None; break; + case SerialFlowControlModes.XOnXOff: + serialSession.FlowControl = SerialFlowControlModes.XOnXOff; break; + case SerialFlowControlModes.RtsCts: + serialSession.FlowControl = SerialFlowControlModes.RtsCts; break; + case SerialFlowControlModes.DtrDsr: + serialSession.FlowControl = SerialFlowControlModes.DtrDsr; break; + } + } + } + + class USBPortOperator : PortOperatorBase, IPortType + { + public USBPortOperator(string address) : base(new UsbSession(address), address) + { + if (!address.ToUpper().Contains("USB")) + throw new ArgumentException($"该地址不含USB字样"); + } + public PortType PortType { get => PortType.USB; } + } + + class GPIBPortOperator : PortOperatorBase, IPortType + { + public GPIBPortOperator(string address) : base(new GpibSession(address), address) + { + if (!address.ToUpper().Contains("GPIB")) + throw new ArgumentException($"该地址不含GPIB字样"); + } + public PortType PortType { get => PortType.GPIB; } + } + + class LANPortOperator : PortOperatorBase, IPortType + { + public LANPortOperator(string address) : base(new TcpipSession(address), address) + { + if (!address.ToUpper().Contains("TCPIP")) + throw new ArgumentException($"该地址不含TCPIP字样"); + } + public PortType PortType { get => PortType.LAN; } + } + + class PortEventArgs : EventArgs + { + public string Address { private set; get; } + public bool Cancel { set; get; } + public PortEventArgs(string address) + { + Address = address; + } + } + + abstract class PortOperatorBase : IPortOperator + { + public string Address { set; get; } + + public PortOperatorBase(IMessageBasedSession session) + { + Session = session; + } + public PortOperatorBase(IMessageBasedSession session, string address) : this(session) + { + Address = address; + } + + public int Timeout { set; get; } = 2000; + + public event EventHandler PortOpenning; + + public event EventHandler PortClosing; + + protected virtual void OnPortOpenning(PortEventArgs e) + { + PortOpenning?.Invoke(this, e); + } + + protected virtual void OnPortClosing(PortEventArgs e) + { + PortClosing?.Invoke(this, e); + } + + public bool IsPortOpen { private set; get; } = false; + + protected IMessageBasedSession Session { private set; get; } + + public virtual void Open() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortOpenning(e); + if (!e.Cancel) + { + Session.TimeoutMilliseconds = Timeout; + this.IsPortOpen = true; + } + } + + public virtual void Close() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortClosing(e); + if (!e.Cancel) + { + Session.Dispose(); + this.IsPortOpen = false; + } + } + + public virtual void Write(string command) + { + Session.RawIO.Write(command); + } + + public virtual void WriteLine(string command) + { + Write($"{command}\n"); + } + + //public const int READ_BUFFER_COUNT = 1024; + + public virtual string Read() + { + return Encoding.UTF8.GetString(Session.RawIO.Read()); + } + + public virtual string ReadLine() + { + string result = Read(); + return result.EndsWith("\n") ? result.TrimEnd(new char[] { '\n' }) : result; + } + } + + interface IPortOperator + { + void Open(); + void Close(); + void Write(string command); + string Read(); + } + + interface IPortType + { + PortType PortType { get; } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs new file mode 100644 index 0000000..acde3e0 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs @@ -0,0 +1,89 @@ +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + enum PortType + { + RS232, USB, GPIB, LAN, None + } + + class PortUltility + { + private static string ToStringFromPortType(PortType portType) + { + switch (portType) + { + case PortType.USB: return "USB"; + case PortType.GPIB: return "GPIB"; + case PortType.LAN: return "TCPIP"; + case PortType.None: return ""; + case PortType.RS232: + default: return "ASRL"; + } + } + + public static string[] FindAddresses(PortType portType) + { + IEnumerable result = new List(); + try + { + result = GlobalResourceManager.Find($"{ToStringFromPortType(portType)}?*INSTR"); + } + catch (Exception ex) + { + if (!(ex is NativeVisaException)) + { + if (ex.InnerException != null) throw ex.InnerException; + else throw ex; + } + } + + return result.ToArray().Where(n => !n.Contains("//")).ToArray(); + } + + public static string[] FindRS232Type(string[] addresses) + { + List list = new List(); + for (int i = 0; i < addresses.Length; i++) + { + try + { + GlobalResourceManager.TryParse(addresses[i], out ParseResult result); + list.Add(result.AliasIfExists); + } + catch + { + //list.Add("None"); + } + } + return list.ToArray(); + } + + public static bool OpenIPAddress(string address, out string fullAddress) + { + bool result = false; + string addressTemp = $"TCPIP0::{address}::INSTR"; + fullAddress = addressTemp; + try + { + using (TcpipSession session = new TcpipSession(addressTemp)) + { + result = true; + } + } + catch { } + return result; + } + + public static string[] FindAddresses() + { + return FindAddresses(PortType.None); + } + } + +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs new file mode 100644 index 0000000..50cfab2 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + public static class RegexUlitity + { + public static bool IsMatch(this string input, string pattern) + { + return Regex.IsMatch(input, pattern); + } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs new file mode 100644 index 0000000..feabfc5 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + static class StringsUlitity + { + public static string[] ToHexString(this string[] asciiCommands) + { + if (asciiCommands.Any(m => m.Any(n => n < 0 || n > 255))) + { + throw new ArgumentException("不是Ascii字符!"); + } + return (from m in (from n in asciiCommands select Encoding.ASCII.GetBytes(n)) select BitConverter.ToString(m).Replace("-", "")).ToArray(); + } + + public static string ToHexString(this string asciiCommand) + { + return new string[] { asciiCommand }.ToHexString()[0]; + } + + public static string[] ToAsciiString(this string[] hexCommands) + { + if (hexCommands.Select(x => x.ToUpper()).Any(m => m.Any(n => (n < 48 || n > 57) && (n < 65 || n > 70)))) + { + throw new ArgumentException("仅能包含十六进制字符!"); + } + + if (hexCommands.Any(m => m.Length % 2 != 0)) + { + throw new ArgumentException("Hex个数必须是2的倍数!"); + } + + string[] resultCommands = new string[hexCommands.Length]; + List list = null; + char[] temp = null; + for (int i = 0; i < hexCommands.Length; i++) + { + list = new List(); + for (int j = 0; j < hexCommands[i].Length; j = j + 2) + { + temp = new char[2]; + temp[0] = hexCommands[i][j]; + temp[1] = hexCommands[i][j + 1]; + list.Add(Convert.ToByte(new string(temp), 16)); + } + resultCommands[i] = Encoding.ASCII.GetString(list.ToArray()); + } + return resultCommands; + } + + public static string ToAsciiString(this string hexCommand) + { + return new string[] { hexCommand }.ToAsciiString()[0]; + } + + } +} \ No newline at end of file diff --git a/RbFreqStandMeasure/App.config b/RbFreqStandMeasure/App.config index 234e3f1..9764ef1 100644 --- a/RbFreqStandMeasure/App.config +++ b/RbFreqStandMeasure/App.config @@ -3,11 +3,16 @@ - - - - - + + + + + + + + + + @@ -72,4 +77,27 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RbFreqStandMeasure/RbFreqStandMeasure.csproj b/RbFreqStandMeasure/RbFreqStandMeasure.csproj index a3b6848..895f633 100644 --- a/RbFreqStandMeasure/RbFreqStandMeasure.csproj +++ b/RbFreqStandMeasure/RbFreqStandMeasure.csproj @@ -94,14 +94,23 @@ False bin\Debug\DevComponents.Instrumentation.Design.dll - - + + False + bin\Debug\DevComponents.SuperGrid.Design.dll + + + False + bin\Debug\DevComponents.TreeGX.dll + ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.dll ..\packages\EntityFramework.6.3.0\lib\net40\EntityFramework.SqlServer.dll + + ..\packages\Kelary.Ivi.Visa.5.11.3422\lib\net20\Ivi.Visa.dll + ..\packages\log4net.2.0.12\lib\net40\log4net.dll @@ -109,6 +118,34 @@ False bin\Debug\MySql.Data.dll + + False + bin\Debug\MySql.Data.EntityFramework.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.dll + + + False + bin\Debug\MySQL.Data.EntityFrameworkCore.Design.dll + + + False + bin\Debug\MySql.Web.dll + + + False + bin\Debug\NationalInstruments.Common.dll + + + False + bin\Debug\NationalInstruments.Visa.dll + + + False + bin\Debug\NationalInstruments.VisaNS.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll @@ -296,6 +333,10 @@ StatusCtrlForm.cs + + + + AddDlg.cs diff --git a/RbFreqStandMeasure/VISA/Port/PortOperator.cs b/RbFreqStandMeasure/VISA/Port/PortOperator.cs new file mode 100644 index 0000000..292588e --- /dev/null +++ b/RbFreqStandMeasure/VISA/Port/PortOperator.cs @@ -0,0 +1,210 @@ +using Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity; +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Port +{ + class RS232PortOperator : PortOperatorBase, IPortType + { + public int BaudRate { private set; get; } + + public SerialParity Parity { private set; get; } + + public SerialStopBitsMode StopBits { private set; get; } + + public int DataBits { private set; get; } + + public PortType PortType { get => PortType.RS232; } + + public SerialFlowControlModes FlowControl { set; get; } = SerialFlowControlModes.None; + + SerialSession serialSession; + + public RS232PortOperator(string address, int baudRate, SerialParity parity, SerialStopBitsMode stopBits, int dataBits) : base(new SerialSession(address), address) + { + if (!address.ToUpper().Contains("ASRL")) throw new ArgumentException($"该地址不含ASRL字样"); + BaudRate = baudRate; + Parity = parity; + StopBits = stopBits; + if (dataBits < 5 || dataBits > 8) throw new NotSupportedException($"不支持数据位为:{dataBits.ToString()}"); + DataBits = dataBits; + serialSession = (SerialSession)Session; + } + + public override void Open() + { + base.Open(); + serialSession.BaudRate = BaudRate; + switch (Parity) + { + case SerialParity.None: + serialSession.Parity = SerialParity.None; break; + case SerialParity.Odd: + serialSession.Parity = SerialParity.Odd; break; + case SerialParity.Even: + serialSession.Parity = SerialParity.Even; break; + case SerialParity.Mark: + serialSession.Parity = SerialParity.Mark; break; + case SerialParity.Space: + serialSession.Parity = SerialParity.Space; break; + } + switch (StopBits) + { + case SerialStopBitsMode.One: + serialSession.StopBits = SerialStopBitsMode.One; break; + case SerialStopBitsMode.OneAndOneHalf: + serialSession.StopBits = SerialStopBitsMode.OneAndOneHalf; break; + case SerialStopBitsMode.Two: + serialSession.StopBits = SerialStopBitsMode.Two; break; + } + serialSession.DataBits = (short)DataBits; + switch (FlowControl) + { + case SerialFlowControlModes.None: + serialSession.FlowControl = SerialFlowControlModes.None; break; + case SerialFlowControlModes.XOnXOff: + serialSession.FlowControl = SerialFlowControlModes.XOnXOff; break; + case SerialFlowControlModes.RtsCts: + serialSession.FlowControl = SerialFlowControlModes.RtsCts; break; + case SerialFlowControlModes.DtrDsr: + serialSession.FlowControl = SerialFlowControlModes.DtrDsr; break; + } + } + } + + class USBPortOperator : PortOperatorBase, IPortType + { + public USBPortOperator(string address) : base(new UsbSession(address), address) + { + if (!address.ToUpper().Contains("USB")) + throw new ArgumentException($"该地址不含USB字样"); + } + public PortType PortType { get => PortType.USB; } + } + + class GPIBPortOperator : PortOperatorBase, IPortType + { + public GPIBPortOperator(string address) : base(new GpibSession(address), address) + { + if (!address.ToUpper().Contains("GPIB")) + throw new ArgumentException($"该地址不含GPIB字样"); + } + public PortType PortType { get => PortType.GPIB; } + } + + class LANPortOperator : PortOperatorBase, IPortType + { + public LANPortOperator(string address) : base(new TcpipSession(address), address) + { + if (!address.ToUpper().Contains("TCPIP")) + throw new ArgumentException($"该地址不含TCPIP字样"); + } + public PortType PortType { get => PortType.LAN; } + } + + class PortEventArgs : EventArgs + { + public string Address { private set; get; } + public bool Cancel { set; get; } + public PortEventArgs(string address) + { + Address = address; + } + } + + abstract class PortOperatorBase : IPortOperator + { + public string Address { set; get; } + + public PortOperatorBase(IMessageBasedSession session) + { + Session = session; + } + public PortOperatorBase(IMessageBasedSession session, string address) : this(session) + { + Address = address; + } + + public int Timeout { set; get; } = 2000; + + public event EventHandler PortOpenning; + + public event EventHandler PortClosing; + + protected virtual void OnPortOpenning(PortEventArgs e) + { + PortOpenning?.Invoke(this, e); + } + + protected virtual void OnPortClosing(PortEventArgs e) + { + PortClosing?.Invoke(this, e); + } + + public bool IsPortOpen { private set; get; } = false; + + protected IMessageBasedSession Session { private set; get; } + + public virtual void Open() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortOpenning(e); + if (!e.Cancel) + { + Session.TimeoutMilliseconds = Timeout; + this.IsPortOpen = true; + } + } + + public virtual void Close() + { + PortEventArgs e = new PortEventArgs(Address); + OnPortClosing(e); + if (!e.Cancel) + { + Session.Dispose(); + this.IsPortOpen = false; + } + } + + public virtual void Write(string command) + { + Session.RawIO.Write(command); + } + + public virtual void WriteLine(string command) + { + Write($"{command}\n"); + } + + //public const int READ_BUFFER_COUNT = 1024; + + public virtual string Read() + { + return Encoding.UTF8.GetString(Session.RawIO.Read()); + } + + public virtual string ReadLine() + { + string result = Read(); + return result.EndsWith("\n") ? result.TrimEnd(new char[] { '\n' }) : result; + } + } + + interface IPortOperator + { + void Open(); + void Close(); + void Write(string command); + string Read(); + } + + interface IPortType + { + PortType PortType { get; } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs new file mode 100644 index 0000000..acde3e0 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/PortUlitity.cs @@ -0,0 +1,89 @@ +using Ivi.Visa; +using NationalInstruments.Visa; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + enum PortType + { + RS232, USB, GPIB, LAN, None + } + + class PortUltility + { + private static string ToStringFromPortType(PortType portType) + { + switch (portType) + { + case PortType.USB: return "USB"; + case PortType.GPIB: return "GPIB"; + case PortType.LAN: return "TCPIP"; + case PortType.None: return ""; + case PortType.RS232: + default: return "ASRL"; + } + } + + public static string[] FindAddresses(PortType portType) + { + IEnumerable result = new List(); + try + { + result = GlobalResourceManager.Find($"{ToStringFromPortType(portType)}?*INSTR"); + } + catch (Exception ex) + { + if (!(ex is NativeVisaException)) + { + if (ex.InnerException != null) throw ex.InnerException; + else throw ex; + } + } + + return result.ToArray().Where(n => !n.Contains("//")).ToArray(); + } + + public static string[] FindRS232Type(string[] addresses) + { + List list = new List(); + for (int i = 0; i < addresses.Length; i++) + { + try + { + GlobalResourceManager.TryParse(addresses[i], out ParseResult result); + list.Add(result.AliasIfExists); + } + catch + { + //list.Add("None"); + } + } + return list.ToArray(); + } + + public static bool OpenIPAddress(string address, out string fullAddress) + { + bool result = false; + string addressTemp = $"TCPIP0::{address}::INSTR"; + fullAddress = addressTemp; + try + { + using (TcpipSession session = new TcpipSession(addressTemp)) + { + result = true; + } + } + catch { } + return result; + } + + public static string[] FindAddresses() + { + return FindAddresses(PortType.None); + } + } + +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs new file mode 100644 index 0000000..50cfab2 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/RegexUlitity.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + public static class RegexUlitity + { + public static bool IsMatch(this string input, string pattern) + { + return Regex.IsMatch(input, pattern); + } + } +} diff --git a/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs b/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs new file mode 100644 index 0000000..feabfc5 --- /dev/null +++ b/RbFreqStandMeasure/VISA/Ulitity/StringsUlitity.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Casic.Birmm.RbFreqStandMeasure.VISA.Ulitity +{ + static class StringsUlitity + { + public static string[] ToHexString(this string[] asciiCommands) + { + if (asciiCommands.Any(m => m.Any(n => n < 0 || n > 255))) + { + throw new ArgumentException("不是Ascii字符!"); + } + return (from m in (from n in asciiCommands select Encoding.ASCII.GetBytes(n)) select BitConverter.ToString(m).Replace("-", "")).ToArray(); + } + + public static string ToHexString(this string asciiCommand) + { + return new string[] { asciiCommand }.ToHexString()[0]; + } + + public static string[] ToAsciiString(this string[] hexCommands) + { + if (hexCommands.Select(x => x.ToUpper()).Any(m => m.Any(n => (n < 48 || n > 57) && (n < 65 || n > 70)))) + { + throw new ArgumentException("仅能包含十六进制字符!"); + } + + if (hexCommands.Any(m => m.Length % 2 != 0)) + { + throw new ArgumentException("Hex个数必须是2的倍数!"); + } + + string[] resultCommands = new string[hexCommands.Length]; + List list = null; + char[] temp = null; + for (int i = 0; i < hexCommands.Length; i++) + { + list = new List(); + for (int j = 0; j < hexCommands[i].Length; j = j + 2) + { + temp = new char[2]; + temp[0] = hexCommands[i][j]; + temp[1] = hexCommands[i][j + 1]; + list.Add(Convert.ToByte(new string(temp), 16)); + } + resultCommands[i] = Encoding.ASCII.GetString(list.ToArray()); + } + return resultCommands; + } + + public static string ToAsciiString(this string hexCommand) + { + return new string[] { hexCommand }.ToAsciiString()[0]; + } + + } +} \ No newline at end of file diff --git a/RbFreqStandMeasure/packages.config b/RbFreqStandMeasure/packages.config index ecdd1da..fafac51 100644 --- a/RbFreqStandMeasure/packages.config +++ b/RbFreqStandMeasure/packages.config @@ -1,6 +1,7 @@  + \ No newline at end of file