Exemplo n.º 1
0
        private void ProcessMessage(string message)
        {
            var datas = message.Split(' '); // 前三个为地址+功能码+应答字节数,后两个为CRC校验

            if (datas.Length != 101)        // 格式不对
            {
                LogHelper.WriteLog(LogType.ERROR, "数据格式不对,跳过此段数据!");
                return;
            }
            LogHelper.WriteLog(LogType.INFO, string.Format("开始处理地址{0}的数据...", datas[0]));
            var tempBattery = this.batteryList.Find(i => i.address == datas[0]);
            // 电压参考系数
            var coefficient = 0.0;

            if (tempBattery != null)
            {
                coefficient = tempBattery.coefficient;
            }
            if (coefficient < 0.001)
            {
                coefficient = 1.0;
            }
            // 电压数据
            List <double> voltageList = new List <double>();

            for (var i = 3; i < 51; i += 2)
            {
                var hexStr = datas[i] + datas[i + 1];
                var vNum   = Int32.Parse(hexStr, System.Globalization.NumberStyles.HexNumber);
                // 乘以参考系数并放大1000倍
                voltageList.Add(vNum * coefficient / 1000.0);
            }

            // 保存到本地csv
            var     curObj  = new List <object>();
            DataRow dr      = dataTable.NewRow();
            var     curTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            dr[0] = curTime;
            dr[1] = datas[0];
            curObj.Add(curTime);
            curObj.Add(datas[0]);
            for (int j = 0; j < voltageList.Count; j++)
            {
                dr[j + 2] = voltageList[j];
                curObj.Add(voltageList[j]);
            }
            for (var j = 0; j < Param.CURRENT_VOLTAGE_DATA.Count; j++)
            {
                if (Param.CURRENT_VOLTAGE_DATA[j][1].ToString() == datas[0])
                {
                    Param.CURRENT_VOLTAGE_DATA.RemoveAt(j);
                    break;
                }
            }
            Param.CURRENT_VOLTAGE_DATA.Add(curObj.ToArray());
            dataTable.Rows.Add(dr);
            CSVFileHelper.SaveCSV(dataTable, dataFile);
            LogHelper.WriteLog(LogType.ERROR, "数据处理完毕!采集到的电压为:" + string.Join(" ", voltageList));
        }
Exemplo n.º 2
0
        private void SaveToStore()
        {
            var     list  = batteryList;
            DataRow dr    = dataTable.NewRow();
            int     index = 0;

            foreach (var item in list)
            {
                var curObj = this.lastestData.FindLast(i => i[1].ToString() == item.address);
                if (curObj != null)
                {
                    dr["采集时间"] = curObj[0];
                    for (var i = 2; i < curObj.Count(); i++)
                    {
                        dr[++index] = curObj[i];
                    }
                }
            }
            dataTable.Rows.Add(dr);
            CSVFileHelper.SaveCSV(dataTable, dataFile);
        }
Exemplo n.º 3
0
        public TaskRT(List <Battery> batteryList, PortConfig portConfig)
        {
            this.batteryList = batteryList.Where(i => i.isEnabled == "是").OrderBy(i => i.uid).ToList();
            this.portConfig  = portConfig;

            // 数据表结构
            dataTable = CSVFileHelper.OpenCSV(dataFile);
            if (dataTable == null)
            {
                dataTable = new DataTable();
                DataColumn dc = new DataColumn("采集时间");
                dataTable.Columns.Add(dc);

                int len = this.batteryList.Count * 24;
                for (var i = 0; i < len; i++)
                {
                    DataColumn col = new DataColumn("单体电压" + (i + 1));
                    dataTable.Columns.Add(col);
                }
                CSVFileHelper.SaveCSV(dataTable, dataFile);
            }
        }
Exemplo n.º 4
0
        public Task(List <Battery> batteryList, PortConfig portConfig)
        {
            this.batteryList = batteryList;
            this.portConfig  = portConfig;

            // 数据表结构
            dataTable = CSVFileHelper.OpenCSV(dataFile);
            if (dataTable == null)
            {
                dataTable = new DataTable();
                DataColumn dc = new DataColumn("采集时间");
                dataTable.Columns.Add(dc);
                DataColumn dc2 = new DataColumn("地址");
                dataTable.Columns.Add(dc2);

                for (var i = 0; i < 24; i++)
                {
                    DataColumn col = new DataColumn("单体电压" + (i + 1));
                    dataTable.Columns.Add(col);
                }
                CSVFileHelper.SaveCSV(dataTable, dataFile);
            }
        }