Commit a955f1fd authored by 徐生海's avatar 徐生海

Initial commit

parent a1ab8d35
Pipeline #172 failed with stages
## ide
# 忽略操作系统生成的文件
DS_Store
# 忽略日志文件
*.log
**/.vs
**/.idea
work.md
__pycache__/
*.py[cod]
$__pycache__$
/logs/
/Resource/
/dist/
/build/
# 忽略环境变量文件
env
# 忽略依赖文件夹
node_modules/
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeiCaMicroscopeAPI
{
public class Class1
{
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BC258950-8D1B-473A-8DAF-7AB127CA8F7A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LeiCaMicroscopeAPI</RootNamespace>
<AssemblyName>LeiCaMicroscopeAPI</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="YQLContexts\LeiCaContext.cs" />
<Compile Include="YQLContexts\SerialContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="YQLControllers\MicController.cs" />
<Compile Include="YQLInterfaces\IMicController.cs" />
<Compile Include="YQLInterfaces\IYQLContext.cs" />
<Compile Include="YQLInterfaces\IMicroscope.cs" />
<Compile Include="YQLInterfaces\ISerialPort.cs" />
<Compile Include="YQLInterfaces\YQLInterface.cs" />
<Compile Include="YQLReaders\APBLController.cs" />
<Compile Include="YQLReaders\DocuPortController.cs" />
<Compile Include="YQLReaders\EventController.cs" />
<Compile Include="YQLReaders\LampController.cs" />
<Compile Include="YQLControllers\LeiCaMicReader.cs" />
<Compile Include="YQLReaders\LFBLController.cs" />
<Compile Include="YQLReaders\MasterController.cs" />
<Compile Include="YQLReaders\ObjectiveController.cs" />
<Compile Include="YQLReaders\SerialPortReader.cs" />
<Compile Include="YQLReaders\SpotLightController.cs" />
<Compile Include="YQLReaders\ZDriverController.cs" />
<Compile Include="YQLUtils\AppTimeLoop.cs" />
<Compile Include="YQLUtils\MicID.cs" />
<Compile Include="YQLUtils\MicUtil.cs" />
<Compile Include="YQLUtils\MicSettings.cs" />
<Compile Include="YQLUtils\SerialPortUtil.cs" />
<Compile Include="YQLUtils\Settings.cs" />
<Compile Include="YQLUtils\YQLUtil.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("LeiCaMicroscopeAPI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LeiCaMicroscopeAPI")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("bc258950-8d1b-473a-8daf-7ab127ca8f7a")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YQLInterfaces
{
public class LeiCaContext : IYQLContext
{
public LeiCaContext(string portname = "COM5", int baudrate = 9600)
{
initialization("徕卡显微镜上下文");
PortName = portname;
BaudRate = baudrate;
}
public override void initValue()
{
PortName = "COM6" ;
BaudRate = 9600 ;
DataBits = 8 ;
StopBits = 1 ;
Parity = 0 ;
ReadTimeout = 100 ;
SendTimeout = 1000 ;
}
public void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0)
{
PortName = portname ;
BaudRate = baudrate ;
DataBits = databits ;
StopBits = stopbits ;
Parity = parity ;
}
public string PortName { get; set; }
public int BaudRate { get; set; }
public int DataBits { get; set; }
public int StopBits { get; set; }
public int Parity { get; set; }
public int ReadTimeout { get; set; }
public int SendTimeout { get; set; }
public bool hasPortName { get{ return hasProperty("PortName" , typeof(string)); }}
public bool hasBaudRate { get{ return hasProperty("BaudRate" , typeof(string)); }}
public bool hasDataBits { get{ return hasProperty("DataBits" , typeof(string)); }}
public bool hasStopBits { get{ return hasProperty("StopBits" , typeof(string)); }}
public bool hasParity { get{ return hasProperty("Parity" , typeof(string)); }}
public bool hasReadTimeout { get{ return hasProperty("ReadTimeout" , typeof(string)); }}
public bool hasSendTimeout { get{ return hasProperty("SendTimeout" , typeof(string)); }}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YQLInterfaces
{
public class SerialContext: IYQLContext
{
public SerialContext(string portname = "COM5", int baudrate=9600)
{
initialization("串口上下文");
PortName = portname;
BaudRate = baudrate;
}
public override void initValue()
{
PortName = "COM5" ;
BaudRate = 9600 ;
DataBits = 8 ;
StopBits = 1 ;
Parity = 0 ;
ReadTimeout = 100 ;
SendTimeout = 1000 ;
}
public void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0)
{
PortName = portname ;
BaudRate = baudrate ;
DataBits = databits ;
StopBits = stopbits ;
Parity = parity ;
}
public string PortName { get; set; }
public int BaudRate { get; set; }
public int DataBits { get; set; }
public int StopBits { get; set; }
public int Parity { get; set; }
public int ReadTimeout { get; set; }
public int SendTimeout { get; set; }
public bool hasPortName { get{ return hasProperty("PortName" , typeof(string)); }}
public bool hasBaudRate { get{ return hasProperty("BaudRate" , typeof(string)); }}
public bool hasDataBits { get{ return hasProperty("DataBits" , typeof(string)); }}
public bool hasStopBits { get{ return hasProperty("StopBits" , typeof(string)); }}
public bool hasParity { get{ return hasProperty("Parity" , typeof(string)); }}
public bool hasReadTimeout { get{ return hasProperty("ReadTimeout" , typeof(string)); }}
public bool hasSendTimeout { get{ return hasProperty("SendTimeout" , typeof(string)); }}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using YQLUtils;
using YQLInterfaces;
namespace ILeiCaMicroscope
{
public class LeiCaMicReader : YQLInterface, IMicroscope
{
private LeiCaMicReader(string portname = "COM6", int baudrate=9600)
{
initialization("SerialPortReader");
setParameter(portname, baudrate);
}
~LeiCaMicReader()
{
deinitialize();
}
public void Dispose()
{
deinitialize();
}
public override void initValue()
{
initParameter ();
initManager ();
}
public override void deleValue()
{
deleManager ();
deleParameter ();
}
public IMicroscope Instance() { return GetInstance(); }
private static IMicroscope uniqueInstance;
// 定义一个标识确保线程同步
private static readonly object locker = new object();
/// <summary>
/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
/// </summary>
/// <returns></returns>
public static IMicroscope GetInstance()
{
// 当第一个线程运行到这里时,此时会对locker对象 "加锁",
// 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
// lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
// 双重锁定只需要一句判断就可以了
if (uniqueInstance == null)
{
lock (locker)
{
// 如果类的实例不存在则创建,否则直接返回
if (uniqueInstance == null)
{
uniqueInstance = new LeiCaMicReader();
}
}
}
return uniqueInstance;
}
/*===========================================================================*/
protected SerialPort Manager = null;
protected LeiCaContext Parameter = null;
protected static readonly object IOLocker = new object();
public bool hasManager { get {return Manager != null ;} }
public bool hasParameter { get {return Parameter != null ;} }
public bool IsValid { get {return hasManager && hasParameter ;} }
public bool IsAvailable { get {return IsValid && IsOpen ;} }
public bool IsOpen { get {return IsValid && Manager.IsOpen ;} }
public int Open () {
int isWarning = Settings.RECODE_FAILURE;
try
{
if (IsOpen)
Close();
if (!hasManager)
initManager();
if (!hasManager)
return isWarning;
Manager.PortName = Parameter.PortName ;
Manager.BaudRate = Parameter.BaudRate ;
Manager.DataBits = Parameter.DataBits ;
Manager.StopBits = (StopBits)Parameter.StopBits ;
Manager.Parity = (Parity)Parameter.Parity ;
Manager.ReadTimeout = Parameter.ReadTimeout ;
Manager.WriteTimeout = Parameter.SendTimeout ;
Manager.Open();
isWarning = Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int Close () {
int isWarning = Settings.RECODE_FAILURE;
try
{
if(hasManager)
{
Manager.Close();
Manager.Dispose();
}
isWarning = !hasManager || !Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int ReOpen () {
Close();
return Open();
}
public void initManager () {
try
{
initParameter();
if(!hasManager)
{
Manager = new SerialPort();
}
}
catch (Exception E) {
YQLUtil.Print_Exception(E);
Manager = null;
}
}
public void deleManager () {
try
{
if(hasManager)
{
Manager.Close();
Manager.Dispose();
}
Manager = null;
GC.Collect();
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
public void initParameter () {
try
{
if (!hasParameter || !Parameter.hasPortName || !Parameter.hasBaudRate)
Parameter = new LeiCaContext();
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
public void deleParameter () {
Parameter = null;
GC.Collect();
}
public void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0)
{
initParameter();
if(hasParameter)
Parameter.setParameter(portname, baudrate, databits, stopbits, parity);
}
public string ReadString (string frameTail, int timeout = 100)
{
var result = Settings.DEFAULT_STR;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.ReadString(Manager, frameTail, timeout);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return result;
}
public byte[] ReadBinary (string frameTail, int timeout = 100)
{
var result = Settings.DEFAULT_BYTE_ARRAY;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.ReadBinary(Manager, frameTail, timeout);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return result;
}
public int SendString (string buffer)
{
var isWarning = Settings.RECODE_FAILURE;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.SendString(Manager, buffer);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int SendBinary (byte[] buffer)
{
var isWarning = Settings.RECODE_FAILURE;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.SendBinary(Manager, buffer);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public string WaitDone (string buffer, string frameTail, int timeout = 100, int waittime = 0)
{
var result = Settings.DEFAULT_STR;
//lock (IOLocker)
//{
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
int isWarning = SerialPortUtil.SendString(Manager, buffer);
if (Settings.RECODE_SUCCEED == isWarning)
{
if(waittime <= 0)
{
}
else
AppTimeLoop.ProcessEvents(waittime);
result = SerialPortUtil.ReadString(Manager, frameTail, timeout);
}
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
//}
return result;
}
public byte[] WaitDone (byte[] buffer, string frameTail, int timeout = 100, int waittime = 0)
{
var result = Settings.DEFAULT_BYTE_ARRAY;
//lock (IOLocker)
//{
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
int isWarning = SerialPortUtil.SendBinary(Manager, buffer);
if (Settings.RECODE_SUCCEED == isWarning)
{
if (waittime <= 0)
{
}
else
AppTimeLoop.ProcessEvents(waittime);
result = SerialPortUtil.ReadBinary(Manager, frameTail, timeout);
}
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
//}
return result;
}
}
}
This diff is collapsed.
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using YQLInterfaces;
namespace ILeiCaMicroscope
{
public interface IMicroscope : IDisposable
{
IMicroscope Instance();
bool hasManager { get; }
bool hasParameter { get; }
bool IsValid { get; }
bool IsAvailable { get; }
bool IsOpen { get; }
int Open ();
int Close ();
int ReOpen ();
void initManager ();
void deleManager ();
void initParameter ();
void deleParameter ();
void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0);
string ReadString (string frameTail, int timeout = 100);
byte[] ReadBinary (string frameTail, int timeout = 100);
int SendString (string buffer);
int SendBinary (byte[] buffer);
string WaitDone (string buffer, string frameTail, int timeout = 100, int waittime = 2);
byte[] WaitDone (byte[] buffer, string frameTail, int timeout = 100, int waittime = 2);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YQLInterfaces
{
public interface ISerialPort: IDisposable
{
ISerialPort Instance();
bool hasManager { get; }
bool hasParameter { get; }
bool IsValid { get; }
bool IsAvailable { get; }
bool IsOpen { get; }
int Open ();
int Close ();
int ReOpen ();
void initManager ();
void deleManager ();
void initParameter ();
void deleParameter ();
void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0);
string ReadString (string frameTail, int timeout = 100);
byte[] ReadBinary (string frameTail, int timeout = 100);
int SendString (string buffer);
int SendBinary (byte[] buffer);
string WaitDone (string buffer, string frameTail, int timeout = 100, int waittime = 50);
byte[] WaitDone (byte[] buffer, string frameTail, int timeout = 100, int waittime = 50);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YQLInterfaces
{
public class IYQLContext : YQLInterface
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace YQLInterfaces
{
public class YQLInterface
{
public string Information { get; set; }
public static bool has_Field(object src, string name, Type TYPE)
{
try
{
if (src == null) return false;
var propertyInfo = src.GetType().GetProperty(name);
if (propertyInfo != null && propertyInfo.GetValue(src, null) != null)
return propertyInfo.GetValue(src, null).GetType() == TYPE;
}
catch (Exception) { }
return false;
}
public static T GetField<T>(object src, string name)
{
try
{
if (src == null) return default(T);
var propertyInfo = src.GetType().GetProperty(name);
return (T)propertyInfo.GetValue(src, null);
}
catch (Exception) { }
return default(T);
}
public static void SetField<T>(object src, string name, T value)
{
try
{
if (src == null) return;
var propertyInfo = src.GetType().GetProperty(name);
propertyInfo.SetValue(src, value, null);
}
catch (Exception) { }
return;
}
public virtual bool hasProperty(string name, Type TYPE)
{
try
{
var propertyInfo = GetType().GetProperty(name);
if (propertyInfo != null && propertyInfo.GetValue(this, null) != null)
return propertyInfo.GetValue(this, null).GetType() == TYPE;
}
catch (Exception) { }
return false;
}
public virtual T getProperty<T>(string name)
{
try
{
var propertyInfo = GetType().GetProperty(name);
return (T)propertyInfo.GetValue(this, null);
}
catch (Exception) { }
return default(T);
}
public virtual void setProperty<T>(string name, T value)
{
try
{
var propertyInfo = GetType().GetProperty(name);
propertyInfo.SetValue(this, value, null);
}
catch (Exception) { }
return;
}
public virtual void initialization(string information)
{
Information = information;
initialize();
}
public virtual void initialize()
{
initData();
initValue();
initProperty();
initConnect();
}
public virtual void deinitialize()
{
deleConnect();
deleProperty();
deleValue();
deleData();
}
public virtual void initData() { }
public virtual void initValue() { }
public virtual void initProperty() { }
public virtual void initConnect() { }
public virtual void deleData() { }
public virtual void deleValue() { }
public virtual void deleProperty() { }
public virtual void deleConnect() { }
public void From(object source)
{
if (source == null)
return;
if (object.ReferenceEquals(this, source))
return;
Type type = source.GetType();
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!property.CanWrite) continue;
if (!hasProperty(property.Name, property.PropertyType)) continue;
object value = property.GetValue(source);
if (value == null || property.PropertyType.IsValueType || property.PropertyType == typeof(string))
{
property.SetValue(this, value);
}
else
{
property.SetValue(this, DeepCopy(value));
}
}
}
public object Copy()
{
Type type = GetType();
object target = DeepCopy(this);
return target;
}
public static object DeepCopy(object source)
{
if (source == null) return null;
Type type = source.GetType();
object target = Activator.CreateInstance(type);
// 遍历所有属性
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!property.CanWrite) continue;
object value = property.GetValue(source);
if (value == null || property.PropertyType.IsValueType || property.PropertyType == typeof(string))
{
property.SetValue(target, value);
}
else
{
property.SetValue(target, DeepCopy(value));
}
}
// 遍历所有字段
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
object value = field.GetValue(source);
if (value == null || field.FieldType.IsValueType || field.FieldType == typeof(string))
{
field.SetValue(target, value);
}
else
{
field.SetValue(target, DeepCopy(value));
}
}
return target;
}
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using YQLUtils;
namespace YQLInterfaces
{
public class SerialPortReader: YQLInterface, ISerialPort
{
public ISerialPort Instance() { return new SerialPortReader(); }
public bool hasManager { get { return Manager != null; } }
public bool hasParameter { get { return Parameter != null; } }
public bool IsValid { get { return hasManager && hasParameter; } }
public bool IsAvailable { get { return IsValid && Manager.IsOpen; } }
public bool IsOpen { get { return IsValid && Manager.IsOpen; } }
public int Open () {
int isWarning = Settings.RECODE_FAILURE;
try
{
if (IsOpen)
Close();
if (!hasManager)
initManager();
if (!hasManager)
return isWarning;
Manager.PortName = Parameter.PortName ;
Manager.BaudRate = Parameter.BaudRate ;
Manager.DataBits = Parameter.DataBits ;
Manager.StopBits = (StopBits)Parameter.StopBits ;
Manager.Parity = (Parity)Parameter.Parity ;
Manager.ReadTimeout = Parameter.ReadTimeout ;
Manager.WriteTimeout = Parameter.SendTimeout ;
Manager.Open();
isWarning = Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int Close () {
int isWarning = Settings.RECODE_FAILURE;
try
{
if(hasManager)
{
Manager.Close();
Manager.Dispose();
}
isWarning = !hasManager || !Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int ReOpen () {
Close();
return Open();
}
public void initManager () {
try
{
initParameter();
if(!hasManager)
{
Manager = new SerialPort();
}
}
catch (Exception E) {
YQLUtil.Print_Exception(E);
Manager = null;
}
}
public void deleManager () {
try
{
if(hasManager)
{
Manager.Close();
Manager.Dispose();
}
Manager = null;
GC.Collect();
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
public void initParameter () {
try
{
if (!hasParameter || !Parameter.hasPortName || !Parameter.hasBaudRate)
Parameter = new SerialContext();
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
public void deleParameter () {
Parameter = null;
GC.Collect();
}
public void setParameter(string portname, int baudrate, int databits = 8, int stopbits = 1, int parity = 0)
{
initParameter();
setParameter(portname, baudrate, databits, stopbits, parity);
}
public string ReadString (string frameTail, int timeout = 100)
{
var result = Settings.DEFAULT_STR;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.ReadString(Manager, frameTail, timeout);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return result;
}
public byte[] ReadBinary (string frameTail, int timeout = 100)
{
var result = Settings.DEFAULT_BYTE_ARRAY;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.ReadBinary(Manager, frameTail, timeout);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return result;
}
public int SendString (string buffer)
{
var isWarning = Settings.RECODE_FAILURE;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.SendString(Manager, buffer);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public int SendBinary (byte[] buffer)
{
var isWarning = Settings.RECODE_FAILURE;
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
return SerialPortUtil.SendBinary(Manager, buffer);
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
return isWarning;
}
public string WaitDone (string buffer, string frameTail, int timeout = 100, int waittime = 50)
{
var result = Settings.DEFAULT_STR;
lock (IOLocker)
{
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
int isWarning = SerialPortUtil.SendString(Manager, buffer);
if (Settings.RECODE_SUCCEED == isWarning)
{
AppTimeLoop.ProcessEvents(waittime);
result = SerialPortUtil.ReadString(Manager, frameTail, timeout);
}
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
return result;
}
public byte[] WaitDone (byte[] buffer, string frameTail, int timeout = 100, int waittime = 50)
{
var result = Settings.DEFAULT_BYTE_ARRAY;
lock (IOLocker)
{
try
{
if (!IsValid) initManager();
if (!IsOpen) Open();
int isWarning = SerialPortUtil.SendBinary(Manager, buffer);
if (Settings.RECODE_SUCCEED == isWarning)
{
AppTimeLoop.ProcessEvents(waittime);
result = SerialPortUtil.ReadBinary(Manager, frameTail, timeout);
}
}
catch (Exception E) { YQLUtil.Print_Exception(E); }
}
return result;
}
private SerialPort Manager = null;
private SerialContext Parameter = null;
protected static readonly object IOLocker = new object();
public SerialPortReader()
{
initialization("SerialPortReader");
}
public override void initValue()
{
initParameter();
initManager ();
}
public override void deleValue()
{
deleParameter();
deleManager ();
}
~SerialPortReader() { deinitialize(); }
public void Dispose()
{
deinitialize();
}
}
}
This diff is collapsed.
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace YQLUtils
{
public class AppTimeLoop
{
public static void ProcessEvents(int interVal)
{
int waittime = 2;
DateTime startTime = DateTime.Now;
while(true)
{
DateTime endTime = DateTime.Now;
if ((int)(endTime - startTime).TotalMilliseconds >= interVal)
break;
Thread.Sleep(waittime);
}
}
public static void delay(int interVal)
{
if (interVal == 0)
return;
Thread.Sleep(Math.Abs(interVal));
}
public static void msleep(int interVal)
{
if (interVal == 0)
return;
Thread.Sleep(Math.Abs(interVal));
}
public static void sleep(int interVal)
{
if (interVal == 0)
return;
Thread.Sleep(Math.Abs(interVal * 1000));
}
}
}
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ILeiCaMicroscope
{
public class MicSettings
{
public static string MicFrameSpace = " " ;
public static string MicFrameTails = "\r" ;
public static string MicParameterFrame = " %s\r" ;
public static int MicFrameTimeOut = 500 ;
public static int MicMaxFrameTimeOut = 60000 ;
public static int MicFrameWaitTimes = 0 ;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ILeiCaMicroscope
{
class MicUtil
{
public static string ConvertToString(List<int> buffer, string space = " ", string tail = "\r")
{
try
{
if(buffer.Count <= 0)
return default(string);
StringBuilder StringBuffer = new StringBuilder();
foreach (int data in buffer)
{
StringBuffer.Append(data.ToString());
StringBuffer.Append(space);
}
StringBuffer.Remove(StringBuffer.Length - space.Length, space.Length);
StringBuffer.Append(tail);
return StringBuffer.ToString();
}
catch (Exception) { }
return default(string);
}
public static string MicroscopeCommand(int ID, string parameter)
{
try
{
string buffer = ID.ToString() + " " + parameter + "\r";
return buffer;
}catch(Exception)
{
return null;
}
}
public static string ConvertToUtf8(string buffer)
{
try
{
if (buffer == null)
return default(string);
UTF8Encoding utf8 = new UTF8Encoding();
return utf8.GetString(utf8.GetBytes(buffer));
}
catch (Exception)
{
}
return default(string);
}
public static int ConvertToInt(object buffer)
{
try
{
if (buffer == null)
return default(int);
if (buffer.GetType() == typeof(int))
return (int)buffer;
return Convert.ToInt32(buffer);
}
catch (Exception)
{
return default(int);
}
}
public static float ConvertToFloat(object buffer)
{
try
{
if (buffer == null)
return default(float);
if (buffer.GetType() == typeof(float))
return (int)buffer;
return Convert.ToSingle(buffer);
}
catch (Exception)
{
return default(float);
}
}
public static int ConvertObjective(string buffer)
{
try
{
if (string.IsNullOrEmpty(buffer))
return default(int);
return Convert.ToInt32(buffer);
}catch(Exception){
return default(int) ;
}
}
public static List<int> Parser(string buffer, string space = " ")
{
List<int> Array = new List<int>();
try
{
if (string.IsNullOrEmpty(buffer))
return Array;
String[] Buffer = buffer.Split(space.ToCharArray());
foreach(string data in Buffer)
{
Array.Add(MicUtil.ConvertObjective(data));
}
}
catch (Exception)
{ }
return Array;
}
public static String[] ConvertStringList(string buffer, string space = " ")
{
try
{
if (string.IsNullOrEmpty(buffer))
return default(String[]);
String[] Buffer = buffer.Split(space.ToCharArray());
return Buffer;
}
catch (Exception)
{ }
return default(String[]);
}
public static int StringListIndexOf(string[] buffer, string target)
{
int index = -1;
int count = 0;
foreach(string data in buffer)
{
if(data.ToLower().Equals(target.ToLower()))
{
index = count;
break;
}
count += 1;
}
return index;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace YQLUtils
{
using YQLInterfaces;
public class SerialPortUtil
{
public static int Open(SerialPort Manager, SerialContext Parameter)
{
int isWarning = Settings.RECODE_FAILURE;
try
{
if (Manager == null || Parameter == null)
return isWarning;
if (Manager.IsOpen)
SerialPortUtil.Close(Manager);
Manager.PortName = Parameter.PortName ;
Manager.BaudRate = Parameter.BaudRate ;
Manager.DataBits = Parameter.DataBits ;
Manager.StopBits = (StopBits)Parameter.StopBits ;
Manager.Parity = (Parity) Parameter.Parity ;
Manager.ReadTimeout = Parameter.ReadTimeout ;
Manager.WriteTimeout = Parameter.SendTimeout ;
Manager.Open ( );
isWarning = Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception) { }
return isWarning;
}
public static int Open(SerialPort Manager, LeiCaContext Parameter)
{
int isWarning = Settings.RECODE_FAILURE;
try
{
if (Manager == null || Parameter == null)
return isWarning;
if (Manager.IsOpen)
SerialPortUtil.Close(Manager);
Manager.PortName = Parameter.PortName ;
Manager.BaudRate = Parameter.BaudRate ;
Manager.DataBits = Parameter.DataBits ;
Manager.StopBits = (StopBits)Parameter.StopBits ;
Manager.Parity = (Parity) Parameter.Parity ;
Manager.ReadTimeout = Parameter.ReadTimeout ;
Manager.WriteTimeout = Parameter.SendTimeout ;
Manager.Open ( );
isWarning = Manager.IsOpen ? Settings.RECODE_SUCCEED : Settings.RECODE_FAILURE;
}
catch (Exception) { }
return isWarning;
}
public static int Close(SerialPort Manager)
{
int isWarning = Settings.RECODE_SUCCEED;
try
{
if (Manager == null)
return isWarning;
if (Manager.IsOpen)
{
Manager.Close();
}
}
catch (Exception) { }
return isWarning;
}
public static int SendString(SerialPort Manager, string buffer)
{
int isWarning = Settings.RECODE_FAILURE;
try
{
if (Manager == null)
return isWarning;
if (buffer.Length == 0)
return isWarning;
Manager.Write(buffer);
isWarning = Settings.RECODE_SUCCEED;
}
catch (Exception) { }
return isWarning;
}
public static int SendBinary(SerialPort Manager, byte[] buffer)
{
int isWarning = Settings.RECODE_FAILURE;
try
{
if (Manager == null)
return isWarning;
if (buffer.Length == 0)
return isWarning;
Manager.Write(buffer, 0, buffer.Length);
isWarning = Settings.RECODE_SUCCEED;
}
catch (Exception) { }
return isWarning;
}
public static string ReadString(SerialPort Manager, string frameTail = null, int timeout=1000)
{
StringBuilder Buffer = new StringBuilder();
try
{
if (Manager == null)
return Buffer.ToString();
DateTime starttime = DateTime.Now ;
int readLength = 0 ;
int timeOffset ;
bool IsTail = (frameTail != null) && (frameTail.Length > 0);
bool atEnd = false;
while (true)
{
readLength = Manager.BytesToRead;
if (readLength > 0)
{
Buffer.Append(Manager.ReadExisting());
}
if (IsTail)
atEnd = Buffer.ToString().IndexOf(frameTail, 0) >= 0;
else
atEnd = Buffer.Length > 0;
if (atEnd)
break;
timeOffset = (int)(DateTime.Now - starttime).TotalMilliseconds;
if (timeOffset >= timeout)
break;
//AppTimeLoop.ProcessEvents(waittimes);
}
}
catch (Exception) { }
return Buffer.ToString();
}
public static byte[] ReadBinary(SerialPort Manager, string frameTail = null, int timeout = 1000)
{
List<byte> Buffer = new List<byte>();
try
{
if (Manager == null)
return Buffer.ToArray();
//int waittimes = 10 ;
DateTime starttime = DateTime.Now ;
int readLength = 0 ;
int timeOffset ;
bool IsTail = (frameTail != null) && (frameTail.Length > 0);
byte[] tailByteArray = IsTail ? Encoding.UTF8.GetBytes(frameTail) : null;
bool atEnd = false;
long index = -1;
while (true)
{
readLength = Manager.BytesToRead;
if (readLength > 0)
{
byte[] readbuffer = new byte[readLength];
int received_length = Manager.Read(readbuffer, 0, readbuffer.Length);
if(received_length > 0)
Buffer.AddRange(readbuffer);
}
if (IsTail)
{
try
{
index = IndexesOf(Buffer.ToArray(), 0, tailByteArray).First<long>();
}
catch (Exception) {
index = -1;
}
atEnd = index >= 0;
}
else
atEnd = Buffer.ToArray().Length > 0;
if (atEnd)
break;
timeOffset = (int)(DateTime.Now - starttime).TotalMilliseconds;
if (timeOffset >= timeout)
break;
//AppTimeLoop.ProcessEvents(waittimes);
}
}
catch (Exception) { }
return Buffer.ToArray();
}
public static IEnumerable<long> IndexesOf(byte[] source, int start, byte[] pattern)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}
long valueLength = source.LongLength;
long patternLength = pattern.LongLength;
if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength))
{
yield break;
}
var badCharacters = new long[256];
for (var i = 0; i < 256; i++)
{
badCharacters[i] = patternLength;
}
var lastPatternByte = patternLength - 1;
for (long i = 0; i < lastPatternByte; i++)
{
badCharacters[pattern[i]] = lastPatternByte - i;
}
long index = start;
while (index <= valueLength - patternLength)
{
for (var i = lastPatternByte; source[index + i] == pattern[i]; i--)
{
if (i == 0)
{
yield return index;
break;
}
}
index += badCharacters[source[index + lastPatternByte]];
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YQLUtils
{
public class Settings
{
public static int RECODE_SUCCEED = 1 ;
public static int RECODE_FAILURE = 0 ;
public static int RECODE_WARNING = 2 ;
public static int CODE_SUCCESS = 200 ;
public static int CODE_FAILURE = 500 ;
public static int CODE_WARNING = 201 ;
public static int AXIS_STATUS_REGISTER_0 = 0 ;
public static int AXIS_STATUS_REGISTER_1 = 1 ;
public static int AXIS_STATUS_REGISTER_2 = 2 ;
public static int AXIS_STATUS_REGISTER_3 = 3 ;
public static int AXIS_STATUS_REGISTER_4 = 4 ;
public static int READONLY_AXIS_STATUS_REGISTER_0 = 0 ;
public static int READONLY_AXIS_STATUS_REGISTER_1 = 1 ;
public static int READONLY_AXIS_STATUS_REGISTER_2 = 2 ;
public static int READONLY_AXIS_STATUS_REGISTER_3 = 3 ;
public static int READONLY_AXIS_STATUS_REGISTER_4 = 4 ;
public static int READONLY_AXIS_STATUS_REGISTER_5 = 5 ;
public static byte[] START_DECODE = { 0x16, 0x54, 0x0D };
public static byte[] STOP_DECODE = { 0x16, 0x55, 0x0D };
public static byte[] CONSOLE_MODE = { 0x16, 0x4D, 0x0D, 0x30, 0x34, 0x30, 0x31, 0x44, 0x30, 0x35, 0x2E };
public static byte[] GET_VERSION = { 0x16, 0x4D, 0x0D, 0x25, 0x25, 0x25, 0x56, 0x45, 0x52, 0x2E };
public static byte[] DECODE_TAIL = { 0x0D, 0x0A};
public static byte[] DECODE_FRAME_TAIL = { 0x06 };
public const string DEFAULT_STR = default(string );
public const bool DEFAULT_BOOL = default(bool );
public const short DEFAULT_SHORT = default(short );
public const ushort DEFAULT_USHORT = default(ushort );
public const int DEFAULT_INT = default(int );
public const uint DEFAULT_UINT = default(uint );
public const long DEFAULT_LONG = default(long );
public const ulong DEFAULT_ULONG = default(ulong );
public const float DEFAULT_FLOAT = default(float );
public const bool [] DEFAULT_BOOL_ARRAY = default(bool [] );
public const short [] DEFAULT_SHORT_ARRAY = default(short [] );
public const ushort [] DEFAULT_USHORT_ARRAY = default(ushort[] );
public const int [] DEFAULT_INT_ARRAY = default(int [] );
public const uint [] DEFAULT_UINT_ARRAY = default(uint [] );
public const long [] DEFAULT_LONG_ARRAY = default(long [] );
public const ulong [] DEFAULT_ULONG_ARRAY = default(ulong [] );
public const float [] DEFAULT_FLOAT_ARRAY = default(float [] );
public const byte [] DEFAULT_BIT = default(byte [] );
public const byte [] DEFAULT_BYTE_ARRAY = default(byte [] );
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace YQLUtils
{
public class YQLUtil
{
public static string get_Exception(Exception E)
{
StringBuilder buffer = new StringBuilder();
if (E != null)
{
var stackTrace = new StackTrace(E, true);
foreach (var frame in stackTrace.GetFrames())
buffer.Append($"FileName: {frame.GetFileName()} MethodName: {frame.GetMethod().Name} Line: {frame.GetFileLineNumber()}\n\r");
}
if (buffer.Length <= 0)
buffer.Append("\n\r");
return buffer.ToString();
}
public static void Print_Exception(Exception E)
{
Console.Write($"{YQLUtil.get_Exception(E)}");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment