注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
我爱下载的个人空间 https://home.eeworld.com.cn/space-uid-101026.html [收藏] [复制] [分享] [RSS]
日志

【得捷电子Follow me第4期】基础任务一:Ping及抓包分析

已有 322 次阅读2024-2-5 08:44 |个人分类:产品评测

【得捷电子Follow me4期】基础任务Ping及抓包分析

硬件环境

基于入门任务的硬件基本环境构建了一个可以连接网络的局域网,局域网内有一台PC和W5500-EVB-Pico设备。

 

图1 网络架构

软件实现

采用入门任务基本软件环境,完成以太网驱动,并实现Ping的功能。

W5500初始化

利用adafruit_wiznet5k库,实现W5500的以太网驱动,并设定静态IP地址。
函数可以输入本机IP,子网掩码,默认网关和dns。
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

 

网络参数显示

当初始化W5500后,可以利用库中的功能接口读取W5500中被初始化的信息,并显示到LCD显示器上。
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()

 

完整实现

# 测试基础任务一

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


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


 

运行输出

模块运行状态

 

Ping的抓包分析

wireshark软件的安装不介绍了。利用wirshark抓取网络ping的数据帧,并解析。
Ping Request数据解析
No.     Time           Source                Destination           Protocol Length Info
    345 51.870236897   192.168.1.4           192.168.1.100         ICMP     98     Echo (ping) request  id=0x0001, seq=1/256, ttl=64 (reply in 346)

Frame 345: 98 bytes on wire (784 bits), 98 bytes captured (784 bits) on interface enp2s0, id 0
    Interface id: 0 (enp2s0)
        Interface name: enp2s0
    Encapsulation type: Ethernet (1)
    Arrival Time: Feb  5, 2024 07:43:16.074747971 CST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1707090196.074747971 seconds
    [Time delta from previous captured frame: 0.000007373 seconds]
    [Time delta from previous displayed frame: 0.000000000 seconds]
    [Time since reference or first frame: 51.870236897 seconds]
    Frame Number: 345
    Frame Length: 98 bytes (784 bits)
    Capture Length: 98 bytes (784 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:icmp:data]
    [Coloring Rule Name: ICMP]
    [Coloring Rule String: icmp || icmpv6]
Ethernet II, Src: WistronI_45:7c:1d (54:ee:75:45:7c:1d), Dst: de:ad:be:ef:fe:ed (de:ad:be:ef:fe:ed)
    Destination: de:ad:be:ef:fe:ed (de:ad:be:ef:fe:ed)
    Source: WistronI_45:7c:1d (54:ee:75:45:7c:1d)
    Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 192.168.1.4, Dst: 192.168.1.100
    0100 .... = Version: 4
    .... 0101 = Header Length: 20 bytes (5)
    Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
    Total Length: 84
    Identification: 0x5554 (21844)
    Flags: 0x40, Don't fragment
    ...0 0000 0000 0000 = Fragment Offset: 0
    Time to Live: 64
    Protocol: ICMP (1)
    Header Checksum: 0x619c [validation disabled]
    [Header checksum status: Unverified]
    Source Address: 192.168.1.4
    Destination Address: 192.168.1.100
Internet Control Message Protocol
    Type: 8 (Echo (ping) request)
    Code: 0
    Checksum: 0xc380 [correct]
    [Checksum Status: Good]
    Identifier (BE): 1 (0x0001)
    Identifier (LE): 256 (0x0100)
    Sequence Number (BE): 1 (0x0001)
    Sequence Number (LE): 256 (0x0100)
    [Response frame: 346]
    Timestamp from icmp data: Feb  5, 2024 07:43:16.000000000 CST
    [Timestamp from icmp data (relative): 0.074747971 seconds]
    Data (48 bytes)

0000  a0 23 01 00 00 00 00 00 10 11 12 13 14 15 16 17   .#..............
0010  18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27   ........ !"#$%&'
0020  28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37   ()*+,-./01234567
        Data: a023010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b…
        [Length: 48]

 

Ping Reply数据解析

No.     Time           Source                Destination           Protocol Length Info
    346 51.870317686   192.168.1.100         192.168.1.4           ICMP     98     Echo (ping) reply    id=0x0001, seq=1/256, ttl=128 (request in 345)

Frame 346: 98 bytes on wire (784 bits), 98 bytes captured (784 bits) on interface enp2s0, id 0
    Interface id: 0 (enp2s0)
        Interface name: enp2s0
    Encapsulation type: Ethernet (1)
    Arrival Time: Feb  5, 2024 07:43:16.074828760 CST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1707090196.074828760 seconds
    [Time delta from previous captured frame: 0.000080789 seconds]
    [Time delta from previous displayed frame: 0.000080789 seconds]
    [Time since reference or first frame: 51.870317686 seconds]
    Frame Number: 346
    Frame Length: 98 bytes (784 bits)
    Capture Length: 98 bytes (784 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:icmp:data]
    [Coloring Rule Name: ICMP]
    [Coloring Rule String: icmp || icmpv6]
Ethernet II, Src: de:ad:be:ef:fe:ed (de:ad:be:ef:fe:ed), Dst: WistronI_45:7c:1d (54:ee:75:45:7c:1d)
    Destination: WistronI_45:7c:1d (54:ee:75:45:7c:1d)
    Source: de:ad:be:ef:fe:ed (de:ad:be:ef:fe:ed)
    Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 192.168.1.100, Dst: 192.168.1.4
    0100 .... = Version: 4
    .... 0101 = Header Length: 20 bytes (5)
    Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
    Total Length: 84
    Identification: 0x0001 (1)
    Flags: 0x40, Don't fragment
    ...0 0000 0000 0000 = Fragment Offset: 0
    Time to Live: 128
    Protocol: ICMP (1)
    Header Checksum: 0x76ef [validation disabled]
    [Header checksum status: Unverified]
    Source Address: 192.168.1.100
    Destination Address: 192.168.1.4
Internet Control Message Protocol
    Type: 0 (Echo (ping) reply)
    Code: 0
    Checksum: 0xcb80 [correct]
    [Checksum Status: Good]
    Identifier (BE): 1 (0x0001)
    Identifier (LE): 256 (0x0100)
    Sequence Number (BE): 1 (0x0001)
    Sequence Number (LE): 256 (0x0100)
    [Request frame: 345]
    [Response time: 0.081 ms]
    Timestamp from icmp data: Feb  5, 2024 07:43:16.000000000 CST
    [Timestamp from icmp data (relative): 0.074828760 seconds]
    Data (48 bytes)

0000  a0 23 01 00 00 00 00 00 10 11 12 13 14 15 16 17   .#..............
0010  18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27   ........ !"#$%&'
0020  28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37   ()*+,-./01234567
        Data: a023010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b…
        [Length: 48]

 

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章