504|0

83

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【得捷Follow me第4期】项目总结 [复制链接]

  本帖最后由 bitter 于 2024-2-24 23:47 编辑

【得捷Follow me第4期】项目总结

很高兴能参加得捷电子Follow me第4期活动。

一、任务实现视频

【得捷Follow me第4期】项目总结-【得捷Follow me第4期】项目总结-EEWORLD大学堂

二、项目任务总结报告

1.入门任务:开发环境搭建,BLINK,驱动液晶显示器进行显示(没有则串口HelloWorld)

  • 硬件环境
W5500-EVB-PICO(主控)
AdafruitSharp Memory Display(显示)
根据相关资料,显示模块是通过SPI接口操作的,所以需要选定主控合适的引脚与显示模块引脚连接,完成spi驱动,选择以下引脚向连接。
W5500-EVB-PICO AdafruitSharp Memory Display
GPIO13 CS
GPIO14 CLK
GPIO15 DI
GND GND
3V3 VIN
  • 软件环境
安装完成后,打开,点击右下角对应的端口即可。
下载固件:
将开发板通过micro USB线接电脑,按住BOOTSEL键后再短按一下RUN键,最后松开BOOTSEL键,电脑会将开发板识别成U盘。
将前面下载好的固件复制到此U盘,复制完成后开发板会自动重启,至此固件烧录完成。
这里需要注意,我们应该选择与之前下载的固件版本对应的库下载。
将下载的压缩包解压,并将需要的库文件复制到CIRCUITPY的盘符的lib文件夹下。
这里需要将font5x8.bin文件放入CIRCUITPY的盘符下,否则后续使用将报错。
  • 相关代码
屏幕显示相关文字,并且每0.5s闪烁LED灯。
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 world")
    display.fill(1)
    display.text(" hello world!", 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()




 

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

  • 实现过程
    将以下库文件复制到CIRCUITPY的盘符的lib文件夹下。

    根据相关资料,RP2040与W5500引脚连接如下:

    将W5500-EVB-PICO接入网络,并确保与本机IP地址处于同一网段。
    查询本机IP地址为192.168.6.4

    配置w5500的IP地址为192.168.6.100,网关为192.168.6.1
    效果图

    本地PC Ping W5500-EVB-PICO

    抓包软件Wireshark抓取本地PC的ping报文

  • 相关代码

屏幕初始化

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

w5500初始化

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

完整代码

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 = (192, 168, 6, 100)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 6, 1)
DNS_SERVER = (8, 8, 8, 8)


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 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()

if __name__ == '__main__':
    # 初始化led和显示模块
    led,display = init()
    #静态初始化w5500
    eth = init_w5500(IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
    

    disp_w5500()

    while True:
        led_flush()

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

建立tcp服务器

def create_tcpserver(port):
    
    # Initialize a socket for our server
    socket.set_interface(eth)
    server = socket.socket()  # Allocate socket for the server
    server_ip = None  # IP address of server
    server_port = port  # Port to listen on
    server.bind((server_ip, server_port))  # Bind to IP and Port
    server.listen()  # Begin listening for incoming clients
    
    return server

完整代码

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 = (192, 168, 6, 100)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 6, 1)
DNS_SERVER = (8, 8, 8, 8)


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 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
    
    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 create_tcpserver(port):
    
    # Initialize a socket for our server
    socket.set_interface(eth)
    server = socket.socket()  # Allocate socket for the server
    server_ip = None  # IP address of server
    server_port = port  # Port to listen on
    server.bind((server_ip, server_port))  # Bind to IP and Port
    server.listen()  # Begin listening for incoming clients
    
    return server

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(5000)
    conn, addr = server.accept()  # Wait for a connection from a client.
    print("socket connected")
    print(addr)
    disp_str("TCP Server TEST", 30)
    disp_str('client:'+addr[0]+','+str(addr[1]), 50)
    disp_str('receive:', 60)
    linenum = 70
    while True:
        led_flush()
        
        with server:
            while True:
                data = conn.recv(20)
                if data:
                    print(data)
                    disp_str(data.decode('utf-8'),linenum)
                    linenum += 10
                    if linenum > 150:
                        linenum = 70




4.进阶任务:从NTP服务器(注意数据交互格式的解析)同步时间,获取时间送显示屏(串口)显示。

  • 实现过程
    不知道为什么使用CircuitPython官方例程都报错

    所以更换MicroPython,这里参考了@太阳上的骑士的代码
    实现效果
  • 相关代码

完整代码

from usocket import socket
from machine import Pin,SPI,UART,RTC
import network
import time
import ntptime

uart = UART(0, baudrate=115200, bits=8, stop=1)

#W5x00 chip init
def w5x00_init():
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
    nic.active(True)
    
    #None DHCP
    nic.ifconfig(('192.168.6.100','255.255.255.0','192.168.6.1','8.8.8.8'))
    
    #DHCP
    #nic.ifconfig('dhcp')
    
    print('IP address :', nic.ifconfig())
    while not nic.isconnected():
        time.sleep(1)
        #print(nic.regs())
            
def show_local_time(timezone=8):
    rtc = RTC()
    now = time.time()
    now += timezone * 3600
    t = time.localtime(now)
    print(f'{t[0]} - {t[1]:02d}-{t[2]:02d} {t[3]:02d}:{t[4]:02d}:{t[5]:02d}')
    uart.write(f'{t[0]} - {t[1]:02d}-{t[2]:02d} {t[3]:02d}:{t[4]:02d}:{t[5]:02d}')
        
def main():
    w5x00_init()
    
    #先手动设置一个错误时间,模拟系统时间不准
    rtc = RTC()
    rtc.datetime((2020, 1, 1, 3, 9, 0, 0, 0))   #年、月、日、星期、时、分、秒、亚秒
    print('校时前系统时间:')
    show_local_time()
    
    #NTP校时
    print('开始NTP校时...')
    ntptime.host = 'ntp1.aliyun.com'
    ntptime.settime()
    print(f'校时后系统时间:')
    show_local_time()
    while True:
        show_local_time()
        time.sleep(1)

if __name__ == "__main__":
    main()



5.终极任务二:使用外部存储器,组建简易FTP文件服务器,并能正常上传下载文件。

  • 实现过程
    这里参考了@ eew_uscYT9的代码
    在window的文件夹内搜索ftp服务器


    将文件文件拖入其中


    断开连接,重新访问

    下载文件
  • 相关代码

完整代码

#
# Small ftp server for ESP8266 ans ESP32 Micropython
#
# Based on the work of chrisgp - Christopher Popp and pfalcon - Paul Sokolovsky
#
# The server accepts passive mode only.
# It runs in foreground and quits, when it receives a quit command
# Start the server with:
#
# import ftp
#
# Copyright (c) 2016 Christopher Popp (initial ftp server framework)
# Copyright (c) 2016 Robert Hammelrath (putting the pieces together
# and a few extensions)
# Distributed under MIT License
#
import socket
import network
import uos
import gc
#from usocket import socket
from machine import Pin,SPI,UART,PWM
import time, network,framebuf

''' static netinfo
'''
ip = '192.168.6.100'
sn = '255.255.255.0'
gw = '192.168.6.1'
dns= '8.8.8.8'

BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9

netinfo=(ip, sn, gw, dns)

localip = ''
localport = 8000
listen_info = (localip, localport)

conn_flag = False

def w5x00_init():
    global localip
  
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20))
    nic.active(True)
    # use dhcp, if fail use static netinfo
    #try:
    #    nic.ifconfig('dhcp')
    #except:
    nic.ifconfig(netinfo)
    localip = nic.ifconfig()[0]
    print('ip :', nic.ifconfig()[0])
    print('sn :', nic.ifconfig()[1])
    print('gw :', nic.ifconfig()[2])
    print('dns:', nic.ifconfig()[3])
 
    
    while not nic.isconnected():
        time.sleep(1)
#         print(nic.regs())
        print('no link')
        
    return nic
        


def send_list_data(path, dataclient, full):
    try:  # whether path is a directory name
        for fname in sorted(uos.listdir(path), key=str.lower):
            dataclient.sendall(make_description(path, fname, full))
    except:  # path may be a file name or pattern
        pattern = path.split("/")[-1]
        path = path[:-(len(pattern) + 1)]
        if path == "":
            path = "/"
        for fname in sorted(uos.listdir(path), key=str.lower):
            if fncmp(fname, pattern):
                dataclient.sendall(make_description(path, fname, full))


def make_description(path, fname, full):
    if full:
        stat = uos.stat(get_absolute_path(path, fname))
        file_permissions = ("drwxr-xr-x"
                            if (stat[0] & 0o170000 == 0o040000)
                            else "-rw-r--r--")
        file_size = stat[6]
        description = "{}    1 owner group {:>10} Jan 1 2000 {}\r\n".format(
                file_permissions, file_size, fname)
    else:
        description = fname + "\r\n"
    return description


def send_file_data(path, dataclient):
    with open(path, "rb") as file:
        chunk = file.read(512)
        while len(chunk) > 0:
            dataclient.sendall(chunk)
            chunk = file.read(512)


