Esempio n. 1
0
        public static Configuration.Message AddMessage(string messageName, string canId, Configuration.Node node, Configuration.Bus parentBus)
        {
            if (messageName == null)
            {
                throw new ArgumentNullException(nameof(messageName));
            }
            if (canId == null)
            {
                throw new ArgumentNullException(nameof(canId));
            }
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }
            if (parentBus == null)
            {
                throw new ArgumentNullException(nameof(parentBus));
            }

            Configuration.Message message = new Configuration.Message
            {
                name = messageName,
                id   = "0x" + CanUtilities.Trim0x(canId)
            };

            NodeRef nodeRef = new NodeRef
            {
                id = node.id
            };

            message.Producer.Add(nodeRef);
            parentBus.Message.Add(message);

            return(message);
        }
Esempio n. 2
0
        private void TbId_Leave(object sender, EventArgs e)
        {
            if (!IsHexString(this.tbId.Text))
            {
                MessageBox.Show("ID is not hex");
            }

            this.canPacket.CanIdBase10 = uint.Parse(CanUtilities.Trim0x(this.tbId.Text), System.Globalization.NumberStyles.HexNumber);
            UpdateInputFields();
        }
Esempio n. 3
0
        public void SaveChargeDataHeader()
        {
            fileStream.Write("Date time     , SOC %, Charge Current , Charge Voltage, Pack mA, Pack mV, Min Cell mV, Max Cell mV, Min Cell Temp, Max Cell Temp, Balance +, Balance -, Charge Voltage Error, Discharge Voltage Error");

            foreach (BMU bmu in BatteryChargeService.Instance.BatteryService.BatteryData.GetBMUs())
            {
                foreach (CMU cmu in bmu.GetCMUs())
                {
                    if (cmu.State == CanReceivingNode.STATE_ON)
                    {
                        fileStream.Write(", CMU" + CanUtilities.AlignLeft(cmu.SerialNumber.ToString(), 3, false) + ", Cell0mV, Cell1mV, Cell2mV, Cell3mV, Cell4mV, Cell5mV, Cell6mV, Cell7mV");
                    }
                }
            }

            fileStream.WriteLine();
        }
