Exemple #1
0
        public override void ProcessData(byte[] buffer, int length)
        {
            bool checkPassed = this.FrameCheck(buffer, length);

            if (checkPassed == true)
            {
                int startIndex = 3;

                string message = "";

                string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                for (int i = 0; i < NumberOfChannels; i++)
                {
                    byte[] bytes = new byte[2];
                    bytes[0] = buffer[startIndex + i * 2];
                    bytes[1] = buffer[startIndex + i * 2 + 1];

                    if (channels.ContainsKey(i + 1))
                    {
                        CurrentVoltageChannel cvc = channels[i + 1];

                        if (cvc != null)
                        {
                            double value = cvc.GetResult(bytes);

                            string key = cvc.sensorId + "-" + cvc.type;

                            //DataValue dv = new DataValue();

                            if (dataBuffer.ContainsKey(key))
                            {
                                dataBuffer[key].Value = Math.Round(value, 3);
                                //dataBuffer[key].Updated = true;
                                dataBuffer[key].TimeStamp = stamp;

                                string result = JsonConvert.SerializeObject(dataBuffer[key]);

                                db.StringSet(key, result);
                                //dataBuffer[key].Value = value;
                                //DataValue dv = dataBuffer[key];
                                //dv.Value = value;
                                //dv.Updated = true;
                                //dv.TimeStamp = stamp;
                                //dataBuffer[key] = dv;
                                //this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = key;
                            }

                            this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;

                            //if (this.dataGridView.InvokeRequired)
                            //{
                            //    this.dataGridView.BeginInvoke(new MethodInvoker(() => {
                            //        this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;
                            //    }));
                            //}
                            //else
                            //{
                            //    this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;
                            //}
                        }
                    }
                    message += "通道" + (i + 1) + " Value: " + "\r\n";
                }
            }
            else
            {
                log.Warn(Tag + "broken frame");
                this.errMsg = "broken frame";
                //message = this.deviceId + "strain broken frame: " + CVT.ByteToHexStr(by) + "\r\n";
            }
        }
Exemple #2
0
        public override void ProcessData(byte[] buffer, int length)
        {
            bool checkPassed = this.FrameCheck(buffer, length);

            if (checkPassed == true)
            {
                //message = this.deviceId + "\r\n";
                int startIndex = 4;

                string message = "";

                //CurrentVoltageChannel cvc = new CurrentVoltageChannel("123", 1, 0, 20, 4, 20, 4);

                string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                StringBuilder sb = new StringBuilder(1024);
                sb.Append(stamp + ",");

                for (int i = 0; i < NumberOfChannels; i++)
                {
                    byte[] bytes = new byte[2];
                    bytes[0] = buffer[startIndex + i * 2];
                    bytes[1] = buffer[startIndex + i * 2 + 1];

                    if (channels.ContainsKey(i + 1))
                    {
                        CurrentVoltageChannel cvc = channels[i + 1];
                        double value = cvc.GetResult(bytes);
                        sb.Append(value.ToString() + ",");

                        string key = cvc.sensorId + "-" + cvc.type;
                        if (dataBuffer.ContainsKey(key))
                        {
                            dataBuffer[key].Value = Math.Round(value, 3);
                            //dataBuffer[key].Value = value;
                            //dataBuffer[key].Updated = true;
                            dataBuffer[key].TimeStamp = stamp;

                            string result = JsonConvert.SerializeObject(dataBuffer[key]);

                            //db.StringSet(key, result);
                            //DataValue dv = dataBuffer[key];
                            //dv.Value = value;
                            //dv.Updated = true;
                            //dv.TimeStamp = stamp;
                            //dataBuffer[key] = dv;
                        }

                        this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;

                        //if (this.dataGridView.InvokeRequired)
                        //{
                        //    this.dataGridView.BeginInvoke(new MethodInvoker(() => {
                        //        this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;
                        //    }));
                        //}
                        //else
                        //{
                        //    this.dataGridView.Rows[rowIndex].Cells[i + 1].Value = value;
                        //}
                    }
                }
                sb.Remove(sb.Length - 1, 1);
                //sb.Append("\r\n");
                times++;
                if (times == 30)
                {
                    times = 0;
                    AppendRecord(sb);
                }
            }
            else
            {
                log.Warn(Tag + "broken frame");
                this.errMsg = "broken frame";
                //message = this.deviceId + "strain broken frame: " + CVT.ByteToHexStr(by) + "\r\n";
            }
        }