604|1

21

帖子

2

TA的资源

一粒金砂(中级)

楼主
 

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

  本帖最后由 eew_uscYT9 于 2024-2-16 00:58 编辑

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

 

参考官方代码建立了tcp服务器,代码如下

''' TCP Server example.
    date: 2023-11-23
'''
from usocket import socket
from machine import Pin,SPI,UART,PWM
import time, network,framebuf

''' static netinfo
'''
ip = '192.168.1.20'
sn = '255.255.255.0'
gw = '192.168.1.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

''' uart0 init
    baudrate: 115200
    tx pin  : gpio0
    rx pin  : gpio1
'''
uart = UART(0, 115200, tx=Pin(0), rx=Pin(1))   
uart.init(115200, bits=8, parity=None, stop=1) 
uart.write('WIZnet chip tcp server example.\r\n')
 
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)

def w5x00_init():
    global localip
    ''' spi0 init
        baudrate: 2000000
        mosi pin: gpio19
        miso pin: gpio16
        sck  pin: gpio18
        cs   pin: gpio17
        rst  pin: gpio20
    '''
    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])
    uart.write('ip :{0}\r\n'.format(nic.ifconfig()[0]))
    uart.write('sn :{0}\r\n'.format(nic.ifconfig()[1]))
    uart.write('gw :{0}\r\n'.format(nic.ifconfig()[2]))
    uart.write('dns:{0}\r\n'.format(nic.ifconfig()[3]))
    
    while not nic.isconnected():
        time.sleep(1)
#         print(nic.regs())
        print('no link')
        uart.write('no link\r\n')




            
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.text("Follow me NO.4",50,2,LCD.red)

 
    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)
    
    






   

 

 

 电脑进行发送消息,服务器能接收到

wireshark抓取信息如下图,抓取信息与发送信息一致

  屏幕上显示接收到的信息

  然后我在esp32s3上搭建了tcp客户端,进行数据的发送

 

服务器访问成功发送helloword

   

屏幕成功接收到信息,并显示出来

 

 

最新回复

官方代码建立了tcp服务器就能方便地实现,完美   详情 回复 发表于 2024-2-16 18:30
点赞 关注
 
 

回复
举报

6807

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

官方代码建立了tcp服务器就能方便地实现,完美

 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

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

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