1035|1

47

帖子

2

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第4期】交作业 [复制链接]

 

代码:

https://download.eeworld.com.cn/detail/leihaozhuce/631463

 

视频:

https://training.eeworld.com.cn/video/39558

 

按照官方要求使用W5500-EVB-Pico、 Adafruit Sharp Memory Display两种器件完成开发。软件选用python语言进行编程,circuit python的库很全,更新也很快,非常好用,大大降低了开发难度。

     在树莓派平台上进行开发,体验还不错。

   

 

    编译器用树莓派自带的Thonny,简单易用。

   首先搭建开发环境,去官网下载最新固件,写入到w5500中,常规操作就不再重复写了。

   第一步电灯,代码如下:

  • import machine
  • import time
  • # 初始化LED引脚为输出模式
  • led = machine.Pin('LED', machine.Pin.OUT)
  • while True:
  • # 点亮LED
  • led.on()
  • time.sleep(1) # 等待1秒钟
  • # 熄灭LED
  • led.off()
  • time.sleep(1) # 等待1秒钟

运行程序

效果见下图

 驱动lcd显示屏,先安装好必要的库文件,去官网下载,比较慢,费了大劲才下载下来。

将adafruit_sharpmemorydisplay.mpy和adafruit_framebuf.mpy两个库文件拷贝到w5500的lib目录下。

连接lcd显示器,接线如下,还有3.3v和GND

  GP15 显示模块DI
  GP14 显示模块CLK
  GP13

显示模块CS

 如图所示

代码如下

  • import time
  • import board
  • import busio
  • import digitalio
  • import adafruit_sharpmemorydisplay
  • def led_flush():
  • led.value = not led.value
  • time.sleep(0.5)
  • def clear_disp():
  • # Clear the display. Always call show after changing pixels to make the display
  • # update visible!
  • display.fill(1)
  • display.show()
  • def disp_helloworld():
  • print("hello EEWORLD")
  • display.fill(1)
  • display.text(" hello EEWORLD!", 30, 50, 0)
  • display.show()
  • if __name__ == '__main__':
  • led = digitalio.DigitalInOut(board.GP25)
  • led.direction = digitalio.Direction.OUTPUT
  • # Initialize SPI bus and control pins
  • spi = busio.SPI(board.GP14, MOSI=board.GP15)
  • scs = digitalio.DigitalInOut(board.GP13) # inverted chip select
  • # pass in the display size, width and height, as well
  • # display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96)
  • display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)
  • clear_disp()
  • disp_helloworld()
  • while True:
  • led_flush()

完成主控板W5500初始化(静态IP配置),并能使用局域网电脑ping通,同时W5500可以ping通互联网站点;通过抓包软件(Wireshark、Sniffer等)抓取本地PC的ping报文,展示并分析。

通过网线将w5500接入到交换机上。

安装树莓派版的wireshark软件

安装好后用

$ sudo wireshark &

命令取得root权限,才能看到端口。

因为树莓派用的无线链接,这里选择    wlan0    ,就可以开始抓包了。

配置代码如下

  • import board
  • import busio
  • import digitalio
  • import time
  • from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
  • ##SPI0
  • SPI0_SCK = board.GP18
  • SPI0_TX = board.GP19
  • SPI0_RX = board.GP16
  • SPI0_CSn = board.GP17
  • ##reset
  • W5x00_RSTn = board.GP20
  • print("Wiznet5k Ping Test (no DHCP)")
  • # Setup your network configuration below
  • # random MAC, later should change this value on your vendor ID
  • MY_MAC = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05)
  • IP_ADDRESS = (192, 168, 1, 100)
  • SUBNET_MASK = (255, 255, 255, 0)
  • GATEWAY_ADDRESS = (192, 168, 1, 1)
  • DNS_SERVER = (8, 8, 8, 8)
  • led = digitalio.DigitalInOut(board.GP25)
  • led.direction = digitalio.Direction.OUTPUT
  • ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
  • ethernetRst.direction = digitalio.Direction.OUTPUT
  • # For Adafruit Ethernet FeatherWing
  • cs = digitalio.DigitalInOut(SPI0_CSn)
  • # For Particle Ethernet FeatherWing
  • # cs = digitalio.DigitalInOut(board.D5)
  • spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)
  • # Reset W5500 first
  • ethernetRst.value = False
  • time.sleep(1)
  • ethernetRst.value = True
  • # Initialize ethernet interface with DHCP
  • # eth = WIZNET5K(spi_bus, cs)
  • # Initialize ethernet interface without DHCP
  • eth = WIZNET5K(spi_bus, cs, is_dhcp=False, mac=MY_MAC)
  • # Set network configuration
  • eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
  • print("Chip Version:", eth.chip)
  • print("MAC Address:", [hex(i) for i in eth.mac_address])
  • print("My IP address is:", eth.pretty_ip(eth.ip_address))
  • while True:
  • led.value = not led.value
  • time.sleep(1)
  • print("Done!")

