//private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) //{ // try // { // var message = serialPort.ReadTo("~") + "~"; // Debug.WriteLine(message); // logger.Trace("Received : {0}", message); // incommingQueue.Add(message); // } // catch (TimeoutException ex) // { // Debug.WriteLine(ex.Message); // logger.Warn(ex, "Time out exeption"); // } // catch (IOException ex) // { // Debug.WriteLine(ex.Message); // logger.Error(ex, "IO Expetion"); // } //} public void HandleParsing(string message) { try { string syncedMessage = SyncMessage(message); Debug.WriteLine("Synced msg = " + syncedMessage); if (string.IsNullOrWhiteSpace(syncedMessage)) { logger.Info("no synced message "); return; } FrameFormat frame = GenericParser.GenericParser.Parse <FrameFormat>(syncedMessage); if (null == frame) { return; } var subStr = syncedMessage.TrimStart(new char[] { FrameFormat.SOI }).TrimEnd(new char[] { FrameFormat.EOI }); subStr = subStr.Substring(0, subStr.Length - 2); var CRCResultStr = FrameFormat.CalculateCRC(subStr); byte CalculatedCRC = byte.Parse(CRCResultStr, System.Globalization.NumberStyles.HexNumber); if (CalculatedCRC != frame.CRC) { logger.Error("CRC not compatible, original message = " + message); return; } logger.Trace(frame.ToStringWithAtt <ParserDefinitionAttribute>()); switch (frame.Version) { case (byte)GenericParser.Version.Version82: ParseCommand(frame); break; default: break; } } catch (Exception ex) { //evAgg.PublishOnUIThread(new MessageBoxMessage() { Title = "Error", Message = ex.Message }); //MessageBox.Show(this, ex.Message, "Error"); logger.Error(ex, "Error parsing incomming message"); } }
//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(); //} }