Esempio n. 4
0
        public override void CanPacketReceived(CanPacket canPacket)
        {
            if (canPacket == null)
            {
                throw new ArgumentNullException(nameof(canPacket));
            }

            try
            {
                if (isRecording)
                {
                    string newLine = "";
                    packetNumber++;

                    newLine += CanUtilities.AlignLeft(DateTime.Now.ToString("HH:mm:ss.fff"), 14, false);
                    newLine += CanUtilities.AlignLeft(packetNumber.ToString(), 12, true);
                    newLine += CanUtilities.AlignLeft(canPacket.CanIdAsHex, 12, true);
                    newLine += CanUtilities.AlignLeft(canPacket.Flags, 7, true);

                    byte[] dataBytes = canPacket.DataBytes;
                    Array.Reverse(dataBytes, 0, dataBytes.Length);

                    newLine += CanUtilities.AlignLeft("0x" + CanUtilities.ByteArrayToString(dataBytes), 20, true);
                    newLine += CanUtilities.AlignLeft(canPacket.Float1.ToString(), 15, true);
                    newLine += CanUtilities.AlignLeft(canPacket.Float0.ToString(), 15, true);
                    newLine += CanUtilities.AlignLeft(canPacket.SourceIPAddress.ToString(), 7, true);

                    recordStatus = "Recording Can Packet No : " + packetNumber;

                    recordStream.WriteLine(newLine);
                    recordStream.Flush();

                    if (currentDataLoggerConfig.IsRotateByMB())
                    {
                        if (recordStream.BaseStream.Length > currentDataLoggerConfig.RotateBytes())
                        {
                            RollLogAndManage();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.StackTrace);
            }
        }
Esempio n. 5
0
        public void SaveChargeData(ChargeData chargeData)
        {
            if (chargeData == null)
            {
                throw new ArgumentNullException(nameof(chargeData));
            }

            string newLine = "";

            newLine += CanUtilities.AlignLeft(chargeData.DateTime.ToString("HH:mm:ss"), 14, false);
            newLine += CanUtilities.AlignLeft(chargeData.SOCAsInt.ToString(), 7, true);
            newLine += CanUtilities.AlignLeft(chargeData.ChargeCurrentmA.ToString(), 17, true);
            newLine += CanUtilities.AlignLeft(chargeData.ChargeVoltagemV.ToString(), 16, true);
            newLine += CanUtilities.AlignLeft(chargeData.PackmA.ToString(), 9, true);
            newLine += CanUtilities.AlignLeft(chargeData.PackmV.ToString(), 9, true);
            newLine += CanUtilities.AlignLeft(chargeData.MinCellmV.ToString(), 13, true);
            newLine += CanUtilities.AlignLeft(chargeData.MaxCellmV.ToString(), 13, true);
            newLine += CanUtilities.AlignLeft(chargeData.MinCellTemp.ToString(), 15, true);
            newLine += CanUtilities.AlignLeft(chargeData.MaxCellTemp.ToString(), 15, true);
            newLine += CanUtilities.AlignLeft(chargeData.BalanceVoltageThresholdRising.ToString(), 11, true);
            newLine += CanUtilities.AlignLeft(chargeData.BalanceVoltageThresholdFalling.ToString(), 11, true);
            newLine += CanUtilities.AlignLeft(chargeData.ChargeCellVoltageError.ToString(), 22, true);
            newLine += CanUtilities.AlignLeft(chargeData.DischargeCellVoltageError.ToString(), 25, true);

            if (chargeData.CellVoltages != null)
            {
                foreach (uint?[] array in chargeData.CellVoltages)
                {
                    newLine += CanUtilities.AlignLeft(array[8].ToString(), 8, true);
                    newLine += CanUtilities.AlignLeft(array[0].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[1].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[2].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[3].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[4].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[5].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[6].ToString(), 9, true);
                    newLine += CanUtilities.AlignLeft(array[7].ToString(), 9, true);
                }
            }

            fileStream.WriteLine(newLine);
        }
Esempio n. 6
0
 private static Boolean IsHexString(String text)
 {
     return(System.Text.RegularExpressions.Regex.IsMatch(CanUtilities.Trim0x(text), @"\A\b[0-9a-fA-F]+\b\Z"));
 }
Esempio n. 7
0
        private async Task ReadCanLogFile(Stream ioStream, bool replayMode, bool logErrors)
        {
            if (ioStream == null)
            {
                throw new ArgumentNullException(nameof(ioStream));
            }

            isReplaying = true;

            StreamReader ioStreamReader = new StreamReader(ioStream);

            do
            {
                double startTime = 0;
                string line;
                double timeStamp;
                int    timeDiff;

                string lastErrorMsg = "";

                int packetCount = 0;

                // read from the start, if we are looping this may not be the start of the file
                // as we may have already run though before
                ioStream.Position = 0;
                ioStreamReader.DiscardBufferedData();

                while (isReplaying && (line = ioStreamReader.ReadLine()) != null)
                {
                    try
                    {
                        string[] components = line.Split(',');

                        if (!components[0].StartsWith("Recv time"))
                        {
                            if (DateTime.TryParseExact(components[0].Trim(), "HH:mm:ss.fff", new CultureInfo("en-US"), DateTimeStyles.None, out DateTime loggedTime))
                            {
                                CanPacket cp = new CanPacket
                                {
                                    CanId = Convert.ToUInt32(components[2].Trim(), 16)
                                };


                                if (replayMode)
                                {
                                    Boolean replayThis = false;

                                    if (FilterType == FILTER_NONE)
                                    {
                                        replayThis = true;
                                    }
                                    if (FilterType == FILTER_INCLUDE)
                                    {
                                        if (cp.CanId >= FilterFrom && cp.CanId <= FilterTo)
                                        {
                                            replayThis = true;
                                        }
                                    }
                                    if (FilterType == FILTER_EXCLUDE)
                                    {
                                        if (cp.CanId < FilterFrom || cp.CanId > FilterTo)
                                        {
                                            replayThis = true;
                                        }
                                    }

                                    if (replayThis)
                                    {
                                        timeStamp = (loggedTime - DateTime.MinValue).TotalMilliseconds;

                                        if (startTime == 0)
                                        {
                                            startTime = timeStamp;
                                        }

                                        timeDiff = (int)(timeStamp - startTime);
                                        if (timeDiff < 0)
                                        {
                                            timeDiff = 0;
                                        }
                                        // This is now the start time for the next gap
                                        startTime = timeStamp;

                                        await Task.Delay(timeDiff).ConfigureAwait(false);

                                        string rawBytesStr = components[4].Trim().Substring(2);
                                        byte[] rawBytes    = CanUtilities.StringToByteArray(rawBytesStr);
                                        Array.Reverse(rawBytes, 0, rawBytes.Length);

                                        for (int i = 0; i <= 7; i++)
                                        {
                                            cp.SetByte(i, rawBytes[i]);
                                        }
                                        CanService.Instance.SendMessage(cp);

                                        replayStatus = "Sending Can Packet No : " + packetCount;
                                        packetCount++;
                                    }
                                }

                                if (logErrors)
                                {
                                    replayStatus = "Checking Can Packet No : " + packetCount;
                                    packetCount++;
                                    Application.DoEvents();

                                    string rawBytesStr = components[4].Trim().Substring(2);
                                    byte[] rawBytes    = CanUtilities.StringToByteArray(rawBytesStr);
                                    Array.Reverse(rawBytes, 0, rawBytes.Length);

                                    for (int i = 0; i <= 7; i++)
                                    {
                                        cp.SetByte(i, rawBytes[i]);
                                    }
                                    CanService.Instance.SendMessage(cp);

                                    foreach (BMU bmu in BatteryService.Instance.BatteryData.GetBMUs())
                                    {
                                        if (!bmu.StateMessage.Equals(lastErrorMsg))
                                        {
                                            lastErrorMsg = bmu.StateMessage;
                                            Debug.WriteLine(components[0].Trim() + " : " + bmu.StateMessage);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                isReplaying  = false;
                                replayStatus = "Error Reading File";
                            }
                        }
                    }
                    catch
                    {
                        isReplaying = false;
                    };
                }

                // Sleep for 1/10th of a second, this also helps if we are trying to loop on a file
                // that doesn't contain any data or is filtered right out
                await Task.Delay(100).ConfigureAwait(false);
            } while (isReplaying && LoopReplay);

            if (ioStreamReader != null)
            {
                ioStreamReader.Close();
            }
            if (ioStream != null)
            {
                ioStream.Close();
            }
            isReplaying = false;
        }