No communiction between vb.net and arduino

I have written a program in vb.net using serialport component to communicate with my arudino nano every through usb port.
The problem is that I first need to start Arduino IDE Serial Monitor and then close it before I can run my program. Else it will no send or receive anything, just empty data. (the SerialPort1_DataReceived is trigged but the message is empty).

I can close the program and start it again, it still works. But if i disconnect my arduino from USB and reconnect it, I need to do the procedure again. Start serial monitor and close it again (No data needs to be sent, just open and close it).

Any guesses?

Best regards
Micke

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

I've never tried a Nano Every but on other Arduinos like the old Nano or UNO, opening the Serial port from the computer reboots the Arduino. So if you VB program sends stuff right after opening the Serial port and expects some sort of ACK then it won't work as the Nano would still be booting and the Serial line (UART 3 on the Nano Every) will not be ready immediately.

I'd suggest you write a minimum example to test things out. vb programs opens up the Serial connection waits for a message from the arduino that you would send in the setup(). if you get it it means the Arduino rebooted.

Thanks alot for replying! okey... If I got back to basics and using this code in vb.net:

    Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
        SerialPort1.Close()
        SerialPort1.PortName = SerialPortComboBox.Text
        SerialPort1.Open()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If SerialPort1.IsOpen = True Then
            SerialPort1.Write("Hello")
        End If
    End Sub

and this code in the arduino:

int incomingByte = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);


}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
}

I still get the same thing. No data is sent before I have started Serial Monitor...

You need to set the DTR LOW wait about 100ms then set it HIGH.
That will reset the arduino

Oh..
As you can probably guess, I am a beginner. How do I do that?

I know nothing about vb.net

This should work but I don't know how to do a delay in vb.net

SerialPort1.DtrEnable (false)
delay (100)
SerialPort1.DtrEnable (true)


I still get the same thing. No data is sent before I have started Serial Monitor...

As you know something is wrong. You should not be able to open the serial port with the Arduino IDE if VB is connected. Are you trying to connect to the correct COM port?

You need to use System.Threading.Thread.Sleep(ms). it's like delay in Arduino, the app gets frozen for that duration

using System;
using System.Threading;
...
  Thread.Sleep(100);

You were adviced to do something different:

write a small code for the arduino that does nothing in loop()
and only in setup()
open serial interface
print something

your vb-code shall do nothing else than
open the serial interface and if something is received
print what was received

Try the sleep that @J-M-L suggested

I have two snippets here that will help you further develop your application I hope. The Arduino code is based on an example written by Nick Gammon, here is a link to his page which I highly recommend you read. https://www.gammon.com.au/serial

const unsigned int MAX_MESSAGE_LENGTH = 20;
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;


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

Serial.println("rdy"); 
}
 
void loop()
   {
   while (Serial.available() > 0)
   {                                                                                      
   char inByte = Serial.read();

   if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
   {                                                                                      
    message[message_pos] = inByte;
    message_pos++;
   }                                                                                        
   else
   {                                                                                         
   message[message_pos] = '\0';
 
   Serial.println(message) ;                                                                    
   message_pos = 0;
   }                                                                                       
   } 
   }

The VB code uses the serial data received event handler which runs on a second thread so also included is a delegate to enable cross thread communication.

Imports System.IO.Ports

Public Class Form1

    Public WithEvents myPort As New SerialPort("COM7", "9600")
    Private Delegate Sub mydelegate(ByVal s As String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not myPort.IsOpen Then
            myPort.DtrEnable = True
            myPort.Open()
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If myPort.IsOpen Then
            myPort.Close()
            TextBox1.Text = ""
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        myPort.WriteLine("Hello World")
    End Sub

    Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
        Dim sData As String
        myPort.ReadTimeout = 20

        Do While myPort.BytesToRead > 0
            Try
                sData = myPort.ReadLine
                Me.BeginInvoke((New mydelegate(AddressOf Text_Out)), sData)

            Catch ex As Exception

            End Try
        Loop
    End Sub

    Private Sub Text_Out(ByVal sData As String)
        TextBox1.Text = sData
    End Sub


End Class

The VB app has 3 buttons for open port close port and one to send a message to the arduino, the arduino will echo back any string that you send, on the VB app there is also a text box to display the received string.

Initially if you repeatedly open and close the serial port you will see "rdy" displayed in the text box, this indicates that the arduino has been succesfully reset

Thanks alot for help! This forum is just great! :star_struck:

This line fixed the problem: SerialPort1.DtrEnable = True

    Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
        SerialPort1.Close()
        SerialPort1.PortName = SerialPortComboBox.Text
        SerialPort1.Open()
        SerialPort1.DtrEnable = True
    End Sub

If you plan to pursue VB.NET and Arduino some very good material can be found here. Look also on the right hand side of the page where you will see:

All of it good reading and well explained. May help with any future endeavors. :slight_smile:

Ron

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.