Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12. using System.IO.Ports;
  13. using System.Threading;
  14. using System.Management;
  15.  
  16.  
  17. namespace ArdentMills_GUI
  18. {
  19. public partial class Form1 : Form
  20. {
  21. SerialPort port;
  22. SerialPort port2;
  23. int count = 0;
  24. double newTempValue = 1;
  25. double newDistValue = 0;
  26. Boolean rollStart;
  27. double weight1;
  28. double weight2;
  29. String weight;
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. try
  34. {
  35. if (port == null)
  36. {
  37. using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
  38. {
  39. string[] portnames = SerialPort.GetPortNames();
  40. var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
  41.  
  42. foreach(var p in ports)
  43. {
  44. // Uno
  45. if(p["Caption"].ToString().Contains("Uno"))
  46. {
  47. port = new SerialPort(p["DeviceID"].ToString(), 9600);//Set your board COM
  48. port.Open();
  49. port.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler);
  50. }
  51. // Mega
  52. if (p["Caption"].ToString().Contains("Mega"))
  53. {
  54. port2 = new SerialPort(p["DeviceID"].ToString(), 9600);//Set your board COM
  55. port2.Open();
  56. port2.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler2);
  57. }
  58. }
  59. }
  60. /*
  61. //Uno
  62. port = new SerialPort("COM5", 9600);//Set your board COM
  63. port.Open();
  64. port.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler);
  65.  
  66. //Mega
  67. port2 = new SerialPort("COM4", 9600);//Set your board COM
  68. port2.Open();
  69. port2.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler2);
  70. */
  71. }
  72. }
  73. catch(IOException ioe)
  74. {
  75.  
  76. }
  77.  
  78. uxChart.Series[0].ChartType = SeriesChartType.Line;
  79. uxChart.ChartAreas[0].BackColor = Color.Black;
  80. uxChart.ChartAreas[0].BorderWidth = 10;
  81. uxChart.ChartAreas[0].BorderColor = Color.Purple;
  82.  
  83. uxChart.Series[0].ChartType = SeriesChartType.Line;
  84. uxChart.Series[0].BorderWidth = 3;
  85. uxChart.Series[0].Color = Color.Green;
  86. uxChart.Series[0].XValueType = ChartValueType.Int32;
  87. uxChart.Series[0].YValueType = ChartValueType.Int32;
  88.  
  89. uxChart.Series[0].MarkerStyle = MarkerStyle.Square;
  90. uxChart.Series[0].MarkerSize = 0;
  91. uxChart.Series[0].MarkerStep = 5;
  92. uxChart.Series[0].MarkerColor = Color.White;
  93. uxChart.Series[0].ToolTip = @"Value";
  94.  
  95. uxChart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
  96. uxChart.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
  97. uxChart.ChartAreas[0].AxisX.MinorGrid.Interval = 15;
  98.  
  99. uxChart.ChartAreas[0].AxisY.Minimum = 60;
  100. uxChart.ChartAreas[0].AxisY.Maximum = 120;
  101.  
  102. for(int i = -150; i < 0; i++)
  103. {
  104. uxChart.Series[0].Points.AddXY(i, 60.0);
  105. }
  106.  
  107. uxChart.Legends.Clear();
  108. }
  109.  
  110. public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
  111. {
  112. SerialPort sp = (SerialPort)sender;
  113. string indata = sp.ReadExisting();
  114. }
  115.  
  116. private void SerialDataReceivedEventHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // Instrukcje jakie mają się wykonac w momencieodebrania danych z portu szeregowego.
  117. {
  118. SerialPort sp = (SerialPort)sender;
  119. string indata = sp.ReadLine();
  120. string[] tokens = indata.Split(' ');
  121. //System.Windows.Forms.MessageBox.Show(tokens[0]);
  122. double value;
  123.  
  124. if (tokens.Length >= 2)
  125. {
  126. bool success = Double.TryParse(tokens[0], out value);
  127. if (success)
  128. {
  129. newDistValue = value;
  130. try
  131. {
  132. this.Invoke(new EventHandler(upDist));
  133. }
  134. catch
  135. {
  136.  
  137. }
  138. }
  139. else
  140. {
  141. newTempValue = 0;
  142. }
  143. success = Double.TryParse(tokens[1], out value);
  144. if (success)
  145. {
  146. newTempValue = value;
  147. try
  148. {
  149. this.Invoke(new EventHandler(upGraph));
  150. }
  151. catch
  152. {
  153.  
  154. }
  155. }
  156. else
  157. {
  158. newTempValue = 0;
  159. }
  160. //newtempValue = Double.Parse(tokens[1]);
  161. //this.Invoke(new EventHandler(upGraph));
  162. }
  163. }
  164.  
  165. private void SerialDataReceivedEventHandler2(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // Instrukcje jakie mają się wykonac w momencieodebrania danych z portu szeregowego.
  166. {
  167. SerialPort sp = (SerialPort)sender;
  168. string indata = sp.ReadLine();
  169. string[] tokens = indata.Split(' ');
  170. double value;
  171. if (tokens.Length >= 2)
  172. {
  173. bool success = Double.TryParse(tokens[0], out value);
  174. if (success)
  175. {
  176. weight1 = value;
  177. /*
  178. try
  179. {
  180. this.Invoke(new EventHandler(upDist));
  181. }
  182. catch
  183. {
  184.  
  185. }*/
  186. }
  187. else
  188. {
  189. weight1 = 0;
  190. }
  191. success = Double.TryParse(tokens[1], out value);
  192. if (success)
  193. {
  194. weight2 = value;
  195. /*try
  196. {
  197. this.Invoke(new EventHandler(upGraph));
  198. }
  199. catch
  200. {
  201.  
  202. }*/
  203. }
  204. else
  205. {
  206. weight2 = 0;
  207. }
  208.  
  209. try
  210. {
  211. this.Invoke(new EventHandler(weightEvent));
  212. }
  213. catch
  214. {
  215.  
  216. }
  217. }
  218. }
  219.  
  220. bool hhh = true;
  221. private void weightEvent(object sender, EventArgs e)
  222. {
  223. weight_1_label.Text = weight1.ToString();
  224. weight_2_label.Text = weight2.ToString();
  225. }
  226.  
  227. private void upGraph(object sender, EventArgs e)
  228. {
  229. if(newTempValue < 80.0)
  230. {
  231. uxChart.Series[0].Color = Color.Green;
  232. }
  233. else if(newTempValue < 100.0)
  234. {
  235. uxChart.Series[0].Color = Color.Yellow;
  236. }
  237. else
  238. {
  239. uxChart.Series[0].Color = Color.Red;
  240. }
  241.  
  242.  
  243. if( uxChart.Series[0].Points.Count() >= 150)
  244. {
  245. uxChart.Series[0].Points.RemoveAt(0);
  246. }
  247.  
  248. uxChart.Series[0].Points.AddXY(count, newTempValue);
  249. uxChart.ChartAreas[0].RecalculateAxesScale();
  250. count++;
  251. }
  252.  
  253. private void upDist(object sender, EventArgs e)
  254. {
  255. currDist_label.Text = newDistValue.ToString();
  256. }
  257.  
  258. void Form1_FormClosed(object sender, FormClosedEventArgs e)
  259. {
  260. if (port != null && port.IsOpen)
  261. {
  262. port.Close();
  263. }
  264. if (port2 != null && port2.IsOpen)
  265. {
  266. port2.Close();
  267. }
  268. }
  269.  
  270. public void UxBut_Click(object sender, EventArgs e)
  271. {
  272. byte[] stringMsg = System.Text.Encoding.UTF8.GetBytes("test");
  273. port2.Write(stringMsg, 0, stringMsg.Length);
  274. }
  275.  
  276. private void UpdateDistBtn_Click(object sender, EventArgs e)
  277. {
  278. currDist_label.Text = DistTextBox.Text;
  279. byte[] stringMsg = System.Text.Encoding.UTF8.GetBytes(DistTextBox.Text);
  280. port.Write(stringMsg, 0, stringMsg.Length);
  281. DistTextBox.Clear();
  282. }
  283.  
  284. private void RollerBtn_Click(object sender, EventArgs e)
  285. {
  286. if (rollStart)
  287. {
  288. //port2.Write("stop/r");
  289. byte[] stringMsg = System.Text.Encoding.UTF8.GetBytes("stop");
  290. port2.Write(stringMsg, 0, stringMsg.Length);
  291. rollStart = false;
  292. }
  293. else
  294. {
  295. //port2.Write("stop/r");
  296. byte[] stringMsg = System.Text.Encoding.UTF8.GetBytes("start");
  297. port2.Write(stringMsg, 0, stringMsg.Length);
  298. rollStart = true;
  299. }
  300. }
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement