【開發(fā)板試用報告】鴻蒙Hi3861環(huán)境搭建,基于tcp client遠程控制LED
https://harmonyos.51cto.com/#zz
非常慚愧由于前段時間太忙到最近才開始鼓搗Hi3861,首先感謝下喬幫主、連老師等大牛的優(yōu)質(zhì)輸出,真乃大神種樹后人乘涼啊。
簡要記錄一下搭建過程的注意事項和tcp client demo。
編譯環(huán)境的搭建,在WIN10上,利用WSL,配合docker簡直不要太爽。具體安裝方式官方有詳細步驟,這里不再贅述,docker環(huán)境可以參考docker無憂包安裝,非常便捷。
編譯環(huán)境和代碼環(huán)境準備好后,用 python build.py wifiiot 就可執(zhí)行代碼編譯。docker環(huán)境下非首次編譯基本就幾十秒就編完。


燒錄環(huán)境需要注意的幾個事項
一、hpm無法加載文件問題:
1. 以管理員身份運行vscode;
2. 執(zhí)行:get-ExecutionPolicy,顯示Restricted,表示狀態(tài)是禁止的;
3. 執(zhí)行:set-ExecutionPolicy RemoteSigned;
4. 這時再執(zhí)行get-ExecutionPolicy,就顯示RemoteSigned;
二、MODULE_NOT_FOUND問題:

更改bundle.json中的%UPLOAD_SCRIPT%
三、固件不存在問題:

修改配置中燒錄文件路徑和燒錄方式

等到下面打印出現(xiàn)說明燒錄完成

可以使用串口工具看下Hi3861的啟動是否正常

在看到wifi init success打印后在串口使用AT指令測試連通性


到此,所有環(huán)境搭建驗證完成??梢杂淇斓臄]代碼了。
接下來簡單的用python在PC端搭建了一個tcp server,在Hi3861上搭建了tcp client,PC上通過socket發(fā)送指令給Hi3861,控制LED燈的亮滅。下面是簡單的代碼摘要。
- int tcp_client_demo(void)
- {
- /* 服務器的地址信息 */
- struct sockaddr_in server_addr;
- ssize_t ret;
- printf("%s %d \r\n", __FILE__, __LINE__);
- sleep(10);
- /* 1 、創(chuàng)建socket */
- if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- printf("%s %d \r\n", __FILE__, __LINE__);
- perror("socket is error\r\n");
- exit(1);
- }
- /* 配置server端的IP和端口 */
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(50007);
- server_addr.sin_addr.s_addr = inet_addr("192.168.1.101");
- printf("%s %d \r\n", __FILE__, __LINE__);
- /* 連接 tcp server*/
- if(connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
- {
- printf("%s %d \r\n", __FILE__, __LINE__);
- perror("connect is error\r\n");
- exit(1);
- }
- printf("%s %d \r\n", __FILE__, __LINE__);
- /* 接收循環(huán) */
- while(1)
- {
- if((ret = recv(sock_fd, recvbuf, sizeof(recvbuf), 0)) == -1){
- printf("recv error \r\n");
- return -1;
- }
- printf("recv :\r\n");
- printf("%s", recvbuf);
- printf("\r\n");
- if(recvbuf[0] == '1')
- {
- /* Turn the LED ON. */
- g_ledState = LED_ON;
- printf("LED turned ON\n");
- sprintf(sendbuf, "LED turned ON");
- }
- else if(recvbuf[0] == '0')
- {
- /* Turn the LED OFF. */
- g_ledState = LED_OFF;
- printf("LED turned OFF\n");
- sprintf(sendbuf, "LED turned OFF");
- }
- else
- {
- printf("Invalid command\n");
- sprintf(sendbuf, "Invalid command");
- }
- if((ret = send(sock_fd, sendbuf, strlen(sendbuf) + 1, 0)) == -1)
- {
- perror("send : ");
- }
- sleep(2);
- }
- close(sock_fd);
- return 0;
- }
下面是python server的相關代碼摘要。
- def echo_server(host, port):
- print("==========================")
- print("TCP Server")
- print("==========================")
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
- try:
- s.bind((host, port))
- s.listen(1)
- except socket.error as msg:
- print("ERROR: ", msg)
- s.close()
- s = None
- if s is None:
- sys.exit(1)
- while 1:
- print("Listening on: %s:%d"%(host, port))
- data_len = 0
- try:
- conn, addr = s.accept()
- except KeyboardInterrupt:
- print("Closing Connection")
- s.close()
- s = None
- sys.exit(1)
- print('Incoming connection accepted: ', addr)
- try:
- while 1:
- data = input("Enter your option: '1' to turn ON LED, '0' to trun OFF LED and Press the 'Enter' key: ")
- conn.send(data.encode())
- data = conn.recv(4096)
- if not data: break
- print("Acknowledgement from TCP Client:", data.decode('utf-8'))
- print("")
- except KeyboardInterrupt:
- print("Closing Connection")
- s.close()
- s = None
- sys.exit(1)
- conn.close()
最終實現(xiàn)的效果如下:

相關代碼已經(jīng)打包上傳,希望接下來能繼續(xù)用鴻蒙做出一些有趣的事情!
©著作權(quán)歸作者和HarmonyOS技術社區(qū)共同所有,如需轉(zhuǎn)載,請注明出處,否則將追究法律責任。
https://harmonyos.51cto.com/#zz