Пример #1
0
 public ResultEnum Load(string FileName, AddressDataGroup <byte> DataGroup)
 {
     using (StreamReader sr = new StreamReader(FileName, System.Text.Encoding.ASCII))
     {
         return(Load(sr, DataGroup));
     }
 }
Пример #2
0
        public static int Write(List <Job> Jobs, AddressDataGroup <byte> DataGroup)
        {
            if (DataGroup.Groups.Count != 1)
            {
                throw new ArgumentException("DataGroup must only have 1 Group.");
            }

            int count = DataGroup.Groups[0].Datas.Count;

            int num = 0;
            int p   = 0;

            while (p < count)
            {
                int len = count - p;
                if (len > 256)
                {
                    len = 256;
                }

                byte[] bs = new byte[len];
                DataGroup.Groups[0].Datas.CopyTo(p, bs, 0, len);

                Job j = new Job(Job.JobType.Write, bs);
                j.Address = DataGroup.Groups[0].Address + p;
                Jobs.Add(j);

                p += 256;
                num++;
            }
            return(num);
        }
Пример #3
0
        public static int EraseWrite(List <Job> Jobs, AddressDataGroup <byte> DataGroup, EraseOptEnum EraseOpt, FlashSectionStruct[] FlashSection, bool IsACKFirst)
        {
            if (EraseOpt == EraseOptEnum.OnlyUsed)
            {
                if (FlashSection == null || FlashSection.Length == 0)
                {
                    throw new ArgumentException("FlashSection not Available.");
                }
            }

            int num = 0;

            if (IsACKFirst)
            {
                Job j = new Job(Job.JobType.ACK);
                Jobs.Add(j);
                num++;
            }

            if (EraseOpt == EraseOptEnum.None)
            {
            }
            else if (EraseOpt == EraseOptEnum.OnlyUsed)
            {
                num += Erase(Jobs, DataGroup, FlashSection);
            }
            else if (EraseOpt == EraseOptEnum.All)
            {
                num += Erase(Jobs);
            }

            num += Write(Jobs, DataGroup);

            return(num);
        }
Пример #4
0
        public static int Erase(List <Job> Jobs, AddressDataGroup <byte> DataGroup, FlashSectionStruct[] FlashSection)
        {
            if (DataGroup.Groups.Count != 1)
            {
                throw new ArgumentException("DataGroup must only have 1 Group.");
            }

            int dataAddress = DataGroup.Groups[0].Address;
            int dataSize    = DataGroup.Groups[0].Datas.Count;

            List <byte> pageNos = new List <byte>();

            bool isDataStartIn = false;
            bool isDataEndIn   = false;

            for (int i = 0; i < FlashSection.Length; i++)
            {
                if (FlashSection[i].IsInSection(dataAddress))
                {
                    isDataStartIn = true;
                }
                if (FlashSection[i].IsInSection(dataAddress + dataSize - 1))
                {
                    isDataEndIn = true;
                }

                if (FlashSection[i].Address > dataAddress + dataSize - 1)
                {
                }
                else if (FlashSection[i].Address + FlashSection[i].Size - 1 < dataAddress)
                {
                }
                else
                {
                    pageNos.Add((byte)i);
                }
            }

            if (isDataStartIn = false || isDataEndIn == false)
            {
                throw new ArgumentException("DataGroup not in FlashSection Range.");
            }

            return(Erase(Jobs, pageNos.ToArray()));
        }
Пример #5
0
        public ResultEnum Load(StreamReader SR, AddressDataGroup <byte> DataGroup)
        {
            IntelHexParser hexParser = new IntelHexParser();

            IntelHexParser.HexRecord  record = new IntelHexParser.HexRecord();
            IntelHexParser.ResultEnum result = IntelHexParser.ResultEnum.Success;

            int no = 0;

            while (!SR.EndOfStream)
            {
                string s = SR.ReadLine();
                result = hexParser.Parse(s, out record);
                if (result != IntelHexParser.ResultEnum.Success)
                {
                    ErrorWriteLine(string.Format("HexString Parse Fail#{0}:{1}", no, s));
                    return((ResultEnum)result);
                    //return ResultEnum.StructWrong;
                }
                if (no == 0)
                {
                    if (record.Type != IntelHexParser.HexRecordType.ExtenLinAdd)//起始地址
                    {
                        ErrorWriteLine(string.Format("Hex File not Begin with ExtenLinAdd Record#{0}:{1}", no, s));
                        return(ResultEnum.StructWrong);
                    }
                }
                else
                {
                    if (record.Type == IntelHexParser.HexRecordType.Data)//数据正常
                    {
                    }
                    else if (record.Type == IntelHexParser.HexRecordType.StartLinAdd)//忽略最后的地址设定命令
                    {
                    }
                    else if (record.Type == IntelHexParser.HexRecordType.EOF)//数据结束
                    {
                        break;
                    }
                    else//无法支持的类型
                    {
                        ErrorWriteLine(string.Format("HexString Type not Support#{0}:{1}", no, s));
                        return(ResultEnum.FormatWrong);
                    }
                }
                if (record.Type == IntelHexParser.HexRecordType.Data)
                {
                    DataGroup.Insert(hexParser.Address, record.Data);
                }
                no++;
            }
            if (!SR.EndOfStream)//未读取到文件结束
            {
                ErrorWriteLine(string.Format("Parse End before EOF#{0}", no));
            }
            if (DataGroup.Groups.Count != 1)
            {
                ErrorWriteLine(string.Format("Hex File not 1 Data Section:{0}", DataGroup.Groups.Count));
                return(ResultEnum.StructWrong);
            }
            return(ResultEnum.Success);
        }
Пример #6
0
        static void Main(string[] args)
        {
            //Config Parse
            WriteLine("===== Config Parse =====");
            ConfigStruct config       = new ConfigStruct();
            string       rString      = "";
            int          numArgAccept = ConfigStruct.Parse(config, args, ref rString);;

            if (numArgAccept < 0)
            {
                ErrorWriteLine(rString);
                return;
            }

            WriteLine("Args Parsed Num:" + numArgAccept);
            ArgsShow(config);

            //Hex Load
            WriteLine("===== Hex Load =====");
            AddressDataGroup <byte> dataGroup = new AddressDataGroup <byte>();

            try
            {
                IntelHexLoader            hexLoader = new IntelHexLoader();
                IntelHexLoader.ResultEnum r         = hexLoader.Load(config.HexFile, dataGroup);
                if (dataGroup.Groups.Count != 1)
                {
                    ErrorWriteLine("Hex Not Have One Data Section.");
                    return;
                }
                if (r != IntelHexLoader.ResultEnum.Success)
                {
                    ErrorWriteLine(r.ToString());
                    return;
                }
                WriteLine("Hex Data Size:" + dataGroup.Groups[0].Datas.Count);
            }
            catch (Exception ee)
            {
                ErrorWriteLine(ee.Message);
                return;
            }

            //FlashSection Load
            JobMaker.FlashSectionStruct[] FlashSection = null;
            if (config.FlashSectionFile != "")
            {
                try
                {
                    WriteLine("===== FlashSection Load =====");
                    FlashSection = (JobMaker.FlashSectionStruct[])SerializerHelper.Deserialize(config.FlashSectionFile, typeof(JobMaker.FlashSectionStruct[]));
                    WriteLine("FlashSection Num:" + FlashSection.Length);
                }
                catch (Exception ee)
                {
                    ErrorWriteLine(ee.Message);
                    //return;
                }
            }

            //Job Make
            List <Job> jobs = new List <Job>();

            try
            {
                WriteLine("===== Job Make =====");
                JobMaker.EraseWrite(jobs, dataGroup, config.EraseOpt, FlashSection, true);

                if (config.JumpToFlash)
                {
                    JobMaker.Go(jobs, dataGroup.Groups[0].Address);
                }

                WriteLine("Job Num:" + jobs.Count);
            }
            catch (Exception ee)
            {
                ErrorWriteLine(ee.Message);
                return;
            }

            //CAN Open
            ICAN CANDev = null;

            try
            {
                WriteLine("===== CAN Open =====");
                string   dllPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.CANDevFile);
                Assembly ass     = Assembly.LoadFile(dllPath);
                Type     tCAN    = ass.GetType(config.CANDevClass);
                CANDev = (ICAN)Activator.CreateInstance(tCAN);
                bool rOpen = CANDev.Open(config.CANDeviceNo);
                if (!rOpen)
                {
                    ErrorWriteLine("CAN Open Fail.");
                    return;
                }
                bool rStart = CANDev.Start(config.CANPortNo, config.CANBPS, false, config.CANSendOnce, false);
                if (!rOpen)
                {
                    ErrorWriteLine("CAN Start Fail.");
                    return;
                }
                WriteLine("CAN Open Success");
            }
            catch (Exception ee)
            {
                ErrorWriteLine(ee.Message);
                return;
            }

            //JobExecutor Init
            WriteLine("===== JobExecutor Init =====");
            JobExecutor JE = new JobExecutor(CANDev, config.CANPortNo);

            JE.IsShowSendRecive = config.ShowReceiveSend;
            JE.TimeoutSpan      = new TimeSpan(0, 0, config.Timeout);
            JE.TimeoutSpanErase = new TimeSpan(0, 0, config.TimeoutErase);
            JE.OnStateChange   += JE_OnStateChange;
            JE.SetJob(jobs.ToArray());

            //Run
            WriteLine("===== JobExecutor Running =====");
            while (JE.IsRunning)
            {
                JE.BackgroundRun(false);
                Thread.Sleep(100);
            }
            WriteLine("End With:" + JE.JobsState);
        }