TCP command from USBhost keyboard

Hello
I have a program which is a TCP IP client and I added an option in which I can type in text (commands) from keyboard connected to the 2nd USB hub of Arduino Due.
The Ethernet module is ENC28J60.
The command must be COMMANDCR, where CR is carriage return
When i write for example:
client.write("SCENE\x0d"); ( SCENE=command, \x0d = CR)
I have a response from the server so I am 100% sure that this excact command is OKAY.

Later when I
a) Type in "SCENE\x0d" FROM KEYBOARD CONNECTED TO ARUDINO USB or
b) Type in "SCENE" FROM KEYBOARD CONNECTED TO ARUDINO USB and add "\x0d" to it later
I do not have response from the server, so the command must be in a bad form...
But when I print "the final command" It says "SCENE" so the "\x0d" works as a carriage return because it disapears.
So the problem is in a format of command or in carriage return. I tried everything 100x times so please help me if you have any idea...

The esential part of code:

void keyloop()
{
  while (mode == 1) {
    String sentence = "";
    bool waitforkey = true;
    while (waitforkey == true)
    {
      // Process USB tasks
      usb.Task();

      // Look for valid ASCII characters
      if (inputString >= 32 && inputString <= 127)
      {
        sentence += char(inputString);


        Serial.write(inputString);
      }

      // Check for ENTER
      else if (inputString == '\')
      {
        Serial.println();
        Serial.println("Your command was sent:");
        Serial.println();
        sortSentence(sentence);
        Serial.println("Length of sentence");
        Serial.println(sentence.length() + 10);
        char komenda[sentence.length()];

        sentence.toCharArray(komenda, sentence.length() + 10);
        //      Serial.println(strlen (komenda));
        strcat(komenda, "\x0d");
        delay(100);
        Serial.println("Final command");
        Serial.println(komenda);
        Serial.println("Size of final command");
        Serial.println(sizeof(komenda));
        client.write(komenda);
        delay(1000);
        if (client.available() > 0) {
          int camretLen = client.available();
          Serial.println("RESULT");
          for (int i = 0 ; i < camretLen; i++) {
            char c = client.read();
            Serial.print(c);
            client.flush();
          }
        }
        Serial.print("CHECK");
        waitforkey = false;
      }
      inputString = 0;
    }
  }
}

whole code:

//Ethernet
#include <EthernetENC.h>
#include <SPI.h>
char incomingByte = 0;
String CR = "";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 110);
EthernetClient client;

//Keyboard
#include <KeyboardController.h>
char inputString = 0;
// Initialize USB Controller
USBHost usb;
// Attach keyboard controller to USB
KeyboardController keyboard(usb);
int mode = 0;
void setup() {
  Ethernet.init(10);
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Type a to listen");
  Serial.println("Type b to send");
}
void loop() {
  Ethernet.maintain();
  // Read serial input:
  if (Serial.available() )
  {
    incomingByte = Serial.read();

  }
  else
  {
    incomingByte = '0';
  }
  switch (incomingByte) {

    case 'a':

      if (client.available())   {
        for (int i = 0 ; i < client.available(); i++) {
          char d = client.read();
          Serial.print(d);
        }
      }
      break;
    case 'b':
      mode = 1;
      keyloop();

      //    client.write("SCENE\x0d");
      //      Serial.print("SENT");
      //      delay(500);
      //      int camretLen = client.available();
      //      if (client.available() > 0) {
      //        Serial.println("RESULT");
      //        for (int i = 0 ; i < camretLen; i++) {
      //          char c = client.read();
      //          Serial.print(c);
      //        }
      //      }
      //      break;
      client.flush();
      if (!client.connected()) {
        client.connect(server, 9876);
      }
  }
}
// This function intercepts key press
void keyPressed()
{
  inputString = keyboard.getKey();
}

// Sort the final sentence
void sortSentence(String sentence)
{
  // Sentence logic goes here
  Serial.println(sentence);
}

void keyloop()
{
  while (mode == 1) {
    String sentence = "";
    bool waitforkey = true;
    while (waitforkey == true)
    {
      // Process USB tasks
      usb.Task();

      // Look for valid ASCII characters
      if (inputString >= 32 && inputString <= 127)
      {
        sentence += char(inputString);


        Serial.write(inputString);
      }

      // Check for ENTER
      else if (inputString == '\')
      {
        Serial.println();
        Serial.println("Your command was sent:");
        Serial.println();
        sortSentence(sentence);
        Serial.println("Length of sentence");
        Serial.println(sentence.length() + 10);
        char komenda[sentence.length()];

        sentence.toCharArray(komenda, sentence.length() + 10);
        //      Serial.println(strlen (komenda));
        strcat(komenda, "\x0d");
        delay(100);
        Serial.println("Final command");
        Serial.println(komenda);
        Serial.println("Size of final command");
        Serial.println(sizeof(komenda));
        client.write(komenda);
        delay(1000);
        if (client.available() > 0) {
          int camretLen = client.available();
          Serial.println("RESULT");
          for (int i = 0 ; i < camretLen; i++) {
            char c = client.read();
            Serial.print(c);
            client.flush();
          }
        }
        Serial.print("CHECK");
        waitforkey = false;
      }
      inputString = 0;
    }
  }
}



write with one parameter expects zero terminated char array.
use write with two parameters to specify size of the buffer.

So you mean to change it to:
client.write ( komenda , (sizeof(komenda)) );
Then it will write the whole message?
So what was I sending for now if the message is "SCENE\x0d" and I used only client.write ?

Quick update:
It doesn't help.

I believe you have an error in the size you are creating komenda. You create it at sentence.length then inform sentence.toCharArray it is 10 chars longer, but then append more to it. This will be outside the dimension on komenda and possibly overwrite something else randomly. When you then call client.write using client.write ( komenda , (sizeof(komenda)) );

I don't think sentence.length includes a null terminator. If you want to dimension komenda, then I suggest you dimension it at sentence.length +10 as you have everything else - this should allow enough space for your Null terminator and the extra characters for that you wan to add. When you call client.write also use sentence.length+10.
If that works then you can improve the code afterwards.

Is this what you meant ?

char komenda[sentence.length() + 10]; 
        char size1 = sentence.length() + 10 ;
        sentence.toCharArray(komenda, sentence.length() + 10); 
        strcat(komenda, "\x0d"); 
        delay(100);
        Serial.println("Final command");
        Serial.println(komenda);
        Serial.println("Size of final command");
        Serial.println(sizeof(komenda));
        client.print(komenda, size1);

If so it gives me that error:
exit status 1
call of overloaded 'print(char [(((sizetype)) + 1)], char&)' is ambiguous

Since sentence.length returns a number I think you should use


unsigned int size1 = sentence.length() + 10;

Using a char for the length could cause problems.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.