# Telnet sending codes problem

**URL:** <https://forum.arduino.cc/t/telnet-sending-codes-problem/968481>\
**Category:** Networking, Protocols, and Devices\
**Created:** [March 12, 2022, 2:04pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481 "2022-03-12T14:04:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thowiba](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@thowiba](https://forum.arduino.cc/u/thowiba)\
**Post date:** [March 12, 2022, 2:04pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/1 "2022-03-12T14:04:15Z")

</div>

Hello  
I have a remote power unit (NP-02E). My use will be to send a telnet on / off command to the unit by Arduino One with Ethernet. My problem:  
The code works and I can connect the unit, I get the report from the NP-02 as connected. But the command in my "void loop" works only, when I push the ENTER on the computerkeyboart in the send line of the Serialwindow. I tried out to send a "\n", before the command, after the command, nothing helps.  
Who tells me what to do.

Thanks thowiba

Testcode:

`````````````````````````````````````````````````````````````````````````````````````cpp
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF} ;
IPAddress ip(192, 168, 1, 2);
IPAddress server(192, 168, 1, 3);

//command for the NP-02E: pset [port] [0=off, 1=on]

EthernetClient client;
const char* on1 = "pset 1 1";
const char* off1 = "pset 1 0";
const char* on2 = "pset 2 1";
const char* off2 = "pset 2 0";
int oneState = 1;
int twoState = 1;

void setup() {

   Ethernet.init(10); 
   Ethernet.begin(mac, ip);

   Serial.begin(9600);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
     }

  if (client.connect(server, 23)) {
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
  }
}

void loop() {

 if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  while (Serial.available() > 0) {
    char inChar = Serial.read();

     if (client.connected()) {
        if (oneState == 0){
        client.println(on1);
        oneState = 1;
        }
      else {
        client.println(off1);
        oneState = 0;
        }
       delay (1000); 
      if (twoState == 0){
        client.println(on2);
        twoState = 1;
        }
      else {
        client.println(off2);
        twoState = 0;
        }
         delay (20);
      }
   }
}
````````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````````````````````````````````````````````

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 12, 2022, 2:14pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/2 "2022-03-12T14:14:22Z")

</div>

> [@thowiba](#):
>
> Who tells me what to do.

Carriage return is a single character (ASCII 13) so if you want send it you do not use "\n", which is a string. Rather you use '\n' which will send a value of 13. Send it after the command to terminate it or concatenate it on the end of the command

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [March 12, 2022, 6:00pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/3 "2022-03-12T18:00:42Z")

</div>

> [@thowiba](#):
>
> Arduino One with Ethernet

I guess you mean an Arduino UNO with an Ethernet Shield.

> [@thowiba](#):
>
> But the command in my "void loop" works only, when I push the ENTER on the computerkeyboart in the send line of the Serialwindow.

Your code doesn't react on **what** you send it simply wait **until** you send something. The serial monitor by default waits for a return/enter key before it sends anything to the Arduino.

---

<div class="post-metadata">

**Author:** ![thowiba](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@thowiba](https://forum.arduino.cc/u/thowiba)\
**Post date:** [March 12, 2022, 6:34pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/4 "2022-03-12T18:34:03Z")

</div>

Hello Pylon

This could be exactly the error. But where is the error? Can you help me please?

thowiba

------ Originalnachricht ------

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [March 12, 2022, 6:46pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/5 "2022-03-12T18:46:32Z")

</div>

> [@thowiba](#):
>
> But where is the error? Can you help me please?

No, because I have no clue what the intended behavior is or what you think is an error. The Serial Monitor of the IDE always waits for a return/enter key before it sends anything to the Arduino. If you don't want that you're free to use another serial terminal program.

---

<div class="post-metadata">

**Author:** ![thowiba](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@thowiba](https://forum.arduino.cc/u/thowiba)\
**Post date:** [March 12, 2022, 7:01pm UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/6 "2022-03-12T19:01:06Z")

</div>

My final goal is to send a command to the NP-02E with a push-button on the arduino. This scetch was only as test. I'm a beginner and I expected that the "void loop" works as loop just changes the state of the NP-02E on and off each 2 seconds. Should I take away the

```cpp
 while (Serial.available() > 0) {
    char inChar = Serial.read();

```

for this?

Thank you  
thowiba

---

<div class="post-metadata">

**Author:** ![thowiba](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@thowiba](https://forum.arduino.cc/u/thowiba)\
**Post date:** [March 13, 2022, 9:27am UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/7 "2022-03-13T09:27:59Z")

</div>

I think I anderstand now this testcode.  
Thanks everybody for your help!

thowiba

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 9, 2022, 9:28am UTC](https://forum.arduino.cc/t/telnet-sending-codes-problem/968481/8 "2022-09-09T09:28:04Z")

</div>

Closed on September 9, 2022, 9:28am UTC

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