stm32+lwip(五):以太网帧发送测试
我是卓波,很高兴你来看我的博客。
系列文章:
stm32+lwip(一):使用STM32CubeMX生成项目
很多时候,我们想直接获取以太网帧的数据或者直接发送以太网帧数据。在使用STM32CubeMX生成的工程当中,有两个函数就是直接跟以太网通信有关:
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become availale since the stack doesn't retry to send a packet
* dropped because of memory failure (except for the TCP timers).
*/ static err_t low_level_output(struct netif *netif, struct pbuf *p)
/**
* Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf * low_level_input(struct netif *netif)
这两个函数就是实现与以太网的通信,本来需要我们自己实现。现在STM32CubeMX已经帮我们实现好,因此收发以太网数据可以通过调用或修改这两个函数实现。
发送测试
/**
*****************************************************************************
* @file ethernet_test.c
* @author Zorb
* @version V1.0.0
* @date 2018-09-04
* @brief 以太网帧数据发送与接收测试的实现
*****************************************************************************
* @history
*
* 1. Date:2018-09-04
* Author:Zorb
* Modification:建立文件
*
*****************************************************************************
*/ #include "stm32f4xx_hal.h"
#include "lwip.h" /******************************************************************************
* 描述 : 以太网帧发送测试1
* 参数 : 无
* 返回 : 无
******************************************************************************/
void ethernet_sendtest1(void)
{
uint8_t frame_data[] =
{
/* 以太网帧格式 */
0x50,0xFA,0x84,0x15,0x3C,0x3C, /* 远端MAC */
0x0,0x80,0xE1,0x0,0x0,0x0, /* 本地MAC */
0x8,0x0, /* ip类型 */
0x45,0x0,0x0,0x26/*l*/,0x0,0x0,0x0,0x0,0xFF,0x11,0x0,0x0, /* UDP报头 */
0xC0,0xA8,0x2,0x8, /* 本地IP */
0xC0,0xA8,0x2,0xC2, /* 远端IP */
0x22,0xB0, /* 本地端口 */
0x22,0xB1, /* 远端端口 */
0x0,0x12, /* UDP长度 */
0x0,0x0, /* UDP校验和 */
0x68,0x65,0x6C,0x6C,0x6F,0x20,0x7A,0x6F,0x72,0x62 /* 数据 */
}; struct pbuf *p; /* 分配缓冲区空间 */
p = pbuf_alloc(PBUF_TRANSPORT, 0x26 + , PBUF_POOL); if (p != NULL)
{
/* 填充缓冲区数据 */
pbuf_take(p, frame_data, 0x26 + ); /* 把数据直接通过底层发送 */
gnetif.linkoutput(&gnetif, p); /* 释放缓冲区空间 */
pbuf_free(p);
}
} /******************************************************************************
* 描述 : 以太网帧发送测试2
* 参数 : 无
* 返回 : 无
******************************************************************************/
void ethernet_sendtest2(void)
{
uint8_t dstAddr[] = {0x50,0xFA,0x84,0x15,0x3C,0x3C}; /* 远端MAC */ uint8_t frame_data[] =
{
/* UDP帧格式 */
0x45,0x0,0x0,0x26/*l*/,0x0,0x0,0x0,0x0,0xFF,0x11,0x0,0x0, /* UDP报头 */
0xC0,0xA8,0x2,0x8, /* 本地IP */
0xC0,0xA8,0x2,0xC2, /* 远端IP */
0x22,0xB0, /* 本地端口 */
0x22,0xB1, /* 远端端口 */
0x0,0x12, /* UDP长度 */
0x0,0x0, /* UDP校验和 */
0x68,0x65,0x6C,0x6C,0x6F,0x20,0x7A,0x6F,0x72,0x62 /* 数据 */
}; struct pbuf *p; /* 分配缓冲区空间 */
p = pbuf_alloc(PBUF_TRANSPORT, 0x26, PBUF_POOL); if (p != NULL)
{
/* 填充缓冲区数据 */
pbuf_take(p, frame_data, 0x26); /* 把数据进行以太网封装,再通过底层发送 */
ethernet_output(&gnetif, p, (const struct eth_addr*)gnetif.hwaddr,
(const struct eth_addr*)dstAddr, ETHTYPE_IP); /* 释放缓冲区空间 */
pbuf_free(p);
}
} /******************************** END OF FILE ********************************/
gnetif.linkoutput和ethernet_output最终调用的还是low_level_output。
上面测试的数据包是给IP:192.168.2.194端口:8881发送”hello zorb”数据,网络调试助手能正确收到数据:

