Arduino udp ping
本文关键字:ping udp Arduino | 更新日期: 2023-09-27 18:20:29
首先,为了解释我的目标,我试图用UDP(用户数据报协议)协议控制我的机器人前进、后退、向右、向左和停止
- 我正在使用
- Arduino Mini Atmega168
- 兰ENC28J60
- 路由器Tp-Link 150Mbps无线N Nano
- 电机驱动器L293D
-
和C#中的应用程序,带有向前、向后、向左、向右和停止按钮
arduino的ip地址是192.168.178.60我正在ping该地址,没有问题,但如果我试图更快地点击按钮进行控制,或者点击任何按钮并留出更多时间。例如,前进阻止或向左阻止,我必须重新启动arduino才能恢复ping。主要的问题是为什么我这么快就断了连接。
这是我的代码
#include "EtherShield.h"
//8 or 16 bit integer
uint8_t mymac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB8};
uint8_t myip[4] = {192,168,178,60};
uint16_t MYWWWPORT = 80;
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
EtherShield es=EtherShield();
uint16_t dat_p;
///----------------------------------------------------------
void setup(){
Setup_Pins();
es.ES_enc28j60Init(mymac);
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
}
///----------------------------------------------------------
void loop(){
// read packet, handle ping and wait for a tcp packet:
buf[IP_PROTO_P]=0;
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
Motor_Control();
buf[IP_PROTO_P]=0;
}
}
//----------------------------------------------------------
void Setup_Pins(){
pinMode(4, OUTPUT); digitalWrite(4, 0);//lev
pinMode(5, OUTPUT); digitalWrite(5, 0);//lev
pinMode(6, OUTPUT); digitalWrite(6, 0);//desen
pinMode(7, OUTPUT); digitalWrite(7, 0);//desen
}
///----------------------------------------------------------
void Motor_Control(){
char* recv = (char*)buf + 42;
// forward
if (strncmp(recv, "forward", 7) == 0) {
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
}
//left
if (strncmp(recv, "left", 4) == 0) {
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
}
//stop
if (strncmp(recv, "stop", 4) == 0) {
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
}
//right
if (strncmp(recv, "right", 5) == 0) {
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
}
//back
if (strncmp(recv, "back", 4) == 0) {
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
}
delay(6);
}
///----------------------------------------------------------
我不知道您的网络堆栈,但ES_packetloop_icmp_tcp
听起来不适用于UDP流量。我认为它处理ICMP和/或TCP数据包。ICMP是常见ping
命令使用的数据包类型,ICMP与UDP不同。
编辑:也许这有助于你开始。