using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Vu_metru_v00 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public System.Drawing.Graphics desen; public System.Drawing.Pen creion_blu; public System.Drawing.Pen creion_rosu; public System.Drawing.Pen creion_gri; public System.Drawing.SolidBrush pens_blu; public System.Drawing.SolidBrush pens_red; public System.Drawing.SolidBrush pens_back; public System.Drawing.SolidBrush radiera; public System.Drawing.Font font_nina; public binar binar1; public System.Drawing.Bitmap im; public voltm voltm1; public voltm voltm2; public termo termo1; public termo termo2; public termo termo3; public termo termo4; public termo termo5; int a0_pozx = 300, a0_pozy = 50; Int32 a0_val, k; int a1_pozx = 610, a1_pozy = 50; Int32 a2_val, a3_val, a4_val, a5_val; UInt64 digi; string txt, cda; int c0, c1, c2, c3, c4, c5, c6, c7, val_cmd, val_cmd_v; static int[] a0_valori = new int[0]; static int[] a1_valori = new int[0]; static String[] date_r = new String[11]; // Date receptionate static String[] date_c = new String[11]; // Date check - verificate daca sunt ok static String delimStr = " ,.:\t"; static Char[] delimiter = delimStr.ToCharArray(); static String[] Nume_porturi = new String[11]; // -------------- Clase pentru instrumente ------------------ // -------------- Afisare binara ------------------ public class binar { int x0; int y0; int w; int h; public void setval(int nrb, UInt64 n, System.Drawing.Graphics zona_des, System.Drawing.Pen creion, System.Drawing.SolidBrush pens_albastra, System.Drawing.SolidBrush radiera) { int wb = w / (3 * nrb); int hb = h / 3; int x = x0 + w - 3 * wb; int y = y0 + hb; int i; //zona_des.DrawRectangle(creion, x0, y0, w, h); for (i = nrb - 1; i >= 0; i--) { System.UInt64 bit = ((n >> (nrb - i - 1)) & 1); zona_des.DrawRectangle(creion, x - 1, y - 1, wb + 1, hb + 1); if (bit == 1) zona_des.FillRectangle(pens_albastra, x, y, wb, hb); else zona_des.FillRectangle(radiera, x, y, wb, hb); x -= 3 * wb; } } public void init_binar(int pozx, int pozy, int lat, int inalt) { x0 = pozx; y0 = pozy; w = lat; h = inalt; } } // -------------- Afisare voltmetru ------------------ public class voltm { int x0; int y0; int w; double vm; System.Drawing.Pen creion = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Font font_ni = new System.Drawing.Font("Nina", 8); System.Drawing.SolidBrush pens_blu = new System.Drawing.SolidBrush(System.Drawing.Color.Blue); public void desen_voltm(System.Drawing.Graphics zona_des) { int lt = 15; int lg = 22; int x1, x2, xt, y1, y2, yt; int xc = x0 + w / 2; int yc = y0 + w / 2; int raza = w / 2; int nrd; int val_a = 0; // alfa_gr unghiul in grade double alfa_gr = 140; nrd = 0; while (alfa_gr >= 40) { double alfa_r = 2 * System.Math.PI * (alfa_gr) / 360;// unghiul in radiani if (nrd % 5 == 0) { x1 = System.Convert.ToInt16(xc + (raza - lt) * System.Math.Cos(alfa_r)); y1 = System.Convert.ToInt16(yc - (raza - lt) * System.Math.Sin(alfa_r)); xt = System.Convert.ToInt16(xc - 5 + raza * System.Math.Cos(alfa_r)); yt = System.Convert.ToInt16(yc - raza * System.Math.Sin(alfa_r)); zona_des.DrawString(System.Convert.ToString(val_a), font_ni, pens_blu, xt, yt); val_a = val_a + System.Convert.ToInt16(vm / 10); } else { x1 = System.Convert.ToInt16(xc + (raza - lg) * System.Math.Cos(alfa_r)); y1 = System.Convert.ToInt16(yc - (raza - lg) * System.Math.Sin(alfa_r)); } x2 = System.Convert.ToInt16(xc + (raza - 2 * lt) * System.Math.Cos(alfa_r)); y2 = System.Convert.ToInt16(yc - (raza - 2 * lt) * System.Math.Sin(alfa_r)); zona_des.DrawLine(creion, x1, y1, x2, y2); alfa_gr -= 2; nrd++; } zona_des.DrawRectangle(creion, xc - raza, yc - raza - 2, 2 * raza, 5 * raza / 4); } public void setval(System.Drawing.Graphics zona_des, double val) { int alfa_gr = 140 - System.Convert.ToInt16(100 * val / vm); ;//unghiul in grade int lg = 17; int xc = x0 + w / 2; int yc = y0 + w / 2; int raza = w / 2; System.Drawing.SolidBrush radiera = new System.Drawing.SolidBrush(System.Drawing.Color.White); zona_des.FillPie(radiera, x0 + 2 * lg - 1, y0 + 2 * lg - 1, w - 4 * lg + 2, w - 4 * lg + 2, 10, -180); double alfa_r = 2 * System.Math.PI * (alfa_gr) / 360;// unghiul in radiani int x = System.Convert.ToInt16(xc + (raza - 2 * lg) * System.Math.Cos(alfa_r)); int y = System.Convert.ToInt16(yc - (raza - 2 * lg) * System.Math.Sin(alfa_r)); zona_des.DrawLine(creion, x, y, xc, yc); alfa_gr = 40; zona_des.DrawRectangle(creion, xc - raza, yc - raza - 2, 2 * raza, 5 * raza / 4); } public voltm(int pozx, int pozy, int lat, double val_max) { x0 = pozx; y0 = pozy; w = lat; vm = val_max; } } // -------------- Afisare termometru ------------------ public class termo { int x0; int y0; int w; int h; int val_max; public void desenez(System.Drawing.Graphics zona_des, System.Drawing.Pen creion_a, System.Drawing.Pen creion_gr, System.Drawing.SolidBrush pens_r, System.Drawing.Font font_ni) { zona_des.DrawRectangle(creion_a, x0, y0, w, h); for (int j = 0; j <= h; j += 5)// desenez gradatii { if (j % 25 == 0) { zona_des.DrawLine(creion_gr, x0 + w + 2, y0 + j, x0 + w + 12, y0 + j); zona_des.DrawString(System.Convert.ToString(val_max - j * val_max / h), font_ni, pens_r, x0 + w + 20, y0 + j - 7); } else { zona_des.DrawLine(creion_gr, x0 + w + 2, y0 + j, x0 + w + 7, y0 + j); } } } public void sterg(System.Drawing.Graphics zona_des, System.Drawing.SolidBrush rad) { zona_des.FillRectangle(rad, x0 + 1, y0 + 1, w - 1, h - 1); } public void setval(float val, System.Drawing.Graphics zona_des, System.Drawing.SolidBrush pens_r, System.Drawing.SolidBrush pens_b) { val = System.Convert.ToInt16(System.Convert.ToDouble(val) * (System.Convert.ToDouble(h) / System.Convert.ToDouble(val_max))); //scalare zona_des.FillRectangle(pens_b, x0 + 1, y0 + 1, w - 1, h - 1); zona_des.FillRectangle(pens_r, x0 + 1, y0 + h - val, w - 1, val); } public termo(int pozx, int pozy, int lat, int inalt, int vmax) { x0 = pozx; y0 = pozy; w = lat; h = inalt; val_max = vmax; } } private void Form1_Load(object sender, EventArgs e) { date_r = "0,0,0,0,0,0,0,0,0,0,0".Split(delimiter); date_c = "0,0,0,0,0,0,0,0,0,0,0".Split(delimiter); desen = this.CreateGraphics(); creion_blu = new System.Drawing.Pen(System.Drawing.Color.Blue); creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red); creion_gri = new System.Drawing.Pen(System.Drawing.Color.LightGray); pens_blu = new System.Drawing.SolidBrush(System.Drawing.Color.Blue); pens_red = new System.Drawing.SolidBrush(System.Drawing.Color.Red); pens_back = new System.Drawing.SolidBrush(this.BackColor); font_nina = new System.Drawing.Font("Nina", 8); binar1 = new binar(); binar1.init_binar(0, 275, 330, 30); voltm1 = new voltm(a0_pozx, a0_pozy, 300, 1023); voltm2 = new voltm(a1_pozx, a1_pozy, 300, 1023); termo1 = new termo(10, 50, 10, 200, 1023); termo2 = new termo(65, 50, 10, 200, 1023); termo3 = new termo(120, 50, 10, 200, 1023); termo4 = new termo(175, 50, 10, 200, 1023); termo5 = new termo(230, 50, 10, 200, 1023); int i; // Listez porturile seriale Nume_porturi = System.IO.Ports.SerialPort.GetPortNames(); this.Porturi_s.Items.Clear(); //Adaug porturile exixtente for (i = 0; i < Nume_porturi.Length; i++) { this.Porturi_s.Items.Add(Nume_porturi[i]); } //Pozitionarea listei pe primul element this.Porturi_s.SelectedIndex = 0; this.label1.Text = "Conectati-va la portul serial!"; } private void Form1_Paint(object sender, PaintEventArgs e) { voltm1.desen_voltm(desen); voltm2.desen_voltm(desen); } private void timer1_Tick(object sender, EventArgs e) { k++; if (k > 123456789) k = 0; this.label2.Text = k.ToString(); if (this.checkBox8.Checked) c0 = 1; else c0 = 0; if (this.checkBox7.Checked) c1 = 1; else c1 = 0; if (this.checkBox6.Checked) c2 = 1; else c2 = 0; if (this.checkBox5.Checked) c3 = 1; else c3 = 0; if (this.checkBox4.Checked) c4 = 1; else c4 = 0; if (this.checkBox3.Checked) c5 = 1; else c5 = 0; if (this.checkBox2.Checked) c6 = 1; else c6 = 0; if (this.checkBox1.Checked) c7 = 1; else c7 = 0; val_cmd = 128 * c7 + 64 * c6 + 32 * c5 + 16 * c4 + 8 * c3 + 4 * c2 + 2 * c1 + c0; this.label16.Text = System.Convert.ToString(val_cmd); if (this.serialPort1.IsOpen) { if (val_cmd == val_cmd_v) // nu am val_cmd noua deci lanxsez comanda "AA" { cda = "AA," + val_cmd.ToString(); this.serialPort1.Write(cda); txt = ""; txt = this.serialPort1.ReadExisting(); if (txt.Length > 0) { //this.label1.Text = txt; //Despachetare date date_r = txt.Split(delimiter); if (date_r.Length == 8) //Verific daca am primit 8 valori { //Pentru comanda M1, verific daca sum ade control e corecta //suma = Convert.ToInt16(date_r[0]) + Convert.ToInt16(date_r[1]) + Convert.ToInt16(date_r[2]) + Convert.ToInt16(date_r[3]) + Convert.ToInt16(date_r[4]) + Convert.ToInt16(date_r[5]) + Convert.ToInt16(date_r[6]) + Convert.ToInt16(date_r[7]); //if (suma % 1024 == Convert.ToInt16(date_r[8])) //{ this.label1.Text = txt; date_c = txt.Split(delimiter); //} } try { a0_val = System.Convert.ToInt16(date_c[1]); } catch (System.FormatException) { this.label1.Text = "Format necorespunzator!"; } catch (System.IndexOutOfRangeException) { this.label1.Text = "Format necorespunzator!"; } //Afisare valori analogice try { a2_val = System.Convert.ToInt16(date_c[5]); a3_val = System.Convert.ToInt16(date_c[4]); a4_val = System.Convert.ToInt16(date_c[3]); a5_val = System.Convert.ToInt16(date_c[2]); this.label4.Text = date_c[5].ToString(); this.label6.Text = date_c[4].ToString(); this.label8.Text = date_c[3].ToString(); this.label10.Text = date_c[2].ToString(); this.label12.Text = date_c[1].ToString(); this.label14.Text = date_c[0].ToString(); digi = System.Convert.ToUInt64(date_c[7]); this.label26.Text = System.Convert.ToString(digi); } catch (System.FormatException) { this.label1.Text = "Format necorespunzator!"; } // Afisare DI sub forma binara binar1.setval(8, digi, desen, creion_rosu, pens_blu, pens_back); //Afisare termo1 termo1.desenez(desen, creion_blu, creion_gri, pens_red, font_nina); termo1.setval(System.Convert.ToInt16(date_c[5]), desen, pens_blu, pens_back); termo2.desenez(desen, creion_blu, creion_gri, pens_red, font_nina); termo2.setval(System.Convert.ToInt16(date_c[4]), desen, pens_blu, pens_back); termo3.desenez(desen, creion_blu, creion_gri, pens_red, font_nina); termo3.setval(System.Convert.ToInt16(date_c[3]), desen, pens_blu, pens_back); termo4.desenez(desen, creion_blu, creion_gri, pens_red, font_nina); termo4.setval(System.Convert.ToInt16(date_c[2]), desen, pens_blu, pens_back); termo5.desenez(desen, creion_blu, creion_gri, pens_red, font_nina); termo5.setval(System.Convert.ToInt16(date_c[1]), desen, pens_blu, pens_back); // Afisare voltmetru pentru A3 si A4 voltm1.setval(desen, System.Convert.ToInt16(date_c[4])); voltm2.setval(desen, System.Convert.ToInt16(date_c[3])); } else { this.label1.Text = "Nu vin date!"; } } else //trebuie sa dau comanda "O,val_cmd" { this.serialPort1.Write("O," + System.Convert.ToString(val_cmd)); val_cmd_v = val_cmd; } } } private void but_con_Click(object sender, EventArgs e) { if (!this.serialPort1.IsOpen) { this.serialPort1.PortName = System.Convert.ToString(this.Porturi_s.Items[this.Porturi_s.SelectedIndex]); this.serialPort1.Open(); this.label1.Text = "Portul a fost deschis"; this.button1.Enabled = true; } else { this.label1.Text = "Portul este deja deschis deschis"; } this.serialPort1.Write("O,192");// aprind led-ul si dau tensiune pe potentiometru } private void but_dec_Click(object sender, EventArgs e) { this.serialPort1.Close(); this.label1.Text = "Portul este deconectat!"; } private void button1_Click(object sender, EventArgs e) { this.checkBox1.Checked = true; this.checkBox2.Checked = true; c0 = 1; c1 = 1; this.serialPort1.Write("O,192");// aprind led-ul si dau tensiune pe potentiometru } } }