Beispiel #1
0
        private void AddVal(UnitData val)
        {
            int errCode = val.ErrorCode;

            double cpres, clev;
            if (double.TryParse(txtCoefPressure.Text, out cpres) &&
                double.TryParse(txtCoefLevel.Text, out clev))
            {
                val.Pressure *= cpres;
                val.Level *= clev;
            }

            lblT1.Text = string.Format("Т1 = {0}", val.Term1);
            lblT2.Text = string.Format("Т2 = {0}", val.Term2);
            lblD1.Text = string.Format("Дав = {0}", val.Pressure);
            lblD2.Text = string.Format("Ур = {0}", val.Level);
            lblTransferError.Text = string.Format("Пакетов потеряно = {0}", _dataTransferCount);
            lblBadPacket.Text = string.Format("Пакетов битых = {0}", _dataTransferBad);

            DateTime time = DateTime.Now;

            if ((errCode & 3) == 0)
            {
                _singleCurves[0].UpdateData(val.Term1, time);
            }

            if ((errCode & (3<<2)) == 0)
            {
                _singleCurves[1].UpdateData(val.Term2, time);
            }

            if ((errCode & (3<<4)) == 0)
            {
                _singleCurves[2].UpdateData(val.Pressure/10, time);
            }

            if ((errCode & (3 << 6)) == 0)
            {
                _singleCurves[3].UpdateData(val.Level/10, time);
            }

            UpdateAsis(time.AddSeconds(-60), time);

            switch (errCode & 3)
            {
                case DEVICE_NOT_FOUND:
                    lblT1.Text = string.Format("Т1 = Датчик не найден");
                    break;
                case ERROR_CRC:
                    lblT1.Text = string.Format("Т1 = Ошибка CRC");
                    break;
                default:

                    break;
            }
            errCode >>= 2;
            switch (errCode & 3)
            {
                case DEVICE_NOT_FOUND:
                    lblT2.Text = string.Format("Т2 = Датчик не найден");
                    break;
                case ERROR_CRC:
                    lblT2.Text = string.Format("Т2 = Ошибка CRC");
                    break;
                default:

                    break;
            }

            errCode >>= 2;
            switch (errCode & 3)
            {
                case ERROR0:
                    lblD1.Text = string.Format("Дав = Ошибка 0");
                    break;
                case ERROR1:
                    lblD1.Text = string.Format("Дав = Ошибка 1");
                    break;
                default:

                    break;
            }

            errCode >>= 2;
            switch (errCode & 3)
            {
                case ERROR0:
                    lblD2.Text = string.Format("Ур = Ошибка 0");
                    break;
                case ERROR1:
                    lblD2.Text = string.Format("Ур = Ошибка 1");
                    break;
                default:

                    break;
            }
        }
Beispiel #2
0
        private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] inBuf = new byte[256];
            int rxCount = _comPort.Read(ref inBuf, 5); //Время чтения
            int rxIndex = 0;
            bool isBad = true;

            if (rxCount < 3) return; //Ошибка чтения

            if (chbIsVisiblePacket.Checked)
            {
                string tmpstr = "";
                for (rxIndex = 0; rxIndex < rxCount; rxIndex++)
                {
                    tmpstr += string.Format("{0:X2} ", inBuf[rxIndex]);
                }
                logAdd("[ " + tmpstr + " ]");
            }

            if (rxCount >= 6) //Данных хватает, начинаем разбор посылки
            {
                for (rxIndex = 0; rxIndex < rxCount - 5; rxIndex++)
                {
                    if ((inBuf[rxIndex] == _devaiceAddr || _lastCommandCode == 0x41) && //Адрес
                        (inBuf[rxIndex + 1] == 0x03 || inBuf[rxIndex + 1] == 0x10 //Код команды
                         || inBuf[rxIndex + 1] == 0x83) &&
                        (inBuf[rxIndex + 2] == 14 || inBuf[rxIndex + 2] == 19
                        || inBuf[rxIndex + 2] == 6)) //Количество байт
                    {
                        if (_comPort.CRC16_Check(ref inBuf, rxIndex, rxIndex + inBuf[rxIndex + 2]) == 0) //CRC OK
                        {

                            switch (inBuf[rxIndex + 2])
                            {
                                case 6:
                                    switch (_lastCommandCode)
                                    {
                                        case 0x41:  //Get Addr
                                            logAdd(string.Format("Адрес получен {0}", inBuf[rxIndex + 3]));
                                            _devaiceAddr = inBuf[rxIndex + 3];
                                            txtDeviceAddr.Text = _devaiceAddr.ToString();
                                            isBad = false;
                                            break;
                                        case (0x41|0x80): //Set Addr
                                            logAdd("Адрес изменен");
                                            isBad = false;
                                            break;
                                        case (0x4F|0x80): //Set Out
                                            logAdd("Состояние выходов");
                                            isBad = false;
                                            break;
                                    }
                                    _lastCommandCode = 0;
                                    break;

                                case 14: //Данные
                                    _dataTransferCount--;
                                    isBad = false;

                            // Парсим данные
                                    double temp;
                            UnitData curVal = new UnitData();

                            rxIndex += 3;
                            temp = BitConverter.ToInt16(inBuf, rxIndex);
                            curVal.Term1 = temp / 16;

                            temp = BitConverter.ToInt16(inBuf, rxIndex + 2);
                            curVal.Term2 = temp / 16;

                            temp = BitConverter.ToUInt16(inBuf, rxIndex + 4);
                            curVal.Pressure = temp;

                            temp = BitConverter.ToUInt16(inBuf, rxIndex + 6);
                            curVal.Level = temp;

                            rxIndex += 8;
                            curVal.ErrorCode = inBuf[rxIndex];
                            rxIndex -= 11;

                                    AddVal(curVal);

                                    break;

                                case 19: //Ошибки

                                    _dataTransferCount--;
                                    isBad = false;

                                    rxIndex += 3;
                                    int upTime = BitConverter.ToInt32(inBuf, rxIndex);

                                    this.Text = string.Format("Настройка (UpTime = {0}:{1:00}:{2:00}.{3})", upTime / 60 / 60 / 10, (upTime / 60 / 10) % 60, (upTime / 10) % 60, upTime%10);
                                    rxIndex += 4;
                                    DateTime time = DateTime.Now;
                                    for (int i = 0; i < 4; i++)
                                    {
                                        _errors[i] += inBuf[rxIndex + i];
                                        _errors[i] += inBuf[rxIndex + i + 4];
                                        _singleCurves[i+4].UpdateData(_errors[i], time);
                                    }

                                     rxIndex -= 7;
                                    break;

                            }

                            rxIndex += inBuf[rxIndex + 2];
                        }
                    }
                }
            }

            if (isBad) _dataTransferBad++;
        }