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)
sqlserver数据库的日志文件其实是由很多个逻辑上的日志文件组成,我们可以通过命令看一下数据库日志文件 可以看到的是sqlserver数据库日志文件是由很多文件组成的,当数据库日志文件已满的时候 ...
- Angular项目新建
Angular新建项目步骤记录 标签(空格分隔): Angular 1. ng new my-app 2. 启动dev环境 cd my-app ng serve --open 3. 修改styles. ...
- 再学UML-深入浅出UML类图(三)
类与类之间的关系(2) 2. 依赖关系 依赖(Dependency)关系是一种使用关系,特定事物的改变有可能会影响到使用该事物的其他事物,在需要表示一个事物使用另一个事物时使用依赖关系. ...
- 三、python webservice
#!/usr/bin/python # -*- coding: utf-8 -*- import logging import suds url="http://172.17.2.199:8 ...
- 如何给VirtualBox虚拟机的ubuntu LVM分区扩容
我在VirtualBox安装的ubuntu里安装Cloud Foundry时遇到错误信息,磁盘空间不够了: 使用这三个命令做了清理之后,结果依然不够理想: (1) sudo apt-get autoc ...
- 关于《Selenium 2自动化测试实战 基于Python语言》学习过程中键盘的常用操作
下边是自己在学习过程中总结的一些常用键盘的操作
- IOS 录音(AVAudioRecorder)
#import "HMViewController.h" #import <AVFoundation/AVFoundation.h> @interface HMView ...
- LA 4015 树形背包
题目链接:https://vjudge.net/contest/164840#problem/D 题意: 给一棵树,每条边上有一些权值,求 长度不超过 x ,最多能走多少个点: 分析: 考虑每一个节点 ...
- Android学习笔记_72_Spinner的用法
一.普通 1. <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android= ...
- Laravel5 打印SQL
在src/Illuminate/Database/Connection.php里打印SQL默认是关闭的,见https://github.com/laravel/framework/commit/e0a ...