运行结果如下图

下显示开发板地址为192.168.1.100,下面进行ping测试,
能ping到开发板。抓包结果如下图。
抓包结果

 

 

 
主控板建立TCPIP或UDP服务器,局域网PC使用TCPIP或UDP客户端进行连接并发送数据,主控板接收到数据后,送液晶屏显示(没有则通过串口打印显示);通过抓包软件抓取交互报文,展示并分析。(TCP和UDP二选一,或者全都操作)
采用基础任务一的软件为基础,完成TCP和UDP服务器,服务器IP = 172.17.100.101,端口号Port = 40001 。

利用adafruit_wiznet5k_socket库,创建一个TCP服务器。函数输入参数为服务器端口。

接收到的信息会显示到LCD显示器上,显示分为三个区域:
区域1:以太网参数区,区域2:TCP连接信息显示区,区域3:接收到的信息显示区,显示函数的输入参数为待显示的字符串和显示的位置。
代码如下:
  • import time
  • import board
  • import busio
  • import digitalio
  • import array
  • import struct
  • import adafruit_sharpmemorydisplay
  • from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
  • import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
  • IP_ADDRESS = (172, 17, 100, 101)
  • SUBNET_MASK = (255, 255, 0, 0)
  • GATEWAY_ADDRESS = (172, 17, 64, 1)
  • DNS_SERVER = (218, 203, 59, 116)
  • def init():
  • led = digitalio.DigitalInOut(board.GP25)
  • led.direction = digitalio.Direction.OUTPUT
  • # Initialize SPI bus and control pins
  • spi = busio.SPI(board.GP14, MOSI=board.GP15)
  • scs = digitalio.DigitalInOut(board.GP13) # inverted chip select
  • # pass in the display size, width and height, as well
  • # display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96)
  • display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)
  • return led,display
  • def led_flush():
  • led.value = not led.value
  • time.sleep(0.5)
  • def clear_disp():
  • # Clear the display. Always call show after changing pixels to make the display
  • # update visible!
  • display.fill(1)
  • display.show()
  • def disp_helloworld():
  • print("hello world")
  • display.fill(1)
  • display.text(" hello world!", 30, 50, 0)
  • display.show()
  • def init_w5500(ip, subnet, gateway, dns):
  • ##SPI0
  • SPI0_SCK = board.GP18
  • SPI0_TX = board.GP19
  • SPI0_RX = board.GP16
  • SPI0_CSn = board.GP17
  • ##reset
  • W5x00_RSTn = board.GP20
  • print("Wiznet5k Ping Test (no DHCP)")
  • ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
  • ethernetRst.direction = digitalio.Direction.OUTPUT
  • # For Adafruit Ethernet FeatherWing
  • cs = digitalio.DigitalInOut(SPI0_CSn)
  • # For Particle Ethernet FeatherWing
  • # cs = digitalio.DigitalInOut(board.D5)
  • spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)
  • # Reset W5500 first
  • ethernetRst.value = False
  • time.sleep(1)
  • ethernetRst.value = True
  • # Initialize ethernet interface without DHCP
  • eth = WIZNET5K(spi_bus, cs, is_dhcp=False)
  • # Set network configuration
  • eth.ifconfig = (ip, subnet, gateway, dns)
  • print("Chip Version:", eth.chip)
  • print("My IP address is:", eth.pretty_ip(eth.ip_address))
  • return eth
  • def disp_w5500():
  • clear_disp()
  • display.fill(1)
  • cst = "chip version:" + eth.chip
  • display.text(cst, 0, 0, 0)
  • cst = "ip:" + eth.pretty_ip(eth.ip_address)
  • display.text(cst, 0, 10, 0)
  • cst = "mac:" + eth.pretty_mac(eth.mac_address)
  • display.text(cst, 0, 20, 0)
  • display.show()
  • def disp_str(s, addr):
  • display.fill_rect(0,addr,144,10,1)
  • display.text(s, 0, addr, 0)
  • display.show()
  • def inCksum(packet):
  • if len(packet) & 1:
  • packet = packet + '\0'
  • words = array.array('h', packet)
  • sum = 0
  • for word in words:
  • sum += (word & 0xffff)
  • sum = (sum>>16) + (sum & 0xffff)
  • sum = sum + (sum >> 16)
  • return (~sum) & 0xffff
  • def create_tcpserver(port):
  • # Initialize a socket for our server
  • socket.set_interface(eth)
  • sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Allocate socket for the server
  • sc_ip = None # IP address of server
  • sc_port = port # Port to listen on
  • sc.bind((sc_ip, sc_port)) # Bind to IP and Port
  • sc.listen() # Begin listening for incoming clients
  • return sc
  • def create_udpserver(port):
  • # Initialize a socket for our server
  • socket.set_interface(eth)
  • sc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Allocate socket for the server
  • # sc_ip = None # IP address of server
  • sc_port = port # Port to listen on
  • sc.bind((eth.pretty_ip(eth.ip_address), sc_port)) # Bind to IP and Port
  • return sc
  • if __name__ == '__main__':
  • # 初始化led和显示模块
  • led,display = init()
  • clear_disp()
  • #静态初始化w5500
  • eth = init_w5500(IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
  • disp_w5500()
  • # 创建一个tcp服务器
  • # server = create_tcpserver(40001)
  • # 创建一个udp服务器
  • server = create_udpserver(40001)
  • # conn, addr = server.accept() # Wait for a connection from a client.
  • print("socket connected")
  • # print(addr)
  • disp_str("UDP Server TEST", 30)
  • disp_str("socket connected", 40)
  • # disp_str('cli:'+addr[0]+','+str(addr[1]), 50)
  • disp_str('receive:', 60)
  • linenum = 70
  • while True:
  • led_flush()
  • with server:
  • while True:
  • # data = conn.recv(20)
  • data,addr = server.recvfrom(20)
  • disp_str('cli:'+addr[0]+','+str(addr[1]), 50)
  • if data:
  • print(data)
  • disp_str(data.decode('utf-8'),linenum)
  • linenum += 10
  • if linenum > 150:
  • linenum = 70
  • # conn.send(data) # Echo message back to client

 

运行结果如下图:

服务器建立起来。

但目前还未找到树莓派上用的TCP和UDP首发软件,具体测试还未进行。

 

搭建一个sntp的客户端从ntp授时服务器获取时间。

代码如下

  • import ntptime
  • import time
  • for i in range(10):
  • try:
  • ntptime.settime()
  • t = time.localtime(time.time() + 8*3600)
  • print("ntp time(BeiJing): %s-%s-%s %s:%s:%s" % (t[0],t[1],t[2],t[3],t[4],t[5]))
  • break
  • except:
  • print("Can not get time!")
  • time.sleep_ms(1000)

运行结果如下图

心得体会

python的开发库维护的挺好,器件种类很全,大大加快的开发进度,是开发的选择,网络编程要学习的还有很多,难度还是挺高的。

最后感谢得捷,感谢EEWORLD提。

最新回复

python的开发库维护的挺好,器件种类很全,大大加快的开发进度,是开发的选择,网络编程要学习的还有很多,难度还是挺高的。 看来上手任务非常简单,要感谢论坛举办这么好的活动。   详情 回复 发表于 2024-3-9 16:19
点赞 关注
 
 

回复
举报

7171

帖子

11

TA的资源

版主

沙发
 

python的开发库维护的挺好,器件种类很全,大大加快的开发进度,是开发的选择,网络编程要学习的还有很多,难度还是挺高的。

看来上手任务非常简单,要感谢论坛举办这么好的活动。

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
【有奖直播】2025是德科技数字月-数字新品来助阵
直播时间:3月19日(周三)14:00
直播奖励:小米口红充电宝、倍思充电线、是德科技十周年鼠标垫

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表