private void straight_read(object com) { int read_buffer = 17; int len = 0; byte[] buffer = new byte[read_buffer]; byte mini_buffer = 0; byte[] temp = new byte[2]; bool reset = false; System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); packet legacy = new packet(); Time reset_time = new Time(0, 0, 0); try { while (true) { if (active_com.BytesToRead > 0) { mini_buffer = (byte)active_com.ReadByte(); if (mini_buffer == 10) { mini_buffer = (byte) active_com.ReadByte(); if (mini_buffer == 13) { while (active_com.BytesToRead < 17) { } len = active_com.Read(buffer,0,17); textBox13.Invoke(new Action(() => textBox13.Text = "" + buffer[0])); textBox12.Invoke(new Action(() => textBox12.Text = "" + buffer[1])); textBox11.Invoke(new Action(() => textBox11.Text = "" + buffer[2])); temp[0] = buffer[5]; temp[1] = buffer[4]; textBox6.Invoke(new Action(() => textBox6.Text = string.Format("{0:F1}", (float)BitConverter.ToInt16(temp, 0) / 10))); temp[0] = buffer[7]; temp[1] = buffer[6]; textBox5.Invoke(new Action(() => textBox5.Text = "" + BitConverter.ToInt16(temp, 0))); temp[0] = buffer[9]; temp[1] = buffer[8]; textBox3.Invoke(new Action(() => textBox3.Text = "" + BitConverter.ToInt16(temp, 0))); temp[0] = buffer[11]; temp[1] = buffer[10]; textBox7.Invoke(new Action(() => textBox7.Text = string.Format("{0:F1}",(float) BitConverter.ToInt16(temp, 0) / 10))); temp[0] = buffer[14]; temp[1] = buffer[13]; textBox4.Invoke(new Action(() => textBox4.Text = "" + buffer[12] + " " + BitConverter.ToUInt16(temp, 0))); if (((buffer[3] == 0) || (buffer[3] == 10)))// && (!reset)) { legacy.time_h = buffer[0]; legacy.time_min = buffer[1]; legacy.time_sec = buffer[2]; legacy.status = buffer[3]; temp[0] = buffer[5]; temp[1] = buffer[4]; legacy.temp = BitConverter.ToInt16(temp, 0); temp[0] = buffer[7]; temp[1] = buffer[6]; legacy.prok = BitConverter.ToInt16(temp, 0); temp[0] = buffer[9]; temp[1] = buffer[8]; legacy.n = BitConverter.ToInt16(temp, 0); temp[0] = buffer[11]; temp[1] = buffer[10]; legacy.vel = BitConverter.ToInt16(temp, 0); legacy.Km = buffer[12]; temp[0] = buffer[14]; temp[1] = buffer[13]; legacy.meter = BitConverter.ToUInt16(temp, 0); legacy.char_pulse = buffer[15]; legacy.check_sum = buffer[16]; reset = true; //reset_timereset_time = new Time(buffer[0], buffer[1], buffer[2]); } if ((buffer[3] != 0))// && (buffer[3]!=10)) reset = false; //reset_time.difference(buffer[0], buffer[1], buffer[2]); if (buffer[2] >= legacy.time_sec) buffer[2] = (byte)(buffer[2] - legacy.time_sec); else { buffer[1] -= 1; buffer[2] = (byte)(60 + (buffer[2] - legacy.time_sec)); } if (buffer[1] >= legacy.time_min) buffer[1] = (byte)(buffer[1] - legacy.time_min); else { buffer[0] -= 1; buffer[1] = (byte)(60 + (buffer[1] - legacy.time_min)); } if (buffer[0] >= legacy.time_h) buffer[0] = (byte)(buffer[0] - legacy.time_h); textBox10.Invoke(new Action(() => textBox10.Text = "" + (buffer[0]))); textBox9.Invoke(new Action(() => textBox9.Text = "" + (buffer[1]))); textBox8.Invoke(new Action(() => textBox8.Text = "" + (buffer[2]))); temp[0] = buffer[9]; temp[1] = buffer[8]; textBox1.Invoke(new Action(() => textBox1.Text = "" + (BitConverter.ToInt16(temp, 0) - legacy.n))); temp[0] = buffer[14]; temp[1] = buffer[13]; textBox2.Invoke(new Action(() => textBox2.Text = "" + (buffer[12] - legacy.Km) + " " + (BitConverter.ToUInt16(temp, 0) - legacy.meter))); textBox14.Invoke(new Action(() => textBox14.Text = "" + buffer[15])); } } timer.Restart(); } if (timer.ElapsedMilliseconds >= 3000) { richTextBox1.Invoke(new Action(() => richTextBox1.ForeColor = Color.Black)); Disconnect(); } } } catch (COMException sd) { MessageBox.Show("Произошла ошибка - " + sd.Message + "\nПопробуйте выбрать другой СОМ порт."); reading_active.Abort(); Thread.Sleep(50); display_active.Abort(); Thread.Sleep(50); return; } timer.Stop(); }
private void display(object com) { byte one = 0; byte two = 0; byte[] buffer = new byte[17]; byte[] full_buffer = new byte[50]; //System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); //timer.Start(); packet legacy = new packet(); byte[] temp = new byte[2]; while (true) { if (byte2display.Count > 1) { full_buffer = (byte[])byte2display.Dequeue(); for (int i = 0; i < full_buffer.Length; i++) { if (i<63) { one = full_buffer[i]; i = i + 1; } if ((one == 10)) { if (i < 63) { two = full_buffer[i]; i = i + 1; } if (two == 13) { /*for (int i = 0; i < 17; i++) if (byte2display.Peek() != null) buffer[i] = (byte)byte2display.Dequeue(); else byte2display.Dequeue(); */ if (i + 17 <= full_buffer.Length) for (int j = 0; j < buffer.Length; j++) buffer[j] = full_buffer[i + j]; textBox13.Invoke(new Action(() => textBox13.Text = "" + buffer[0])); textBox12.Invoke(new Action(() => textBox12.Text = "" + buffer[1])); textBox11.Invoke(new Action(() => textBox11.Text = "" + buffer[2])); temp[0] = buffer[5]; temp[1] = buffer[4]; textBox6.Invoke(new Action(() => textBox6.Text = string.Format("{0:F1}", BitConverter.ToInt16(temp, 0) / 10))); temp[0] = buffer[7]; temp[1] = buffer[6]; textBox5.Invoke(new Action(() => textBox5.Text = "" + BitConverter.ToInt16(temp, 0))); temp[0] = buffer[9]; temp[1] = buffer[8]; textBox3.Invoke(new Action(() => textBox3.Text = "" + BitConverter.ToInt16(temp, 0))); temp[0] = buffer[11]; temp[1] = buffer[10]; textBox7.Invoke(new Action(() => textBox7.Text = "" + BitConverter.ToInt16(temp, 0))); temp[0] = buffer[14]; temp[1] = buffer[13]; textBox4.Invoke(new Action(() => textBox4.Text = "" + buffer[12] + " " + BitConverter.ToUInt16(temp, 0))); if ((buffer[3] != 0) && (buffer[3] != 0x10)) { legacy.time_h = buffer[0]; legacy.time_min = buffer[1]; legacy.time_sec = buffer[2]; legacy.status = buffer[3]; legacy.temp = BitConverter.ToInt16(buffer, 4); legacy.prok = BitConverter.ToInt16(buffer, 6); legacy.n = BitConverter.ToInt16(buffer, 8); legacy.vel = BitConverter.ToInt16(buffer, 10); legacy.Km = buffer[12]; legacy.meter = BitConverter.ToUInt16(buffer, 13); legacy.char_pulse = buffer[15]; legacy.check_sum = buffer[16]; } textBox10.Invoke(new Action(() => textBox10.Text = "" + (buffer[0] - legacy.time_h))); textBox9.Invoke(new Action(() => textBox9.Text = "" + (buffer[1] - legacy.time_min))); textBox8.Invoke(new Action(() => textBox8.Text = "" + (buffer[2] - legacy.time_sec))); temp[0] = buffer[9]; temp[1] = buffer[8]; textBox1.Invoke(new Action(() => textBox1.Text = "" + (BitConverter.ToInt16(temp, 0) - legacy.n))); temp[0] = buffer[14]; temp[1] = buffer[13]; textBox2.Invoke(new Action(() => textBox2.Text = "" + (buffer[12] - legacy.Km) + " " + (BitConverter.ToUInt16(temp, 0) - legacy.meter))); textBox14.Invoke(new Action(() => textBox1.Text = "" + buffer[15])); } } } } } //timer.Stop(); }