Go Down

Topic: Arduino MEGA & Ethernet Shield ENC28J60 (Read 2 times) previous topic - next topic

Perico2005

Hello everybody, I am a new user of Arduino from Spain.

I have an Arduino MEGA2560 and a Ethernet Shied type ENC28J60, I have connected them & every is OK with this library:

http://iteadstudio.com/application-note/ethernet-shield-go-with-arduino-libraries/

I am able to do ping, like a server and like a client. But I need some suggestions. Let me explain my project:

I need to extract some information from the Microsoft FSX Simulator through the TCP protocol using the IOCP software. I have checked it usig LabView environment and it works fine. But I need not to use a PC, and use only the Arduino connected to a router.

It is easy, my Arduino needs to write to the server (192.2.1.2 port 8090) the next command: "Arn.Inicio:Var419"
After that, I must to read the incommnig messages "Arn.Var419:xxxxxx"


Must I connect the Arduino like a clent? I have tried (see bellow) this code but It doesn't work, what do you think?
Thanks you very much & My best regards.
Pedro



Perico2005

#include <Ethernet.h>

byte mac[] = { 0x00, 0x1B, 0xB9, 0x84, 0xCB, 0x60 };
byte ip[] = { 192, 2, 1, 4 };
byte server[] = { 192, 2, 1, 2 };

Client client(server, 8090);

void setup() {
  Serial.begin(9600);
  Serial.println("Iniciando conexion FSX");
  delay(100);
  Ethernet.begin(mac, ip);
  delay(5000);
  Serial.println("conextando a ip: 192.2.1.2.??????...");
 
  if (client.connect()) {
    Serial.println("FSX Conectado----->");
    client.println("Arn.Inicio:Var419");
    //client.println();
    delay(10000);
  }
  else {
    Serial.println("FSX NO conectado *********");
  }
}

void loop() {
cnx:
  delay (1000);
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  if (!client.connected()) {
    Serial.println();
    Serial.println("Reintentando");
    goto cnx;

  }
}

PaulS

If there client is no client connected, goto the start of a function that will be called again, in an infinite loop anyway. Why?

Quote
I have tried (see bellow) this code but It doesn't work, what do you think?

I think you need to lean a little to the left. You are blocking your screen. We can't see what the program actually does, and you haven't told us.

A little farther. Still can't see.

A little more...

Perico2005

Thanks Paul,

I have simplified the code, still not working:

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 2, 1, 4 };                                     //my ip
byte server[] = { 192, 2, 1, 2 };                               //FSX ip

Client client(server, 8090);                                     //port must be 8090

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);
  Serial.println("Begin FSX connection");
}

void loop() {

  client.println("Arn.Inicio:Var419");       //command from Arduino to FSX
  client.println();
  delay (10000);
rdx:                                                 // scan for responses from FSX
  delay (100);
  if (client.available()) {
    char c = client.read();
    Serial.print(c);                 //link TCP messages to serial (for debugging)
    goto rdx;
  }
 
}

PaulS

Code: [Select]
void loop() {

  client.println("Arn.Inicio:Var419");       //command from Arduino to FSX
  client.println();
  delay (10000);
rdx:                                                 // scan for responses from FSX
  delay (100);
  if (client.available()) {
    char c = client.read();
    Serial.print(c);                 //link TCP messages to serial (for debugging)
    goto rdx;
  }

}

The client.available() function returns the number of characters available to read. Read them in a while loop, rather than inventing your own. The goto statement has very little going for it here.

Quote
I have simplified the code, still not working:

You have some expectation of what the code should do. There is something that the code does. All we can tell from the information you have provided is that those two are not the same thing. We have a pretty good idea of what you expect it to do. What it actually does is a mystery.

You need to clear up that mystery if you expect help.

Go Up