private void DataTransferTimer_Tick(object sender, EventArgs e) //порядок опроса ТИ { if (ComPort.IsOpen) { switch (commandQueue) { case 0: BytesOperations.State_pressure(); break; case 1: BytesOperations.State_valve(); break; //case 2: BytesOperations.State_electrometer(); break; //case 3: BytesOperations.State_full(); break; default: commandQueue = 0; goto case 0; } } if (connectAttempts >= 1 && connectAttempts <= 3) { ConnectLabel.Foreground = Brushes.Green; ConnectLabel.Content = "Связь: Установленна"; } if (connectAttempts > 0) { connectAttempts--; } else { ConnectLabel.Foreground = Brushes.Red; ConnectLabel.Content = "Связь: Отсутствует"; } }
void Manager(byte[] reciveBytes) { if (reciveBytes.Length == 0) { return; //защитка, хз почему-то иногда может прийти "ничего" ¯\_(ツ)_/¯ } #region 2F and 2D script if (reciveBytes[0] == 0x2D && reciveBytes.Length == 1) { ConnectLabel.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { connectAttempts = 8; ConnectLabel.Foreground = Brushes.Yellow; ConnectLabel.Content = "Связь: подождите немного"; })); return; } if (reciveBytes[0] == 0x2F && reciveBytes.Length == 1) { ConnectLabel.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { connectAttempts = 3; ConnectLabel.Foreground = Brushes.Green; ConnectLabel.Content = "Связь: Установленна"; BytesOperations.Trash_off(); })); return; } #endregion switch (commandQueue) { case 0: if (reciveBytes[0] == 0xAB && reciveBytes.Length == 12) { double[] pressure = new double[2] { reciveBytes[8] + reciveBytes[9] * 256, reciveBytes[10] + reciveBytes[11] * 256 }; //хранит п1 и п2 соответственно for (int n = 0; n < pressure.Length; n++) { if (pressure[n] < 100) { pressure[n] = 100; } if (pressure[n] > 65471) { pressure[n] = 65471; } for (int i = 0; i < pArr1.Length; i++) { if (pressure[n] < pArr1[i]) { pressure[n] = ((pArr2[i] - pArr2[i - 1]) / (pArr1[i] - pArr1[i - 1]) * pressure[n] + pArr2[i - 1] - (pArr2[i] - pArr2[i - 1]) / (pArr1[i] - pArr1[i - 1]) * pArr1[i - 1]); break; } } if (pressure[n] > 90000) { pressure[n] = 100000; } } ConnectLabel.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { P1Label.Content = $"P1 = {pressure[0]:0.0} Па"; P3Label.Content = $"P3 = {pressure[1]:0.0} Па"; })); commandQueue++; connectAttempts = 3; } break; //State_pressure(); case 1: if (reciveBytes[0] == 0xAB && reciveBytes.Length == 5) { } break; //case 2: BytesOperations.State_electrometer(); break; //case 3: BytesOperations.State_full(); break; //default: commandQueue = 0; goto case 0; break; default: break; } }