using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommonLib { public class MTF6K { public const string connect = "002000010060 "; public const string keepAlive = "002099990010 "; public const string switchPSET1 = "002300180010 001 "; public const string switchPSET2 = "002300180010 002 "; public const string switchPSET3 = "002300180010 003 "; public const string switchBatch1 = "002400380020 0001 "; public const string switchBatch2 = "002400380020 0002 "; public const string switchBatch3 = "002400380020 0003 "; public const string subscribeTightingResult = "006000080010 1201001310000000000000000000000000000001 "; public const string subscribeLastTightingCurve = "006700080010 0900001380 02001002 "; public const string unsubscribeTightingResult = "002900090010 120100100 "; public const string unsubscribeLastTightingCurve = "003700090010 09000010802001002 "; public const string starttighting = "002302240010 001 "; public const string startloosening = "002302240010 003 "; public const string stoptighting = "002302240010 005 "; public const string disconnect = "002000030010 "; public static string MID; public static int Length = 0; public static string otherData; public static bool isDataRecvFinish = false; public static class TightingResult { public static string PearkTorque;//峰值扭矩 public static string TotalAngle;//总角度 public static string TotalDuration;//总持续时间 public static string ResultCode;//结果代码 public static string PearkTorque1;//峰值扭矩 public static string TotalAngle1;//总角度 public static void Convert(out int _ResultCode, out double _pearkTorque, out double _totalAngle, out double _totalDuration) { try { _pearkTorque = double.Parse(PearkTorque); _totalAngle = double.Parse(TotalAngle); _totalDuration = double.Parse(TotalDuration); _ResultCode = int.Parse(ResultCode); } catch (Exception e) { _pearkTorque = 0.0; _totalAngle = 0.0; _totalDuration = 0.0; //NOK _ResultCode = 3; } } } public static class CurveResult { public static string torCoef;//PID系数C-扭矩 // public static string torB;//值-扭矩 public static byte[] torBByte;//值-扭矩 public static string angCoef;//PID系数C-角度 // public static string angB;//值-角度 public static byte[] angBByte;//值-角度 public static string TraceType; public static int SampleCount;//点个数 public static bool GetResult = false; public static void Convert(out double[] _torBInt, out double _torCoef, out double[] _angBInt, out double _angCoef) { double[] res1 = new double[SampleCount]; double[] res2 = new double[SampleCount]; try { byte[] tmp = new byte[SampleCount * 2]; //tmp = Encoding.Default.GetBytes(angB); tmp = angBByte; int j = 0; for (int i = 0; i < SampleCount; i++) { //res1[i] = tmp[i + 1] * 16 + tmp[i]; res1[i] = HexStringToDec(tmp[j + 1].ToString("x2") + tmp[j].ToString("x2")) * 1.0; j += 2; } _angBInt = res1; _angCoef = double.Parse(angCoef); } catch (Exception e) { _angBInt = res1; _angCoef = 0.0; } try { byte[] tmp = new byte[SampleCount * 2];//torB.Length //tmp = Encoding.Default.GetBytes(torB); tmp = torBByte; int j = 0; for (int i = 0; i < SampleCount; i++)//torB.Length { //res2[i] = tmp[i + 1] * 16 + tmp[i]; res2[i] = HexStringToDec(tmp[1 + j].ToString("x2") + tmp[j].ToString("x2")) * 1.0; j += 2; } _torBInt = res2; _torCoef = double.Parse(torCoef); } catch (Exception e) { _torBInt = res2; _torCoef = 0.0; } finally { GetResult = true; } } } /// /// 十六进制转为负数 /// /// /// private static int HexStringToDec(string strNumber) { int iNegate = 0; int iNumber = Convert.ToInt32(strNumber, 16); if (iNumber > 32767) { int iComplement = iNumber - 1; string strNegate = string.Empty; char[] binChar = Convert.ToString(iComplement, 2).PadLeft(16, '0').ToArray(); foreach (char ch in binChar) { //反转 if (Convert.ToInt32(ch) == 48) { strNegate += "1"; } else { strNegate += "0"; } } iNegate = -Convert.ToInt32(strNegate, 2); } else { iNegate = iNumber; } return iNegate; } /// /// 对接收到的数据进行解析 /// /// /// public static void ParseKerwords(byte[] bytes, bool isConnMTF6K) { //var dl = DebugLabel.解析MID_PID; string words = Encoding.ASCII.GetString(bytes); if (!isConnMTF6K) { try { MID = words.Substring(4, 4); Length = int.Parse(words.Substring(0, 4)); if (MID == "1202") { TightingResult.ResultCode = words.Substring(106, 1); TightingResult.PearkTorque = words.Substring(709, 100); TightingResult.TotalAngle = words.Substring(755, 12); TightingResult.TotalDuration = words.Substring(814, 12); //bianliang.NL_MEAN = Convert.ToDouble(TightingResult.PearkTorque); //bianliang.JD_MEAN = Convert.ToDouble(TightingResult.TotalAngle); //bianliang.Sls_time = Convert.ToDouble(TightingResult.TotalDuration); //TightingResult.ResultCode = words.Substring(106, 1); //TightingResult.PearkTorque = words.Substring(709, 12); //TightingResult.TotalAngle = words.Substring(738, 12); //TightingResult.TotalDuration = words.Substring(767, 12); string[] Timestr = words.Substring(words.IndexOf("30232") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries); int timemulti = Convert.ToInt32(Timestr[1]); double time = Convert.ToDouble(Timestr[0]); //bianliang.Sls_time = time * Math.Pow(10, timemulti); //bianliang.Sls_time = Convert.ToDouble(words.Substring(words.IndexOf("30232") + 15, 10)); string[] JDstr = words.Substring(words.IndexOf("30231") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries); int jdmulti = Convert.ToInt32(JDstr[1]); double jd = Convert.ToDouble(JDstr[0]); //AtlasScrew.JD_MEAN = jd * Math.Pow(10, jdmulti); //AppLog.Record("我的角度" + jd * Math.Pow(10, jdmulti)); string[] NLstr = words.Substring(words.IndexOf("30237") + 15, 14).Split(new string[] { "e" }, StringSplitOptions.RemoveEmptyEntries); int nlmulti = Convert.ToInt32(NLstr[1]); double nl = Convert.ToDouble(NLstr[0]); //AtlasScrew.NL_MEAN = nl * Math.Pow(10, nlmulti); //AppLog.Record("我的扭力" + nl * Math.Pow(10, nlmulti)); //bianliang.PLCWriteData(66, "Real", bianliang.NL_MEAN.ToString("f3")); //bianliang.PLCWriteData(70, "Real", bianliang.JD_MEAN.ToString("f3")); //bianliang.PLCWriteData(74, "Real", bianliang.Sls_time.ToString("f3")); TightingResult.PearkTorque1 = Math.Round(nl * Math.Pow(10, nlmulti),2).ToString(); TightingResult.TotalAngle1 = Math.Round(jd * Math.Pow(10, jdmulti), 0).ToString(); } else if (MID == "0900") { CurveResult.TraceType = words.Substring(81, 2); if (CurveResult.TraceType == "01") { CurveResult.SampleCount = Convert.ToInt16(words.Substring(124, 5)); CurveResult.angCoef = words.Substring(69, 12); //CurveResult.angB = words.Substring(130);//数据; CurveResult.angBByte = new byte[CurveResult.SampleCount * 2]; Buffer.BlockCopy(bytes, 130, CurveResult.angBByte, 0, CurveResult.SampleCount * 2); } if (CurveResult.TraceType == "02") { CurveResult.SampleCount = Convert.ToInt16(words.Substring(124, 5)); CurveResult.torCoef = words.Substring(69, 12); // CurveResult.torB = words.Substring(130);//数据; CurveResult.torBByte = new byte[CurveResult.SampleCount * 2]; Buffer.BlockCopy(bytes, 130, CurveResult.torBByte, 0, CurveResult.SampleCount * 2); isDataRecvFinish = true; } } } catch (Exception e) { } } else { if (words.Length == 24) { try { MID = words.Substring(4, 4); //keywords.LengthStr = words.Substring(0, 4); //keywords.MID = int.Parse(words.Substring(4, 4)); //keywords.Version = int.Parse(words.Substring(8, 3)); otherData = words.Substring(20, 4); } catch { MID = "Error"; } } else { try { MID = words.Substring(4, 4); } catch { MID = "Error"; } } } } /// /// 对接收到的数据进行解析 /// /// /// public static void ParseKerwords2(string words) { if (words.Length>8) { try { MID = words.Substring(4, 4); Length = int.Parse(words.Substring(0, 4)); } catch (Exception e) { } } else { try { MID = "Error"; } catch { MID = "Error"; } } } } }