最后
本文主要介绍了怎样发送以太网帧数据,当能给网络发送任意数据,就可以做一些有趣的事情。
github:https://github.com/54zorb/stm32-lwip
版权所有,转载请打赏哟
如果你喜欢我的文章,可以通过微信扫一扫给我打赏哟

stm32+lwip(五):以太网帧发送测试的更多相关文章
- stm32+lwip(三):TCP测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(二):UDP测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(四):网页服务器测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- stm32+lwip(一):使用STM32CubeMX生成项目
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
- IFG以太网帧间隙
交换机的线速 描述交换机性能可以使用“线速”这个概念,那它是什么意思呢?所谓的线速是指经过交换机处理的理想状态下最大数据率.描述数据率可以用bps(bit per second)和mpps(milli ...
- RFC2889错误帧过滤测试----网络测试仪实操
一.简介 RFC 2889为LAN交换设备的基准测试提供了方法学,它将RFC 2544中为网络互联设备基准测试所定义的方法学扩展到了交换设备,提供了交换机转发性能(Forwarding Perform ...
- RFC2899广播帧转发测试——网络测试仪实操
一.简介 RFC 2889为LAN交换设备的基准测试提供了方法学,它将RFC 2544中为网络互联设备基准测试所定义的方法学扩展到了交换设备,提供了交换机转发性能(Forwarding Perform ...
- s4-5 以太网帧
以太网所处的层次 IEEE 802.3/以太网MAC子层协议 IEEE802.3协议描述了运行在各种介质上1 Mb/s~10 Mb/s的1- 持续CSMA/CD协议的局域网标准. 很多人对以太 ...
- # 第五周课下测试(ch03)补交
第五周课下测试(ch03)补交 1.( 多选题 | 1 分) 有关gdb调试汇编,下面说法正确的是() A . 可以用disas反汇编当前函数 B . 以16进制形式打印%rax中内容的命令是 pri ...
随机推荐
- sqlserver内存管理之lazy writer
任何在缓冲区被修改的页都会被标记为“脏”页.将这个脏页写入到数据磁盘就是CheckPoint或者Lazy Writer的工作.前面就大体说了一下checkpoint,这会就简单的理解一下这个lazy ...
- 基于iframe的CFS(Cross Frame Script)和Clickjacking(点击劫持)攻击
攻击原理: CFS攻击(Cross Frame Script(跨框架脚本)攻击)是利用浏览器允许框架(frame)跨站包含其它页面的漏洞,在主框架的代码 中加入scirpt,监视.盗取用户输入 ...
- day013-流
1. 常用函数式接口 1.1 Predicate接口 有时候我们需要对某种的数据进行判断,从而得到一个boolean值结果.这时可以使用java.util.function.Predicate< ...
- day6-基础 模块详解
1.定义: 1)模块:本质上就是一个python程序,封装成一个"模块",可以调用里面的方法,用来实现一个功能.逻辑上用来组织python代码. 2)包:本质上是一个目录(必须带有 ...
- 设计模式——模板方法模式(TemplateMethod Pattern)
模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. UML图: 抽象模板: package com.cnblo ...
- Android开发经验01:31个Android开发实战经验
1. 在Android library中不能使用switch-case语句访问资源ID:在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案 2. 不能 ...
- 异常:org.hibernate.id.IdentifierGenerationException
在有关联关系的数据表中保存数据时,先保存一端,再保存多端的抛出的异常(此时不管一端,还是多端的对象都没有设置id,属性,也就是要保存的两个对象的id 属性为空.) org.hibernate.id.I ...
- VirtualBox改变虚拟硬盘位置
原本放虚拟硬盘的位置容量不足,因此将原来的虚拟硬盘放到了一个相对空闲的分区.设置虚拟硬盘位置时出现一点小问题,解决过程记录如下. 1. 将虚拟硬盘复制到目标位置后,假设为“F:\Ubuntu 16.0 ...
- 线段tree~讲解+例题
最近学习了线段树这一重要的数据结构,有些许感触.所以写一篇博客来解释一下线段树,既是对自己学习成果的检验,也希望可以给刚入门线段树的同学们一点点建议. 首先声明一点,本人是个蒟蒻,如果在博客中有什么不 ...
- HTML5 小实例
1.时钟: <!doctype html> <html> <head></head> <body> <canvas id=" ...