Arduino stop to work with VB interface

Hello Everybody

Hello Everybody

I'm new in Arduino and I have a problem when I try to communicate VB.net 2017 and Arduino Mega. It's work fine once, but after it read it stop to work. Some times I need to close the program many time, to work again.

This is the code in Arduino

int jqz[]={8,9,10,11,12,13}; 
int ent[]={2,3,4,5,6,7};             
int cont=6;    

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

for(int conta=0;conta<cont;conta++)
  {pinMode(jqz[conta],OUTPUT);}

for(int conta=0;conta<cont;conta++)
  {pinMode(ent[conta],INPUT_PULLUP);}
}

void loop() {
String jacu = Serial.readStringUntil('\n');

   if (jacu.equals("081")){          // Codigo de activacion de Jacuzzi
     digitalWrite(jqz[0],HIGH);
   } 
   else if (jacu.equals("080")){
     digitalWrite(jqz[0],LOW);
   }

   if (digitalRead(pts[0])){            // Codigo del pin aseo 1
     digitalWrite(pts[0],true);
     Serial.write("pin0421");}
   else{
     digitalWrite(pts[0],false);
     Serial.write("pin0420");
  } 


}

When I compile it works fine and the serial monitor shows all information fine.

This is the code in VB

Imports ctrlHabitacion
Imports System.Windows.Forms
Imports System.IO.Ports
Imports System.IO
Imports System.Text

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Start()
        Inicializa(hab1, 1)

        arduino.PortName = "COM8"
        arduino.BaudRate = 9600
        arduino.DataBits = 8
        arduino.Parity = IO.Ports.Parity.None
        arduino.StopBits = IO.Ports.StopBits.One

        If Not arduino.IsOpen Then
            arduino.Open()
        End If

    End Sub


Private Sub arduino_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles arduino.DataReceived

        Dim string1 As String = arduino.ReadExisting()

        If string1.Equals("pin020") Then
            hab1.bgcolor = Color.Blue
        End If

        Invoke(subrotinaSTR, string1)

    End Sub

    Sub tratamentoSTR(ByVal meustring As String)
        TextBox2.Text = meustring
    End Sub

It works only once and stops the communication and the Arduino turn off all lights.

Thanks for all help

A common problem is the Arduino is reset when the VB program opens the serial connection. The VB program must wait for that reset to complete before continuing. You do not wait for a few seconds after opening the VB serial connection.

Paul

I get my Arduino program to print a message from setup() - for example Serial.println("Arduino is ready"); - and my PC program waits until it receives that before trying to talk to the Arduino.

...R