The previous article detailed the file transfer protocol Xmodem family.
The Xmodem family is usually used for short-distance file transfers via serial ports, but in reality, many file transfer needs are much more complex, requiring us to flexibly design communication protocols for specific scenarios to achieve more diversified file transfers.
This article expands on the content of file transfer protocols and elaborates on several projects.
File Transfer via CAN
The file transfer via CAN originates from ZhenNan’s “Electric Vehicle Shared Charging Cabinet” project, as shown in the images of the shared charging cabinet’s appearance and internal circuitry.
Overall schematic of the shared charging cabinet.
The main controller has 4G communication capabilities and can receive commands issued from the cloud, such as opening the cabinet, starting charging, ending charging, etc., and can also upload status information to the cloud, such as cabinet door status, current power, etc.
For firmware upgrades of the main controller and slave devices, remote updates have also been implemented. Let’s first discuss the firmware flashing of the slave devices:
The main controller downloads the firmware file for the slave device from the cloud or obtains it from the host computer, and then transfers this firmware to a specific slave device via the CAN bus (distinguished by CAN ID), ultimately completing the flashing via the slave device’s Bootloader.
In this process, the CAN file transfer protocol is key. As shown in the diagram, it illustrates the protocol for file transfer via CAN.
In fact, the CAN bus is not suitable for bulk data transmission, as each data frame can only transmit 8 bytes. However, to achieve highly automated bulk flashing, I still implemented file transfer based on the CAN bus (which indeed feels a bit challenging).
If you take a closer look at the transmission process in the above diagram, you will find that it is actually quite similar to Xmodem, with the same implementation idea.
Some may ask: “Compared to CAN file transfer, I am more interested in how your main controller downloads firmware from the cloud, that is, how embedded network file transfer is implemented?”
Don’t worry, the next section will cover this topic, taking the “Smart Charging Cabinet” project as an example to introduce this issue.
Downloading Files via HTTP
ZhenNan uses the SIM800 module in the project, as shown in the images of the actual main control board and SIM800 module for the “Smart Charging Cabinet” project.
The SIM800 module has an integrated HTTP protocol, allowing it to directly GET firmware data from the server. The implementation code is as follows:
int WEBDOWN(void)
{
char bin_name[30];
if(SIM800_Check_GPRS())
{
return 1; //**No network
}
if(SIM800_Config_Bearer1())
{
return 2;
} //**Failed to set network parameters
if(SIM800_Config_Bearer2())
{
return 3;
} //**Failed to set network parameters
if(SIM800_Open_GPRS_Context())
{
return 4;
} //**Open GPRS context
if(SIM800_Query_GPRS_Context())
{
return 5;
} //**Request GPRS context information
if(SIM800_HTTP_INIT())
{
return 6;
} //**Failed to initialize HTTP protocol
if(SIM800_HTTP_SET_PARA1())
{
return 7;
} //**Set HTTP parameters 1
strcpy(bin_name,"master.bin");
if(SIM800_HTTP_SET_PARA2("www.znmcu.com",bin_name))
{
return 8;
};//**Set HTTP parameters 2
if(SIM800_HTTP_GET())
{
return 9;
}; //GET method, can obtain the size of the file to be downloaded
{
unsigned int temp=(web_download_file_len/1024);
unsigned int i=0,prog_addr=0;
unsigned char total_chksum=0;
unsigned char *p;
DEBUG_OUT("T Ts:%d\r\n",temp);
for(i=0;i<temp;i++)
{
SIM800_HTTP_READDATA(i*1024L,1024);
}
if(web_download_file_len%1024)
{
SIM800_HTTP_READDATA(i*1024L,(web_download_file_len%1024));
i++;
}
}
//**Verify firmware, reboot, and write firmware to APP area
}
The actual code is quite lengthy with many unrelated components. The above code is a simplified version, mainly to illustrate the primary process of pulling firmware files from the server via HTTP.
http://www.znmcu.com/tg/519.html
File Transfer via JSON
Experienced embedded engineers are not unfamiliar with JSON, which can be used for structured data exchange between different platforms.
Therefore, in some applications, JSON is used for file data transfer. For example, in some Wi-Fi devices I have developed, as shown in the image, firmware data is transmitted via JSON.
We know that JSON itself is a string, so it cannot directly transmit binary data. How can the binary data of firmware be transmitted via JSON?
This is not just a problem for JSON but also for all text protocols, such as the email communication protocol we often use, and the HTTP protocol we commonly use.
HTTP itself is a hypertext transfer protocol, which actually only transmits text, i.e., ASCII. However, when we browse the internet, we often use HTTP to download files. How is its binary data transmitted? I wonder if anyone has considered this question?
To solve this problem, Base64 encoding was proposed. As shown in the image, binary data is expressed through Base64 encoding. I hope everyone can appreciate its cleverness.
The basic idea of Base64 is: to separate the binary data sequence into 6-bit segments and represent them using 64 printable characters. Therefore, every 3 bytes can be converted into 4 characters. If there are insufficient 3 bytes, padding is done using =.
This series of articles introduces several file transfer-related protocols and expands on them through various projects.
The content covered here may have broader applications in actual usage. For example, the Xmodem protocol has been improved for satellite data transmission.
No matter how complex the technology is, it is composed of simple elements. Understanding and mastering the basics will allow us to go further in technology. I hope everyone can be inspired by the content of this article and ultimately apply it to their actual development, turning knowledge into value.
Source: Learning Embedded Together
Due to recent changes in the WeChat public platform’s push rules, many readers have reported not seeing updated articles in a timely manner. According to the latest rules, it is recommended to frequently click “Recommended Reading, Share, Collect, etc.,” to become a regular reader.
Recommended Reading:
-
The end of the domestic car price reduction wave? 16 car companies jointly signed an agreement to resist price wars~
-
Toyota announces major breakthrough in solid-state batteries: 10 minutes of charging for 1200 km range!
-
Stealing students’ information to build a website for rating appearance, the person involved once released an AI program for undressing!
-
Taiwan Semiconductor Manufacturing Company suddenly suffered a cyber attack! Ransom of 70 million dollars demanded by hackers~
-
Many universities announced the discontinuation of WeChat payment, Tencent responds urgently!
Please click 【See】 to give the editor a thumbs up
