Converter.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. namespace Aitex.Core.Util
  7. {
  8. public static class Converter
  9. {
  10. /// <summary>
  11. /// Converts chamber Ids into Chinese string.
  12. /// </summary>
  13. /// <param name="value"></param>
  14. /// <returns></returns>
  15. //public static string ToCstring(ChamberSet value)
  16. //{
  17. // switch (value)
  18. // {
  19. // case ChamberSet.ReactorA:
  20. // return "A腔";
  21. // case ChamberSet.ReactorB:
  22. // return "B腔";
  23. // case ChamberSet.ReactorC:
  24. // return "C腔";
  25. // case ChamberSet.ReactorD:
  26. // return "D腔";
  27. // case ChamberSet.Buffer1:
  28. // return "缓冲腔1";
  29. // case ChamberSet.Cooldown:
  30. // return "冷却腔";
  31. // case ChamberSet.Loadlock:
  32. // return "装卸腔";
  33. // case ChamberSet.Loadlock:
  34. // return "传送腔";
  35. // case ChamberSet.System:
  36. // return "系统";
  37. // default:
  38. // return "未知腔";
  39. // }
  40. //}
  41. ///// <summary>
  42. ///// Convert susceptor status
  43. ///// </summary>
  44. ///// <param name="status"></param>
  45. ///// <returns></returns>
  46. //public static string SusceptorStatus(SusceptorStatus status)
  47. //{
  48. // switch (status)
  49. // {
  50. // case Platform.SusceptorStatus.Empty: return "空盘";
  51. // case Platform.SusceptorStatus.Failed: return "运行出错";
  52. // case Platform.SusceptorStatus.InProcessing: return "正在运行";
  53. // case Platform.SusceptorStatus.Preprocessing: return "运行准备";
  54. // case Platform.SusceptorStatus.Processed: return "运行完毕";
  55. // case Platform.SusceptorStatus.Troubled: return "运行出错";
  56. // case Platform.SusceptorStatus.Unprocessed: return "未处理";
  57. // default:
  58. // return "未知";
  59. // }
  60. //}
  61. //public static string ToRoutineString(ChamberStateEnum routine)
  62. //{
  63. // switch (routine)
  64. // {
  65. // case ChamberStateEnum.Pump:
  66. // return "抽真空";
  67. // case ChamberStateEnum.Vent:
  68. // return "充气";
  69. // case ChamberStateEnum.CyclePurge:
  70. // return "循环吹扫";
  71. // case ChamberStateEnum.LeakCheck:
  72. // return "泄漏检查";
  73. // case ChamberStateEnum.Idle:
  74. // return "空闲";
  75. // case ChamberStateEnum.Error:
  76. // return "出错";
  77. // case ChamberStateEnum.Aborted:
  78. // return "空闲";
  79. // case ChamberStateEnum.Servoing:
  80. // return "伺服压力";
  81. // case ChamberStateEnum.Transferring:
  82. // return "传盘中";
  83. // }
  84. // return "未知";
  85. //}
  86. ///// <summary>
  87. ///// Unit converter from physical to logical in linear formula
  88. ///// </summary>
  89. ///// <returns></returns>
  90. public static double Phy2Logic(double physicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
  91. {
  92. var ret = (logicalMax - logicalMin) * (physicalValue - physicalMin) / (physicalMax - physicalMin) + logicalMin;
  93. //if (ret < logicalMin) ret = logicalMin;
  94. //if (ret > logicalMax) ret = logicalMax;
  95. if (double.IsNaN(ret)) throw new DivideByZeroException("Phy2Logic除0操作");
  96. return ret;
  97. }
  98. ///// <summary>
  99. ///// Unit converter from logical to physical in linear formula
  100. ///// </summary>
  101. ///// <returns></returns>
  102. public static double Logic2Phy(double logicalValue, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
  103. {
  104. var ret = (physicalMax - physicalMin) * (logicalValue - logicalMin) / (logicalMax - logicalMin) + physicalMin;
  105. if (ret < physicalMin) ret = physicalMin;
  106. if (ret > physicalMax) ret = physicalMax;
  107. if (double.IsNaN(ret))
  108. throw new DivideByZeroException("Logic2Phy除0操作");
  109. return ret;
  110. }
  111. ///// <summary>
  112. ///// Convert a structure to byte array
  113. ///// </summary>
  114. ///// <typeparam name="T">structure type</typeparam>
  115. ///// <param name="object1">structure object</param>
  116. ///// <returns></returns>
  117. //public static byte[] Structure2Bytes<T>(T object1)
  118. //{
  119. // int size = Marshal.SizeOf(typeof(T));
  120. // byte[] byteArr = new byte[size];
  121. // IntPtr ptr = Marshal.AllocHGlobal(size);
  122. // Marshal.StructureToPtr(object1, ptr, true);
  123. // Marshal.Copy(ptr, byteArr, 0, size);
  124. // Marshal.FreeHGlobal(ptr);
  125. // return byteArr;
  126. //}
  127. ///// <summary>
  128. ///// Convert a byte array to a structure
  129. ///// </summary>
  130. ///// <typeparam name="T">Structure type</typeparam>
  131. ///// <param name="byteArr">byte array</param>
  132. ///// <param name="offset">byte array start index</param>
  133. ///// <param name="length">effective byte length</param>
  134. ///// <returns></returns>
  135. //public static T Bytes2Structure<T>(byte[] byteArr, int offset, int length)
  136. //{
  137. // T object1;
  138. // IntPtr ptr = Marshal.AllocHGlobal(length);
  139. // Marshal.Copy(byteArr, offset, ptr, length);
  140. // object1 = (T)Marshal.PtrToStructure(ptr, typeof(T));
  141. // Marshal.FreeHGlobal(ptr);
  142. // return object1;
  143. //}
  144. ///// <summary>
  145. ///// 转换不成功返回-1
  146. ///// </summary>
  147. ///// <param name="indata"></param>
  148. ///// <returns></returns>
  149. //public static double DoubleConverter(string indata)
  150. //{
  151. // double ret = -1;
  152. // double.TryParse(indata, out ret);
  153. // return ret;
  154. //}
  155. /// <summary>
  156. /// Convert DeviceNet mac number to status string
  157. /// 0 = No error
  158. /// 1 = Station deactivated
  159. /// 2 = Station not exists
  160. /// 18 = Station ready
  161. /// 31 = only for EtherCAT gateways: WC-State of cyclic EtherCAT frame is 1
  162. /// 40 = Heartbeat Message not received
  163. /// 41 = Shutdown Message received
  164. /// 42 = Electronic Key Fault: Vendor Id
  165. /// 43 = Electronic Key Fault: Device Type
  166. /// 44 = Electronic Key Fault: Product Code
  167. /// 45 = Electronic Key Fault: Revision
  168. /// 46 = Fault while writing Start-Up Attributs
  169. /// 47 = wrong Produced IO-Data Size
  170. /// 48 = wrong Consumed IO-Data Size
  171. /// 49 = Idle Mode (for Slave Devices): no valid IO-Data is exchanged via DeviceNet
  172. /// </summary>
  173. /// <param name="macStatusInt"></param>
  174. /// <returns></returns>
  175. public static string MacStatus(int macStatusInt)
  176. {
  177. string ret = "未知错误代码" + macStatusInt.ToString();
  178. switch (macStatusInt)
  179. {
  180. case 0: ret = "No error"; break;
  181. case 1: ret = "Station deactivated"; break;
  182. case 2: ret = "Station not exists"; break;
  183. case 18: ret = "Station ready"; break;
  184. case 31: ret = "only for EtherCAT gateways: WC-State of cyclic EtherCAT frame is 1"; break;
  185. case 40: ret = "Heartbeat Message not received"; break;
  186. case 41: ret = "Shutdown Message received"; break;
  187. case 42: ret = "Electronic Key Fault: Vendor Id"; break;
  188. case 43: ret = "Electronic Key Fault: Device Type"; break;
  189. case 44: ret = "Electronic Key Fault: Product Code"; break;
  190. case 45: ret = "Electronic Key Fault: Revision"; break;
  191. case 46: ret = "Fault while writing Start-Up Attributs"; break;
  192. case 47: ret = "wrong Produced IO-Data Size"; break;
  193. case 48: ret = "wrong Consumed IO-Data Size"; break;
  194. case 49: ret = "Idle Mode (for Slave Devices): no valid IO-Data is exchanged via DeviceNet"; break;
  195. }
  196. return ret;
  197. }
  198. //public static string GetShowCommandString(string command)
  199. //{
  200. // switch ((command+"").ToLower())
  201. // {
  202. // case "tmllgatevalve": return "装卸腔闸板阀开关";
  203. // case "tmreactoragate": return "反应腔A闸板阀开关";
  204. // case "tmreactorbgate": return "反应腔B闸板阀开关";
  205. // case "tmreactorcgate": return "反应腔C闸板阀开关";
  206. // case "tmreactordgate": return "反应腔D闸板阀开关";
  207. // }
  208. // return "";
  209. //}
  210. public static string MFCDisplay(double value)
  211. {
  212. if (value >= 10000)
  213. return value.ToString("F0");
  214. else if (value >= 10)
  215. return value.ToString("0.0");
  216. else
  217. return value.ToString("0.00");
  218. }
  219. }
  220. }