diff --git a/docs/FAQ/zh/Format_String/Format_String.md b/docs/FAQ/zh/Format_String/Format_String.md index cab62ec1c2d02028a4034736f56c5363522cb0b0..f845f3382200a8a119a497d2d0e2afb36a182f2f 100644 --- a/docs/FAQ/zh/Format_String/Format_String.md +++ b/docs/FAQ/zh/Format_String/Format_String.md @@ -1,4 +1,8 @@ -# Format String +## 修订历史 + +| 版本 | 日期 | 作者 | 变更表述 | +| ---- | --------- | ----- | ------------------------------------------------------------ | +| 1.0 | 2021-4-07 | David | 首次编写,包括位运算、字符串与十六进制转换、以及Struct的说明 | 对于字符串与十六进制之间的转换需求,特编写这篇文档,详细介绍相关知识 @@ -73,3 +77,49 @@ ## 字符串与十六进制 +python客户端与服务端(c程序)进行通讯,需接收服务端发来的16进制码流,并对16进制数据进行解码,得到相应字段的数据,并可以将数据打包成对应格式的码流发送给服务端,多字节整数传输采用网络字节序。此处介绍一下字符串转换成十六进制的方法: + +```python +str_test = 'Quectel build a smarter world' +def str_to_hex(s): + list_hex = ' '.join([hex(ord(c)) for c in s]).split() + list = [int(i,16) for i in list_hex] + bytearr = bytearray(list) + return bytearr +hex_test = str_to_hex(str_test) +``` + +下面使用EC600SCNLB模组做个简单的实验 + +​ 1.编写如下代码并命名为str_hex_test.py。 + +```python +from machine import UART +uart0 = UART(UART.UART0, 115200, 8, 0, 1, 0) +str_test = 'Quectel build a smarter world' +def str_to_hex(s): + list_hex = ' '.join([hex(ord(c)) for c in s]).split() + list = [int(i,16) for i in list_hex] + bytearr = bytearray(list) + return bytearr +hex_test = str_to_hex(str_test) +uart0.write(hex_test) +``` + +​ 2.下载上述文件到模组中运行,并打开串口调试工具(此处使用UartAssist) + +image-20210407143228884 + +image-20210407144219267 + +3.运行程序,在串口工具中查看结果 + +image-20210407144126291 + +**Tips:** + +如果使用ASCII接收的话,就会显示字符串形式 + +​ 4.对于模组的串口接收:无论串口工具发送ASCII或者HEX数据,串口读取都是bytes型数据 + +![image-20210407151559820](media/uart_read.png) \ No newline at end of file diff --git a/docs/FAQ/zh/Format_String/media/UartAssist.png b/docs/FAQ/zh/Format_String/media/UartAssist.png new file mode 100644 index 0000000000000000000000000000000000000000..95d51c3d8dd3f9035688e5ac945519459011680c Binary files /dev/null and b/docs/FAQ/zh/Format_String/media/UartAssist.png differ diff --git a/docs/FAQ/zh/Format_String/media/file_download.png b/docs/FAQ/zh/Format_String/media/file_download.png new file mode 100644 index 0000000000000000000000000000000000000000..ae3d1ab20339489871623fa38631d5efd5af0a2b Binary files /dev/null and b/docs/FAQ/zh/Format_String/media/file_download.png differ diff --git a/docs/FAQ/zh/Format_String/media/str_hex_result.png b/docs/FAQ/zh/Format_String/media/str_hex_result.png new file mode 100644 index 0000000000000000000000000000000000000000..41442a593cd957cf457af1b69ebc9ccba27e2a2f Binary files /dev/null and b/docs/FAQ/zh/Format_String/media/str_hex_result.png differ diff --git a/docs/FAQ/zh/Format_String/media/uart_read.png b/docs/FAQ/zh/Format_String/media/uart_read.png new file mode 100644 index 0000000000000000000000000000000000000000..f17f3bfd40c75334b617219016d39a22c6fe657c Binary files /dev/null and b/docs/FAQ/zh/Format_String/media/uart_read.png differ diff --git a/docs/Quecpython_intro/zh/Qp_Hw_EC600X/media/image-20210407143154984.png b/docs/Quecpython_intro/zh/Qp_Hw_EC600X/media/image-20210407143154984.png new file mode 100644 index 0000000000000000000000000000000000000000..ae3d1ab20339489871623fa38631d5efd5af0a2b Binary files /dev/null and b/docs/Quecpython_intro/zh/Qp_Hw_EC600X/media/image-20210407143154984.png differ