737|0

182

帖子

1

TA的资源

一粒金砂(高级)

楼主
 

【得捷电子Follow me第4期】基础任务二-TCP server和UDP server [复制链接]

  本帖最后由 chrisrh 于 2024-2-18 20:52 编辑

TCP Server

将pico+W5500通过网线接到路由或者交换机上

通过CMD确定ipconfig的IP地址,将TCP和UDP与其配置在同一个网段下进行连接

这里主机地址是192.168.31.171

将pico+W5500配置为192.168.31.100,端口号2233

import board
import busio
import digitalio
import time
import array

from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_sharpmemorydisplay

'''LED init'''
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT

'''Sharp Display init'''
# 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)

'''W5500 init start'''
##SPI0-W5500
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17

##reset
W5x00_RSTn = board.GP20

print("Wiznet5k Simple TCP Server Test")

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

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)#, mac=MY_MAC, debug=True)
# # Set network configuration
eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)

# Initialize ethernet interface with DHCP
#eth = WIZNET5K(spi_bus, cs, is_dhcp=True, mac=MY_MAC, debug=True)

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

'''W5500 init end'''

'''TCP socket init'''
# 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 = 2233  # Port to listen on
server.bind((server_ip, server_port))  # Bind to IP and Port
server.listen()  # Begin listening for incoming clients
#conn, addr = server.accept()  # Wait for a connection from a client.
#print("socket connected")

'''display show'''
display.fill(1)
chip_version = "chip version:" + eth.chip
display.text(chip_version, 0, 0, 0)
ip_addr = "ip:" + eth.pretty_ip(eth.ip_address)
display.text(ip_addr, 0, 10, 0)
mac_addr = "mac:" + eth.pretty_mac(eth.mac_address)
display.text(mac_addr, 0, 20, 0)
display.show()

row = 30
print("socket connected")

while True:
    # Maintain DHCP lease
    #eth.maintain_dhcp_lease()
    
    led.value = not led.value
    time.sleep(1)
    
    with server:
        while True:
#           data = conn.recv(20)
            data,addr = server.recvfrom(20)
            if data:
                print(data)
                display.fill_rect(0,row,144,10,1)
                display.text(data.decode('utf-8'), 0, row, 0)
                display.show()

                row += 10
                if row > 150:
                    row = 30

 

 

 

 

手机端TCP/IP连接测试

 

 

 

 


UDP server

import board
import busio
import digitalio
import time
import array

from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_sharpmemorydisplay

'''LED init'''
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT

'''Sharp Display init'''
# 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)

'''W5500 init start'''
##SPI0-W5500
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17

##reset
W5x00_RSTn = board.GP20

print("Wiznet5k Simple UDP Server Test")

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

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)#, mac=MY_MAC, debug=True)
# # Set network configuration
eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)

# Initialize ethernet interface with DHCP
#eth = WIZNET5K(spi_bus, cs, is_dhcp=True, mac=MY_MAC, debug=True)

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

'''W5500 init end'''

'''UDP socket init'''
# Initialize a socket for our server
socket.set_interface(eth)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # Allocate socket for the server
server_ip = None  # IP address of server
server_port = 2233  # Port to listen on
server.bind((eth.pretty_ip(eth.ip_address), server_port))  # Bind to IP and Port
    

'''display show'''
display.fill(1)
chip_version = "chip version:" + eth.chip
display.text(chip_version, 0, 0, 0)
ip_addr = "ip:" + eth.pretty_ip(eth.ip_address)
display.text(ip_addr, 0, 10, 0)
mac_addr = "mac:" + eth.pretty_mac(eth.mac_address)
display.text(mac_addr, 0, 20, 0)
display.show()

row = 30
print("socket connected")

while True:
    # Maintain DHCP lease
    #eth.maintain_dhcp_lease()
    
    led.value = not led.value
    time.sleep(1)
    
    with server:
        while True:
#           data = conn.recv(20)
            data,addr = server.recvfrom(20)
            if data:
                print(data)
                display.fill_rect(0,row,144,10,1)
                display.text(data.decode('utf-8'), 0, row, 0)
                display.show()

                row += 10
                if row > 150:
                    row = 30
                    server.send(data)
                    #conn.send(data)  # Echo message back to client

 

 

 

 

手机端UDP连接测试

 

 

 

 

点赞 关注
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

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

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