Arduino not responding to .NET SerialPort data

Hello from a new member, just got my board!

I'm trying to make it respond to commands over the serial port (something like Firmata, but simpler).

I've written this sketch:

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

void loop()
{
  if (Serial.available()>0) {
    int inLen = Serial.available();
    char inStr[inLen];

    for(int i=0; i<inLen; i++){
      inStr[i]=Serial.read();
    }
    CommandResponse(inStr);
  }
  delay(100); 
}

boolean StringComparer(char str1[],char str2[]){
  for(int i=0;i<sizeof(str1)/sizeof(char);i++){
    if(str1[i]!=str2[i]){
      return false;
    }
  }
   return true;
}

void CommandResponse(char command[]){
  char AT[2]={'A','T'};
    
  if(StringComparer(command, AT)){
    Serial.println("OK");
  }
  else{
    Serial.println("ERR");}
}

It responds nicely, with 'OK' to 'AT' and 'ERR' to everything else using the built in serial monitor.

Next I tried to do the same with VB.NET, but I'm failing. I can open the port, send the string, and the RX LED on the Arduino flashes, but there is no response (either in VB.NET, or the TX LED).
This has me thinking that the board doesn't understand whatever VB is sending. Am I right? If not, what could be the problem?

Here is the method I use to send the data:

    Private Sub Send(ByVal msg As String)
        If msg = String.Empty OrElse MyPort Is Nothing Then Return
        Try
            'Write to the port.
            MyPort.Write((msg & vbCrLf).ToCharArray)
            'Write to the console.
            WriteToConsole(msg)
        Catch ex As Exception
            ErrorNotifyer("Error sending data.", ex)
        End Try
    End Sub

By the way, have also tried this without success:

            MyPort.WriteLine(msg)
            MyPort.WriteLine(msg & vbCrLf)
            MyPort.Write(msg & vbNewLine)
            MyPort.WriteLine((msg & vbNewLine).ToCharArray)
            MyPort.Write("A"c)
            Dim b As Byte = 30
            MyPort.Write(b)

How do you open and initialise the port ?

I use this VB.net (2005) code and it works:

Public Sub serialPortOpen()

        'Configure and open the serial port

        'If the port is already open, close it first
        If serialPort.IsOpen Then
            serialPort.Close()
        End If

        Try
            With serialPort
                .PortName = comxxx
                .BaudRate = 9600
                 .Encoding = System.Text.Encoding.ASCII
                .NewLine = Chr(13) + Chr(10)
            End With

            'Open the port and clear any junck in the input buffer
            serialPort.Open()
            serialPort.DiscardInBuffer()
            statusAndLog("Serial port : " + serialPort.PortName + " opened at " + settings.getSetting("serialPortbaud") + " baud")

        Catch Ex As Exception
           'do errorhandeling
        End Try
    End Sub

Note these two lines, they might be what you are missing:

.Encoding = System.Text.Encoding.ASCII
.NewLine = Chr(13) + Chr(10)

EDIT: This is neede also of course:

Dim WithEvents serialPort As New IO.Ports.SerialPort

That did the trick! Thank you MikMo. Now on to reciving the data...

Threading is hard, let's go shopping! :slight_smile: