123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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;
- }
- }
- }
- /// <summary>
- /// 十六进制转为负数
- ///</summary>
- ///<param name="strNumber"></param>
- ///<returns></returns>
- 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;
- }
- /// <summary>
- /// 对接收到的数据进行解析
- /// </summary>
- /// <param name="keywords"></param>
- /// <returns></returns>
- 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";
- }
- }
- }
- }
- /// <summary>
- /// 对接收到的数据进行解析
- /// </summary>
- /// <param name="keywords"></param>
- /// <returns></returns>
- 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";
- }
-
- }
- }
- }
- }
|