Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading.Tasks;
  10. using System.Net.Sockets;
  11. using System.Net;
  12. using System.Threading;
  13. using System.IO;
  14.  
  15. namespace AnalizaSieci
  16. {
  17. public partial class Form1 : Form
  18. {
  19. private const string endPointIpv4Address = "192.168.100.100";
  20. private const int endPointTCPPort = 502;
  21. private TcpClient tcpClient;
  22. private NetworkStream ns;
  23.  
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. tcpClient = new TcpClient();
  28. try
  29. {
  30. tcpClient.Connect(IPAddress.Parse(endPointIpv4Address), endPointTCPPort);
  31. ns = tcpClient.GetStream();
  32. }
  33. catch (Exception e)
  34. {
  35. Console.WriteLine(e.Message);
  36. }
  37.  
  38. Task loopTask = new Task(() =>
  39. {
  40. while (true)
  41. {
  42. Task voltageL1LNRefreshTask = new Task(() =>
  43. {
  44. this.VoltageL1TextBox.Invoke(new Action(() =>
  45. {
  46. this.VoltageL1TextBox.Text = getVoltageL1LN();
  47. }));
  48. });
  49. voltageL1LNRefreshTask.Start();
  50.  
  51. Task currentL1RefreshTask = new Task(() =>
  52. {
  53. this.currentL1TextBox.Invoke(new Action(() =>
  54. {
  55. this.currentL1TextBox.Text = getCurrentL1();
  56. }));
  57.  
  58. });
  59. currentL1RefreshTask.Start();
  60.  
  61. Task cosPhiL1RefreshTask = new Task(() =>
  62. {
  63. this.cosPhiL1TextBox.Invoke(new Action(() =>
  64. {
  65. this.cosPhiL1TextBox.Text = getCosPhi();
  66. }));
  67.  
  68. });
  69. cosPhiL1RefreshTask.Start();
  70.  
  71. Task activePowerL1RefreshTask = new Task(() =>
  72. {
  73. this.activePowerL1TexBox.Invoke(new Action(() =>
  74. {
  75. this.activePowerL1TexBox.Text = getActivePowerL1();
  76. }));
  77.  
  78. });
  79. activePowerL1RefreshTask.Start();
  80.  
  81. this.calculatedActivePowerL1TextBox.Invoke(new Action(() =>
  82. {
  83. this.calculatedActivePowerL1TextBox.Text = getCalculatedActivePowerL1();
  84. }));
  85.  
  86. }
  87.  
  88. });
  89. loopTask.Start();
  90.  
  91. }
  92. string getVoltageL1LN()
  93. {
  94. byte[] request =
  95. {
  96. 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x02, 0x00, 0x02
  97. };
  98. ns.Write(request, 0, request.Length);
  99.  
  100. byte[] response = new byte[15];
  101. ns.Read(response, 0, response.Length);
  102. Console.WriteLine(BitConverter.ToString(response));
  103.  
  104. byte[] voltageL1LNHex = new byte[4];
  105. Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
  106. Array.Reverse(voltageL1LNHex);
  107. return (BitConverter.ToUInt32(voltageL1LNHex, 0) / 1000f).ToString();
  108. }
  109. string getCurrentL1()
  110. {
  111. byte[] request =
  112. {
  113. 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x10, 0x00, 0x02
  114. };
  115. ns.Write(request, 0, request.Length);
  116.  
  117. byte[] response = new byte[15];
  118. ns.Read(response, 0, response.Length);
  119. Console.WriteLine(BitConverter.ToString(response));
  120.  
  121. byte[] voltageL1LNHex = new byte[4];
  122. Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
  123. Array.Reverse(voltageL1LNHex);
  124. return (BitConverter.ToUInt32(voltageL1LNHex, 0) / 1000f).ToString();
  125. }
  126. string getCosPhi()
  127. {
  128. byte[] request =
  129. {
  130. 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x20, 0x00, 0x02
  131. };
  132. ns.Write(request, 0, request.Length);
  133.  
  134. byte[] response = new byte[15];
  135. ns.Read(response, 0, response.Length);
  136. Console.WriteLine(BitConverter.ToString(response));
  137.  
  138. byte[] voltageL1LNHex = new byte[4];
  139. Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
  140. Array.Reverse(voltageL1LNHex);
  141. return (BitConverter.ToInt32(voltageL1LNHex, 0) / 1000f).ToString();
  142. }
  143.  
  144. string getActivePowerL1()
  145. {
  146. byte[] request =
  147. {
  148. 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x10, 0x30, 0x00, 0x02
  149. };
  150. ns.Write(request, 0, request.Length);
  151.  
  152. byte[] response = new byte[15];
  153. ns.Read(response, 0, response.Length);
  154. Console.WriteLine(BitConverter.ToString(response));
  155.  
  156. byte[] voltageL1LNHex = new byte[4];
  157. Array.ConstrainedCopy(response, 9, voltageL1LNHex, 0, voltageL1LNHex.Length);
  158. Array.Reverse(voltageL1LNHex);
  159. return (BitConverter.ToInt32(voltageL1LNHex, 0)).ToString();
  160. }
  161.  
  162. string getCalculatedActivePowerL1()
  163. {
  164. double activePower = Convert.ToDouble(this.VoltageL1TextBox.Text) * Convert.ToDouble(this.currentL1TextBox.Text) * Convert.ToDouble(this.cosPhiL1TextBox.Text);
  165. return Math.Round(activePower, 2).ToString();
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement