Go Down

Topic: Arduino Ethernet - TCP IP Questions (Read 4534 times) previous topic - next topic

mjnewsum

#15
Jun 09, 2017, 07:43 pm Last Edit: Jun 09, 2017, 07:53 pm by mjnewsum
@PaulS & @ieee488 That makes a lot of sense.
I just wanted to make sure you knew that I am not just trying to print the characters.

I made the modification suggested.
This is how I started out initially.
It prints the correct message.

Code: [Select]



void setup() {
  Serial.begin(9600);
 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;
 
  while(!client){
    client = server.available();
  }
 
  if(client.available()){
    char c = client.read();
    Serial.print(c);
    }
  }



The code that I posted previously was my attempt to collect the characters into a message that could be used later for things like analog output.

Thank you for the help.

mjnewsum

I am still trying to figure out how to make these incoming message into an int, but I can't find anything on the forum.

It seems like it is in here somewhere, but I only find reference to how this is done with Serial.read whereas I am using Ethernet Stream class.

Here is a post that seemed to relate in some ways:
https://forum.arduino.cc/index.php?topic=126138.0

And this tutorial makes sense (for Serial.read()):
https://www.gammon.com.au/serial

I just don't see any example using the Ethernet Shield.

ieee488

#17
Jun 09, 2017, 09:46 pm Last Edit: Jun 09, 2017, 09:46 pm by ieee488
It would help if you post your entire sketch.
I have no idea what you have at this moment.

PaulS

You need to show us what is in "these incoming message". For all we know, the incoming message looks like:

"It is going to rain on Tuesday".

Now, what should the integer value of that message be?

That last bit of code is NOT collecting the data in an array. It is NOT NULL-terminating the array.

Until you do BOTH of those steps, AND the array actually contains a representation of an int, then passing anything to atoi() is a waste of effort.
The art of getting good answers lies in asking good questions.

mjnewsum

Here is the code as is when printing the received message one character at a time.
The message prints correctly.
Message sent is in this format:

<###,###,###>

example message:

<255,255,255>

I am adding the '<' and '>' to identify the start and end of messages.
This is not required and can be modified if needed.

Code: [Select]


#include <Ethernet.h>

byte mac[] = {0x54, 0x52, 0x49, 0x41, 0x44, 0x00};
byte ip[] = {192, 168, 2, 99};
byte gateway[] = {192, 168, 0, 1};
byte subnet[] = {255, 255, 0, 0};
EthernetServer server(4444);
EthernetClient client;

int sizeMessage = 11;


void setup() {
  Serial.begin(9600);
 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;
 
  while(!client){
    client = server.available();
  }
 
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}






mjnewsum

@PaulS I am reading one of you other responses in this forum post:
http://forum.arduino.cc/index.php?topic=45629.0

I will try to apply these ideas and see if I can get it to work.


PaulS

"255,255,255" is 11 characters. With a terminating NULL, you need space in the array for TWELVE characters, not 11.

The art of getting good answers lies in asking good questions.

mjnewsum

@PaulS the code below is from this link.

Code: [Select]

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
  while(Serial.available() > 0) // Don't read unless
                                                 // there you know there is data
  {
      if(index < 19) // One less than the size of the array
      {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
      }
  }
  // Now do something with the string (but not using ==)
}


Would this produce the array size + 1 for the NULL terminating character?
It seems that you are NULL terminating the string for each character in the message.

I made the following modifications to work with Ethernet. I am getting the correct message printing.
Now, I just need to turn each comma separated value into an int.

Code: [Select]

#include <Ethernet.h>

byte mac[] = {0x54, 0x52, 0x49, 0x41, 0x44, 0x00};
byte ip[] = {192, 168, 2, 99};
byte gateway[] = {192, 168, 0, 1};
byte subnet[] = {255, 255, 0, 0};
EthernetServer server(4444);
EthernetClient client;

const int sizeMessage = 12;
char inData[sizeMessage];
char inChar;
byte index = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Serial Started");
 
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.println("Server Started");
  client = server.available();
}

void loop() {
  char inByte;
  char message[sizeMessage];
  int bytes;

  while(!client){
    client = server.available();
    Serial.println("No connection");
  }
 
  while(client.available() > 0){
    if (index < sizeMessage -1){
      inChar = client.read();
      inData[index] = inChar;
      index++;
      inData[index] = '\0'; //Null terminate the string
    }else{
      Serial.println(inData);
      index = 0;
    }
  }
  Serial.println("no data");
}



Thanks again for the help!



PaulS

Quote
It seems that you are NULL terminating the string for each character in the message.
The string is being kept NULL-terminated. In my mind, that is a far better practice than NULL terminating it after all the data is written to the array. This way, you never end up with a situation where there is NOT a NULL in the array.

Quote
I am getting the correct message printing.
Great.

Quote
Now, I just need to turn each comma separated value into an int.
strtok() and atoi(). I think I've mentioned them before. Maybe it wasn't to you, though.
The art of getting good answers lies in asking good questions.

mjnewsum

@PaulS - thank you for the details. I am still getting used to the idea of Null Terminating the string.

In this post , the string was only Null Terminated after the collection of all chars. I wasn't sure if Null terminating each char would add to the string each time.

I think I will change my messages format so I don't collect information with comma space variables so I can convert the string to int without splitting the sting at each comma.

I still am a bit unsure why it takes so long to connect sometimes (original question 1). I am going to try to have the arduino send out a signal to let VVVV know when the connection has been made so I don't start sending before the arduino is ready.

I figured out that I do need the client = serial.available() for some functionality (original question 2) but I am not entirely sure what it is doing.

Thank you for all the help. I should be able to get things working from here.

PaulS

Quote
I wasn't sure if Null terminating each char would add to the string each time.
I'm not sure what you mean by "add to the string each time".

Suppose you have a bookshelf that can hold 100 books. But, the books fall over if you don't use a book end, when the shelf isn't full.

Now, you need to unwrap and put 10 books on the shelf. You can unwrap a book, and put it on the shelf, and let if fall over while you get the next book ready. When it is ready, you pick up the books that have fallen over, and put the new book at the end, and let it fall over...

Or, you can unwrap the first book, put it on the shelf, and put the book end in place. Then, you unwrap the next book, put it in place of the book end, and put the book end back at the end.

THAT is what is happening when you put a character in place of the NULL and put another NULL at the end.

Quote
I figured out that I do need the client = serial.available() for some functionality (original question 2) but I am not entirely sure what it is doing.
That line of code is not what you actually have. You have either
Code: [Select]
   byte numBytes = Serial.available();
or
Code: [Select]
   client = server.available();

In the first case, you are asking the Serial instance how much unread data there is. In the second case, you are asking the server if there are any clients that need attention.
The art of getting good answers lies in asking good questions.

mjnewsum

@PaulS - Thank you for clarifying.

Quote
I'm not sure what you mean by "add to the string each time".
Earlier when you stated that the char array would need to be 12 (my message size +1) I thought the Null Termination added the +1 each time it was called. I see in the book end description that I am simply moving the null terminator to the end of the char array each time it is called.


Go Up