示例#1
0
        public void TestByte()
        {
            CanPacket canPacket = new CanPacket((int)0x1806E5F4ul);

            canPacket.SetByte(0, 1);
            Assert.AreEqual(canPacket.GetByte(0), 1);
            canPacket.SetByte(0, 12);
            Assert.AreEqual(canPacket.GetByte(0), 12);

            Boolean gotException = false;

            try
            {
                canPacket.SetByte(8, 12);
            }
            catch
            {
                gotException = true;
            }

            Assert.IsTrue(gotException);

            gotException = false;

            try
            {
                canPacket.GetByte(8);
            }
            catch
            {
                gotException = true;
            }

            Assert.IsTrue(gotException);
        }
        public void DisengageContactor()
        {
            contactorUpdateTimer.Stop();

            CanPacket canPacket = new CanPacket(0x508);

            canPacket.SetByte(7, 0x0);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);
        }
        public async Task <bool> EngageContactor()
        {
            CanPacket canPacket = new CanPacket(0x508);

            canPacket.SetByte(7, 0x0);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);

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

            canPacket.SetByte(7, 0x30);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);

            contactorUpdateTimer = new System.Timers.Timer
            {
                Interval  = 100,
                AutoReset = true,
                Enabled   = true
            };
            contactorUpdateTimer.Elapsed += ContactUpdate;

            return(true);
        }
        public async Task <bool> StartDischarge()
        {
            CanPacket canPacket = new CanPacket(0x508);

            canPacket.SetByte(7, 0x0);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);

            await Task.Delay(1000);

            await batteryService.EngageContactors();

            if (!await batteryService.WaitUntilContactorsEngage(5000))
            {
                return(false);
            }

            // Not really necessary but a double check
            if (!batteryService.IsContactorsEngaged)
            {
                return(false);
            }

            canPacket.SetByte(7, 0x30);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);

            isDischarging = true;

            chargerUpdateTimer = new System.Timers.Timer
            {
                Interval  = 100,
                AutoReset = true,
                Enabled   = true
            };
            chargerUpdateTimer.Elapsed += DischargerUpdate;

            return(true);
        }
        public async void StopDischarge()
        {
            chargerUpdateTimer.Stop();

            batteryService.DisengageContactors();

            if (!await batteryService.WaitUntilContactorsDisengage(2000))
            {
                return;
            }

            CanPacket canPacket = new CanPacket(0x508);

            canPacket.SetByte(7, 0x0);
            canControl.ComponentCanService.SetCanToSendAt10Hertz(canPacket);

            isDischarging = false;
        }
示例#6
0
        public async Task StartReplaying(Stream ioStream)
        {
            isReplaying = true;

            StreamReader ioStreamReader = new StreamReader(ioStream);

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

                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)
                                };

                                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);

                                    string rawBytesStr = components[4].Trim().Substring(2);
                                    byte[] rawBytes    = MyExtensions.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++;
                                }
                            }
                            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);
            } while (isReplaying && LoopReplay);

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