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 Osciloscop { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public System.Drawing.Graphics desen; public System.Drawing.Pen creion_rosu; //public System.Drawing.Bitmap im; public osciloscop osciloscop1; int i, pozx = 10, pozy = 10, n_maxx = 400, n_maxy = 300, val_max = 550; double val, val_v, f, f0, f1, f2, f3, f4, f_v, f0_v, f1_v, f2_v, f3_v, f4_v, ampl, tr_1, tr_2; double val_y0, val_y1, val_y2, val_y3, val_y4; int a0_val, a1_val, a2_val, a3_val, a4_val, pwm, bt; string txt, cda, cd_c; static double[] valori_0 = new double[0]; static double[] valori_1 = new double[0]; static double[] valori_2 = new double[0]; static double[] valori_3 = new double[0]; static double[] valori_4 = new double[0]; static String[] date_c = new String[2000]; static String[] date_r = new String[11]; // Date receptionate static String delimStr = " ,.:\t"; static Char[] delimiter = delimStr.ToCharArray(); static String[] Nume_porturi = new String[11]; private void Form1_Load(object sender, EventArgs e) { Array.Resize(ref valori_0, n_maxx + 2); Array.Resize(ref valori_1, n_maxx + 2); Array.Resize(ref valori_2, n_maxx + 2); Array.Resize(ref valori_3, n_maxx + 2); Array.Resize(ref valori_4, n_maxx + 2); desen = this.CreateGraphics(); creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red); osciloscop1 = new osciloscop(desen, pozx, pozy, n_maxx, n_maxy, val_max, -50, Color.Black, Color.DarkGreen, Color.Black, Color.Yellow); // Listez porturile seriale //osciloscop1.auto_sy(5.5, 0); 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 timer1_Tick(object sender, EventArgs e) { int transl = 0; int amplif = 1; int zero = 0; pwm = this.trackBar5.Value; bt = this.trackBar4.Value; this.osciloscop1.auto_sx(400 + this.trackBar4.Value, 0); //int fr = n_maxx; //this.label2.Text = this.trackBar1.Value.ToString(); if (this.serialPort1.IsOpen) { if (this.radioButton1.Checked)// Achizitie in timp real { this.timer1.Interval = 50; //this.label2.ForeColor = Color.Red; cda = "AA," + pwm.ToString(); } if (this.radioButton2.Checked) { this.timer1.Interval = 1000; //this.label2.ForeColor = Color.LimeGreen; if (this.radioButton3.Checked) { if (this.checkBox1.Checked) { cda = "AI0," + bt.ToString()+"," + pwm.ToString(); } if (this.checkBox2.Checked) { cda = "AI1," + bt.ToString() + "," + pwm.ToString(); } if (this.checkBox3.Checked) { cda = "AI2," + bt.ToString() + "," + pwm.ToString(); } if (this.checkBox4.Checked) { cda = "AI3," + bt.ToString() + "," + pwm.ToString(); } if (this.checkBox5.Checked) { cda = "AI4," + bt.ToString() + "," + pwm.ToString(); } } else { cda = "D1I," + bt.ToString() + "," + pwm.ToString(); } } this.serialPort1.Write(cda); txt = ""; txt = this.serialPort1.ReadExisting(); txt = txt.Trim(); this.label1.Text = "L=" + txt.Length.ToString(); this.label8.Text = cda ; if ((txt.Length > 0 && txt.Length < 50)) { //Despachetare date date_r = txt.Split(delimiter); //label2.Text = txt; label4.Text = "Date citite: " + txt; if (date_r.Length > 0) //Verific daca am primit 4 valori { // Afisare grafica try { a0_val = System.Convert.ToInt16(date_r[1]); a1_val = System.Convert.ToInt16(date_r[2]); a2_val = System.Convert.ToInt16(date_r[3]); a3_val = System.Convert.ToInt16(date_r[4]); a4_val = System.Convert.ToInt16(date_r[5]); } catch (System.FormatException) { this.label1.Text = "Format necorespunzator!"; } catch (System.IndexOutOfRangeException) { this.label1.Text = "Format necorespunzator!"; } // Trasare grafic //A0 f0_v = f0; f0 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a0_val * 500.0 / 1024.0)); if (f0 > val_max) f0 = val_max - 1; if (f0 < 0) f0 = 0; val_y0 = (Math.Round(Convert.ToDouble(f0 / 100.0), 2)); if(radioButton5.Checked) f0 = System.Convert.ToInt32(n_maxy*7.5/5 + zero + amplif * System.Convert.ToDouble(a0_val * 75.0 / 1024.0)); //A1 f1_v = f1; f1 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a1_val) * 500.0 / 1024.0); if (f1 > val_max) f1 = val_max - 1; if (f1 < 0) f1 = 0; val_y1 = (Math.Round(Convert.ToDouble(f1 / 100.0), 2)); if (radioButton5.Checked) f1 = System.Convert.ToInt32(n_maxy * 5.8 / 5 + zero + amplif * System.Convert.ToDouble(a1_val) * 75.0 / 1024.0); //A2 f2_v = f2; f2 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a2_val) * 500.0 / 1024.0); if (f2 > val_max) f2 = val_max - 1; if (f2 < 0) f2 = 0; val_y2 = (Math.Round(Convert.ToDouble(f2 / 100.0), 2)); if (radioButton5.Checked) f2 = System.Convert.ToInt32(n_maxy * 4.15/ 5 + zero + amplif * System.Convert.ToDouble(a2_val) * 75.0 / 1024.0); //A3 f3_v = f3; f3 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a3_val) * 500.0 / 1024.0); if (f3 > val_max) f3 = val_max - 1; if (f3 < 0) f3 = 0; val_y3 = (Math.Round(Convert.ToDouble(f3 / 100.0), 2)); if (radioButton5.Checked) f3 = System.Convert.ToInt32(n_maxy * 2.45 / 5 + zero + amplif * System.Convert.ToDouble(a3_val) * 75.0 / 1024.0); //A4 f4_v = f4; f4 = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(a4_val) * 500.0 / 1024.0); if (f4 > val_max) f4 = val_max - 1; if (f4 < 0) f4 = 0; val_y4 = (Math.Round(Convert.ToDouble(f4 / 100.0), 2)); if (radioButton5.Checked) f4 = System.Convert.ToInt32(n_maxy * 0.8 / 5 + zero + amplif * System.Convert.ToDouble(a4_val) * 75.0 / 1024.0); for (int i = 0; i < n_maxx - 1; i++) { valori_0[i] = valori_0[i + 1]; valori_1[i] = valori_1[i + 1]; valori_2[i] = valori_2[i + 1]; valori_3[i] = valori_3[i + 1]; valori_4[i] = valori_4[i + 1]; } // Comanda valori_0[n_maxx - 1] = f0; valori_1[n_maxx - 1] = f1; valori_2[n_maxx - 1] = f2; valori_3[n_maxx - 1] = f3; valori_4[n_maxx - 1] = f4; //Afisare grafica valori analogice osciloscop1.sterg(); //this.label2.Text = "Comanda : " + this.trackBar1.Value.ToString(); //this.trackBar1.BackColor = Color.Red; if (this.checkBox1.Checked) { osciloscop1.setval(valori_0, n_maxx, Color.DeepSkyBlue); this.label10.Text = "y0 = " + val_y0.ToString() + " V"; } if (this.checkBox2.Checked) { osciloscop1.setval(valori_1, n_maxx, Color.Yellow); this.label11.Text = "y1 = " + val_y1.ToString() + " V"; } if (this.checkBox3.Checked) { osciloscop1.setval(valori_2, n_maxx, Color.Lime); this.label12.Text = "y2 = " + val_y2.ToString() + " V"; } if (this.checkBox4.Checked) { osciloscop1.setval(valori_3, n_maxx, Color.Red); this.label13.Text = "y3 = " + val_y3.ToString() + " V"; } if (this.checkBox5.Checked) { osciloscop1.setval(valori_4, n_maxx, Color.Magenta); this.label14.Text = "y4 = " + val_y4.ToString() + " V"; } osciloscop1.display(); } } else // pachet de date { this.label1.Text = "L=" + txt.Length.ToString(); date_c = txt.Split(delimiter); //label4.Text = "Date citite: "+txt; label4.Text = "Citire pachet de date: "; int nr_v = date_c.Count(); this.label3.Text = nr_v.ToString(); if (this.radioButton3.Checked)// Am date analogice { this.label10.Text = "A0"; this.label11.Text = "A0-A1"; this.label12.Text = "A0-A2"; this.label13.Text = "A0-A3"; this.label14.Text = "A0-A4"; //Despachetare date ampl = Convert.ToDouble(this.trackBar1.Value) / 100.0; tr_1 = Convert.ToDouble(this.trackBar2.Value); tr_2 = Convert.ToDouble(this.trackBar3.Value); //for (int j = 0; j < 2 * n_maxx2; j++) for (int j = 0; j < nr_v; j++) { try { val = System.Convert.ToInt32(System.Convert.ToDouble(date_c[j]) * 500.0 / 1024.0); if ((val > 500) || (val < 0)) val = val_v; } catch (System.FormatException) { //this.label1.Text = "j="+j.ToString()+" :"+date_c[j].ToString()+": Format necorespunzator !"; val = val_v; } catch (System.IndexOutOfRangeException) { val = val_v; } val_v = val; f_v = f; f = System.Convert.ToInt32(transl + zero + amplif * System.Convert.ToDouble(val)); if (f > val_max) f = val_max - 1; if (f < 0) f = 0; // Primesc 200 de puncte si inscriu 400 if (j < nr_v / 2) { valori_0[2 * j] = tr_1 + ampl * (f_v + f) / 2.0; valori_0[2 * j + 1] = tr_1 + ampl * f; //valori_2[2 * j] = Convert.ToInt32(Convert.ToDouble(this.trackBar1.Value * 1.0)); valori_2[2 * j] = 250; //valori_2[2 * j + 1] = Convert.ToInt32(Convert.ToDouble(this.trackBar1.Value * 1.0)); valori_2[2 * j + 1] = 250; } else { valori_1[2 * (j - nr_v / 2)] = tr_2 + ampl * (f_v + f) / 2.0; valori_1[2 * (j - nr_v / 2) + 1] = tr_2 + ampl * f; } } osciloscop1.sterg(); if (this.checkBox1.Checked) { osciloscop1.setval(valori_0, n_maxx, System.Drawing.Color.DeepSkyBlue); } if (this.checkBox2.Checked) { osciloscop1.setval(valori_1, n_maxx, System.Drawing.Color.Yellow); } if (this.checkBox3.Checked) { osciloscop1.setval(valori_1, n_maxx, System.Drawing.Color.Lime); } if (this.checkBox4.Checked) { osciloscop1.setval(valori_1, n_maxx, System.Drawing.Color.Red); } if (this.checkBox5.Checked) { osciloscop1.setval(valori_1, n_maxx, System.Drawing.Color.Magenta); } osciloscop1.display(); } else // am date digitale { this.label10.Text = "DI0"; this.label11.Text = "DI1"; this.label12.Text = "DI2"; this.label13.Text = "DI3"; this.label14.Text = "-"; for (int j = 0; j < nr_v; j++) { try { val = System.Convert.ToInt32(date_c[j]); if ((val > 128) || (val < 0)) val = val_v; } catch (System.FormatException) { //this.label1.Text = "j="+j.ToString()+" :"+date_c[j].ToString()+": Format necorespunzator !"; val = val_v; } catch (System.IndexOutOfRangeException) { val = val_v; } val_v = val; Int32 vali = System.Convert.ToInt32(val); valori_0[j] = 350 + 75 * (vali % 2); vali = vali / 2; valori_1[j] = 250 + 75 * (vali% 2); vali = vali / 2; valori_2[j] = 150 + 75 * (vali % 2); vali = vali / 2; valori_3[j] = 50 + 75 * (vali % 2); } osciloscop1.sterg(); osciloscop1.setval(valori_0, n_maxx, System.Drawing.Color.DeepSkyBlue); osciloscop1.setval(valori_1, n_maxx, System.Drawing.Color.Yellow); osciloscop1.setval(valori_2, n_maxx, System.Drawing.Color.Lime); osciloscop1.setval(valori_3, n_maxx, System.Drawing.Color.Red); osciloscop1.display(); } } } } 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("SG"); } private void but_dec_Click(object sender, EventArgs e) { this.serialPort1.Close(); this.label1.Text = "Portul este deconectat!"; } private void radioButton1_Click(object sender, EventArgs e) { this.timer1.Enabled=false; this.serialPort1.Write("SG");// Start generatoare this.timer1.Enabled = true; this.checkBox1.Checked = true; this.checkBox2.Checked = true; this.checkBox3.Checked = true; this.checkBox4.Checked = true; this.checkBox5.Checked = true; } private void radioButton2_Click(object sender, EventArgs e) { this.checkBox1.Checked = true; this.checkBox2.Checked = true; this.checkBox3.Checked = false; this.checkBox4.Checked = false; this.checkBox5.Checked = false; } private void checkBox2_Click(object sender, EventArgs e) { if (this.radioButton2.Checked) { this.checkBox3.Checked = false; this.checkBox4.Checked = false; this.checkBox5.Checked = false; } } private void checkBox3_Click(object sender, EventArgs e) { if (this.radioButton2.Checked) { this.checkBox2.Checked = false; this.checkBox4.Checked = false; this.checkBox5.Checked = false; } } private void checkBox4_Click(object sender, EventArgs e) { if (this.radioButton2.Checked) { this.checkBox2.Checked = false; this.checkBox3.Checked = false; this.checkBox5.Checked = false; } } private void checkBox5_Click(object sender, EventArgs e) { if (this.radioButton2.Checked) { this.checkBox2.Checked = false; this.checkBox3.Checked = false; this.checkBox4.Checked = false; } } private void checkBox6_Click(object sender, EventArgs e) { if (this.radioButton2.Checked) { this.checkBox2.Checked = false; this.checkBox3.Checked = false; this.checkBox4.Checked = false; this.checkBox5.Checked = false; } } } public class osciloscop { int x0, y0, w, h, val, val_v; double v_max, v_min, x_max, x_min; System.Drawing.Graphics zona_des; System.Drawing.Font font_ni = new System.Drawing.Font("Nina", 8); Color cul_val; 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 auto_sy(double val_max, double val_min) { if (val_max - val_min != 0) { v_max = val_max; v_min = val_min; } } public void auto_sx(double x_maxim, double x_minim) { if (x_max - x_min != 0) { x_max = x_maxim; x_min = x_minim; } else { x_max = w; x_min = 0; } } public void setval(double[] vals, int nrv, System.Drawing.Color culoare) { int i, j; if (w > 0 && h > 0) { double amplif; if ((v_max - v_min) != 0) amplif = (System.Convert.ToDouble(h) / System.Convert.ToDouble(v_max - v_min)); else amplif = 1; double transl = v_min * amplif; val_v = System.Convert.ToInt16(h + transl - amplif * System.Convert.ToDouble(vals[0])); //scalare if (val_v >= h) val_v = h - 1; if (val_v <= 0) val_v = 1; // afisare grafic sub forma de linii cu puncte for (i = 0; i < w; i++) { val = System.Convert.ToInt16(h + transl - amplif * System.Convert.ToDouble(vals[i])); //scalare if (val >= h) val = h - 1; if (val <= 0) val = 1; if (val_v < val) { // unesc doua puncte cu o linie crescatoare for (j = val_v; j <= val; j++) { img.SetPixel(i, j, culoare); } } else { // unesc doua puncte cu o linie descrescatoare for (j = val; j <= val_v; j++) { img.SetPixel(i, j, culoare); } } val_v = val; } Graphics g = Graphics.FromImage(img); //valori axa x double vx = x_min; double pasx = System.Convert.ToDouble(x_max - x_min) / System.Convert.ToDouble(w) * 50; for (i = 50; i < w; i += 50) { vx = vx + pasx; g.DrawString(Math.Round(vx, 2).ToString(), font_ni, new SolidBrush(cul_val), i, h - 15); } //valori axa y double vy = v_min; double pasy = System.Convert.ToDouble(v_max - v_min) / System.Convert.ToDouble(h) * 50; for (i = 50; i < h; i += 50) { vy = vy + pasy; g.DrawString(Math.Round(vy, 2).ToString(), font_ni, new SolidBrush(cul_val), 2, h - i - 10); } } } public osciloscop(System.Drawing.Graphics desen, int pozx, int pozy, int n_maxx, int n_maxy, double val_max, double val_min, Color culoare_fundal, Color culoare_grid_m, Color culoare_grid, Color culoare_valori) { x0 = pozx; y0 = pozy; w = n_maxx; h = n_maxy; v_max = val_max; v_min = val_min; x_max = n_maxx; x_min = 0; zona_des = desen; cul_val = culoare_valori; if (w > 0 && h > 0) { img = new Bitmap(w, h, zona_des); ims = new Bitmap(w, h, zona_des); int i = 0, j = 0; // sterg imaginea for (j = 0; j < h; j++) { for (i = 0; i < w; i++) { ims.SetPixel(i, j, culoare_fundal); } } // grid for (j = 0; j < h; j++) { // grid orizontal if (j % 10 == 0) { for (i = 0; i < w; i++) { if (j % 50 == 0) ims.SetPixel(i, j, culoare_grid_m); else ims.SetPixel(i, j, culoare_grid); } } else { // grid orizontal vertical for (i = 0; i < w; i++) { if (i % 10 == 0) { if (i % 50 == 0) ims.SetPixel(i, j, culoare_grid_m); else ims.SetPixel(i, j, culoare_grid); } } } } //chenar for (i = 0; i < w; i++) { ims.SetPixel(i, 0, culoare_grid_m); ims.SetPixel(i, h - 1, culoare_grid_m); } for (j = 0; j < h; j++) { ims.SetPixel(0, j, culoare_grid_m); ims.SetPixel(w - 1, j, culoare_grid_m); } Graphics s = Graphics.FromImage(ims); sterg(); } } } }