CraneScaleControl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Common;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MeterPlugInLibrary
  11. {
  12. public delegate void CraneScaleEventDataCollection(object o, CraneScaleCollectModel e);
  13. public class CraneScaleControl
  14. {
  15. public event CraneScaleEventDataCollection EventDataCollectionArgs;//定义事件
  16. private Thread CollectionThread;//采集线程
  17. private string strPntNo = "";//计量点编号CAR11 CAR12
  18. /// <summary>
  19. /// 开启数据采集线程
  20. /// </summary>
  21. public void Start(string strPntID)
  22. {
  23. this.strPntNo = strPntID;
  24. CraneScaleCache.collect = new CraneScaleCollectModel();
  25. CollectionThread = new Thread(new ThreadStart(WgtThread));
  26. CollectionThread.Start();
  27. }
  28. public void Stop()
  29. {
  30. if (CollectionThread != null)
  31. {
  32. CollectionThread.Abort();
  33. CollectionThread = null;
  34. }
  35. }
  36. /// <summary>
  37. /// 重量采集线程
  38. /// </summary>
  39. private void WgtThread()
  40. {
  41. int icount = 0;
  42. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);//内存表
  43. while (true)
  44. {
  45. try
  46. {
  47. icount++;
  48. Thread.Sleep(500);
  49. CraneScaleCollectModel arg = new CraneScaleCollectModel();
  50. JArray jArray = MemoClass.TrackTable(strPntNo);
  51. if (jArray != null) //正常采集,若为null则重量采集线程中断了
  52. {
  53. arg.mainWgt = Convert.ToInt32(jArray[3].ToString());
  54. arg.weightStatus = Convert.ToInt32(jArray[4].ToString());
  55. arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
  56. //2021年7月16日 杨秀东对丢失精度进行处理
  57. if (arg.mainWgt.ToString().EndsWith("1"))
  58. arg.mainWgt = arg.mainWgt - 1;
  59. if (arg.mainWgt.ToString().EndsWith("9"))
  60. arg.mainWgt = arg.mainWgt + 1;
  61. }
  62. //每隔0.5秒调用一次写入到界面数据
  63. EventDataCollectionArgs(this, arg);
  64. }
  65. catch (Exception exp)
  66. {
  67. WriteLog("数据采集线程异常!" + exp.Message);
  68. }
  69. }
  70. }
  71. public void WriteLog(string str)
  72. {
  73. try
  74. {
  75. string m_szRunPath;
  76. m_szRunPath = System.Environment.CurrentDirectory;
  77. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  78. {
  79. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  80. }
  81. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  82. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  83. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  84. {
  85. Directory.CreateDirectory(strPathFile);
  86. }
  87. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\吊钩秤数据采集_" + strDate + ".log", true);
  88. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  89. tw.WriteLine(str);
  90. tw.WriteLine("\r\n");
  91. tw.Close();
  92. }
  93. catch (Exception exp)
  94. {
  95. }
  96. }
  97. }
  98. }