2

首先解释我的目标我试图控制我的机器人前进,后退,右,左和停止与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);

} 


///----------------------------------------------------------
4

1 回答 1

0

我不知道您的网络堆栈,但ES_packetloop_icmp_tcp听起来它不适用于UDP流量。我假设它处理 ICMP 和/或 TCP 数据包。ICMP 是常用ping命令使用的数据包类型,ICMPUDP 不同。

编辑:也许有助于你开始。

于 2013-07-03T13:05:11.310 回答