public static int GetRGBColor(Color ColorSelected) { string R = NumberBaseConverter.Dec2Hex(Convert.ToInt32(ColorSelected.R)); string G = NumberBaseConverter.Dec2Hex(Convert.ToInt32(ColorSelected.G)); string B = NumberBaseConverter.Dec2Hex(Convert.ToInt32(ColorSelected.B)); if (R.Length < 2) { R = "0" + R; } if (G.Length < 2) { G = "0" + G; } if (B.Length < 2) { B = "0" + B; } string RGB = R + G + B; return(Convert.ToInt32(NumberBaseConverter.Hex2Dec(RGB))); }
private static String ConvertToString(byte[] bytes, HashFormat hashFormat) { if (bytes == null || bytes.Length == 0) { return(null); } switch (hashFormat) { case HashFormat.Base64: return(Convert.ToBase64String(bytes)); case HashFormat.Binary: { var sb = new StringBuilder(); foreach (var b in bytes) { var bin = Convert.ToString(b, 2).PadLeft(8, '0'); sb.Append(bin); } return(sb.ToString()); } case HashFormat.Bytes: { return(String.Join(",", bytes)); } case HashFormat.Dec: { var binaryNumber = ConvertToString(bytes, HashFormat.Binary); return(NumberBaseConverter.ChangeBase(binaryNumber, 2, 10)); } case HashFormat.Hex: { using (var writer = new StringWriter()) { writer.Write("0x"); foreach (var b in bytes) { writer.Write("{0:x2}", b); } return(writer.ToString()); } } } return(null); }
/// <summary> /// Create the graphic data file of the current cycle for the Ctrl_GraphWindow control /// </summary> /// <returns>Ctrl_GraphWindow data file</returns> public GW_DataFile CreateCycleGraphData() { GW_DataFile oGraphData = new GW_DataFile(); oGraphData.DataSamplingMode = SamplingMode.MultipleRates; if (!(oCanNodesMap == null)) { foreach (CycleTimeEvent TxEvent in TimeEvents) { foreach (CANMessageData MsgData in TxEvent.MessagesData) { CANMessage MsgCfg = oCanNodesMap.GetCANMessage(NumberBaseConverter.Dec2Hex(MsgData.uMessageId), MessageResearchOption.Identifier); if (!(MsgCfg == null)) { TPCANMsg sTPMsg = new TPCANMsg(); sTPMsg.DATA = MsgData.byteMessageData; CANMessageDecoded oMsgDecoded = new CANMessageDecoded(MsgCfg, sTPMsg); if (oMsgDecoded.bMessageDecoded) { foreach (CANParameter oParam in oMsgDecoded.Parameters) { GW_DataChannel oGraphChan = oGraphData.Get_DataChannel(oParam.Name); if (oGraphChan == null) { oGraphChan = new GW_DataChannel(oParam.Name, SamplingMode.MultipleRates); oGraphData.Channels.Add(oGraphChan); } SerieSample sSample = new SerieSample(); sSample.SampleTime = (double)TxEvent.TimeEvent / 1000; sSample.SampleValue = oParam.DecodedValue; oGraphChan.Add_ChannelValue(sSample); } } } } } } return(oGraphData); }
/// <summary> /// Returns all bytes of the record data in a single value in binary format /// </summary> /// <returns>binary of record data</returns> public string GetRecordBinaryData() { string BinValue = ""; for (int iByte = 0; iByte < MessageData.Length; iByte++) { string ByteBinValue = NumberBaseConverter.Dec2Bin(MessageData[iByte]); while (ByteBinValue.Length < 8) { ByteBinValue = "0" + ByteBinValue; } BinValue = BinValue + ByteBinValue; } return(BinValue); }
public static string[] GetBinaryRowData(int ParamLength, long lRowValue) { string sBinValue = NumberBaseConverter.Dec2Bin(lRowValue); while (sBinValue.Length < ParamLength) { sBinValue = "0" + sBinValue; } string[] BinRowValue = new string[ParamLength]; int i = ParamLength - 1; while (i >= 0) { BinRowValue[i] = sBinValue.Substring(i, 1); i--; } return(BinRowValue); }
/// <summary> /// Trace file (*.trc) reading function /// </summary> /// <param name="FilePath">Trace file path</param> /// <param name="HeaderOnly">Header only reading option</param> /// <returns>File reading result (True: No error /False: Error)</returns> public bool ReadTrcFile(string FilePath, bool HeaderOnly) { bool Ret = true; BaseTrcFilePath = FilePath; StreamReader SR = new StreamReader(FilePath); try { while (!(SR.EndOfStream)) { string Line = SR.ReadLine(); if (Line.StartsWith(";")) //Header { if (Line.Contains("$FILEVERSION")) { int i = Line.IndexOf("=") + 1; FileVersion = Line.Substring(i, Line.Length - i); } if (Line.Contains("$STARTTIME")) { int i = Line.IndexOf("=") + 1; string StrStartTime = Line.Substring(i, Line.Length - i); double DblStartTime = 0; if (Double.TryParse(StrStartTime, out DblStartTime)) { AbsDblStartTime = DblStartTime; DblStartTime = (double)((int)DblStartTime); DateTime Dec30th1899 = new DateTime(1899, 12, 30); AbsDTStartTime = Dec30th1899.AddDays(DblStartTime); AbsDTStartTime = AbsDTStartTime.AddMilliseconds((AbsDblStartTime - DblStartTime) * 86400000); //86400000 millisecond in 1 day } } if (Line.Contains("Start time:")) { int i = Line.IndexOf(":") + 2; string StrStartTime = Line.Substring(i, Line.Length - i); string[] SplitedStartTime = StrStartTime.Split(Char.Parse(" ")); string[] SplitedDate = SplitedStartTime[0].Split(Char.Parse("/")); string[] SplitedTime = SplitedStartTime[1].Split(Char.Parse(":")); string[] SplitedSecond = SplitedTime[2].Split(Char.Parse(".")); StartTime = StartTime.AddYears(int.Parse(SplitedDate[2]) - 1); StartTime = StartTime.AddMonths(int.Parse(SplitedDate[1]) - 1); StartTime = StartTime.AddDays(Double.Parse(SplitedDate[0]) - 1); StartTime = StartTime.AddHours(Double.Parse(SplitedTime[0])); StartTime = StartTime.AddMinutes(Double.Parse(SplitedTime[1])); StartTime = StartTime.AddSeconds(Double.Parse(SplitedSecond[0])); StartTime = StartTime.AddMilliseconds(Double.Parse(SplitedSecond[1])); } if (Line.Contains("PCAN-Channel:")) { int i = Line.IndexOf(":") + 2; PCAN_Channel = Line.Substring(i, Line.Length - i); } } else //Data { if (HeaderOnly) { break; //Loop exit if header reading only option is set } TraceRecord oRecord = new TraceRecord(); string[] StrRecord = Line.Split(Char.Parse(" ")); //Remove empty cells { //Count empty cells int EmptyCnt = 0; for (int i = 0; i < StrRecord.Length; i++) { if (StrRecord[i].Equals("")) { EmptyCnt++; } } //Create a new temporary array sized to the non empty cells count string[] TmpArray = new string[StrRecord.Length - EmptyCnt]; //Fill the temporary array int j = 0; for (int i = 0; i < StrRecord.Length; i++) { if (!(StrRecord[i].Equals(""))) { TmpArray[j] = StrRecord[i]; j++; } } //Update the final array StrRecord = TmpArray; TmpArray = null; } oRecord.RecordIndex = long.Parse(StrRecord[0].Substring(0, StrRecord[0].Length - 1)); oRecord.TimeOffset = double.Parse(StrRecord[1]); CanMsgRxTx Test; if (Enum.TryParse(StrRecord[2], out Test)) { oRecord.MessageType = Test; oRecord.MessageIdentifier = StrRecord[3]; //Remove not necessary 0 at the begining of identifier if (oRecord.MessageIdentifier.StartsWith("0")) { while (oRecord.MessageIdentifier.StartsWith("0")) { oRecord.MessageIdentifier = oRecord.MessageIdentifier.Substring(1, oRecord.MessageIdentifier.Length - 1); } } oRecord.MessageLength = int.Parse(StrRecord[4]); oRecord.MessageData = new byte[oRecord.MessageLength]; for (int i = 0; i < oRecord.MessageLength; i++) { oRecord.MessageData[i] = (byte)(NumberBaseConverter.Hex2Dec(StrRecord[i + 5])); } Records.Add(oRecord); } } } } catch { Ret = false; } SR.Close(); SR = null; return(Ret); }
public NumberBaseConverterTests() { _numberBaseConverter = new NumberBaseConverter(); }
private void ImportTrcFiles(BackgroundWorker Worker, object ImportOptions) { if (!(ImportOptions == null)) { object[] Options = (object[])ImportOptions; if (Options.Length == 3) { string[] TrcFileList = (string[])Options[0]; CANMessagesConfiguration oCanCfg = (CANMessagesConfiguration)Options[1]; string CyclePath = (string)Options[2]; CANStreamCycle oCycle = new CANStreamCycle(); oCycle.oCanNodesMap = oCanCfg; long CycleTime = 0; foreach (string TrcFile in TrcFileList) { PcanTrcFile oTrace = new PcanTrcFile(TrcFile); long TraceTime = 0; int iRecord = 0; int Progress = 0; Worker.ReportProgress(Progress); while (iRecord < oTrace.Records.Count) { CycleTimeEvent oCycleTxEvt = new CycleTimeEvent(); oCycleTxEvt.TimeEvent = CycleTime; while ((long)oTrace.Records[iRecord].TimeOffset == TraceTime) { CANMessageData oMsgData = new CANMessageData(); oMsgData.uMessageId = (uint)(NumberBaseConverter.Hex2Dec(oTrace.Records[iRecord].MessageIdentifier)); oMsgData.byteMessageData = oTrace.Records[iRecord].MessageData; oCycleTxEvt.MessagesData.Add(oMsgData); iRecord++; if (iRecord == oTrace.Records.Count) { break; } } if (oCycleTxEvt.MessagesData.Count > 0) { oCycle.TimeEvents.Add(oCycleTxEvt); } TraceTime++; CycleTime++; Progress = (int)(iRecord * 100 / oTrace.Records.Count); if (Progress > 0) { Worker.ReportProgress(Progress); } } } oCycle.WriteStreamCycle(CyclePath); } } }
/// <summary> /// Convert the DBC object into a CANStream CAN configuration object /// </summary> /// <param name="Rate">CAN bus Baud rate</param> /// <param name="CanNodeName">CAN device name</param> /// <returns>CANStream CAN configuration object of the DBC</returns> public CANMessagesConfiguration Convert_DBC_to_XCC(int Rate, string CanNodeName) { CANMessagesConfiguration oXCC = new CANMessagesConfiguration(); //Bus Baud rate oXCC.CanRate = Rate; //Config name oXCC.Name = "DBC:"; if (!(BusComponents == null)) { for (int i = 0; i < BusComponents.Length; i++) { oXCC.Name = oXCC.Name + " " + BusComponents[i]; } } foreach (DBCFrame oFrame in Frames) { CANMessage oCSMsg = new CANMessage(); oCSMsg.Identifier = NumberBaseConverter.Dec2Hex(oFrame.Identifier); oCSMsg.DLC = oFrame.ByteLength; oCSMsg.Name = oFrame.Name; oCSMsg.Comment = oFrame.GetNodeComments(); oCSMsg.Period = 1000; //Default value //oCSMsg.RxTx=CanMsgRxTx.Tx; //Default value if (oFrame.Transmitter.Equals(CanNodeName)) { oCSMsg.RxTx = CanMsgRxTx.Tx; } else { oCSMsg.RxTx = CanMsgRxTx.Rx; } oCSMsg.MultiplexerName = oFrame.GetMultiplexerSignalName(); foreach (DBCSignal oSignal in oFrame.Signals) { if ((oCSMsg.RxTx.Equals(CanMsgRxTx.Tx)) || (oCSMsg.RxTx.Equals(CanMsgRxTx.Rx) && oSignal.Destination.Equals(CanNodeName))) { CANParameter oCSParam = new CANParameter(); oCSParam.Name = oSignal.Name; oCSParam.Comment = oSignal.GetSignalComments(); oCSParam.Unit = oSignal.Unit; oCSParam.StartBit = oSignal.StartBit; oCSParam.Length = oSignal.Length; oCSParam.Endianess = (CanParameterEndianess)Enum.Parse(typeof(CanParameterEndianess), oSignal.Endianess.ToString()); oCSParam.Signed = oSignal.Signed; oCSParam.Gain = oSignal.Gain; oCSParam.Zero = oSignal.Zero; if (!(oSignal.Multiplexer == null)) { if (!(oSignal.Multiplexer.IsMultiplexerSignal)) { oCSParam.IsMultiplexed = true; oCSParam.MultiplexerValue = oSignal.Multiplexer.MultiplexerValue; } } oCSMsg.Parameters.Add(oCSParam); } } if (oCSMsg.Parameters.Count > 0) { oXCC.Messages.Add(oCSMsg); } } return(oXCC); }