def save_file_data(path, dataclient):
    with open(path, "wb") as file:
        chunk = dataclient.recv(512)
        while len(chunk) > 0:
            file.write(chunk)
            chunk = dataclient.recv(512)


def get_absolute_path(cwd, payload):
    # Just a few special cases "..", "." and ""
    # If payload start's with /, set cwd to /
    # and consider the remainder a relative path
    if payload.startswith('/'):
        cwd = "/"
    for token in payload.split("/"):
        if token == '..':
            if cwd != '/':
                cwd = '/'.join(cwd.split('/')[:-1])
                if cwd == '':
                    cwd = '/'
        elif token != '.' and token != '':
            if cwd == '/':
                cwd += token
            else:
                cwd = cwd + '/' + token
    return cwd


# compare fname against pattern. Pattern may contain
# wildcards ? and *.
def fncmp(fname, pattern):
    pi = 0
    si = 0
    while pi < len(pattern) and si < len(fname):
        if (fname[si] == pattern[pi]) or (pattern[pi] == '?'):
            si += 1
            pi += 1
        else:
            if pattern[pi] == '*':  # recurse
                if (pi + 1) == len(pattern):
                    return True
                while si < len(fname):
                    if fncmp(fname[si:], pattern[pi+1:]):
                        return True
                    else:
                        si += 1
                return False
            else:
                return False
    if pi == len(pattern.rstrip("*")) and si == len(fname):
        return True
    else:
        return False


