首先解释一下我的目标我试图通过UDP (用户数据报协议)协议控制我的机器人前进,后退,右移,左移和停止
和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);
}
///----------------------------------------------------------
答案 0 :(得分:0)
我不知道您的网络堆栈,但ES_packetloop_icmp_tcp
听起来不适用于 UDP 流量。我假设它处理ICMP和/或TCP数据包。 ICMP是公共ping
命令使用的数据包类型,ICMP 不与UDP相同。
编辑:也许this有助于您入门。