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 ...
随机推荐
- solidity语言5
结构体 pragma solidity ^0.4.11; // 众筹合约 contract CrowdFunding { // 投资者 struct Funder { address addr; ui ...
- IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)
最近项目要交付了,对方安全测试的时候检测出高危险漏洞,由于刚参加工作不久,经验不足,未涉及过此方面的东西.经过一番查询和探索,最终解决了这个问题,记录一下. 发现的漏洞为缺少跨框架脚本保护.跨框架脚本 ...
- 如何用ABAP代码读取CDS view association的数据
我有如下一个CDS view, 这个view的数据来自CRMD_ORDERADM_H, 定义了一个名称为_statushelp的association, 指向了另一个CDS view Z_C_Stat ...
- (第五场)G max 【数论】
题目链接:https://www.nowcoder.com/acm/contest/143/G 题目描述 Give two positive integer c, n. You need to fin ...
- PHP面试题分享与答案
由于之前的每一个问题都是一个比较大的知识点,作者希望可以尽量一一详细解答,如果有不足的地方欢迎大家补充和修改,同时借鉴牛人写的Mysql中算法的实现以及内存原理,Btree结构等. 1:MySQL数据 ...
- HBuilder实现WiFi调试Android
要求手机是开发模式 wifi实现 条件:已ROOT手机.手机和电脑需要在一个网段 第一步:安装在应用商店下载WiFi ADB (注意这里显示的ip等下使用) 第二步:打开WIFI ADB 第三步:切换 ...
- Hive中使用LZO
hive 中使用lzo 1 启动hive 错误Exception in thread "main" java.lang.NoClassDefFoundError: org/apac ...
- tomcate8配置多个二级域名问题解决根目录空白2017年12月9日
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...
- AngularJS 控制器函数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Vue node.js商城-购物车模块
一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){ return { car ...