Problem witch received strings

Hi,
I recently started to coding arduino after long time to use FPGA. I also not very good in C.
Here is my problem:
I created such code:
'''
v0oid loop() {
String a;
Int dl;
while(Serial.available()>0) {
a = Serial.readString();
dl = a.length();
Serial.println(dl); // printing on the console
}
}
'''
Obviosly all other section off program are correct because if line //printing on the console is like:
'Serial.println(a);'
I have returned on the console what I sent to the board. However, when I like to print length of received string, the program display string only like after Serial.println(a);
Also no one of string functions works on received string. All these functions work correct if string is declared in program not received by UART.
Could somebody tell me where is the problem?
Thanks in advance.
Thomas690

Welcome to the forum, please post working code and use code tags to format it..

1. Assume that you are a receiving a string (say: Forum) from the InputBox of the Serial Monitor (Fig-1).


Figure-1:

2. Upload the following sketch in UNO.

char myArray[10];
int arrayIndex = 0;

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
  byte n = Serial.available();
  if(n != 0)
  {
    char ch = Serial.read();
    if(ch != '\n') //not Newline charcater
    {
      myArray[arrayIndex] = ch;
      arrayIndex++;
    }
    else
    {
      myArray[arrayIndex] = '\0'; //insert null-charcater
      Serial.println(myArray);
      arrayIndex = 0;  //reset
    }
  }
}

3. Open Serial Monitor at Baud Rate = 9600.
4. Select Newline option for Line ending tab of SM.
5. Enter Forum in the InputBox.
6. Click on the Send Button.
7. Check that Forum has appeared on SM.
8. Repeat the process from Step-5 with some other string/text.

Hi,
Thanks for all aswers. Especially for Mustafa.
Finally, I found the answer: Im ......idiot.
The problem was mostly with my hardware. I powered my board from USB obviously. But when I start to wtite something to parse with receiving strings I would like check at the same time how it goes with external serial-usb converter. For doing it I used tx rx pin on board and usb cable pluged in another power source- phone charger. I forgot, that this bloody stuff has also some usb communication features. Thus, when I used at the beginning only sequence of commands Serial.read(string) - Serial.print(string) it looked like all is good. But it was only echo from UART. All data was NOT readed into UART buffer because basically 2 devices was connected to the same tx-rx lines. Now all i ok. To check serial converter I have to buy just PS with correct plug.
Thanks anyway for so fast reaction.
I keep going to learn.

No, you are learning!

Serial is a 1:1 technology and as you have discovered, you already have 2 linked devices, the UART and the MCU. You can't simultaneously link together multiple serial devices to Tx and Rx. For monitoring purposes you can sometimes get away with connecting just the Rx on the serial-usb converter to the Tx on the Arduino, leaving the other pin disconnected.

Incidentally, it is possible to use an Arduino board as a serial-USB converter, but you have to hold the MCU in reset so as to stop it running. Obviously if you want to run a sketch and observe the results, then the MCU needs to be running.

So often the answer. Welcome to computer programming. :expressionless:

a7

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