/// <summary>
    /// Motorola S/EXORciser/EXORmacs/32位格式  S-Record
    /// 解析  EXORciser-S19   EXORmacs-S28  32位-S37 文件使用 
    /// 作者只使用过 S37文件验证过
    /// </summary>

public class SRecord //  
    {
        public enum Header { S0, S1, S2, S3, S4, S5, S6, S7, S8, S9 } // 文件开始
        public enum Type { S19, S28, S37 }
        public byte[] address;
        public byte len = 0x0;
        public byte[] data;
        public byte cs = 0x0;
        public List<byte> read = new List<byte>();
        public UInt32 headAddress { get; set; }
        public UInt32 execAddress { get; set; }
        public Type type = Type.S37;
        public bool firstLine = false;

public void genFile(byte[] content, UInt32 address, UInt32 execAddress, string filePath)
        {
            List<string> list = new List<string>();
            int len = 32;   //<=32 字节
            byte[] buf = System.Text.Encoding.Default.GetBytes(filePath);
            Header header = Header.S0;
            int pStart = 0;
            //address   累加    buf循环  长度
            this.execAddress = execAddress;
            list.Add(genRecord(0x0, buf, Header.S0));  //开始S0 开始

buf = new byte[len];
            //循环 内容  
            while (pStart < content.Length)
            {
                if (pStart + len <= content.Length)
                {
                    Array.Copy(content, pStart, buf, 0, len);
                }
                else
                {
                    len = content.Length - pStart;
                    buf = new byte[len];
                    Array.Copy(content, pStart, buf, 0, len);
                }
                list.Add(genRecord(address, buf, Header.S3));
                address = address + (uint)len;
                pStart = pStart + len;
            }

list.Add(genRecord(execAddress, new byte[0], Header.S7));  //最后结束

//保存文件

FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);

foreach (string str in list)
            { sw.WriteLine(str); }
            sw.Flush();
            sw.Close();
            fs.Close();

}

private string genRecord(UInt32 address, byte[] buf, Header header)
        {
            //int len = 0;
            string str = "";
            data = new byte[buf.Length];
            Array.Copy(buf, 0, data, 0, buf.Length);
            if (header == Header.S0)
            {
                this.address = new byte[] { 0x0, 0x0 };
            }
            else if (header == Header.S3)
            {
                this.address = Common.uInt32ToByte(address);   //转地址                
            }
            else if (header == Header.S7)
            {
                this.address = Common.uInt32ToByte(address);

}

len = (byte)(buf.Length + 1 + this.address.Length);
            genCheckSum();

Array.Reverse(this.address);

str += header.ToString() + len.ToString("X2") + Common.byte2HexStr(this.address).Replace(" ", "")
                + Common.byte2HexStr(buf).Replace(" ", "") + cs.ToString("X2");

return str;
        }

private void genCheckSum()
        {
            ushort sum = 0;
            sum += len;

for (int i = 0; i < address.Length; i++)
            {
                sum += address[i];
            }

for (int i = 0; i < data.Length; i++)
            {
                sum += data[i];
            }

cs = (byte)(sum & 0xff);

cs = (byte)~cs;

}

public bool readRecord(string filePath, out string errorMsg)
        {
            string[] lines = System.IO.File.ReadAllLines(filePath, System.Text.Encoding.Default);
            errorMsg = "";
            bool result = true;

for (int i = 0; i < lines.Length; i++)
            {
                anaysisRecord(lines[i], out errorMsg);
                if (errorMsg != "")
                {
                    result = false;
                    break;
                }
            }

return result;

}

private void anaysisRecord(string line, out string errorMsg)
        {
            errorMsg = "";
            if (line == "") { return; }
            if (line.Length < 2) { errorMsg = "this line is too little"; }
            string headStr = line.Substring(0, 2);
            byte[] buf = Common.hexStr2Bytes(line.Substring(2));
            //S325AF07FFE0000000000000000000000000000000000000000000000000000000000000000045
            if (buf[0] != buf.Length - 1) { errorMsg = "this line data_len area do not match line length "; }
            if (headStr == "S0")
            {
                //头行 没有实际内容
            }
            else if (headStr == "S2")
            {
                type = Type.S28;
                // 等等 后续开发 
            }
            else if (headStr == "S3")
            {
                address = new byte[4];
                Array.Copy(buf, 1, address, 0, 4);
                //Array.Reverse(address);
                if (firstLine == false)
                {
                    firstLine = true;
                    type = Type.S37;
                    headAddress = Common.byteToUInt(address);
                }
                for (int i = 0; i < buf.Length - 4 - 1 - 1; i++)  //buf - 长度区域(1)- 地址区域(4)-校验和(1)
                {
                    read.Add(buf[1 + 4 + i]);   // 演示 demo    第一个字节 长度 然后4个字节地址 是第五个字节
                }

}
            else if (headStr == "S7")
            {
                address = new byte[4];
                Array.Copy(buf, 1, address, 0, 4);
                execAddress = Common.byteToUInt(address);
            }
        }

}

S37文件解析(Motorola S/EXORciser/EXORmacs/32位格式)相关推荐

  1. Android如何把图片转为深度为32位格式为.bmp的图片

    前几天公司的产品要求做个手机拍的照片转深度为32位,格式为.bmp格式的图片,仔细研究了下,通过调系统相机根据照片存的路径以及FileInputStream获得照片的bitmap,拿到这个bitmap ...

  2. Fashion MNIST数据集的处理——“...-idx3-ubyte”文件解析

    Fashion MNIST MNIST数据集可能是计算机视觉所接触的第一个图片数据集.而 Fashion MNIST 是在遵循 MNIST 的格式和大小的基础上,提升了一定的难度,在比较算法的性能时可 ...

  3. 15.windbg-dds、dps、dqs、PE文件解析

    以下默认windbg加载calc程序 d*s dds.dps和dqs命令显示给定范围内存的内容,它们是把内存区域转储出来,并把内存中每个元素都视为一个符号对其进行解析,dds是四字节视为一个符号,dq ...

  4. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑

    自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...

  5. EXCEL通过配置导入规则,实现任意格式EXCEL文件解析

    EXCEL文件导入到数据库,是一种常见的文件导入格式. 本文实现的EXCEL文件解析方法,优点是适合任意格式的EXCEL文件,缺点是每一张表单都需要配置对应的导入规则,在字段数量多时,配置导入规则就尴 ...

  6. MSVCP120D.dll引起的32位和64位程序运行区别

    本人的第一篇技术博客,如有遗漏请见谅. 在没有装vs2013的电脑上(装了vcredist_x86),一个64位应用程序在运行时,弹出0xc000007b错误.而装了vs2013,可以运行.让人摸不着 ...

  7. 转载】将32位代码向64位平台移植的注意事项

    转载]将32位代码向64位平台移植的注意事项 新近的64位平台在二进制上与32位应用程序兼容,这意味着可以非常简单地移植现有的程序.许多目前在32位平台上运行良好的程序也许不必移植,除非程序有以下要求 ...

  8. VS系列编译器内部调用malab(32位/64位)函数指南

    最近项目用到绘制3D图形,使用了许多开源库没有得到理想的效果,后来想到可以使用matlab软件绘制图形通过MFC项目工程进行调用.   有了这个想法后,便在网上搜索相关的资料,实际调用matlab生成 ...

  9. python3.7 win10 64位系统下用pyinstaller打包的程序在32位系统下无法运行

    问题背景: 在64位的win10系统下,用python 3.7.4 写了个定时报警的小玩意儿,主要用到了pyaudio模块,写完之后,使用pyinstaller打包成.exe文件,在其他64位的电脑下 ...

最新文章

  1. python gpu加速 显卡_PyTorch-GPU加速实例
  2. (72)FPGA面试题-使用不同的代码实现2-4译码器?使用if语句
  3. 远程无法连接数据库的问题
  4. MindManager思维导图中文版免费下载使用教程
  5. qrcode生成带logo的二维码(插件,示例代码)
  6. IDEA 谷歌翻译报错 TKK
  7. 三菱RS指令无协议通信功能概述
  8. 浅谈DNS协议,DNS协议的作用以及DNS的查询方式,使用DNS做负载均衡
  9. 重学前端学习笔记(一)--前端发展史以及学习痛点
  10. c++11并发与多线程
  11. html遮罩点击,点击按钮实现遮罩效果
  12. SumatraPDF一个很nice的PDF开源项目
  13. flink 简单上手
  14. android 通知静音_如何使电话静音(但不包括短信和通知)
  15. 公司官网建站笔记(五):域名工信部备案完整流程并解析公网访问
  16. 群晖虚拟机安装centos7.9
  17. 【T+】畅捷通T+存货档案批量修改存货属性
  18. TreeSet集合如何保证元素唯一
  19. 使用 ChatGPT 模型实现 JavaScript 加密解密技术
  20. AltiumDesigner PCB设计规则

热门文章

  1. 站外SEO优化怎么做?
  2. BT 种子 tracker 磁链
  3. PHP获取某一年的每个星期六星期天
  4. R语言epiDisplay包的alpha函数计算dataframe数据中指定数据列之间的信度分析、克朗巴哈系数、标准化系数、每当剔除某一条目后系数的变化、设置reverse参数不对任何数据对象进行反序
  5. 2021年B证(安全员)考试APP及B证(安全员)实操考试视频
  6. C++基础入门---9.项目:通讯录管理系统【P72~P83】
  7. 云南大学计算机科学与技术录取分数线,2020云南大学各专业录取分数线公布
  8. 亲测,openxlpy和xlrd 统计excel的总行数,删除最后几行,再统计的话,还是原来的行数
  9. Mac地址自动生成器核心处理类
  10. 关于for i in list1: 中i变量的内容