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 Multiio { 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 osciloscop grafic0; public termo termo1; public termo termo2; public termo termo3; public termo termo4; public termo termo5; int pozx = 310, pozy = 50, n_maxx = 225, n_maxy = 200; Int32 val_max = 1025, k; Int32 a0_val, a1_val, a2_val, a3_val, a4_val; double val_y0, val_y1, val_y2, val_y3, val_y4; 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 int[] a2_valori = new int[0]; static int[] a3_valori = new int[0]; static int[] a4_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]; 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); Array.Resize(ref a0_valori, n_maxx + 1); Array.Resize(ref a1_valori, n_maxx + 1); Array.Resize(ref a2_valori, n_maxx + 1); Array.Resize(ref a3_valori, n_maxx + 1); Array.Resize(ref a4_valori, n_maxx + 1); grafic0 = new osciloscop(desen, pozx, pozy, n_maxx, n_maxy, val_max); termo1 = new termo(10, 50, 10, 200, 1025); termo2 = new termo(65, 50, 10, 200, 1025); termo3 = new termo(120, 50, 10, 200, 1025); termo4 = new termo(175, 50, 10, 200, 1025); termo5 = new termo(230, 50, 10, 200, 1025); //int i, j; // Listez porturile seriale Nume_porturi = System.IO.Ports.SerialPort.GetPortNames(); this.Porturi_s.Items.Clear(); //Adaug porturile exixtente for (int 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 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); int transl = 0; int amplif = 1; int zero = 0; 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); //} } // Afisare grafica try { a0_val = System.Convert.ToInt16(date_c[1]); a1_val = System.Convert.ToInt16(date_c[2]); a2_val = System.Convert.ToInt16(date_c[3]); a3_val = System.Convert.ToInt16(date_c[4]); a4_val = System.Convert.ToInt16(date_c[5]); } catch (System.FormatException) { this.label1.Text = "Format necorespunzator!"; } catch (System.IndexOutOfRangeException) { this.label1.Text = "Format necorespunzator!"; } // Trasare grafic //A0 int f0 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a0_val)); if (f0 > val_max) f0 = val_max - 1; if (f0 < 0) f0 = 0; val_y0 = (Math.Round(Convert.ToDouble(5 * f0 / 1024.0), 2)); if (radioButton5.Checked) f0 = System.Convert.ToInt32(n_maxy * 20 / 5 + zero + amplif * System.Convert.ToDouble(a0_val / 5)); //A0 int f1 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a1_val)); if (f1 > val_max) f1 = val_max - 1; if (f1 < 0) f1 = 0; val_y1 = (Math.Round(Convert.ToDouble(5 * f1 / 1024.0), 2)); if (radioButton5.Checked) f1 = System.Convert.ToInt32(n_maxy * 15 / 5 + zero + amplif * System.Convert.ToDouble(a1_val / 5)); //A2 int f2 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a2_val)); if (f2 > val_max) f2 = val_max - 1; if (f2 < 0) f2 = 0; val_y2 = (Math.Round(Convert.ToDouble(5 * f2 / 1024.0), 2)); if (radioButton5.Checked) f2 = System.Convert.ToInt32(n_maxy * 10 / 5 + zero + amplif * System.Convert.ToDouble(a2_val / 5)); //A3 int f3 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a3_val)); if (f3 > val_max) f3 = val_max - 1; if (f3 < 0) f3 = 0; val_y3 = (Math.Round(Convert.ToDouble(5 * f3 / 1024.0), 2)); if (radioButton5.Checked) f3 = System.Convert.ToInt32(n_maxy * 5 / 5 + zero + amplif * System.Convert.ToDouble(a3_val / 5)); //A4 int f4 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a4_val)); if (f4 > val_max) f4 = val_max - 1; if (f4 < 0) f4 = 0; val_y4 = (Math.Round(Convert.ToDouble(5 * f4 / 1024.0), 2)); if (radioButton5.Checked) f4 = System.Convert.ToInt32(n_maxy / 5 + zero + amplif * System.Convert.ToDouble(a4_val / 5)); for (int i = 0; i < n_maxx - 1; i++) { a0_valori[i] = a0_valori[i + 1]; a1_valori[i] = a1_valori[i + 1]; a2_valori[i] = a2_valori[i + 1]; a3_valori[i] = a3_valori[i + 1]; a4_valori[i] = a4_valori[i + 1]; } a0_valori[n_maxx - 1] = f0; a1_valori[n_maxx - 1] = f1; a2_valori[n_maxx - 1] = f2; a3_valori[n_maxx - 1] = f3; a4_valori[n_maxx - 1] = f4; //Afisare grafica valori analogice grafic0.sterg(); if (this.checkBox13.Checked) { grafic0.setval(a0_valori, n_maxx, Color.DeepSkyBlue); this.label31.Text = "y0 = " + val_y0.ToString() + " V"; } if (this.checkBox12.Checked) { grafic0.setval(a1_valori, n_maxx, Color.Yellow); this.label30.Text = "y1 = " + val_y1.ToString() + " V"; } if (this.checkBox11.Checked) { grafic0.setval(a2_valori, n_maxx, Color.Lime); this.label29.Text = "y2 = " + val_y2.ToString() + " V"; } if (this.checkBox10.Checked) { grafic0.setval(a3_valori, n_maxx, Color.Red); this.label28.Text = "y3 = " + val_y3.ToString() + " V"; } if (this.checkBox9.Checked) { grafic0.setval(a4_valori, n_maxx, Color.Magenta); this.label27.Text = "y4 = " + val_y4.ToString() + " V"; } grafic0.display(); //Afisare valori analogice try { digi = System.Convert.ToUInt64(date_c[7]); this.label26.Text = System.Convert.ToString(digi); 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(); } 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); } 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"; } else { this.label1.Text = "Portul este deja deschis deschis"; } this.serialPort1.Write("AA"); } private void but_dec_Click(object sender, EventArgs e) { this.serialPort1.Close(); this.label1.Text = "Portul este deconectat!"; } } // -------------- Clasa osciloscop ------------------ public class osciloscop { int x0, y0, w, h, v_max, val, val_v, vl, s_x = 2; System.Drawing.Graphics zona_des; System.Drawing.Pen creion_r = 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); System.Drawing.SolidBrush radiera = new System.Drawing.SolidBrush(System.Drawing.Color.White); System.Drawing.Bitmap img; System.Drawing.Bitmap ims; public void sterg() { img = new Bitmap(ims); } public void display() { zona_des.DrawImage(img, x0, y0); } public void setval(int[] vals, int nrv, System.Drawing.Color cul) { int i, j, l; double r; // afisare grafic sub forma de puncte val_v = h - 1 - System.Convert.ToInt16(System.Convert.ToDouble(vals[0]) * (System.Convert.ToDouble(h) / System.Convert.ToDouble(v_max))); //scalare if (val_v >= h) val_v = h - 1; if (val_v <= 0) val_v = 1; for (i = 0; i < w; i++) { val = h - 1 - System.Convert.ToInt16(System.Convert.ToDouble(vals[i]) * (System.Convert.ToDouble(h) / System.Convert.ToDouble(v_max))); //scalare if (val >= h) val = h - 1; if (val <= 0) val = 1; if (val_v <= val) { // unesc doua puncte cu o linie crescatoare r = (val - val_v) / s_x; vl = val_v; for (l = 0; l < s_x; l++) { for (j = System.Convert.ToInt16(vl + r); j >= vl; j--) { img.SetPixel(System.Convert.ToInt16(s_x * i + l), j, cul); } vl = System.Convert.ToInt16(vl + r); } } else { // unesc doua puncte cu o linie descrescatoare r = (val_v - val) / s_x; vl = val; for (l = s_x; l > 0; l--) { for (j = vl; j <= vl + r; j++) { img.SetPixel(System.Convert.ToInt16(s_x * i + l), j, cul); } vl = System.Convert.ToInt16(vl + r); } } val_v = val; } //valori axa x for (i = 0; i <= s_x * w; i += 50) { val = System.Convert.ToInt16(System.Convert.ToDouble(i) * (System.Convert.ToDouble(w) / System.Convert.ToDouble(w) / s_x)); //scalare zona_des.DrawString(val.ToString(), font_ni, pens_blu, x0 + i, y0 + h); } //valori axa y for (i = 0; i <= h; i += 50) { val = System.Convert.ToInt16(System.Convert.ToDouble(i) * (System.Convert.ToDouble(v_max) / System.Convert.ToDouble(h))); //scalare zona_des.DrawString(val.ToString(), font_ni, pens_blu, x0 - 20, y0 + h - i - 10); } } public osciloscop(System.Drawing.Graphics desen, int pozx, int pozy, int n_maxx, int n_maxy, int val_max) { x0 = pozx; y0 = pozy; w = n_maxx; h = n_maxy; v_max = val_max; zona_des = desen; img = new Bitmap(s_x * w + 5 * s_x, h, zona_des); ims = new Bitmap(s_x * w + 5 * s_x, h, zona_des); int i, j; // sterg imaginea for (j = 0; j < h; j++) { for (i = 0; i < s_x * w + s_x; i++) { //ims.SetPixel(i, j, System.Drawing.Color.WhiteSmoke); ims.SetPixel(i, j, System.Drawing.Color.Black); } } // grid for (j = 0; j < h; j++) { // grid orizontal if (j % 10 == 0) { for (i = 0; i < s_x * w; i++) { if (j % 50 == 0) ims.SetPixel(i, j, System.Drawing.Color.DarkGreen); else //ims.SetPixel(i, j, System.Drawing.Color.LightGray); ims.SetPixel(i, j, System.Drawing.Color.DarkBlue); } } else { // grid orizontal vertical for (i = 0; i < s_x * w; i++) { if (i % 10 == 0) { if (i % 50 == 0) ims.SetPixel(i, j, System.Drawing.Color.DarkGreen); else //ims.SetPixel(i, j, System.Drawing.Color.LightGray); ims.SetPixel(i, j, System.Drawing.Color.DarkBlue); } } } } //chenar for (i = 0; i < s_x * w; i++) { ims.SetPixel(i, 0, System.Drawing.Color.Blue); ims.SetPixel(i, h - 1, System.Drawing.Color.Blue); } for (j = 0; j < h; j++) { ims.SetPixel(0, j, System.Drawing.Color.Blue); ims.SetPixel(s_x * w - 1, j, System.Drawing.Color.Blue); } } } // --------------Sfarsit clasa osciloscop ------------------ // ----------------- Clasa binar ---------------------- 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; } } // ----------------- Clasa termo ---------------------- 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; } } //---Sfarsit clasa termo }