private void LoadChannels() { using (SQLiteConnection connection = new SQLiteConnection(this.config.Database)) { connection.Open(); string strainStatement = "select SensorId,ChannelNo,InitValue,OutputRangeTop,OutputRangeBottom,MeasureRangeTop,MeasureRangeBottom,Type from CVChannels where GroupNo ='" + config.DeviceId + "'"; SQLiteCommand command = new SQLiteCommand(strainStatement, connection); using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string sensorId = reader.GetString(0); int channelNo = reader.GetInt32(1); double initValue = reader.GetDouble(2); double outputRangeTop = reader.GetDouble(3); double outputRangeBottom = reader.GetDouble(4); double measureRangeTop = reader.GetDouble(5); double measureRangeBottom = reader.GetDouble(6); string type = reader.GetString(7); CurrentVoltageChannel channel = new CurrentVoltageChannel(sensorId, channelNo, initValue, outputRangeTop, outputRangeBottom, measureRangeTop, measureRangeBottom, type); channels.Add(channelNo, channel); } } } }
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"; } }
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"; } }