public void fill() { long StopBytes = 0; var watch = Stopwatch.StartNew(); long StartBytes = System.GC.GetTotalMemory(true); while (true) // work to end of times { line = file.ReadLine(); if (line == null) { Run = false; break; } ramec = new Ramec(line); listOfFrame.Add(ramec); } StopBytes = System.GC.GetTotalMemory(true); GC.KeepAlive(listOfFrame); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; MessageBox.Show("Loaded " + listOfFrame.Count + " items\n" + "Size is " + Math.Round(((long)(StopBytes - StartBytes)) / 1024.0 / 1024.0, 2) + " MB\n" + "Loading time is " + elapsedMs / 1000 + " sec", "Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information); }
void proc() { long counter = 0; switch (source) { case Source.File: { while (Run) // work to end of times { line = file.ReadLine(); if (line == null) { Run = false; break; } if (LogFile != null) { LogFile.WriteLine(line); } ramec = new Ramec(line); counter++; ParseFrame(ramec); } MessageBox.Show("Data načtena. (celkem " + counter + " CAN bus rámců)", "", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } case Source.SerialPort: { port.NewLine = "\r\n"; // define string of end of line (\r and \n) try { port.Open(); // try to open serial port } catch (Exception ex) // problem with opening serial port { throw new Exception("Problém s otevřením sériového portu. " + ex.Message); } while (Run) // work to end of times { ramec = new Ramec(port.ReadLine()); if (LogFile != null) { LogFile.WriteLine(line); } ParseFrame(ramec); } break; } default: break; } if (LogFile != null) { LogFile.Flush(); LogFile.Close(); } }
public void Test() { long StopBytes = 0; var watch = Stopwatch.StartNew(); long StartBytes = System.GC.GetTotalMemory(true); while (true) // work to end of times { line = file.ReadLine(); if (line == null) { Run = false; break; } ramec = new Ramec(line); Value value = CarInterface.GrepDataString(line); var result = ParseFrame(ramec); if ((result != null) && (result.GetType() == typeof(CAN_0x02C1))) { if (winkersPrevious != winkers) { value.data = winkers; winkerChangeList.Add(value); winkersPrevious = winkers; } } } GC.KeepAlive(listOfFrame); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; MessageBox.Show("Loaded " + winkerChangeList.Count + " items\n" + "Size is " + Math.Round(((long)(StopBytes - StartBytes)) / 1024.0, 2) + " kB\n" + "Loading time is " + elapsedMs / 1000 + " sec", "Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information); }
void Test() { // Few test CAN Bus packets String[] StrRamcu = new string[] { "0x02C3 11", "0x060E 08 01", "0xFFFF 00 01 02", "0x042B 19 01 00 00 00 00", "0x0000 00 01 02 03 04 05 06 07", "0x065F 00 00 00 00 00 54 4D 42", // VIN, first part "0x065F 01 42 30 30 30 30 30 35", // next part of VIN "0x065F 02 32 30 30 30 30 30 30", // and last part of VIN "0x065D F1 2E EE 02 00 D0 1C 09" // 192046 km, 13:14:18 //"0x065D 5E 62 38 02 00 00 E7 1D" // 16:51:59 }; if (FileName.Text == "") { OpenFiles(); // open files } else { OpenFiles(FileName.Text); } Whitelist = new UInt16[] { 0xFFFF }; // fill WhiteList by 0xFFFF (only 0xFFFF will by listed) Blacklist = new UInt16[] { 0x2C3 }; // fill BlackList by 0x2C3 (only 0x2C3 will be ignored) /*for (int i = 0; i < 10; i++) // test fill of data * {*/ foreach (string Radek in StrRamcu) { FilterInsert(Radek); Ramec ramec = new Ramec(Radek); string debug = ramec.ToStringTime(); Thread.Sleep(100); // slower, for GUI test } //}//*/ //listBox1.Items.Add(new Ramec(Radek));//Ramce.Add(new Ramec(Str)); }
bool FilterInsert(String Radek) // insert with filtering by WhiteList of BlackList if is enabled { Ramec ramec = new Ramec(Radek); // make new CAN Bus frame String retezec = ""; switch (ramec.ID) { case 0x470: // doors state // 0x470 00 XX 00 00 00 – kontakty dveří { break; } case 0x02C1: // winkers state // 0x2C1 0X 00 00 00 04 – blinkry { if ((ramec.Data[0] & 1) > 1) { LeftWinker = true; } if ((ramec.Data[0] & 2) > 1) { RightWinker = true; } break; } case 0x0571: // Batery Voltage // 0x571 XX 00 00 00 00 00 – napětí akumulátoru[V] { BateryVoltage = (ramec.Data[0] / 2 + 50) / 10; break; } case 0x351: // Speed // 0x351 00 XX YY 00 00 00 00 00 - rychlost { Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; break; } case 0x359: // Speed //0x359 00 XX YY 00 00 00 00 00 - rychlost { Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; break; } case 0x35B: // RPM, Cooling Temp // 0x35B 00 XX YY ZZ 00 00 00 – otáčky motoru + teplota vody { if (ramec.Data[0] != 0x07) { RPM = (UInt16)((ramec.Data[2] * 256 + ramec.Data[1]) / 4); CoolingTemp = ramec.Data[3] - 10; } break; } case 0x65F: // mh to bude VIN (oh, it's VIN, it need's special work) { byte[] pole; switch (ramec.Data[0]) // first part of VIN { case 0: pole = ramec.Data.Skip(5).Take(3).ToArray(); // take 3 bytes from 6st byte VIN = System.Text.Encoding.Default.GetString(pole); break; case 1: pole = ramec.Data.Skip(1).Take(7).ToArray(); // take 7 bytes from firts byte VIN = VIN.Substring(0, 3) + System.Text.Encoding.Default.GetString(pole); // replace midle part of VIN break; case 2: pole = ramec.Data.Skip(1).Take(7).ToArray(); // the same VIN = VIN.Substring(0, 3 + 7) + System.Text.Encoding.Default.GetString(pole); // replace last part of VIN Status.Invoke((MethodInvoker) delegate() // show VIN on GUI { Status.Text = "VIN: " + VIN + ", odo: " + Odometer + " km, Time: " + Time.ToShortTimeString(); }); break; default: break; } break; } case 0x065D: // oh, data from Odometer, date (sometimes) and time { Odometer = (UInt32)(ramec.Data[1] + ramec.Data[2] * 256 + (ramec.Data[3] & 0x0F) * 256 * 256); int Hour = (ramec.Data[5] & 0xF0) / 16 + (ramec.Data[6] & 1) * 16; int Min = (ramec.Data[6] & 0x7E) / 2; int Sec = (ramec.Data[7] & 0x1F) * 2 + (ramec.Data[6] & 0x80) / 128; int Year = ramec.Data[3] / 128 + (ramec.Data[4] & 0x07) * 128; int Month = (ramec.Data[4] & 0x78) / 8; int Day = (ramec.Data[4] & 0x80) / 128 + (ramec.Data[5] & 0x0F) * 2; if (Year == 0) { Year = 2000; // if date not supported by car the fill 01.01.2000 } if (Month == 0) { Month = 1; } if (Day == 0) { Day = 1; } Time = new DateTime(Year, Month, Day, Hour, Min, Sec); Status.Invoke((MethodInvoker) delegate() // show VIN on GUI { Status.Text = "VIN: " + VIN + ", odo: " + Odometer + " km, Time: " + Time.ToLongTimeString(); }); break; } default: break; } if (SouborRaw != null) // is file avaliable for writing? { // log all arrived CAN Bus data to file xxxRaw if (LogWithTime.Checked) { retezec = (LogWithMs.Checked ? ramec.ToStringTimeMs() : ramec.ToStringTime()); // log to file, with time of without } else { retezec = Radek.Replace(" ", "\t"); } SouborRaw.WriteLine(retezec); SouborRaw.Flush(); } if ((UseWhiteList.Checked && Whitelist.Contains(ramec.ID)) || // filter by WhiteList (UseBlackList.Checked && !Blacklist.Contains(ramec.ID)) || // filter by BlackList (!UseWhiteList.Checked && !UseBlackList.Checked)) // no filter selected { listBox1.Items.Add(ramec); // add item to listBox listBox1.SelectedIndex = listBox1.Items.Count - 1; // scroll to last item if (SouborFiltered != null) // is file avaliable for writing? { // write filter data only if (LogWithTime.Checked) { retezec = (LogWithMs.Checked ? ramec.ToStringTimeMs() : ramec.ToStringTime()); } else { retezec = Radek.Replace(" ", "\t"); } SouborFiltered.WriteLine(retezec); SouborFiltered.Flush(); } } return(false); }
void proc() { long counter = 0; switch (source) { case Source.File: { while (Run) // work to end of times { line = file.ReadLine(); if (line == null) { Run = false; break; } if (LogFile != null) LogFile.WriteLine(line); ramec = new Ramec(line); counter++; ParseFrame(ramec); } MessageBox.Show("Data načtena. (celkem " + counter + " CAN bus rámců)", "", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } case Source.SerialPort: { port.NewLine = "\r\n"; // define string of end of line (\r and \n) try { port.Open(); // try to open serial port } catch (Exception ex) // problem with opening serial port { throw new Exception("Problém s otevřením sériového portu. " + ex.Message); } while (Run) // work to end of times { ramec = new Ramec(port.ReadLine()); if (LogFile != null) LogFile.WriteLine(line); ParseFrame(ramec); } break; } default: break; } if (LogFile != null) { LogFile.Flush(); LogFile.Close(); } }
public object ParseFrame(Ramec ramec) { object result = null; switch (ramec.ID) { case 0x470: // doors state // 0x470 00 XX 00 00 00 – kontakty dveří { break; } case 0x02C1: // winkers state // 0x2C1 0X 00 00 00 04 – blinkry { winkers = WinkerState.None; if ((ramec.Data[0] & 1) > 1) winkers = WinkerState.LeftWinker; if ((ramec.Data[0] & 2) > 1) winkers = WinkerState.RightWinker; result = new CAN_0x02C1(winkers); break; } case 0x0571: // Batery Voltage // 0x571 XX 00 00 00 00 00 – napětí akumulátoru[V] { //BateryVoltage = (ramec.Data[0] / 2 + 50) / 10; result = new CAN_0x0571((ramec.Data[0] / 2 + 50) / 10); break; } case 0x351: // Speed // 0x351 00 XX YY 00 00 00 00 00 - rychlost { //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; speed = BitConverter.ToUInt16(ramec.Data, 1) / 201; result = new CAN_0x351(speed); break; } case 0x359: // Speed //0x359 00 XX YY 00 00 00 00 00 - rychlost { //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; speed = BitConverter.ToUInt16(ramec.Data, 1) / 201; result = new CAN_0x359(speed); break; } case 0x35B: // RPM, Cooling Temp // 0x35B 00 XX YY ZZ 00 00 00 – otáčky motoru + teplota vody { if (ramec.Data[0] != 0x07) { //RPM = (UInt16)((ramec.Data[2] * 256 + ramec.Data[1]) / 4); RPM = (UInt16)(BitConverter.ToUInt16(ramec.Data, 1) / 4); CoolingTemp = ramec.Data[3] - 10; result = new CAN_0x35B(RPM, CoolingTemp); } break; } case 0x054B: // get distance from parking sensors { distance.FrontLeft = ramec.Data[0]; distance.FrontRight = ramec.Data[1]; distance.RearLeft = ramec.Data[2]; distance.RearRight = ramec.Data[3]; distance.FrontLeft = ramec.Data[4]; distance.FrontRight = ramec.Data[5]; distance.RearLeft = ramec.Data[6]; distance.RearRight = ramec.Data[7]; result = new CAN_0x054B(distance); break; } case 0x65F: // mh to bude VIN (oh, it's VIN, it need's special work) { byte[] pole; switch (ramec.Data[0]) // first part of VIN { case 0: VIN_Ready = false; pole = ramec.Data.Skip(5).Take(3).ToArray(); // take 3 bytes from 6st byte VIN = System.Text.Encoding.Default.GetString(pole); result = new CAN_0x065F(VIN); break; case 1: if (VIN.Length >= 3) { pole = ramec.Data.Skip(1).Take(7).ToArray(); // take 7 bytes from firts byte string partOfVin = System.Text.Encoding.Default.GetString(pole); VIN = VIN.Substring(0, 3) + partOfVin; // replace midle part of VIN result = new CAN_0x065F(partOfVin); } break; case 2: if (VIN.Length >= (3 + 7)) { pole = ramec.Data.Skip(1).Take(7).ToArray(); // the same VIN = VIN.Substring(0, 3 + 7) + System.Text.Encoding.Default.GetString(pole); // replace last part of VIN VIN_Ready = true; result = new VIN_Code(VIN); } break; default: break; } break; } case 0x065D: // oh, data from Odometer, date (sometimes) and time { int Hour = (ramec.Data[5] & 0xF0) / 16 + (ramec.Data[6] & 1) * 16; int Min = (ramec.Data[6] & 0x7E) / 2; int Sec = (ramec.Data[7] & 0x1F) * 2 + (ramec.Data[6] & 0x80) / 128; int Year = ramec.Data[3] / 128 + (ramec.Data[4] & 0x07) * 128; int Month = (ramec.Data[4] & 0x78) / 8; int Day = (ramec.Data[4] & 0x80) / 128 + (ramec.Data[5] & 0x0F) * 2; if (Year == 0) Year = DateTime.Now.Year; // if date not supported by car the fill today date if (Month == 0) Month = DateTime.Now.Month; if (Day == 0) Day = DateTime.Now.Day; Time = new DateTime(Year, Month, Day, Hour, Min, Sec); //Odometer = (UInt32)(ramec.Data[1] + ramec.Data[2] * 256 + (ramec.Data[3] & 0x0F) * 256 * 256); ramec.Data[3] &= 0x0F; Odometer = BitConverter.ToUInt32(ramec.Data, 1); result = new CAN_0x065D(Time, Odometer); break; } default: break; } return result; }
public object ParseFrame(Ramec ramec) { object result = null; switch (ramec.ID) { case 0x470: // doors state // 0x470 00 XX 00 00 00 – kontakty dveří { break; } case 0x02C1: // winkers state // 0x2C1 0X 00 00 00 04 – blinkry { winkers = WinkerState.None; if ((ramec.Data[0] & 1) > 1) { winkers = WinkerState.LeftWinker; } if ((ramec.Data[0] & 2) > 1) { winkers = WinkerState.RightWinker; } result = new CAN_0x02C1(winkers); break; } case 0x0571: // Batery Voltage // 0x571 XX 00 00 00 00 00 – napětí akumulátoru[V] { //BateryVoltage = (ramec.Data[0] / 2 + 50) / 10; result = new CAN_0x0571((ramec.Data[0] / 2 + 50) / 10); break; } case 0x351: // Speed // 0x351 00 XX YY 00 00 00 00 00 - rychlost { //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; speed = BitConverter.ToUInt16(ramec.Data, 1) / 201; result = new CAN_0x351(speed); break; } case 0x359: // Speed //0x359 00 XX YY 00 00 00 00 00 - rychlost { //Speed = (ramec.Data[2] * 256 + ramec.Data[1]) / 201; speed = BitConverter.ToUInt16(ramec.Data, 1) / 201; result = new CAN_0x359(speed); break; } case 0x35B: // RPM, Cooling Temp // 0x35B 00 XX YY ZZ 00 00 00 – otáčky motoru + teplota vody { if (ramec.Data[0] != 0x07) { //RPM = (UInt16)((ramec.Data[2] * 256 + ramec.Data[1]) / 4); RPM = (UInt16)(BitConverter.ToUInt16(ramec.Data, 1) / 4); CoolingTemp = ramec.Data[3] - 10; result = new CAN_0x35B(RPM, CoolingTemp); } break; } case 0x054B: // get distance from parking sensors { distance.FrontLeft = ramec.Data[0]; distance.FrontRight = ramec.Data[1]; distance.RearLeft = ramec.Data[2]; distance.RearRight = ramec.Data[3]; distance.FrontLeft = ramec.Data[4]; distance.FrontRight = ramec.Data[5]; distance.RearLeft = ramec.Data[6]; distance.RearRight = ramec.Data[7]; result = new CAN_0x054B(distance); break; } case 0x65F: // mh to bude VIN (oh, it's VIN, it need's special work) { byte[] pole; switch (ramec.Data[0]) // first part of VIN { case 0: VIN_Ready = false; pole = ramec.Data.Skip(5).Take(3).ToArray(); // take 3 bytes from 6st byte VIN = System.Text.Encoding.Default.GetString(pole); result = new CAN_0x065F(VIN); break; case 1: if (VIN.Length >= 3) { pole = ramec.Data.Skip(1).Take(7).ToArray(); // take 7 bytes from firts byte string partOfVin = System.Text.Encoding.Default.GetString(pole); VIN = VIN.Substring(0, 3) + partOfVin; // replace midle part of VIN result = new CAN_0x065F(partOfVin); } break; case 2: if (VIN.Length >= (3 + 7)) { pole = ramec.Data.Skip(1).Take(7).ToArray(); // the same VIN = VIN.Substring(0, 3 + 7) + System.Text.Encoding.Default.GetString(pole); // replace last part of VIN VIN_Ready = true; result = new VIN_Code(VIN); } break; default: break; } break; } case 0x065D: // oh, data from Odometer, date (sometimes) and time { int Hour = (ramec.Data[5] & 0xF0) / 16 + (ramec.Data[6] & 1) * 16; int Min = (ramec.Data[6] & 0x7E) / 2; int Sec = (ramec.Data[7] & 0x1F) * 2 + (ramec.Data[6] & 0x80) / 128; int Year = ramec.Data[3] / 128 + (ramec.Data[4] & 0x07) * 128; int Month = (ramec.Data[4] & 0x78) / 8; int Day = (ramec.Data[4] & 0x80) / 128 + (ramec.Data[5] & 0x0F) * 2; if (Year == 0) { Year = DateTime.Now.Year; // if date not supported by car the fill today date } if (Month == 0) { Month = DateTime.Now.Month; } if (Day == 0) { Day = DateTime.Now.Day; } Time = new DateTime(Year, Month, Day, Hour, Min, Sec); //Odometer = (UInt32)(ramec.Data[1] + ramec.Data[2] * 256 + (ramec.Data[3] & 0x0F) * 256 * 256); ramec.Data[3] &= 0x0F; Odometer = BitConverter.ToUInt32(ramec.Data, 1); result = new CAN_0x065D(Time, Odometer); break; } default: break; } return(result); }