649|0

166

帖子

1

TA的资源

一粒金砂(高级)

楼主
 

【得捷电子Follow me第4期】基础任务二:建立TCP服务器,发送数据并显示,抓包数据 [复制链接]

  本帖最后由 电子烂人 于 2024-2-4 11:17 编辑

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

由于TCP个人感觉用的更多一些,这里选择使用TCP完成作业。

1.建立TCP服务器

关于TCP的说明可以参考WIZnet 的官方文章说明:五、W5100S/W5500+RP2040之MicroPython开发<TCP Server示例>-CSDN博客

示例代码如下:

from usocket import socket
from machine import Pin,SPI
import time, network

ip = '192.168.2.114'
sn = '255.255.255.0'
gw = '192.168.2.1'
dns= '8.8.8.8'

netinfo=(ip, sn, gw, dns)

localip = ''
localport = 8000
listen_info = (localip, localport)
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.ifconfig(netinfo)
    localip = nic.ifconfig()[0]
    while not nic.isconnected():
        time.sleep(1)
        print('no link')

conn_flag = False

def server_loop():
    global localip
    global conn_flag
    while True:
        if(conn_flag == False):
            s = socket()
            s.bind(listen_info) # Source IP Address and Port
            s.listen(5) # max conncet counts
            conn_flag = True
            print("TEST server Loop")
            conn, addr = s.accept()
            print('Connect from %s' % str(addr))
        else:
            
            try:
                
                data = conn.recv(2048)
                data = data.decode('utf-8')

                data+='\r\n'
                if data != 'NULL':
                    conn.send(data)
            except:
                conn_flag = False
            
def main():
    w5x00_init()
    server_loop()
if __name__ == "__main__":
    main()

将代码烧录到开发板进行调试,当开发板检测到网络链接后会打印相应数据

2.TCP通信测试

打开电脑端的串口调试器,设置串口调试助手端口号为TCPClient模式,远程地址为开发板代码中的地址,本地端口(localport)8000

用传口调试助手发送字符串,可以在串口助手中看到开发板成功接收数据并发回

3.抓包分析

使用wareshark捕获PC机(110)到开发板TCP服务器(114)的数据报文

注意图中的第58条报文,可以准确的抓取到PC端发送的字符串数据。

4.将发送到TCP服务器的数据在屏幕上显示

更改代码如下:

from machine import Pin,SPI,PWM
from usocket import socket

import network
import framebuf
import time

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

ip = '192.168.1.11'
sn = '255.255.255.0'
gw = '192.168.1.1'
dns= '8.8.8.8'

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

class LCD_1inch14(framebuf.FrameBuffer):
    def __init__(self):
        self.width = 240
        self.height = 135
        
        self.cs = Pin(CS,Pin.OUT)
        self.rst = Pin(RST,Pin.OUT)
        
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1,1000_000)
        self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
        self.dc = Pin(DC,Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        
        self.red   =   0x07E0
        self.green =   0x001f
        self.blue  =   0xf800
        self.white =   0xffff
        
    def write_cmd(self, cmd):
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)

    def write_data(self, buf):
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(bytearray([buf]))
        self.cs(1)

    def init_display(self):
        """Initialize dispaly"""  
        self.rst(1)
        self.rst(0)
        self.rst(1)
        
        self.write_cmd(0x36)
        self.write_data(0x70)

        self.write_cmd(0x3A) 
        self.write_data(0x05)

        self.write_cmd(0xB2)
        self.write_data(0x0C)
        self.write_data(0x0C)
        self.write_data(0x00)
        self.write_data(0x33)
        self.write_data(0x33)

        self.write_cmd(0xB7)
        self.write_data(0x35) 

        self.write_cmd(0xBB)
        self.write_data(0x19)

        self.write_cmd(0xC0)
        self.write_data(0x2C)

        self.write_cmd(0xC2)
        self.write_data(0x01)

        self.write_cmd(0xC3)
        self.write_data(0x12)   

        self.write_cmd(0xC4)
        self.write_data(0x20)

        self.write_cmd(0xC6)
        self.write_data(0x0F) 

        self.write_cmd(0xD0)
        self.write_data(0xA4)
        self.write_data(0xA1)

        self.write_cmd(0xE0)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0D)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2B)
        self.write_data(0x3F)
        self.write_data(0x54)
        self.write_data(0x4C)
        self.write_data(0x18)
        self.write_data(0x0D)
        self.write_data(0x0B)
        self.write_data(0x1F)
        self.write_data(0x23)

        self.write_cmd(0xE1)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0C)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2C)
        self.write_data(0x3F)
        self.write_data(0x44)
        self.write_data(0x51)
        self.write_data(0x2F)
        self.write_data(0x1F)
        self.write_data(0x1F)
        self.write_data(0x20)
        self.write_data(0x23)
        
        self.write_cmd(0x21)

        self.write_cmd(0x11)

        self.write_cmd(0x29)

    def show(self):
        self.write_cmd(0x2A)
        self.write_data(0x00)
        self.write_data(0x28)
        self.write_data(0x01)
        self.write_data(0x17)
        
        self.write_cmd(0x2B)
        self.write_data(0x00)
        self.write_data(0x35)
        self.write_data(0x00)
        self.write_data(0xBB)
        
        self.write_cmd(0x2C)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)
  
if __name__=='__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)#max 65535

    LCD = LCD_1inch14()
    #color BRG
    LCD.fill(LCD.white)
 
    LCD.show()
    
 
    
    LCD.text("Follow me NO.4",0,5,LCD.red)
    LCD.text("Basic Task 2:",130,5,LCD.green)
       
    LCD.hline(10,15,220,LCD.blue)
    LCD.hline(10,115,220,LCD.blue)
    LCD.vline(10,15,100,LCD.blue)
    LCD.vline(230,15,100,LCD.blue)
    
    LCD.text("by ee_jark",120,120,LCD.blue)
    LCD.show() 
    w5x00_init()
    while True:
        if(conn_flag == False):
            s = socket()
            s.bind(listen_info) # Source IP Address and Port
            s.listen(5) # max conncet counts
            conn_flag = True
            print("TEST TCP server ")
            conn, addr = s.accept()
            print('Connect from %s' % str(addr))
            LCD.text('Connect from',20,20,LCD.blue)
            LCD.text('%s' % str(addr),20,40,LCD.blue)
            LCD.show()
        else:
            try:
                
                data = conn.recv(2048)
                data = data.decode('utf-8')

                data+='\r\n'
                if data != 'NULL':
                    #conn.send(data)
                    LCD.text('data:',20,60,LCD.blue)
                    LCD.text(data,20,80,LCD.blue)
                    LCD.show() 
            except:
                conn_flag = False
    
    time.sleep(1)
    LCD.fill(0xFFFF)

烧录代码可得:

在通过串口助手发送一段字符:(hello eeworld!!)
在开发板屏幕上显示为:
同时使用wareshark 抓包,内容与所发送字符一致

 

 

点赞 关注
个人签名

没用比没有强

 
 

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

随便看看
查找数据手册?

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