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.