Serial Communication with PC Software problem -- FIXED

Hi!!
I'm new to the forum and to the Arduino programming and I would like your help.

I'm trying to implement a communication protocol between a software runnig at the PC and Arduino.
I've written a code for getting/sending commands from/into the Arduino but I'm facing a strange problem.

The first request response must be:
request -> #:GS#
response->13:25:54#
My code is this:

void loop(){
  int index = 0;//the incoming message length
  
  while (Serial.available()){//check for incoming message
    
    delay(20);// Each character does 1ms in order to transfer
    cmd[index] = Serial.read();//Stores the word and decreases the buffer
    delay(1);
    index++;

  }

  if (index > 0){
    
    //Serial.print(cmd);

    if (strcmp(cmd, "#:GS#")  == 0){
      Serial.print("13:25:54#");
    }
  }
}

When I test this with the Serial monitor it works well but in the PC software I cannot get the response.
This is due to the fact that the cmd array stores strange chatacters because when I uncomment the Serial.print(cmd) I get strange characters and the comparisson inside the if fails.
Although this doesn't happen when I test it with Serial Monitor.

Do you know what cause the problem?
I'm getting crazy with this. :~

It might help if we could see the code you are running on the PC.

Suggestions:
Do you have the PC software set to use the right baud rate?
Are you sending the #:GS# request in the correct datatype (byte, DEC, HEX, etc.)

Thanks for your reply!
Unfortunatelly the PC program is commercial (it's not developed by me) and the protocol is fixed.

I have a monitoring program which I can see the requests/responces messages and I saw that the PC program sends the command properly (from the monitoring program I see that the program sends: "#:GS#").

From the specification of the protocol it says that the baud rate is 9600 and messages must be Hex datatyped so I assume that the PC program would be properly configured (or not?).

Your Arduino code is expecting all the serial data to arrive while you are delaying. That may not be the case. If the incoming serial data is a fixed number of bytes, simply do nothing is Serial.available() returns a smaller value.

When you do add a character to the array, you should add a NULL immediately after it:

    cmd[index] = Serial.read();
    index++;
    cmd[index]  = '\0';

You might need to test the value that Serial.read() returns, before stuffing it in the array.

Ok,
I've done some debugging and the Serial.read() gets odd characters and stores it to the array.
For example when I send a '#' char the Serial.read() stores a '.' char.
I think this is the cause of the problem.

How do I fix this?

For example when I send a '#' char the Serial.read() stores a '.' char.

How are you sending the '#' character?

It is sent by the PC program.
Anyway, I found a library called Messenger here Arduino Playground - Messenger.
It says that "is a "toolkit" that facilitates the parsing of ASCII messages"
Is it going to solve this issue?

I've done some debugging and the Serial.read() gets odd characters and stores it to the array.

In other words "I've made some code changes, but I'm not going to show them to you. It still doesn't work, though, and I still need help."

It says that "is a "toolkit" that facilitates the parsing of ASCII messages"
Is it going to solve this issue?

No. If there is a mismatch between what the PC is sending and what you think the Arduino is receiving, a library that facilitates the collection and parsing of the data isn't going to magically make the data correct.

Sorry, my mistake.
The code is this:

void loop(){
  int index = 0;//the incoming message length

  while (Serial.available()){//check for incoming message

    delay(20);// Each character does 1ms in order to transfer
    Serial.print(Serial.read());//Need to fix this
    //cmd[index] = Serial.read();//Stores the word and decreases the buffer
    //delay(1);
    //index++;
    

  }
  /*

  if (index > 0){
    Serial.print(index);
    if (strcmp(cmd, "#:GS#")  == 0){
      Serial.print("13:25:54#");
    }
  }
  */
}

I'm not able to see the exaclty what the PC programm sends and the only way is by a monitoring programm.
This is the request/reaponse from the monitoring program:

What does this (in place of the Serial.print statement you have now) print in the serial monitor?

Serial.println(Serial.read(), DEC);

PaulS:
What does this (in place of the Serial.print statement you have now) print in the serial monitor?

Serial.println(Serial.read(), DEC);

Hi! I've tried the above code and I got "29..77..141.." as response. Is it good or bad? :blush:

I just figoured out what was the cause of the problem.
I have to open the PC program first and then to plug in the usb cable to the Arduino board.
I don't know why is this happening but I think its not a problem.
Now I see that the commands are being sending and reciving properly without any encoding (i.e. DEC).

Thanks for your help. Now I'm ready to implement the communication protocol XD.
Maybe I will use the Messenger library.