namespace Multiio_v05
{
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 osciloscop grafic1;
public termo termo1;
public termo termo2;
public termo termo3;
public termo termo4;
public termo termo5;
int a0_pozx = 350, a0_pozy = 50, a0_n_maxx = 300, a0_n_maxy = 200;
Int32 a0_val, a0_val_max = 500, k, suma;
int a1_pozx = 700, a1_pozy = 50, a1_n_maxx = 300, a1_n_maxy = 200;
Int32 a1_val, a1_val_max = 1000;
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];
// -------------- Osciloscop ------------------
public class osciloscop
{
int x0;
int y0;
int w;
int h;
int val_max,val_max_af, val, val_v;
int nr_max;
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 setval(int[] vals, int nrv)
{
img = new Bitmap(nr_max, val_max, zona_des);
int val, i, j;
// afisare grafic sub forma de puncte
val_v = System.Convert.ToInt16(System.Convert.ToDouble(vals[0]) * (System.Convert.ToDouble(h) / System.Convert.ToDouble(val_max))); //scalare
for (i = 0; i < w; i++)
{
val = System.Convert.ToInt16(System.Convert.ToDouble(vals[i]) * (System.Convert.ToDouble(h) / System.Convert.ToDouble(val_max))); //scalare
if (val_v < val)
{
for (j = val_v; j <= val; j++)
img.SetPixel(i, j, System.Drawing.Color.Red);
}
else
{
for (j = val; j <= val_v; j++)
img.SetPixel(i, j, System.Drawing.Color.Red);
}
val_v = val;
}
zona_des.DrawImage(ims, x0, y0);
zona_des.DrawImage(img, x0, y0);
//zona_des.FillRectangle(radiera, x0, y0 + h, w + 20, 20);
for (i = 0; i <= w; i += 50)
{
val = System.Convert.ToInt16(System.Convert.ToDouble(i) * (System.Convert.ToDouble(nr_max) / System.Convert.ToDouble(w))); //scalare
zona_des.DrawString(val.ToString(), font_ni, pens_blu, x0 + i, y0 + h);
}
//zona_des.FillRectangle(radiera, x0 - 20, y0 - 10, 20, h + 20);
for (i = 0; i <= h; i += 50)
{
val = System.Convert.ToInt16(System.Convert.ToDouble(i) * (System.Convert.ToDouble(val_max_af) / 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 vmaxa)
{
x0 = pozx;
y0 = pozy;
w = n_maxx;
h = n_maxy;
nr_max = n_maxx;
val_max = n_maxy;
val_max_af = vmaxa;
zona_des = desen;
int i, j;
img = new Bitmap(nr_max, n_maxy, zona_des);
ims = new Bitmap(nr_max, n_maxy, zona_des);
// sterg imaginea
for (j = 0; j < val_max; j++)
{
for (i = 0; i < nr_max; i++)
{
ims.SetPixel(i, j, System.Drawing.Color.WhiteSmoke);
}
}
// grid
for (j = 0; j < val_max; j++)
{
// grid orizontal
if (j % 10 == 0)
{
for (i = 0; i < nr_max; i++)
{
if (j % 50 == 0)
ims.SetPixel(i, j, System.Drawing.Color.Gray);
else
ims.SetPixel(i, j, System.Drawing.Color.LightGray);
}
}
else
{
// grid orizontal vertical
for (i = 0; i < nr_max; i++)
{
if (i % 10 == 0)
{
if (i % 50 == 0)
ims.SetPixel(i, j, System.Drawing.Color.Gray);
else
ims.SetPixel(i, j, System.Drawing.Color.LightGray);
}
}
}
}
//chenar
for (i = 0; i < n_maxx; i++)
{
ims.SetPixel(i, 0, System.Drawing.Color.Blue);
ims.SetPixel(i, val_max - 1, System.Drawing.Color.Blue);
}
for (j = 0; j < val_max; j++)
{
ims.SetPixel(0, j, System.Drawing.Color.Blue);
ims.SetPixel(nr_max - 1, j, System.Drawing.Color.Blue);
}
}
}
// -------------- Clasa instrument ------------------
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;
}
}
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);
Array.Resize(ref a0_valori, a0_n_maxx + 1);
grafic0 = new osciloscop(desen, a0_pozx, a0_pozy, a0_n_maxx, a0_n_maxy, a0_val_max);
Array.Resize(ref a1_valori, a1_n_maxx + 1);
grafic1 = new osciloscop(desen, a1_pozx, a1_pozy, a1_n_maxx, a1_n_maxy, a1_val_max);
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, 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 = a0_n_maxy;
int zero = a0_n_maxy - 1;
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 A1
try
{
a0_val = System.Convert.ToInt16(date_c[2]);
}
catch (System.FormatException)
{
this.label1.Text = "Format necorespunzator!";
}
catch (System.IndexOutOfRangeException)
{
this.label1.Text = "Format necorespunzator!";
}
// Trasare grafic
int f = System.Convert.ToInt32(transl + zero - amplif * System.Convert.ToDouble(a0_val) / a0_val_max);
if (f > a0_n_maxy)
f = a0_n_maxy - 1;
if (f < 0)
f = 0;
for (int i =0;i< a0_n_maxx - 1; i++)
{
a0_valori[i] = a0_valori[i + 1];
}
a0_valori[a0_n_maxx - 1] = f;
//desen.DrawImage(im, a0_pozx, a0_pozy);
grafic0.setval(a0_valori, a0_n_maxx);
// Afisare A0
try
{
a1_val = System.Convert.ToInt16(date_c[1]);
}
catch (System.FormatException)
{
this.label1.Text = "Format necorespunzator!";
}
catch (System.IndexOutOfRangeException)
{
this.label1.Text = "Format necorespunzator!";
}
// Trasare grafic
f = System.Convert.ToInt32(transl + zero - amplif * System.Convert.ToDouble(a1_val) / a1_val_max);
if (f > a1_n_maxy)
f = a1_n_maxy - 1;
if (f < 0)
f = 0;
for (int i = 0; i < a1_n_maxx - 1; i++)
{
a1_valori[i] = a1_valori[i + 1];
}
a1_valori[a1_n_maxx - 1] = f;
//desen.DrawImage(im, a1_pozx, a1_pozy);
grafic1.setval(a1_valori, a1_n_maxx);
//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);
}
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!";
}
}
}
|