MTF6K.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CommonLib
  7. {
  8. public class MTF6K
  9. {
  10. public const string connect = "002000010060 ";
  11. public const string keepAlive = "002099990010 ";
  12. public const string switchPSET1 = "002300180010 001 ";
  13. public const string switchPSET2 = "002300180010 002 ";
  14. public const string switchPSET3 = "002300180010 003 ";
  15. public const string switchBatch1 = "002400380020 0001 ";
  16. public const string switchBatch2 = "002400380020 0002 ";
  17. public const string switchBatch3 = "002400380020 0003 ";
  18. public const string subscribeTightingResult = "006000080010 1201001310000000000000000000000000000001 ";
  19. public const string subscribeLastTightingCurve = "006700080010 0900001380 02001002 ";
  20. public const string unsubscribeTightingResult = "002900090010 120100100 ";
  21. public const string unsubscribeLastTightingCurve = "003700090010 09000010802001002 ";
  22. public const string starttighting = "002302240010 001 ";
  23. public const string startloosening = "002302240010 003 ";
  24. public const string stoptighting = "002302240010 005 ";
  25. public const string disconnect = "002000030010 ";
  26. public static string MID;
  27. public static int Length = 0;
  28. public static string otherData;
  29. public static bool isDataRecvFinish = false;
  30. public static class TightingResult
  31. {
  32. public static string PearkTorque;//峰值扭矩
  33. public static string TotalAngle;//总角度
  34. public static string TotalDuration;//总持续时间
  35. public static string ResultCode;//结果代码
  36. public static string PearkTorque1;//峰值扭矩
  37. public static string TotalAngle1;//总角度
  38. public static void Convert(out int _ResultCode, out double _pearkTorque, out double _totalAngle, out double _totalDuration)
  39. {
  40. try
  41. {
  42. _pearkTorque = double.Parse(PearkTorque);
  43. _totalAngle = double.Parse(TotalAngle);
  44. _totalDuration = double.Parse(TotalDuration);
  45. _ResultCode = int.Parse(ResultCode);
  46. }
  47. catch (Exception e)
  48. {
  49. _pearkTorque = 0.0;
  50. _totalAngle = 0.0;
  51. _totalDuration = 0.0;
  52. //NOK
  53. _ResultCode = 3;
  54. }
  55. }
  56. }
  57. public static class CurveResult
  58. {
  59. public static string torCoef;//PID系数C-扭矩
  60. // public static string torB;//值-扭矩
  61. public static byte[] torBByte;//值-扭矩
  62. public static string angCoef;//PID系数C-角度
  63. // public static string angB;//值-角度
  64. public static byte[] angBByte;//值-角度
  65. public static string TraceType;
  66. public static int SampleCount;//点个数
  67. public static bool GetResult = false;
  68. public static void Convert(out double[] _torBInt, out double _torCoef, out double[] _angBInt, out double _angCoef)
  69. {
  70. double[] res1 = new double[SampleCount];
  71. double[] res2 = new double[SampleCount];
  72. try
  73. {
  74. byte[] tmp = new byte[SampleCount * 2];
  75. //tmp = Encoding.Default.GetBytes(angB);
  76. tmp = angBByte;
  77. int j = 0;
  78. for (int i = 0; i < SampleCount; i++)
  79. {
  80. //res1[i] = tmp[i + 1] * 16 + tmp[i];
  81. res1[i] = HexStringToDec(tmp[j + 1].ToString("x2") + tmp[j].ToString("x2")) * 1.0;
  82. j += 2;
  83. }
  84. _angBInt = res1;
  85. _angCoef = double.Parse(angCoef);
  86. }
  87. catch (Exception e)
  88. {
  89. _angBInt = res1;
  90. _angCoef = 0.0;
  91. }
  92. try
  93. {
  94. byte[] tmp = new byte[SampleCount * 2];//torB.Length
  95. //tmp = Encoding.Default.GetBytes(torB);
  96. tmp = torBByte;
  97. int j = 0;
  98. for (int i = 0; i < SampleCount; i++)//torB.Length
  99. {
  100. //res2[i] = tmp[i + 1] * 16 + tmp[i];
  101. res2[i] = HexStringToDec(tmp[1 + j].ToString("x2") + tmp[j].ToString("x2")) * 1.0;
  102. j += 2;
  103. }
  104. _torBInt = res2;
  105. _torCoef = double.Parse(torCoef);
  106. }
  107. catch (Exception e)
  108. {
  109. _torBInt = res2;
  110. _torCoef = 0.0;
  111. }
  112. finally
  113. {
  114. GetResult = true;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// 十六进制转为负数
  120. ///</summary>
  121. ///<param name="strNumber"></param>
  122. ///<returns></returns>
  123. private static int HexStringToDec(string strNumber)
  124. {
  125. int iNegate = 0;
  126. int iNumber = Convert.ToInt32(strNumber, 16);
  127. if (iNumber > 32767)
  128. {
  129. int iComplement = iNumber - 1;
  130. string strNegate = string.Empty;
  131. char[] binChar = Convert.ToString(iComplement, 2).PadLeft(16, '0').ToArray();
  132. foreach (char ch in binChar)
  133. {
  134. //反转
  135. if (Convert.ToInt32(ch) == 48)
  136. {
  137. strNegate += "1";
  138. }
  139. else
  140. {
  141. strNegate += "0";
  142. }
  143. }
  144. iNegate = -Convert.ToInt32(strNegate, 2);
  145. }
  146. else
  147. {
  148. iNegate = iNumber;
  149. }
  150. return iNegate;
  151. }
  152. /// <summary>
  153. /// 对接收到的数据进行解析
  154. /// </summary>
  155. /// <param name="keywords"></param>
  156. /// <returns></returns>
  157. public static void ParseKerwords(byte[] bytes, bool isConnMTF6K)
  158. {
  159. //var dl = DebugLabel.解析MID_PID;
  160. string words = Encoding.ASCII.GetString(bytes);
  161. if (!isConnMTF6K)
  162. {
  163. try
  164. {
  165. MID = words.Substring(4, 4);
  166. Length = int.Parse(words.Substring(0, 4));
  167. if (MID == "1202")
  168. {
  169. TightingResult.ResultCode = words.Substring(106, 1);
  170. TightingResult.PearkTorque = words.Substring(709, 100);
  171. TightingResult.TotalAngle = words.Substring(755, 12);
  172. TightingResult.TotalDuration = words.Substring(814, 12);
  173. //bianliang.NL_MEAN = Convert.ToDouble(TightingResult.PearkTorque);
  174. //bianliang.JD_MEAN = Convert.ToDouble(TightingResult.TotalAngle);
  175. //bianliang.Sls_time = Convert.ToDouble(TightingResult.TotalDuration);
  176. //TightingResult.ResultCode = words.Substring(106, 1);
  177. //TightingResult.PearkTorque = words.Substring(709, 12);
  178. //TightingResult.TotalAngle = words.Substring(738, 12);
  179. //TightingResult.TotalDuration = words.Substring(767, 12);
  180. string[] Timestr = words.Substring(words.IndexOf("30232") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries);
  181. int timemulti = Convert.ToInt32(Timestr[1]);
  182. double time = Convert.ToDouble(Timestr[0]);
  183. //bianliang.Sls_time = time * Math.Pow(10, timemulti);
  184. //bianliang.Sls_time = Convert.ToDouble(words.Substring(words.IndexOf("30232") + 15, 10));
  185. string[] JDstr = words.Substring(words.IndexOf("30231") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries);
  186. int jdmulti = Convert.ToInt32(JDstr[1]);
  187. double jd = Convert.ToDouble(JDstr[0]);
  188. //AtlasScrew.JD_MEAN = jd * Math.Pow(10, jdmulti);
  189. //AppLog.Record("我的角度" + jd * Math.Pow(10, jdmulti));
  190. string[] NLstr = words.Substring(words.IndexOf("30237") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries);
  191. int nlmulti = Convert.ToInt32(NLstr[1]);
  192. double nl = Convert.ToDouble(NLstr[0]);
  193. //AtlasScrew.NL_MEAN = nl * Math.Pow(10, nlmulti);
  194. //AppLog.Record("我的扭力" + nl * Math.Pow(10, nlmulti));
  195. //bianliang.PLCWriteData(66, "Real", bianliang.NL_MEAN.ToString("f3"));
  196. //bianliang.PLCWriteData(70, "Real", bianliang.JD_MEAN.ToString("f3"));
  197. //bianliang.PLCWriteData(74, "Real", bianliang.Sls_time.ToString("f3"));
  198. TightingResult.PearkTorque1 = Math.Round(nl * Math.Pow(10, nlmulti),2).ToString();
  199. TightingResult.TotalAngle1 = Math.Round(jd * Math.Pow(10, jdmulti), 0).ToString();
  200. }
  201. else if (MID == "0900")
  202. {
  203. CurveResult.TraceType = words.Substring(81, 2);
  204. if (CurveResult.TraceType == "01")
  205. {
  206. CurveResult.SampleCount = Convert.ToInt16(words.Substring(124, 5));
  207. CurveResult.angCoef = words.Substring(69, 12);
  208. //CurveResult.angB = words.Substring(130);//数据;
  209. CurveResult.angBByte = new byte[CurveResult.SampleCount * 2];
  210. Buffer.BlockCopy(bytes, 130, CurveResult.angBByte, 0, CurveResult.SampleCount * 2);
  211. }
  212. if (CurveResult.TraceType == "02")
  213. {
  214. CurveResult.SampleCount = Convert.ToInt16(words.Substring(124, 5));
  215. CurveResult.torCoef = words.Substring(69, 12);
  216. // CurveResult.torB = words.Substring(130);//数据;
  217. CurveResult.torBByte = new byte[CurveResult.SampleCount * 2];
  218. Buffer.BlockCopy(bytes, 130, CurveResult.torBByte, 0, CurveResult.SampleCount * 2);
  219. isDataRecvFinish = true;
  220. }
  221. }
  222. }
  223. catch (Exception e)
  224. {
  225. }
  226. }
  227. else
  228. {
  229. if (words.Length == 24)
  230. {
  231. try
  232. {
  233. MID = words.Substring(4, 4);
  234. //keywords.LengthStr = words.Substring(0, 4);
  235. //keywords.MID = int.Parse(words.Substring(4, 4));
  236. //keywords.Version = int.Parse(words.Substring(8, 3));
  237. otherData = words.Substring(20, 4);
  238. }
  239. catch
  240. {
  241. MID = "Error";
  242. }
  243. }
  244. else
  245. {
  246. try
  247. {
  248. MID = words.Substring(4, 4);
  249. }
  250. catch
  251. {
  252. MID = "Error";
  253. }
  254. }
  255. }
  256. }
  257. /// <summary>
  258. /// 对接收到的数据进行解析
  259. /// </summary>
  260. /// <param name="keywords"></param>
  261. /// <returns></returns>
  262. public static void ParseKerwords2(string words)
  263. {
  264. if (words.Length>8)
  265. {
  266. try
  267. {
  268. MID = words.Substring(4, 4);
  269. Length = int.Parse(words.Substring(0, 4));
  270. }
  271. catch (Exception e)
  272. {
  273. }
  274. }
  275. else
  276. {
  277. try
  278. {
  279. MID = "Error";
  280. }
  281. catch
  282. {
  283. MID = "Error";
  284. }
  285. }
  286. }
  287. }
  288. }