MainWindow.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows;
  4. using System.Windows.Documents;
  5. using ToolLibrary;
  6. using Yunda.ISAS.MongoDB.Entities.MeasuresTemperature;
  7. namespace Send104DataForm
  8. {
  9. /// <summary>
  10. /// Interaction logic for MainWindow.xaml
  11. /// </summary>
  12. public partial class MainWindow : Window
  13. {
  14. public MainWindow()
  15. {
  16. InitializeComponent();
  17. DataContext = this;
  18. }
  19. private bool isTesting = false;
  20. public int DevAddr { get; set; } = 142;
  21. public int CpuSector { get; set; } = 1;
  22. public int StartInfoAddr { get; set; } = 1783;
  23. public int InfoCount { get; set; } = 300;
  24. public int Interval { get; set; } = 1000;
  25. public string Url { get; set; } = "192.168.81.32";
  26. //int _devAddr = 142;
  27. //int cpuSector = 1;
  28. //int startInfoAddr = 1783;
  29. //int _infoCount = 1000;
  30. //string _requestSendDataUri = "http://192.168.81.32:32346/Iec104Data/SendData";
  31. double testValueMax = 40.0;
  32. double testValueMin = -40.0;
  33. //private void StartTest()
  34. //{
  35. // Random random = new Random();
  36. // // 生成-40到40之间的随机浮点数
  37. // for (int i = startInfoAddr; i < startInfoAddr+ _infoCount; i++)
  38. // {
  39. // IecServerData input = new IecServerData()
  40. // {
  41. // devCpu = (byte)cpuSector ,
  42. // dataType = 1,
  43. // dateTime = DateTime.Now,
  44. // devAddr = (byte)_devAddr,
  45. // inf = i,
  46. // ycValue = (float)( random.NextDouble() * (80) - 40)
  47. // };
  48. // HttpHelper.HttpPostRequest
  49. // (_requestSendDataUri, input);
  50. // Task.Delay(100).Wait();
  51. // }
  52. //}
  53. private async void StartTest_Click(object sender, RoutedEventArgs e)
  54. {
  55. if (!isTesting)
  56. {
  57. isTesting = true;
  58. Paragraph paragraph = new Paragraph();
  59. paragraph.Inlines.Add(new Run($"测试开始\n"));
  60. InfoTextBlock.Document.Blocks.Add(paragraph);
  61. await Task.Run(() => StartTest());
  62. paragraph.Inlines.Add(new Run($"测试结束\n"));
  63. InfoTextBlock.Document.Blocks.Add(paragraph);
  64. isTesting = false;
  65. }
  66. else
  67. {
  68. MessageBox.Show("测试正在进行中,请等待测试结束后再开始新的测试。");
  69. }
  70. }
  71. private void StopTest_Click(object sender, RoutedEventArgs e)
  72. {
  73. isTesting = false;
  74. Paragraph paragraph = new Paragraph();
  75. paragraph.Inlines.Add(new Run($"测试被停止\n"));
  76. InfoTextBlock.Document.Blocks.Add(paragraph);
  77. }
  78. private void StartTest()
  79. {
  80. Random random = new Random();
  81. while (true)
  82. {
  83. Dispatcher.Invoke(() => InfoTextBlock.Document.Blocks.Clear());
  84. for (int i = StartInfoAddr; i < StartInfoAddr + InfoCount && isTesting; i++)
  85. {
  86. IecServerData input = new IecServerData()
  87. {
  88. devName = "",
  89. devCpu = (byte)CpuSector,
  90. dataType = 1,
  91. dateTime = DateTime.Now,
  92. devAddr = (byte)DevAddr,
  93. inf = i,
  94. ycValue = (float)(random.NextDouble() * (80) - 40)
  95. };
  96. // 模拟HTTP请求
  97. HttpHelper.HttpPostRequest($"http://{Url}:32346/Iec104Data/SendData", input);
  98. // 打印信息
  99. Dispatcher.Invoke(() =>
  100. {
  101. Paragraph paragraph = new Paragraph();
  102. paragraph.Inlines.Add(new Run($"发送数据:地址:{i},值{input.ycValue}\n"));
  103. InfoTextBlock.Document.Blocks.Add(paragraph);
  104. InfoTextBlock.ScrollToEnd(); // 将滚动条滚动到最底部
  105. });
  106. }
  107. Task.Delay(Interval).Wait();
  108. }
  109. }
  110. }
  111. public struct IecServerData
  112. {
  113. /// <summary>
  114. /// 数据类型 1遥测 2遥信
  115. /// </summary>
  116. public int dataType { get; set; }
  117. /// <summary>
  118. /// 装置地址
  119. /// </summary>
  120. public byte devAddr { get; set; }
  121. /// <summary>
  122. /// cpu扇区号
  123. /// </summary>
  124. public byte devCpu { get; set; }
  125. /// <summary>
  126. /// 信息体地址
  127. /// </summary>
  128. public int inf { get; set; }
  129. /// <summary>
  130. /// 遥测值
  131. /// </summary>
  132. public float ycValue { get; set; }
  133. /// <summary>
  134. /// 遥信值
  135. /// </summary>
  136. public int yxValue { get; set; }
  137. /// <summary>
  138. /// 时间
  139. /// </summary>
  140. public DateTime dateTime { get; set; }
  141. /// <summary>
  142. /// 装置名称
  143. /// </summary>
  144. public string devName { get; set; }
  145. /// <summary>
  146. /// 测温结果,可不填
  147. /// </summary>
  148. public MeasureTemperatureResult measureTemperatureResult { get; set; }
  149. }
  150. }