本文實(shí)現(xiàn)了用潤和 HiHope Pegasus 3861 開發(fā)板實(shí)現(xiàn) TCP 電子琴。
樣例運(yùn)行的 OpenHarmony 源碼環(huán)境下載:
https://gitee.com/HiSpark/hi3861_hdu_iot_application通過本文您將了解:
HiHopePegasus 3861 開發(fā)環(huán)境選擇
HiHopePegasus 3861 連接 wifi
HiHopePegasus 3861 TCP 連接
HiHopePegasus 3861 環(huán)境監(jiān)測板上蜂鳴器的控制
TCP電子琴樣例運(yùn)行效果
3861 開發(fā)板作為 TCP 服務(wù)端與 TCP 客戶端(自己的設(shè)備)連接。
TCP 客戶端發(fā)送 12345678,控制開發(fā)板上環(huán)境拓展板上的蜂鳴器發(fā)出 duō lái mī fā suō lā xī duō 8 種音調(diào)。
樣例運(yùn)行步驟
①下載源文件
https://gitee.com/from-north-to-north/open-armony-north/tree/master/潤和3861輕量系統(tǒng)TCP電子琴
將源文件文件放置在命名為 TCP_keyboard_demo 的文件夾下,將其放置在 OpenHarmony 輕量系統(tǒng)源碼 applicationssamplewifi-iotapp 目錄下。
②修改 net_params.h 文件的相關(guān)代碼
如下:
PARAM_HOTSPOT_SSID 設(shè)置為要連接的熱點(diǎn)名稱
PARAM_HOTSPOT_PSK 設(shè)置為要連接的熱點(diǎn)秘碼
PARAM_SERVER_ADDR 設(shè)置為要連接的作為 TCP 客戶端的設(shè)備 IP 地址
PARAM_SERVER_PORT 設(shè)置 3861開發(fā)板(作為 TCP 服務(wù)端) TCP socket端口號(hào)
③在源碼 applicationssamplewifi-iotappBUILD.gn 文件 features 字段下添加"TCP_keyboard_demo:TCP_keyboard_demo",使樣例 demo 加入編譯。
④根據(jù) 3861 開發(fā)板的 ip(串口打印出來的)和 net_params.h 中填寫的 TCP socket 端口號(hào)創(chuàng)建客戶端,連接 3861 開發(fā)板。
主控芯片對應(yīng)關(guān)系
環(huán)境監(jiān)測板上蜂鳴器與主控芯片(Pegasus)引腳的對應(yīng)關(guān)系:GPIO9/PWM0。
源碼分析
文件說明:
tcp_server_test.c 文件解析:
#include#include #include #include //TCP連接的部分 #include"net_demo.h" #include"net_common.h" /********************環(huán)境監(jiān)測板上蜂鳴器的部分*********************************/ #include"iot_gpio.h" #include"iot_pwm.h" #include"hi_pwm.h" #include"hi_io.h" #defineBEEP_PIN_NAME9 #defineBEEP_PIN_FUNCTION5 #defineWIFI_IOT_PWM_PORT_PWM00 #defineBEEP_PWM_DUTY50 /*********************************************************************/ #defineDELAY_1S(1) voidTcpServerTest(unsignedshortport) { /********************環(huán)境監(jiān)測板上蜂鳴器的部分*********************************/ //初始化蜂鳴器 //蜂鳴器:GPIO9/PWM0 //環(huán)境監(jiān)測板上蜂鳴器與主控芯片(Pegasus)引腳的對應(yīng)關(guān)系GPIO9/PWM0 //IoTGpioInit用于初始化GPIO設(shè)備 IoTGpioInit(BEEP_PIN_NAME); //hi_io_set_func用于配置某個(gè)IO的復(fù)用功能 hi_io_set_func(BEEP_PIN_NAME,BEEP_PIN_FUNCTION); //IoTGpioSetDir用于設(shè)置GPIO引腳的方向 IoTGpioSetDir(BEEP_PIN_NAME,IOT_GPIO_DIR_OUT); //IoTPwmInit用于初始化PWM設(shè)備 IoTPwmInit(WIFI_IOT_PWM_PORT_PWM0); /***************************************************************************************/ ssize_tretval=0; intbacklog=1; intsockfd=socket(AF_INET,SOCK_STREAM,0);//TCPsocket intconnfd=-1; structsockaddr_inclientAddr={0}; socklen_tclientAddrLen=sizeof(clientAddr); structsockaddr_inserverAddr={0}; serverAddr.sin_family=AF_INET; serverAddr.sin_port=htons(port);//端口號(hào),從主機(jī)字節(jié)序轉(zhuǎn)為網(wǎng)絡(luò)字節(jié)序 serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);//允許任意主機(jī)接入,0.0.0.0 /*************************************綁定端口****************************************************/ retval=bind(sockfd,(structsockaddr*)&serverAddr,sizeof(serverAddr)); if(retval0)?{ ????????printf("_______________________________________ "); ????????printf("bind?failed,?%ld! ",?retval); ????????//關(guān)閉socket ????????printf("do_cleanup... "); ????????close(sockfd); ????}else{ ????printf("_______________________________________ "); ????printf("bind?to?port?%hu?success! ",?port); ????} /***********************************************************************************************/ ????retval?=?listen(sockfd,?backlog);?//?開始監(jiān)聽 ????if?(retval?0)?{ ????????printf("_______________________________________ "); ????????printf("listen?failed! "); ????????//關(guān)閉socket ????????printf("do_cleanup... "); ????????close(sockfd); ????}else{ ????printf("_______________________________________ "); ????printf("listen?with?%d?backlog?success! ",?backlog); ????} /**************************************接受客戶端連接*********************************************************/ ????//?接受客戶端連接,成功會(huì)返回一個(gè)表示連接的 socket , clientAddr 參數(shù)將會(huì)攜帶客戶端主機(jī)和端口信息?;失敗返回?-1 ????//?此后的?收、發(fā)?都在?表示連接的 socket 上進(jìn)行;之后 sockfd 依然可以繼續(xù)接受其他客戶端的連接, ????//??UNIX系統(tǒng)上經(jīng)典的并發(fā)模型是“每個(gè)連接一個(gè)進(jìn)程”——?jiǎng)?chuàng)建子進(jìn)程處理連接,父進(jìn)程繼續(xù)接受其他客戶端的連接 ????//??鴻蒙liteos-a內(nèi)核之上,可以使用UNIX的“每個(gè)連接一個(gè)進(jìn)程”的并發(fā)模型 ????//?????liteos-m內(nèi)核之上,可以使用“每個(gè)連接一個(gè)線程”的并發(fā)模型 ????connfd?=?accept(sockfd,?(struct?sockaddr?*)&clientAddr,?&clientAddrLen); ????if?(connfd?0)?{ ????????printf("accept?failed,?%d,?%d ",?connfd,?errno); ????????//關(guān)閉socket ????????printf("do_cleanup... "); ????????close(sockfd); ????}else{ ????printf("_______________________________________ "); ????printf("accept?success,?connfd?=?%d! ",?connfd); ????printf("client?addr?info:?host?=?%s,?port?=?%hu ",?inet_ntoa(clientAddr.sin_addr),?ntohs(clientAddr.sin_port)); ????} /***********************************socket收、發(fā)的部分************************************************************/ ??//?后續(xù)?收、發(fā)?都在?表示連接的 socket 上進(jìn)行; ????while?(1)?{ ???????char?request[128]?=?""; ???????//接收客戶端發(fā)送過來的數(shù)據(jù) ???????retval?=?recv(connfd,?request,?sizeof(request),?0); ???????if?(retval?0)?{ ???????????printf("_______________________________________ "); ???????????printf("recv?request?failed,?%ld! ",?retval); ???????????//關(guān)閉與客戶端的連接 ???????????printf("do_disconnect... "); ???????????sleep(DELAY_1S); ???????????close(connfd); ???????????sleep(DELAY_1S);?//?for?debug ????????}else{ ???????????printf("_______________________________________ "); ???????????printf("The?data?received?from?the?client?is?%s? ",?request); ????????} ????????//發(fā)送數(shù)據(jù)到客戶端 ????????retval?=?send(connfd,?request,?strlen(request),?0); ????????if?(retval?<=?0)?{ ???????????printf("_______________________________________ "); ???????????printf("send?response?failed,?%ld! ",?retval); ???????????//關(guān)閉與客戶端的連接 ???????????printf("do_disconnect... "); ???????????sleep(DELAY_1S); ???????????close(connfd); ???????????sleep(DELAY_1S);?//?for?debug ????????}else{ ???????????printf("The?data?responsed?to?the?client?is?%s ",?request); ????????} /************************tcp?客戶端發(fā)送12345678控制不同的音調(diào)****************************/ ??????int?i=atoi(request);?//?將字符串request轉(zhuǎn)換成int類型 ??????hi_pwm_set_clock(PWM_CLK_XTAL);?//?設(shè)置時(shí)鐘源為晶體時(shí)鐘(40MHz,默認(rèn)時(shí)鐘源160MHz) ????????switch?(i) ????????{ ?????????case?1: ??????????printf("Tone?is?1 "?); ????????/** ??????????* IoTPwmStart 根據(jù)給定的輸出頻率和占空比啟動(dòng)指定端口的 PWM 信號(hào)輸出。 ??????????*?-?port?指示?PWM?設(shè)備的端口號(hào) ??????????*?-?占空比?表示 PWM 信號(hào)輸出的占空比。值范圍為 1 到 99。 ??????????*?-?頻率?指示 PWM 信號(hào)輸出的頻率。 ????????**/ ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?2500);??//通過調(diào)節(jié)PWM?信號(hào)輸出的頻率控制蜂鳴器聲調(diào)(調(diào)到2000聽不到蜂鳴器聲音) ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break;?? ?????????case?2: ??????????printf("Tone?is?2 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?3000); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break;???? ?????????case?3: ??????????printf("Tone?is?3 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?3500); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ?????????case?4: ??????????printf("Tone?is?3 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?4000); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ?????????case?5: ??????????printf("Tone?is?5 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?4500); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ?????????case?6: ??????????printf("Tone?is?6 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?5000); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ?????????case?7: ??????????printf("Tone?is?7 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?5500); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ???????case?8: ??????????printf("Tone?is?8 "); ??????????IoTPwmStart(WIFI_IOT_PWM_PORT_PWM0,?BEEP_PWM_DUTY,?6000); ??????????usleep(500000);//音長 ??????????IoTPwmStop(WIFI_IOT_PWM_PORT_PWM0); ??????????break; ?????????default:break; ????????} /***********************************************************************************************/ ???????usleep(10); ????} /***********************************************************************************************/ } SERVER_TEST_DEMO(TcpServerTest);
審核編輯:湯梓紅
-
TCP
+關(guān)注
關(guān)注
8文章
1353瀏覽量
79068 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5046瀏覽量
97437 -
電子琴
+關(guān)注
關(guān)注
4文章
152瀏覽量
30515 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2350瀏覽量
42843 -
HiHope
+關(guān)注
關(guān)注
0文章
63瀏覽量
3542
原文標(biāo)題:鴻蒙開發(fā)板上實(shí)現(xiàn)TCP電子琴
文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論