当前位置:首页 > 单片机 > 单片机
[导读]  TCP/IP Stack  使用pic18f97j60开发过多个项目,项目中都使用了Microchip免费提供的TCP/IP Stack实现远程控制。但是每次更新程序,都需要将pic18f97j60目标板取回来重新烧录,很不方便。既然可以实现远程控制,

  TCP/IP Stack

  使用pic18f97j60开发过多个项目,项目中都使用了Microchip免费提供的TCP/IP Stack实现远程控制。但是每次更新程序,都需要将pic18f97j60目标板取回来重新烧录,很不方便。既然可以实现远程控制,为什么不实现远程更新呢?这就是我的ethernet bootloader的由来。Microchip的TCP/IP Stack功能很强大,我决定只用它的UDP模块来实现。为了实现远程更新,我需要写pic18f97j60单片机端UDP协议的ethernet bootloader程序--我将其命名为PhnBoot_v1.0; 同时还需要写PC端与bootloader交互的UDP通信程序--我将其命名为PhnLoader_v1.0。我还定义了PhnBoot_v1.0和PhnLoader_v1.0之间传输数据的通信协定。

  通信协定

  单片机端PhnBoot_v1.0和PC端PhnLoader_v1.0之间的通信数据包采用以下协定

...

  定义如下:

STX - Start of packet indicator
ETX - End of packet indicator
LEN - The length of true data
DATA - General data 16 bytes, only first LEN of datas are true
CMD - Base command
ADDR - Address up to 24 bits ( ADDRL , ADDRH , ADDRH)

  具体有以下Base command:

RD-VER: 0x00 -- Read Version Information (最终版本删除了此命令)
RD_MEM: 0x01 -- Read Program Memory (最终版本删除了此命令)
ER_MEM: 0x03 -- Erase Program Memory
WR_MEM: 0x02 -- Write Program Memory
WR_CFG: 0x04 -- Write Configuration Registers

  PhnLoader_v1.0 功能

  定义好了通讯协定, 接着就按照协定去实现PhnLoader_v1.0。 PhnLoader_v1.0的具体功能包括选择IP地址,端口和协议类型,目前只支持UDP协议, 创建UDP服务器,加载应用程序Hex文件,Parse 应用程序的Hex文件,一行一行解读Hex文件,一旦收到更新请求,立刻按照通讯协定采用UDP协议发送Hex记录到单片机,接收单片机发送回来的Response,发送完毕后断开UDP通信,发送期间出现问题就立马结束发送。

  PhnLoader_v1.0 主要代码段

  PhnLoader_v1.0是用C#实现的,是我在利用空余时间自学C#后写的,上面提到的功能都实现了。


privatevoidbtnDownload_Click(objectsender,EventArgse){btnDownload.Enabled=false;pBarLoading.Visible=false;if(!this.connect()){Debug.WriteLine("Udpserverbuildingunsuccessfully");textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Udpserverbuildingunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;btnDownload.Enabled=true;return;}try{loaderReader=newStreamReader(textBoxFile.Text);}catch(Exceptionex){Debug.WriteLine("Error:"+ex.Message);textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Readhexfileunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}loaderFrame=newSerialFrame();DateTimestartTime=DateTime.Now;IPEndPointclientPoint=newIPEndPoint(IPAddress.Any,0);if(!loaderServer.Read(readyMsg,timeSpan)){Debug.WriteLine("Error:Timeoutreceivereadymessagefrombootloader");textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Timeoutreceivereadymessagefrombootloaderrn");textBoxStatus.ForeColor=Color.Black;loaderServer.Close();loaderReader.Close();btnDownload.Enabled=true;return;}if(!erase()){textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Eraseunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}pBarLoading.Refresh();pBarLoading.Visible=true;pBarLoading.Value=0;pBarLoading.Maximum=loaderLines;pBarLoading.Step=1;stringrecordLine;Address_U=0;boolisNextLineUserID=false;boolisNextLineConfigBits=false;textBoxStatus.AppendText("rnDownloadinghexfile...rn");try{while(loaderReader.Peek()>=0){pBarLoading.PerformStep();recordLine=loaderReader.ReadLine();if(recordLine.Contains(EXTEND_TOKEN)==true){if(recordLine.Contains(USER_ID_TOKEN)==true){isNextLineUserID=true;continue;}elseif(recordLine.Contains(CONFIG_BITS_TOKEN)==true){constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);Address_U=Convert.ToInt32(addrU,16)<<16;isNextLineConfigBits=true;continue;}else{constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);Address_U=Convert.ToInt32(addrU,16)<<16;continue;}}elseif(((recordLine.Contains(J_TYPE_CONFIG_BITS_1)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_2)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_3)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_4)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_5)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_6)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_TOKEN_1)==true)||(recordLine.Contains(J_TYPE_CONFIG_BITS_TOKEN_2)==true))&&(Address_U==0x010000)){if(!DownloadConfigLine(recordLine)){Debug.WriteLine("Errorfoundduringconfigurationbitsprogramming");loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}continue;}elseif(recordLine.Contains(END_OF_HEX_FILE_TOKEN)==true){break;}if(isNextLineUserID){isNextLineUserID=false;//donothing;}elseif(isNextLineConfigBits){if(!DownloadConfigLine(recordLine)){Debug.WriteLine("Errorfoundduringconfigurationbitsprogramming");loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}isNextLineConfigBits=false;}else{if(!DownloadDataLine(recordLine)){Debug.WriteLine("Errorfoundduringdataprogramming");loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}}}}catch(Exceptionex){Debug.WriteLine("Error:"+ex.Message);textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Downloadingfailedrn");textBoxStatus.ForeColor=Color.Black;loaderServer.Close();loaderReader.Close();btnDownload.Enabled=true;return;}textBoxStatus.AppendText("Downloadingcompletedrn");if(!run()){textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("JumptoApplicationunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderServer.Close();btnDownload.Enabled=true;return;}loaderServer.Close();loaderReader.Close();btnDownload.Enabled=true;}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

总线-Ethernet与EtherCAT的比较

关键字: ethercat ethernet

    UDP简介         UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OS

关键字: udp协议

  UDP协议也是互联网基础协议之一。它和TCP一样同属于传输层当中的一个协议。   不过UDP协议是一个面向无连接的协议(TCP是向面连接的协议)。一个UDP连接的建立,不必象TCP协

关键字: udp协议 包格式

  QUdpSocket   QUdpSocket是QAbstractSocket 的子类,它们都继承了QIODevice。所以可以用QUdpSocket进行发送接收数据。它和QTcpS

关键字: qudpsocket udp协议

  在进行UDP协议的使用中,我们通常会借助其他语言工具来完成工作。那么今天我们主要介绍一下Java下的UDP协议的使用。首先我们来了解一下UDP协议的基本概念。UDP协议的全称是用户数据报,在

关键字: udp协议

  随着半导体技术的不断进步(按照摩尔定律),MCU内部集成的逻辑功能外设越来越多,存储器也越来越大。消费者对于汽车节能(经济和法规对排放的要求)型、舒适性、互联性、安全性(功能安全和信息安全)

关键字: bootloader 汽车电子

1.之所以要实现一个专用的bootloader,一是为了更好的移植和自身的升级,二是为了方便操作系统的调试,当然,你完全可以将这部分所要实现的与操作系统相关的功能集成到操作

关键字: bootloader 如何实现 嵌入式开发
关闭