private static void UpdateViewModel(RealtimeDataMap_V82 realTimeData, BatteryStatViewModel vm) { if (realTimeData.Current[1] != 0 && realTimeData.Current[0] != 0) { vm.Current = 0; } else { vm.Current = realTimeData.Current[0] == 0 ? (realTimeData.Current[1] * (-1)) : realTimeData.Current[0]; } vm.SOC = realTimeData.SOC; vm.Temperature = realTimeData.Temp.Max(); vm.Voltage = realTimeData.Vbat; vm.CFET = (realTimeData.FETState & (byte)FETSTATE.CFET) == (byte)FETSTATE.CFET; vm.DFET = (realTimeData.FETState & (byte)FETSTATE.DFET) == (byte)FETSTATE.DFET; vm.ChargeState = realTimeData.CState; vm.TemperatureState = realTimeData.TState; vm.VoltageState = realTimeData.VState; string protection = string.Empty; Color backColor = SystemColors.Control; Color foreColor = Color.Black; if (vm.VoltageState == 0) { vm.VoltageStateTxt = string.Empty; vm.VoltageForeColor = Color.Black; } else { vm.VoltageStateTxt = ((VSTATE)vm.VoltageState).ToEnumDescription(); vm.VoltageForeColor = (System.Drawing.Color)((VSTATE)vm.VoltageState).ToEnumDefaultValue(); } if (vm.ChargeState == 0) { vm.CurrentStateTxt = string.Empty; vm.CurrentForeColor = Color.Black; } else { vm.CurrentStateTxt = ((CSTATE)vm.ChargeState).ToEnumDescription(); vm.CurrentForeColor = (System.Drawing.Color)((CSTATE)vm.ChargeState).ToEnumDefaultValue(); } if (vm.TemperatureState == 0) { vm.TemperatureStateTxt = string.Empty; vm.TemperatureForeColor = Color.Black; } else { vm.TemperatureStateTxt = ((TSTATE)vm.TemperatureState).ToEnumDescription(); vm.TemperatureForeColor = (System.Drawing.Color)((TSTATE)vm.TemperatureState).ToEnumDefaultValue(); } Debug.WriteLine(vm.ToString()); logger.Trace(vm.ToStringAllProperties()); }
private void ParseCommand(FrameFormat frame) { try { switch (frame.Cmd) { case (byte)CommandResponse.RealTimeData: RealtimeDataMap_V82 realTimeData = GenericParser.GenericParser.Parse <RealtimeDataMap_V82>(frame.Data); if (null == realTimeData) { return; } HandleRealTimeData(frame, realTimeData); logger.Trace(realTimeData.ToStringWithAtt <ParserDefinitionAttribute>()); break; default: break; } } catch (Exception e) { logger.Error(e, "Error parsing realtime data"); } }
public string SendReceive(string data) { Debug.WriteLine("Pack Received : " + data); var ff = GenericParser.GenericParser.Parse <FrameFormat>(data); frameFormat.Address = ff.Address; RealtimeDataMap_V82 newRt = propertyGrid1.SelectedObject as RealtimeDataMap_V82; var rtAsStr = newRt.AsString; // //if(!batteries.ContainsKey(ff.Address.ToString())) // //{ // // //batteries.Add(ff.Address.ToString(),rt); //// } // //foreach (var item in batteries) // //{ // // // } frameFormat.Data = rtAsStr; // //return @":058252008A0000000000000030E2070DC90DFF0DFF0DFF0E000E000DFF00000000053E3D3E3D3E00000000000000000F00000000000000000000000000002000AF02BC54~"; var str = frameFormat.AsString; return(str); // Debug.WriteLine("Pack Send : " + str); }
private void HandleRealTimeData(FrameFormat frame, RealtimeDataMap_V82 realTimeData) { try { BatteryStatViewModel vm; if (SharedData.Default.BatteryPackContainer.TryGetValue(frame.Address.ToString(), out vm)) { UpdateViewModel(realTimeData, vm); } } catch (Exception e) { logger.Error(e, "Error updateing view model"); } }
public Form1() { InitializeComponent(); rt = new RealtimeDataMap_V82() { Alarm = 0, BalanceState = 0, CapFull = 700, CapNow = 175, ChgNum = 0, CState = 0, Current = new ushort[] { 0, 0 }, DchgNum = 0, FETState = 15, SOC = 32, Temp = new byte[] { 62, 61, 62, 61, 62 }, TempNum = 5, Time_t = 0, TState = 0, Vbat = 12514, VCell = new ushort[] { 3529, 3583, 3583, 3583, 3584, 3584, 3583 }, VCell_num = 7, VState = 0, Warn_VHigh = 0, Warn_VLow = 0, Warn_VOV = 0, Warn_VUV = 0 }; frameFormat = new FrameFormat() { Address = 0, Cmd = (byte)CommandResponse.RealTimeData, Version = (byte)GenericParser.Version.Version82 }; }
public string SendReceive(string data) { Debug.WriteLine("Pack Received : " + data); var ff = GenericParser.GenericParser.Parse <FrameFormat>(data); var rt = new RealtimeDataMap_V82() { Alarm = 0, BalanceState = 0, CapFull = 700, CapNow = 175, ChgNum = 0, CState = 0, Current = new ushort[] { 0, 0 }, DchgNum = 0, FETState = 15, SOC = 32, Temp = new byte[] { 62, 61, 62, 61, 62 }, TempNum = 5, Time_t = 0, TState = 0, Vbat = 12514, VCell = new ushort[] { 3529, 3583, 3583, 3583, 3584, 3584, 3583 }, VCell_num = 7, VState = 0, Warn_VHigh = 0, Warn_VLow = 0, Warn_VOV = 0, Warn_VUV = 0 }; FrameFormat fff = new FrameFormat() { Address = ff.Address, Cmd = (byte)Command.RealTimeData, Version = (byte)GenericParser.Version.Version82, Data = rt.AsString }; // //if(!batteries.ContainsKey(ff.Address.ToString())) // //{ // // //batteries.Add(ff.Address.ToString(),rt); //// } // //foreach (var item in batteries) // //{ // // // } // //return @":058252008A0000000000000030E2070DC90DFF0DFF0DFF0E000E000DFF00000000053E3D3E3D3E00000000000000000F00000000000000000000000000002000AF02BC54~"; return(fff.ToString()); // Debug.WriteLine("Pack Send : " + str); return(string.Empty); }
private static BatteryStatViewModel HandleRealTimeData(RealtimeDataMap_V82 realTimeData) { BatteryStatViewModel vm = new BatteryStatViewModel(WindowsFormsSynchronizationContext.Current); //if (SharedData.Default.BatteryPackContainer.TryGetValue(frame.Address.ToString(), out vm)) //{ if (realTimeData.Current[1] != 0 && realTimeData.Current[0] != 0) { vm.Current = 0; } else { vm.Current = realTimeData.Current[0] == 0 ? (realTimeData.Current[1] * (-1)) : realTimeData.Current[0]; } vm.SOC = realTimeData.SOC; vm.Temperature = realTimeData.Temp.Max(); vm.Voltage = realTimeData.Vbat; vm.CFET = (realTimeData.FETState & (byte)FETSTATE.CFET) == (byte)FETSTATE.CFET; vm.DFET = (realTimeData.FETState & (byte)FETSTATE.DFET) == (byte)FETSTATE.DFET; vm.ChargeState = realTimeData.CState; vm.TemperatureState = realTimeData.TState; vm.VoltageState = realTimeData.VState; if (vm.VoltageState == 0) { vm.Protection = string.Empty; //vm.ProtectionBackColor = Color.Transparent; } else if ((vm.VoltageState & (ushort)VSTATE.VUV) == (ushort)VSTATE.VUV) { vm.Protection = "Single cell undervoltage"; //vm.ProtectionBackColor = Color.Red; } else if ((vm.VoltageState & (ushort)VSTATE.BVUV) == (ushort)VSTATE.BVUV) { vm.Protection = "Battery pack undervoltage "; //vm.ProtectionBackColor = Color.Red; } else { vm.Protection = ((VSTATE)vm.VoltageState).ToEnumDescription(); //vm.ProtectionBackColor = Color.Orange; } if (vm.ChargeState == 0) { vm.Protection = string.Empty; //vm.ProtectionBackColor = Color.Transparent; } else { vm.Protection = ((CSTATE)vm.ChargeState).ToEnumDescription(); //vm.ProtectionBackColor = Color.Orange; } if (vm.TemperatureState == 0) { vm.Protection = string.Empty; //vm.ProtectionBackColor = Color.Transparent; } else { vm.Protection = ((TSTATE)vm.TemperatureState).ToEnumDescription(); //vm.ProtectionBackColor = Color.Orange; } Debug.WriteLine(vm.ToString()); //} return(vm); }
//Address=5 Voltage=25.028 Current=0 Temp=22 SOC=32 DFET=True CFET=True Protection= ChargeState=0 TempState=0 VoltState=0 static void Main(string[] args) { string s6 = "010252000E"; var r6 = FrameFormat.CalculateCRC(s6); var frameFormat1 = new FrameFormat() { Address = 1, Cmd = (byte)Command.RealTimeData, Version = (byte)GenericParser.Version.Version82 }; var frameFormat1Str = frameFormat1.AsString; var res = FrameFormat.CalculateCRC("ABC"); FrameFormat a6 = GenericParser.GenericParser.Parse <FrameFormat>(realTimeData82_4, DataType.ASCII_HEX); RealtimeDataMap_V82 rtd6 = GenericParser.GenericParser.Parse <RealtimeDataMap_V82>(a6.Data); var s = rtd6.ToStringAllProperties(); FrameFormat realTimeCmd = new FrameFormat() { Address = Convert.ToByte(5), Cmd = 2, Version = 82 }; FrameFormat.SOI = ':'; FrameFormat.EOI = '~'; var rt = new RealtimeDataMap_V82() { Alarm = 0, BalanceState = 0, CapFull = 700, CapNow = 175, ChgNum = 0, CState = 0, Current = new ushort[] { 0, 0 }, DchgNum = 0, FETState = 15, SOC = 32, Temp = new byte[] { 62, 61, 62, 61, 62 }, TempNum = 5, Time_t = 0, TState = 0, Vbat = 12514, VCell = new ushort[] { 3529, 3583, 3583, 3583, 3584, 3584, 3583 }, VCell_num = 7, VState = 0, Warn_VHigh = 0, Warn_VLow = 0, Warn_VOV = 0, Warn_VUV = 0 }; var frameFormat = new FrameFormat() { Address = 0, Cmd = (byte)CommandResponse.RealTimeData, Version = (byte)GenericParser.Version.Version82 }; for (int i = 0; i < 2; i++) { //var inStr = @":050252000E00~"; var inStr = realTimeCmd.ToString(); var ff = GenericParser.GenericParser.Parse <FrameFormat>(inStr); frameFormat.Address = ff.Address; var rtAsStr = rt.AsString; frameFormat.Data = rtAsStr; // return @":058252008A0000000000000030E2070DC90DFF0DFF0DFF0E000E000DFF00000000053E3D3E3D3E00000000000000000F00000000000000000000000000002000AF02BC54~"; var str = frameFormat.AsString; Console.WriteLine(str); } return; //var res = GenericParser.GenericParser.Parse<FrameFormat>(realTimeData82_5); //var rt = GenericParser.GenericParser.Parse<RealtimeDataMap_V82>(res.Data); //var rtStr = GenericParser.GenericParser.Build<RealtimeDataMap_V82>(rt); //int p = 0; //#region //var test1 = (System.Drawing.Color)VSTATE.BVOV.ToEnumDefaultValue(); //string first = "008A0000000000000030E7070DF80DF90DF90DF90DF90DF90DF900000000053D3D3C3D3D00000000000000000F00000000000000000000000000000F00D202BC39~:04825200840000000000000030DD070DF60DF60"; //string sec = "DF60DF60DF60DF60DF600000000023E3E00000000000000000F00000000000000000000000000001E00EA030CC2~:0A82520084000000000000003"; //string third = "0F1070DFB0DFB0DFA0DFB0DFE0DFD0DFD00000000023E3D00000000000000000F00000000000000000000000000001E00EA030C6D~:098252008A0000000000000030D8070DF50DF3"; //MainLogic logic = new MainLogic(); //logic.HandleParsing(first); //logic.HandleParsing(sec); //logic.HandleParsing(third); //return; //#endregion #region //Configuration conf = new Configuration() //{ // PortName = "COM3", // BaudRate = 9600, // ParityType = Parity.None, // DataBits = 8, // StopBitsType = StopBits.One, // HandShakeType = Handshake.XOnXOff, // ReadTimeout = 500, // WriteTimeout = 500 //}; //using (var stream = File.OpenWrite("configuration.json")) //{ // DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Configuration)); // ser.WriteObject(stream, conf); //} // PortChat.Start(); //#region TESt //var portNames = SerialPort.GetPortNames(); //StringBuilder sb = new StringBuilder(); //for (int i = 0; i < portNames.Length; i++) //{ // sb.Append(i + 1 + ": "); // sb.Append(portNames[i]); //} //Console.WriteLine(sb.ToString()); //Console.WriteLine("Please Enter port number :"); //var input = Console.ReadLine(); //var choice = int.Parse(input); //Console.WriteLine(string.Format("port chosen - {0}, Connecting...", portNames[choice - 1])); //System.IO.Ports.SerialPort port = new SerialPort(portNames[choice - 1], 9600, Parity.None, 8, StopBits.One); //port.Handshake = Handshake.XOnXOff; //Console.WriteLine("Connected !"); //Console.WriteLine("Press enter to send Data..."); //Console.ReadLine(); ////string data = ":000264000EFE~"; //string data = ":000100000E09~"; //// Create two different encodings. //Encoding ascii = Encoding.ASCII; //Encoding unicode = Encoding.Unicode; //// Convert the string into a byte array. //byte[] unicodeBytes = unicode.GetBytes(data); //// Perform the conversion from one encoding to the other. //byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes); //// Convert the new byte[] into a char[] and then into a string. //char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)]; //ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0); //string asciiString = new string(asciiChars); //Console.WriteLine("Sending :" + asciiString); //port.ReadBufferSize = 500; //port.WriteBufferSize = 500; //port.ReadTimeout = 5000; //port.WriteTimeout = 5000; //port.Open(); //port.DataReceived += Port_DataReceived; //port.WriteLine(asciiString); //#endregion #endregion //#region //ICommPort port = new CommPortMock(); //port.InitializePort("COM3"); //port.Open(); //FrameFormat realTimeCmd = new FrameFormat() //{ // Address = Convert.ToByte("1"), // Cmd = 2, // Version = 82 //}; //var r1 = realTimeCmd.AsString; //Console.WriteLine(r1); //FrameFormat test = new FrameFormat() //{ // Address = 4, // Cmd = (byte)Command.RealTimeData, // Version = (byte)GenericParser.Version.Version82 //}; //Console.WriteLine(test.AsString); //if (port.IsOpen) //{ // port.SendWrite(test.AsString); // Thread.Sleep(50); //} ////nsole.ReadKey(); //for (int i = 0; i < 100; i++) //{ // if (port.IsOpen) // { // var sw = Stopwatch.StartNew(); // sw.Start(); // var result = port.SendReceive(test.AsString); // sw.Stop(); // Console.WriteLine(sw.ElapsedMilliseconds + " " + result); // } // Thread.Sleep(250); //} //Console.ReadLine(); //return; //#endregion #region RealtimeDataMap_V82 rtm1 = new RealtimeDataMap_V82() { Current = new ushort[] { 0, 0 }, SOC = 90, TempNum = 2, Temp = new byte[] { 68, 72 }, Vbat = 24000 }; RealtimeDataMap_V82 rtm2 = new RealtimeDataMap_V82() { Current = new ushort[] { 0, 0 }, SOC = 29, TempNum = 2, Temp = new byte[] { 55, 65 }, Vbat = 18000 }; var r = (string)GenericParser.GenericParser.Build <RealtimeDataMap_V82>(rtm1); FrameFormat b = new FrameFormat() { Address = 4, Cmd = (byte)Command.RealTimeData, Data = r, Version = (byte)GenericParser.Version.Version82, Length = 138 }; var conf = Common.Configuration.Default; conf.Load(); //Console.ReadKey(); FrameFormat a3 = GenericParser.GenericParser.Parse <FrameFormat>(realTimeData82_4, DataType.ASCII_HEX); FrameFormat a5 = GenericParser.GenericParser.Parse <FrameFormat>(realTimeData82_5, DataType.ASCII_HEX); //a.ValidateCRC(realTimeData); RealtimeDataMap_V82 r3 = GenericParser.GenericParser.Parse <RealtimeDataMap_V82>(a3.Data); RealtimeDataMap_V82 r5 = GenericParser.GenericParser.Parse <RealtimeDataMap_V82>(a5.Data); BatteryStatViewModel vm3 = HandleRealTimeData(r3); BatteryStatViewModel vm5 = HandleRealTimeData(r5); BatteryStatViewModel vm4 = HandleRealTimeData(rtm1); BatteryStatViewModel vm6 = HandleRealTimeData(rtm2); var vmGroup1 = new List <BatteryStatViewModel>(); vmGroup1.Add(vm3); vmGroup1.Add(vm4); var group1 = new SeriesStatViewModel(WindowsFormsSynchronizationContext.Current, "Group1", vmGroup1); var vmGroup2 = new List <BatteryStatViewModel>(); vmGroup2.Add(vm5); vmGroup2.Add(vm6); var group2 = new SeriesStatViewModel(WindowsFormsSynchronizationContext.Current, "Group2", vmGroup2); group1.UpdateSeriesProperties(); group2.UpdateSeriesProperties(); List <SeriesStatViewModel> sgroup = new List <SeriesStatViewModel>(); sgroup.Add(group1); sgroup.Add(group2); var cluster = new ClusterStatViewModel(WindowsFormsSynchronizationContext.Current, sgroup); cluster.UpdateProperties(); //:000264000EFE~ Console.ReadLine(); return; #endregion //FrameFormat b = new FrameFormat() //{ // SOI = ':', // Address = 3, // Cmd = (byte)Command.RealTimeData, // Version = (byte)GenericParser.Version.Version82, // EOI = '~' //}; //var s = b.AsString; //Encoding ascii = Encoding.ASCII; //Encoding unicode = Encoding.Unicode; //// Convert the string into a byte array. //byte[] unicodeBytes = unicode.GetBytes(s); //// Perform the conversion from one encoding to the other. //byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes); //// Convert the new byte[] into a char[] and then into a string. //char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)]; //ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0); //string asciiString = new string(asciiChars); //using (SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One)) //{ // Task.Run(() => // { // port.Open(); // port.DataReceived += (sender, e) => // { // var data = port.ReadTo("~"); // data += "~"; // Debug.WriteLine("Received : " + data); // FrameFormat a1 = GenericParser.GenericParser.Parse<FrameFormat>(data, DataType.ASCII_HEX); // RealtimeDataMap_V82 r1 = GenericParser.GenericParser.Parse<RealtimeDataMap_V82>(a.Data); // }; // Debug.WriteLine("Sending : " + asciiString); // port.Write(asciiChars,0,asciiChars.Length); // Thread.Sleep(250); // port.Write(asciiChars, 0, asciiChars.Length); // }); // Console.ReadKey(); //} }