def ftpserver(net, port=21, timeout=None):

    DATA_PORT = 13333

    ftpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    datasocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    ftpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    datasocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    ftpsocket.bind(socket.getaddrinfo("0.0.0.0", port)[0][4])
    datasocket.bind(socket.getaddrinfo("0.0.0.0", DATA_PORT)[0][4])

    ftpsocket.listen(1)
    ftpsocket.settimeout(timeout)
    datasocket.listen(1)
    datasocket.settimeout(None)

    msg_250_OK = '250 OK\r\n'
    msg_550_fail = '550 Failed\r\n'
    addr = net.ifconfig()[0]
    print("FTP Server started on ", addr)
    try:
        dataclient = None
        fromname = None
        do_run = True
        while do_run:
            cl, remote_addr = ftpsocket.accept()
            cl.settimeout(300)
            cwd = '/'
            try:
                # print("FTP connection from:", remote_addr)
                cl.sendall("220 Hello, this is Micropython.\r\n")
                while True:
                    gc.collect()
                    data = cl.readline().decode("utf-8").rstrip("\r\n")
                    if len(data) <= 0:
                        print("Client disappeared")
                        do_run = False
                        break

                    command = data.split(" ")[0].upper()
                    payload = data[len(command):].lstrip()

                    path = get_absolute_path(cwd, payload)

                    print("Command={}, Payload={}".format(command, payload))

                    if command == "USER":
                        cl.sendall("230 Logged in.\r\n")
                    elif command == "SYST":
                        cl.sendall("215 UNIX Type: L8\r\n")
                    elif command == "NOOP":
                        cl.sendall("200 OK\r\n")
                    elif command == "FEAT":
                        cl.sendall("211 no-features\r\n")
                    elif command == "PWD" or command == "XPWD":
                        cl.sendall('257 "{}"\r\n'.format(cwd))
                    elif command == "CWD" or command == "XCWD":
                        try:
                            files = uos.listdir(path)
                            cwd = path
                            cl.sendall(msg_250_OK)
                        except:
                            cl.sendall(msg_550_fail)
                    elif command == "CDUP":
                        cwd = get_absolute_path(cwd, "..")
                        cl.sendall(msg_250_OK)
                    elif command == "TYPE":
                        # probably should switch between binary and not
                        cl.sendall('200 Transfer mode set\r\n')
                    elif command == "SIZE":
                        try:
                            size = uos.stat(path)[6]
                            cl.sendall('213 {}\r\n'.format(size))
                        except:
                            cl.sendall(msg_550_fail)
                    elif command == "QUIT":
                        cl.sendall('221 Bye.\r\n')
                        do_run = False
                        break
                    elif command == "PASV":
                        cl.sendall('227 Entering Passive Mode ({},{},{}).\r\n'.
                                   format(addr.replace('.', ','), DATA_PORT >> 8,
                                          DATA_PORT % 256))
                        dataclient, data_addr = datasocket.accept()
                        print("FTP Data connection from:", data_addr)
                        DATA_PORT = 13333
                        active = False
                    elif command == "PORT":
                        items = payload.split(",")
                        if len(items) >= 6:
                            data_addr = '.'.join(items[:4])
                            # replace by command session addr
                            if data_addr == "127.0.1.1":
                                data_addr = remote_addr
                            DATA_PORT = int(items[4]) * 256 + int(items[5])
                            dataclient = socket.socket(socket.AF_INET,
                                                       socket.SOCK_STREAM)
                            dataclient.settimeout(10)
                            dataclient.connect((data_addr, DATA_PORT))
                            print("FTP Data connection with:", data_addr)
                            cl.sendall('200 OK\r\n')
                            active = True
                        else:
                            cl.sendall('504 Fail\r\n')
                    elif command == "LIST" or command == "NLST":
                        if not payload.startswith("-"):
                            place = path
                        else:
                            place = cwd
                        try:
                            cl.sendall("150 Here comes the directory listing.\r\n")
                            send_list_data(place, dataclient,
                                           command == "LIST" or payload == "-l")
                            cl.sendall("226 Listed.\r\n")
                        except:
                            cl.sendall(msg_550_fail)
                        if dataclient is not None:
                            dataclient.close()
                            dataclient = None
                    elif command == "RETR":
                        try:
                            cl.sendall("150 Opening data connection.\r\n")
                            send_file_data(path, dataclient)
                            cl.sendall("226 Transfer complete.\r\n")
                        except:
                            cl.sendall(msg_550_fail)
                        if dataclient is not None:
                            dataclient.close()
                            dataclient = None
                    elif command == "STOR":
                        try:
                            cl.sendall("150 Ok to send data.\r\n")
                            save_file_data(path, dataclient)
                            cl.sendall("226 Transfer complete.\r\n")
                        except:
                            cl.sendall(msg_550_fail)
                        if dataclient is not None:
                            dataclient.close()
                            dataclient = None
                    elif command == "DELE":
                        try:
                            uos.remove(path)
                            cl.sendall(msg_250_OK)
                        except:
                            cl.sendall(msg_550_fail)
                    elif command == "RMD" or command == "XRMD":
                        try:
                            uos.rmdir(path)
                            cl.sendall(msg_250_OK)
                        except:
                            cl.sendall(msg_550_fail)
                    elif command == "MKD" or command == "XMKD":
                        try:
                            uos.mkdir(path)
                            cl.sendall(msg_250_OK)
                        except:
                            cl.sendall(msg_550_fail)
                    elif command == "RNFR":
                            fromname = path
                            cl.sendall("350 Rename from\r\n")
                    elif command == "RNTO":
                            if fromname is not None:
                                try:
                                    uos.rename(fromname, path)
                                    cl.sendall(msg_250_OK)
                                except:
                                    cl.sendall(msg_550_fail)
                            else:
                                cl.sendall(msg_550_fail)
                            fromname = None
                    elif command == "MDTM":
                        try:
                            tm=localtime(uos.stat(path)[8])
                            cl.sendall('213 {:04d}{:02d}{:02d}{:02d}{:02d}{:02d}\r\n'.format(*tm[0:6]))
                        except:
                            cl.sendall('550 Fail\r\n')
                    elif command == "STAT":
                        if payload == "":
                            cl.sendall("211-Connected to ({})\r\n"
                                       "    Data address ({})\r\n"
                                       "211 TYPE: Binary STRU: File MODE:"
                                       " Stream\r\n".format(
                                           remote_addr[0], addr))
                        else:
                            cl.sendall("213-Directory listing:\r\n")
                            send_list_data(path, cl, True)
                            cl.sendall("213 Done.\r\n")
                    else:
                        cl.sendall("502 Unsupported command.\r\n")
                        print("Unsupported command {} with payload {}".format(
                            command, payload))
            except Exception as err:
                print(err)

            finally:
                cl.close()
                cl = None
    except Exception as e:
        print(e)
    finally:
        datasocket.close()
        ftpsocket.close()
        if dataclient is not None:
            dataclient.close()

if __name__ == "__main__":
    
    nic = w5x00_init()
    ftpserver(nic)




三、心得体会

很高兴能够参加本期由EEWORLD和得捷电子共同举办得捷电子Follow me第4期活动。在这次活动,我了解了如何建立TCP或UDP连接,如何从NTP服务器同步时间,如何组建简易FTP文件服务器。
最后,希望电子工程世界和得捷电子越办越好,以后能够多多的举办这样的活动,也希望大家能够从中收获满满。
task0.py (1.09 KB, 下载次数: 0)
tesk1.py (2.69 KB, 下载次数: 0)
task2.py (4.11 KB, 下载次数: 0)
task3.py (1.49 KB, 下载次数: 0)
task4.py (13.94 KB, 下载次数: 1)
点赞 关注
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
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
快速回复 返回顶部 返回列表