简体   繁体   English

通过UDP从Arduino以太网发送POT值

[英]Sending POT values from Arduino ethernet via UDP

I have an Arduino ethernet which I am using to read POT values on a simulator, I want to then send these values to the computer with the simulator software on it so they can be input to the game. 我有一个Arduino以太网,用于在模拟器上读取POT值,然后我想将这些值发送到装有模拟器软件的计算机,以便可以将其输入到游戏中。

I doing this via UDP as the guy who wrote the program to integrate it with the game said it would be easier this way and that recording data from the the controls (for research purposes) would also be easier. 我通过UDP进行此操作,因为编写该程序以将其与游戏集成的人说,这样做会更容易,并且从控件(出于研究目的)记录数据也将更加容易。 Regardless of whether this is true, I can not change this. 不管这是否正确,我都无法更改。

I am currently trying to write some code to send values read by a POT to some processing code which then displays this value. 我目前正在尝试编写一些代码,以将POT读取的值发送到一些处理代码,然后显示该值。 This is just as an intermediate step before I try and integrate this code with the game etc. I am a beginner with regards to programming in this respect, I have just been thrown in at the deep end here. 这只是我尝试将此代码与游戏等集成之前的一个中间步骤。在这方面,我是编程方面的初学者,而我刚刚被深深地迷住了。 While I can program, this has only really been making mathematical programs with matlab. 虽然我可以编程,但这实际上只是使用matlab编写数学程序。

I have tried to modify some code given in examples and have ended up with the following but its not working: 我试图修改示例中给出的一些代码,但最终得到以下结果,但无法正常工作:

Aruino Code: Aruino代码:

#include // needed for Arduino versions later than 0018 #include #include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 #include // 0018之后的Arduino版本所需的#include #include // UDP库,来自:bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0F, 0xC6, 0x1F
};
IPAddress ip(192, 168, 0, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming  packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

int potPin = 2;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor
int mapped = 0;

void setup() {

 // start the Ethernet and UDP:

  Ethernet.begin(mac,ip);

  Udp.begin(localPort);

}



void loop() {

  val = analogRead(potPin);  // read the value from the sensor
  mapped = map(val, 0, 1023, 0, 5000);

  Serial.print(analogRead(potPin));
  Serial.print(mapped);    
  delay (400);

   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

    Udp.write(mapped);

   Udp.endPacket();

}

Processing code: 处理代码:

 import hypermedia.net.*;

 UDP udp;  // define the UDP object
 String value = "0";

 void setup() {
 size(480,640);
 background(0);
 smooth();
 udp = new UDP( this, 3000 );  // create a new datagram connection on port 6000
 udp.log( true );     // <-- printout the connection activity
 udp.listen( true );           // and wait for incoming message
 }

 void receive( byte[] data ) {       // <-- default handler
 //void receive( byte[] data, String ip, int port ) {  // <-- extended handler

  value=new String(data);
  println(value);

 }

 void draw()
  {
  fill(50);
  if (value != "0") {
  text(value, 10, 10, 70, 80); 
  }

 }

Note: There maybe be some uneccesary lines in there which I have failed to delete from the code which I modified. 注意:可能有一些不必要的行,我未能从我修改的代码中删除这些行。 Also the mapped value of the analogue read is just as Im not sure what range of values the game will want to receive as of yet. 同样,模拟读取的映射值就像Im不确定游戏到目前为止将要接收的值的范围。

Thanks for any help! 谢谢你的帮助!

I can't say exactly what is going wrong, but here are some things that stuck out in your code. 我无法确切地说出出了什么问题,但是这是代码中存在的一些问题。 The Arduino code sends the message out on the UDP.remotePort port. Arduino代码在UDP.remotePort端口上发送消息。 From what I read elsewhere it looks like this would only be set by receiving a message, so maybe try hard-coding this along with the remote IP to ensure it is set correctly. 从我在其他地方阅读过的内容来看,似乎只能通过接收消息来进行设置,因此请尝试将其与远程IP一起进行硬编码以确保正确设置。

Also I would eliminate one of your problems for the time being and just use a UDP terminal. 另外,我暂时将消除您的问题之一,仅使用UDP终端即可。 I have used Hercules before on Windows http://www.hw-group.com/products/hercules/index_en.html . 我以前在Windows http://www.hw-group.com/products/hercules/index_en.html上使用过Hercules。 This will let you just focus on the Arduino code and it should let you see everything that is going on. 这将使您仅关注Arduino代码,并应让您看到正在发生的一切。 Wireshark would be another more complicated option that will really let you see exactly what you are sending out on the network. Wireshark将是另一个更复杂的选项,它将使您真正看到网络上正在发送的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM