How to send string value to Arduino from C# code and display it in LCD?

Hello guys,

I have a problem and I can't solve it for hours. I want to send string value to Arduino from C# code, and display it in LCD screen.

Here is my C# code:

private void btn_sendit_Click(object sender, EventArgs e)
    {
            byte[] data = CommandMessage("Hello World");
            arduino.Write(data, 0, data.Length); // send to arduino
    }

    public byte[] CommandMessage(string CMD)
    {
        //This takes in a string and converts it to a byte array ready to be sent over serial

        byte[] message = new byte[CMD.Length + 2]; 
        message[0] = BitConverter.GetBytes('~')[0]; 
        for (int i = 1; i < message.Length - 1; i++)
        {
            message[i] = BitConverter.GetBytes(CMD[i - 1])[0];
        }
        message[message.Length - 1] = BitConverter.GetBytes('~')[0];                     
        return message;
    }

and here is my arduino code:

byte nextByte() {
       while(1) {
         if(Serial.available() > 0) {
             byte b =  Serial.read();
           return b;
          }
       }
    }

    void loop ()
    {
      if(Serial.available() > 0){

      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
      }
      lcd.setCursor(0, 1);
      lcd.print(inData);
    }
      if (QS == true) { // when the pulse founded

          Serial.println(BPM); // write pulse to serial monitor
          QS = false;

      delay (1000);  
      }
     }

So, I send to C# formm app from Arduino pulse values in loop method, but I can't get string value from C#. Where is my mistake? Thanks in advance. Please help me.

pseudocode:
I have a problem and I can't solve it for hours.

Then you need more patience. Some of my problems take days to figure out.

I know almost nothing about C# but the examples in Serial Input Basics should be useful. I recommend that you use the technique in the third example and make your C# program send the data in that format. That's what I do with Python.

...R

You've got a bunch of code there. You need to narrow it down.

You need to test

Does lcd.print("hello") work?

if yes, can you do this. . .

inData = "blah blah";
lcd.print(inData);

If yes, can you go one more step. Etc etc.

That place where you have lcd.print(inData), I'd be putting a "serial.print(indata)" there and see if that is writing what you expect it to be.

Is it even going in there? What is index initialized to? If you're trying to run that multiple times, are you resetting index to something less than 19? Can you really print 19 chars (or 18) to your LCD? Mine is 16 and I don't know what happens when I overwrite it.