|
对于Arduino的开始关注得益于极客工坊,发现Arduino确实是一个很好的平台,很简单的方法就可以实现以往单片机复杂的应用。
但发现坛子的讨论热度一直不是很高,可能是由于Arduino在国内还不是很普及的原因。现将自己学习Arduino的一点心得拿出来,希望能起到抛砖引玉的效果,并希望能引起大家的积极讨论,能完善这个功能就最好了。
用到的是官方的ethernet-Shield,实现的功能是通过以太网的udp发送数据到Arduino,转成串口的数据发出,反过来也可,实现串口和以太网的双向通信。程序很粗糙,功能很简单,希望大家一起去完善。
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[24]; //buffer to send packet,
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
int packetSize = Udp.parsePacket();
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
if(packetSize)
{
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println(packetBuffer);//send the data to 232
memset(packetBuffer,0,sizeof(packetBuffer));
}
if (Serial.available() > 0)
{
ReplyBuffer[0]= Serial.read();
delay(2);
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|