namespace Multiio_v06
{
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!";
}
}
public class osciloscop ...
public class binar ...
public class termo ...
}
|