3

新版发布及鸿蒙设备Python网络编程简介

 1 year ago
source link: https://www.51cto.com/article/720428.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

新版发布及鸿蒙设备Python网络编程简介

作者:唐佐林 2022-10-11 15:26:16
这一年里,我利用业余时间在多种场合下对Py4OH进行了应用开发测试,结果可谓是收获满满,也因此有了这个Beta版本的Release。
112a3375932443ab14f047ba83fc969ccc12b1.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

  大家好,自从去年(没错,是去年)发布了Py4OH第一个正式版之后,我开启了佛系开发模式,没有继续更新了。有小伙伴可能会问:这一年你干嘛去了,为啥没继续更新?

嗯。。。还是有必要解释一下!

Py4OH作为OpenHarmony轻量级设备上的Python开发平台,其目标是能够胜任工业应用程序的开发及部署,因此在稳定性上就会有要求,而稳定性只能通过时间验证。。。这一年里,我利用业余时间在多种场合下对Py4OH进行了应用开发测试,结果可谓是收获满满(臭虫多多),也因此有了这个Beta版本的Release。

Py4OH 1.1.0 Beta Release Notes:

  1. 优化REPL与设备的连接方式,增强连接稳定性
  2. 优化设备端内存利用率,减少长时间运行导致的内存碎片
  3. 重写Python内置函数print()的实现,解决高频打印时的内存泄露问题
  4. 重构外设接口(gpio,adc,i2c,uart)
  5. 新增网络接口(wifi,socket)
  6. 新增常用数学函数(sin,cos,log,......)

TODO:

  1. 适配小凌派开发板(RK2206)
  2. 适配拓维开发板(ESP32)
  3. 适配龙芯开发板(1C300B)
  4. 重构spi外设接口
  5. Bug Fix......

从上述介绍可以看出Py4OH 1.1.0新增了网络编程接口,那么具体如何使用呢?

废话不多说,直接上代码!

TCP客户端编程:

import socket
import gc
import os
# 创建客户端
client = socket.tcp_client()
# 打印客户端ID值
print('client = ', client)
# 连接服务端,成功返回 0,失败返回 -1
r = socket.connect(client, '192.168.3.3', 8989)
while r == 0 :
    # 获取当前可收取的数据量
    len = socket.available(client)  
    if len > 0 :
        # 收取数据,
        # 成功 r --> 0,失败 r --> -1
        # 数据 d --> 列表类型
        r, d = socket.recv(client, len)
        if r == 0 :
            print(d)     # 打印收到的数据
            socket.send(client, d)   # 将数据原封不动发回    
    gc.collect()
    os.usleep(10 * 1000)

TCP 客户端相关接口:

tcp_client()

TCP客户端ID值

创建TCP客户端

connect(fd, ip, port)

fd : int,客户端ID值

ip : string,IP地址

port : int,端口号

0 -- 连接成功

-1 -- 连接失败

通过ip地址和端口号连接到指定服务端

recv(fd, n)

fd : int,客户端ID值

n : int,字节数

(r, d)

r -- 0 成功,-1 失败

d -- 字节列表

最多收取n字节的TCP数据

send(fd, bytes)

fd : int,客户端ID值

bytes : list,字节列表

0 -- 发送成功

-1 -- 发送失败

发送TCP数据

available(fd)

fd : int,客户端ID值

n -- 字节数

获取当前有多少TCP数据可收取

close(fd)

fd : int,客户端ID值

关闭TCP客户端

TCP 服务端编程:

import socket
import wifi
import gc
import os
# 通过wifi模块获取当前设备IP地址
print('ip = ', wifi.ip_addr())
# 创建服务端
# 参数block指定服务端工作模式
# 阻塞模式   block -> True
# 非阻塞模式 block -> False
fd = socket.tcp_server(True)
# 打印服务端ID值
print('fd = ', fd)
# 监听设备8888端口,成功返回 0,失败返回 -1
r = socket.listen(fd, 8888)
print('r = ', r)
# 永久等待客户端连接(阻塞模式)
# 返回值 -> socket ID 用于与客户端通信
client = socket.accept(fd)
print('client = ', client)
feedback = [ord(c) for c in 'Tang ZuoLin']
while True:
    # 接收客户端数据,最多收取30字节
    r, d = socket.recv(client, 30)
    if r == 0 :
        print(d)           # 打印收到的数据
        socket.send(client, feedback)    # 将feedback发回客户端    
    gc.collect()
    os.usleep(10 * 1000)

