using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Aitex.Core.Util
{
public static class Converter
{
///
/// Converts chamber Ids into Chinese string.
///
///
///
//public static string ToCstring(ChamberSet value)
//{
// switch (value)
// {
// case ChamberSet.ReactorA:
// return "A腔";
// case ChamberSet.ReactorB:
// return "B腔";
// case ChamberSet.ReactorC:
// return "C腔";
// case ChamberSet.ReactorD:
// return "D腔";
// case ChamberSet.Buffer1:
// return "缓冲腔1";
// case ChamberSet.Cooldown:
// return "冷却腔";
// case ChamberSet.Loadlock:
// return "装卸腔";
// case ChamberSet.Loadlock:
// return "传送腔";
// case ChamberSet.System:
// return "系统";
// default:
// return "未知腔";
// }
//}
/////
///// Convert susceptor status
/////
/////
/////
//public static string SusceptorStatus(SusceptorStatus status)
//{
// switch (status)
// {
// case Platform.SusceptorStatus.Empty: return "空盘";
// case Platform.SusceptorStatus.Failed: return "运行出错";
// case Platform.SusceptorStatus.InProcessing: return "正在运行";
// case Platform.SusceptorStatus.Preprocessing: return "运行准备";
// case Platform.SusceptorStatus.Processed: return "运行完毕";
// case Platform.SusceptorStatus.Troubled: return "运行出错";
// case Platform.SusceptorStatus.Unprocessed: return "未处理";
// default:
// return "未知";
// }
//}
//public static string ToRoutineString(ChamberStateEnum routine)
//{
// switch (routine)
// {
// case ChamberStateEnum.Pump:
// return "抽真空";
// case ChamberStateEnum.Vent:
// return "充气";
// case ChamberStateEnum.CyclePurge:
// return "循环吹扫";
// case ChamberStateEnum.LeakCheck:
// return "泄漏检查";
// case ChamberStateEnum.Idle:
// return "空闲";
// case ChamberStateEnum.Error:
// return "出错";
// case ChamberStateEnum.Aborted:
// return "空闲";
// case ChamberStateEnum.Servoing:
// return "伺服压力";
// case ChamberStateEnum.Transferring:
// return "传盘中";
// }
// return "未知";
//}
/////
///// Unit converter from physical to logical in linear formula
/////
/////
public static double Phy2Logic(double physicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
{
var ret = (logicalMax - logicalMin) * (physicalValue - physicalMin) / (physicalMax - physicalMin) + logicalMin;
//if (ret < logicalMin) ret = logicalMin;
//if (ret > logicalMax) ret = logicalMax;
if (double.IsNaN(ret)) throw new DivideByZeroException("Phy2Logic除0操作");
return ret;
}
/////
///// Unit converter from logical to physical in linear formula
/////
/////
public static double Logic2Phy(double logicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
{
var ret = (physicalMax - physicalMin) * (logicalValue - logicalMin) / (logicalMax - logicalMin) + physicalMin;
if (ret < physicalMin) ret = physicalMin;
if (ret > physicalMax) ret = physicalMax;
if (double.IsNaN(ret))
throw new DivideByZeroException("Logic2Phy除0操作");
return ret;
}
/////
///// Convert a structure to byte array
/////
///// structure type
///// structure object
/////
//public static byte[] Structure2Bytes(T object1)
//{
// int size = Marshal.SizeOf(typeof(T));
// byte[] byteArr = new byte[size];
// IntPtr ptr = Marshal.AllocHGlobal(size);
// Marshal.StructureToPtr(object1, ptr, true);
// Marshal.Copy(ptr, byteArr, 0, size);
// Marshal.FreeHGlobal(ptr);
// return byteArr;
//}
/////
///// Convert a byte array to a structure
/////
///// Structure type
///// byte array
///// byte array start index
///// effective byte length
/////
//public static T Bytes2Structure(byte[] byteArr, int offset, int length)
//{
// T object1;
// IntPtr ptr = Marshal.AllocHGlobal(length);
// Marshal.Copy(byteArr, offset, ptr, length);
// object1 = (T)Marshal.PtrToStructure(ptr, typeof(T));
// Marshal.FreeHGlobal(ptr);
// return object1;
//}
/////
///// 转换不成功返回-1
/////
/////
/////
//public static double DoubleConverter(string indata)
//{
// double ret = -1;
// double.TryParse(indata, out ret);
// return ret;
//}
///
/// Convert DeviceNet mac number to status string
/// 0 = No error
/// 1 = Station deactivated
/// 2 = Station not exists
/// 18 = Station ready
/// 31 = only for EtherCAT gateways: WC-State of cyclic EtherCAT frame is 1
/// 40 = Heartbeat Message not received
/// 41 = Shutdown Message received
/// 42 = Electronic Key Fault: Vendor Id
/// 43 = Electronic Key Fault: Device Type
/// 44 = Electronic Key Fault: Product Code
/// 45 = Electronic Key Fault: Revision
/// 46 = Fault while writing Start-Up Attributs
/// 47 = wrong Produced IO-Data Size
/// 48 = wrong Consumed IO-Data Size
/// 49 = Idle Mode (for Slave Devices): no valid IO-Data is exchanged via DeviceNet
///
///
///
public static string MacStatus(int macStatusInt)
{
string ret = "未知错误代码" + macStatusInt.ToString();
switch (macStatusInt)
{
case 0: ret = "No error"; break;
case 1: ret = "Station deactivated"; break;
case 2: ret = "Station not exists"; break;
case 18: ret = "Station ready"; break;
case 31: ret = "only for EtherCAT gateways: WC-State of cyclic EtherCAT frame is 1"; break;
case 40: ret = "Heartbeat Message not received"; break;
case 41: ret = "Shutdown Message received"; break;
case 42: ret = "Electronic Key Fault: Vendor Id"; break;
case 43: ret = "Electronic Key Fault: Device Type"; break;
case 44: ret = "Electronic Key Fault: Product Code"; break;
case 45: ret = "Electronic Key Fault: Revision"; break;
case 46: ret = "Fault while writing Start-Up Attributs"; break;
case 47: ret = "wrong Produced IO-Data Size"; break;
case 48: ret = "wrong Consumed IO-Data Size"; break;
case 49: ret = "Idle Mode (for Slave Devices): no valid IO-Data is exchanged via DeviceNet"; break;
}
return ret;
}
//public static string GetShowCommandString(string command)
//{
// switch ((command+"").ToLower())
// {
// case "tmllgatevalve": return "装卸腔闸板阀开关";
// case "tmreactoragate": return "反应腔A闸板阀开关";
// case "tmreactorbgate": return "反应腔B闸板阀开关";
// case "tmreactorcgate": return "反应腔C闸板阀开关";
// case "tmreactordgate": return "反应腔D闸板阀开关";
// }
// return "";
//}
public static string MFCDisplay(double value)
{
if (value >= 10000)
return value.ToString("F0");
else if (value >= 10)
return value.ToString("0.0");
else
return value.ToString("0.00");
}
}
}