TCP服务端相关接口:

tcp_server(block)

block : bool,是否阻塞

TCP服务端ID值

创建TCP服务端

listen(fd, port)

fd : int,服务端ID值

port : int,端口号

0 -- 监听成功

-1 -- 监听失败

监听指定端口

accept(fd)

fd : int,服务端ID值

socket id -- 通信ID值

接受客户端连接

close(fd)

fd : int,服务端ID值

关闭TCP服务端

UDP 编程:

import socket
import wifi
import gc
import os
# 通过wifi模块获取当前设备IP地址
print('ip = ', wifi.ip_addr())
# 创建通信端
point = socket.udp_point()
# 打印通信端ID值
print('point = ', point)
# 绑定设备端口,成功返回 0,失败返回 -1
r = socket.bind(point, 8989)
while r == 0 :
    # 获取当前可收取的数据量
    len = socket.available(point)    
    if len > 0 :
        # 收取数据,
        # 成功 r --> 0,失败 r --> -1
        # 数据 d --> 列表类型
        # 对端地址
        # 对端端口
        r, d, remote, port = socket.recvfrom(point, len)

        if r == 0 :
            print(d)           # 打印收到的数据
            print(remote)   # 打印对端IP地址
            print(port)       # 打印对端通信端口
            # 将数据原封不动发回
            socket.sendto(point, d, remote, port) 
    
    gc.collect()
    os.usleep(10 * 1000)

UDP 通信端相关接口:

udp_point()

UDP通信端ID值

创建UDP通信端

bind(fd, port)

fd : int,通信端ID值

port : int,端口号

0 -- 绑定成功

-1 -- 绑定失败

绑定UDP通信端口

recvfrom(fd, n)

fd : int,通信端ID值

n : int,字节数

(r, d, remote, port)

r -- 0 成功,-1 失败

d -- 字节列表

remote -- 对端地址

port -- 对端端口

最多收取n字节的UDP数据

sendto(fd, bytes, remote, port)

fd : int,通信端ID值

bytes : list,字节列表

remote : string,对端地址

port : int,对端通信端口

0 -- 发送成功

-1 -- 发送失败

发送UDP数据

available(fd)

fd : int,通信端ID值

n -- 字节数

获取当前有多少UDP数据可收取

close(fd)

fd : int,通信端ID值

关闭UDP通信端

既然提供了socket网络编程接口,那么wifi联网接口肯定是少不了的。

示例代码如下:

import wifi
import os
# 打印Py4OH版本号
print('version: ', os.version())
# 初始化wifi模块
wifi.wifi_init()
# 连接指定的wifi热点
r = wifi.connect('py4oh-test', '12345678')
print('r = ', r)
# 启动联网
r = wifi.start()
print('r = ', r)
# 打印当前设备IP地址
print('ip = ', wifi.ip_addr())

WIFI 联网相关接口:

wifi_init()

初始化WIFI模块

connect(id, pwd)

id : string,热点ID值

pwd : string,热点密码

0 -- 连接成功

-1 -- 连接失败

通过用户名和密码连接热点

start()

0 -- 启动成功

-1 -- 启动失败

开启设备联网

stop()

停止设备联网

is_ok()

True -- 联网成功

False -- 联网失败

获取设备联网状态

ip_addr()

设备IP地址

获取设备IP地址

wifi_deinit()

反初始化WIFI模块

文章相关附件可以点击下面的原文链接前往下载:

​https://ost.51cto.com/resource/2322。​

​https://ost.51cto.com/resource/2323。​

​https://ost.51cto.com/resource/2324。​

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 51CTO开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK