Visual Basic, SerialPort.ReadLine() freezes

I am trying to ReadLine from my Visual Basic application. I am new to both Arduino and Visual Basic so please bare with me :slight_smile: I am using Arduino Leonardo.

This is my Arduino application so far, I tried looping only this line Serial.println(TempOutside + 48); but still VB application freezes

int greenLED = 8;
int arduinoLED = 13;

int TempOutside = 25;
int TempInside = 26;
int HumOutside = 27;
int HumInside = 28;

void setup()
{
  Serial.begin(57600);
  pinMode(greenLED, OUTPUT);
  pinMode(arduinoLED, OUTPUT);
  digitalWrite(greenLED, LOW);
  digitalWrite(arduinoLED, LOW);
}

void loop()
{
/*  byte buffer[2];
  while(Serial.available() == 0);
  int bytes=Serial.available();
  
  for(int i=0;i<bytes;i++){
    buffer[i]=Serial.read();
  }
  
  if (buffer[0] - '0' == 0){
    digitalWrite(greenLED, LOW);
    Serial.println("Green LED Off");
  }
  else if (buffer[0] - '0' == 1){
    digitalWrite(greenLED, HIGH);
    Serial.println("Green LED On");
  }
  else if (buffer[0] - '0' == 2){
    Serial.println("Unknown");
  }
  else if (buffer[0] - '0' == 3){
    digitalWrite(arduinoLED, HIGH);
    Serial.println("Arduino LED On");
  }
  else if (buffer[0] - '0' == 4){
    digitalWrite(arduinoLED, LOW);
    Serial.println("Arduino LED Off");
  }
  else if (buffer[0] - '0' == 5){
    Serial.println("Motor On");
  }
  else if (buffer[0] - '0' == 6){
    Serial.println("Motor Off");
  }
  else if (buffer[0] - '0' == 7){
    if (buffer[1] - '0'  == 22){
      digitalWrite(greenLED, HIGH);
    }
    else{
      digitalWrite(arduinoLED, HIGH);
    }
  }
  else if(buffer[0] - '0' == 8){
   if (buffer[1] - '0'  == 60){
     digitalWrite(greenLED, HIGH);
     }
   else{
     digitalWrite(arduinoLED, HIGH);
   }
 }
 else if(buffer[0] - '0' == 9){*/
    Serial.println(TempOutside + 48);
    /*Serial.write(TempInside + 48);
    Serial.write(HumOutside + 48);
    Serial.write(HumInside + 48);
  }
 else{
   digitalWrite(arduinoLED, LOW);
   digitalWrite(greenLED, LOW);
 }*/
 Serial.flush();
}

This is my Visual Basic code

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class ControlPanel
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Sub DefaultSettings()
        Dim WatteringTime As Date = #3/1/2015 9:00:00 PM#
        Dim ccg1 As New Sensors()
        ccg1.TempInside = 32
        ccg1.HumInside = 30
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub ControlPanel_load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 57600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        DefaultSettings()
    End Sub

    Private Sub GreenLED_On_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_On.Click
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub GreenLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_Off.Click
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub

    Private Sub ArdionoLED_On_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_On.Click
        SerialPort1.Open()
        SerialPort1.Write("3")
        SerialPort1.Close()
    End Sub

    Private Sub ArdionoLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_Off.Click
        SerialPort1.Open()
        SerialPort1.Write("4")
        SerialPort1.Close()
    End Sub

    Private Sub MotorOn_Click(sender As System.Object, e As System.EventArgs) Handles MotorOn.Click
        SerialPort1.Open()
        SerialPort1.Write("5")
        SerialPort1.Close()
    End Sub

    Private Sub MotorOff_Click(sender As System.Object, e As System.EventArgs) Handles MotorOff.Click
        SerialPort1.Open()
        SerialPort1.Write("6")
        SerialPort1.Close()
    End Sub

  
    Private Sub Temp_Set_Click(sender As System.Object, e As System.EventArgs) Handles Temp_Set.Click
        SerialPort1.Open()
        Dim array As String
        array = "7" & Chr(tb_TempInside.Text + 48)
        SerialPort1.Write(array)
        SerialPort1.Close()
        Dim ccg1 As New Sensors()
        ccg1.TempSet = tb_TempInside.Text
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
    End Sub

    Private Sub Humidity_Set_Click(sender As System.Object, e As System.EventArgs) Handles Humidity_Set.Click
        SerialPort1.Open()
        Dim array As String
        array = "8" & Chr(tb_Humidity.Text + 48)
        SerialPort1.Write(array)
        SerialPort1.Close()
        Dim ccg1 As New Sensors()
        ccg1.TempSet = tb_Humidity.Text
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub bt_Read_Sensors_Click(sender As System.Object, e As System.EventArgs) Handles bt_Read_Sensors.Click
        SerialPort1.Open()
        'SerialPort1.Write("9")
        'Dim comBuffer As Byte() = New Byte(bytes - 1) {}
        'SerialPort1.Read(comBuffer, 0, bytes)
        Dim data_rx As String
        data_rx = SerialPort1.ReadLine()
        MsgBox(data_rx)
        SerialPort1.Close()
    End Sub
End Class

Can someone show me how to read 1 line, simplest code ever?
I followed this tutorial and though it will work in VB6. I was wrong...

Any help is appreciated :slight_smile:

I can only help you on the Arduino code, not on the VB. In the Arduino-Part, I don't see anything wrong.

Try to monitor the serial communication from the Arduino using the Serial Monitor inside the IDE. Do you get the messages as defined? If yes, the problem is probably within the VB part. If no, you have a problem inside the arduino part.

Hi,

Arduino part works just fine. I checked with Serial monitor and it prints values. I know its problem in VB code. Cause I don't know yet how to read serial data using VB code.

I tried this tutorial as well

I receive
"Error: Serial Port read timed out."

Tried as well this one

I get nothing on console.

Tried to follow this one

I get nothing in my label tb_Sensors

Tried this one

Got nothing...

VB code

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class ControlPanel
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim data_rx As String
    Delegate Sub SetTextCallBack(ByVal [text] As String)
    Dim ccg1 As New Sensors()

    Sub DefaultSettings()
        Dim WatteringTime As Date = #3/1/2015 9:00:00 PM#
        ccg1.TempInside = 32
        ccg1.HumInside = 30
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub ControlPanel_load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 57600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        DefaultSettings()
    End Sub

    Private Sub GreenLED_On_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_On.Click
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub GreenLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_Off.Click
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub

    Private Sub ArdionoLED_On_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_On.Click
        SerialPort1.Open()
        SerialPort1.Write("3")
        SerialPort1.Close()
    End Sub

    Private Sub ArdionoLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_Off.Click
        SerialPort1.Open()
        SerialPort1.Write("4")
        SerialPort1.Close()
    End Sub

    Private Sub MotorOn_Click(sender As System.Object, e As System.EventArgs) Handles MotorOn.Click
        SerialPort1.Open()
        SerialPort1.Write("5")
        SerialPort1.Close()
    End Sub

    Private Sub MotorOff_Click(sender As System.Object, e As System.EventArgs) Handles MotorOff.Click
        SerialPort1.Open()
        SerialPort1.Write("6")
        SerialPort1.Close()
    End Sub

  
    Private Sub Temp_Set_Click(sender As System.Object, e As System.EventArgs) Handles Temp_Set.Click
        SerialPort1.Open()
        Dim array As String
        array = "7" & Chr(tb_TempInside.Text + 48)
        SerialPort1.Write(array)
        SerialPort1.Close()
        ccg1.TempSet = tb_TempInside.Text
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
    End Sub

    Private Sub Humidity_Set_Click(sender As System.Object, e As System.EventArgs) Handles Humidity_Set.Click
        SerialPort1.Open()
        Dim array As String
        array = "8" & Chr(tb_Humidity.Text + 48)
        SerialPort1.Write(array)
        SerialPort1.Close()
        ccg1.HumSet = tb_Humidity.Text
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub bt_Read_Sensors_Click(sender As System.Object, e As System.EventArgs) Handles bt_Read_Sensors.Click
        SerialPort1.Open()
        SerialPort1.Write("9")
        data_rx = ReceiveSerialData()
        'data_rx = SerialPort1.ReadLine
        MsgBox(data_rx)
        SerialPort1.Close()
    End Sub

    Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        data_rx = SerialPort1.ReadLine
        MsgBox(data_rx)
        If Me.tb_Sensors.InvokeRequired Then
            Dim x As New SetTextCallBack(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.tb_Sensors.Text &= [text]
        End If

    End Sub

    Function ReceiveSerialData() As String
        ' Receive strings from a serial port. 
        Dim returnStr As String = ""

        Dim com1 As IO.Ports.SerialPort = Nothing
        Try
            'com1 = My.Computer.Ports.OpenSerialPort("COM1")
            'com1.ReadTimeout = 10000
            Do
                'SerialPort1.Open()
                SerialPort1.ReadTimeout = 1000
                Dim Incoming2 As String = SerialPort1.ReadLine()
                'Dim Incoming As String = com1.ReadLine()
                If Incoming2 Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming2 & vbCrLf
                End If
            Loop
        Catch ex As TimeoutException
            returnStr = "Error: Serial Port read timed out."
        Finally
            If com1 IsNot Nothing Then com1.Close()
        End Try

        Return returnStr
    End Function

End Class

Arduino code

int greenLED = 8;
int arduinoLED = 13;

int TempOutside = 25;
int TempInside = 26;
int HumOutside = 27;
int HumInside = 28;

void setup()
{
  Serial.begin(57600);
  pinMode(greenLED, OUTPUT);
  pinMode(arduinoLED, OUTPUT);
  digitalWrite(greenLED, LOW);
  digitalWrite(arduinoLED, LOW);
}

void loop()
{
  byte buffer[2];
  while(Serial.available() == 0);
  int bytes=Serial.available();
  
  for(int i=0;i<bytes;i++){
    buffer[i]=Serial.read();
  }
  
  if (buffer[0] - '0' == 0){
    digitalWrite(greenLED, LOW);
    Serial.println("Green LED Off");
  }
  else if (buffer[0] - '0' == 1){
    digitalWrite(greenLED, HIGH);
    Serial.println("Green LED On");
  }
  else if (buffer[0] - '0' == 2){
    Serial.println("Unknown");
  }
  else if (buffer[0] - '0' == 3){
    digitalWrite(arduinoLED, HIGH);
    Serial.println("Arduino LED On");
  }
  else if (buffer[0] - '0' == 4){
    digitalWrite(arduinoLED, LOW);
    Serial.println("Arduino LED Off");
  }
  else if (buffer[0] - '0' == 5){
    Serial.println("Motor On");
  }
  else if (buffer[0] - '0' == 6){
    Serial.println("Motor Off");
  }
  else if (buffer[0] - '0' == 7){
    if (buffer[1] - '0'  == 22){
      digitalWrite(greenLED, HIGH);
    }
    else{
      digitalWrite(arduinoLED, HIGH);
    }
  }
  else if(buffer[0] - '0' == 8){
   if (buffer[1] - '0'  == 60){
     digitalWrite(greenLED, HIGH);
     }
   else{
     digitalWrite(arduinoLED, HIGH);
   }
 }
 else if(buffer[0] - '0' == 9){
    Serial.println(TempOutside + 48, DEC);
    Serial.println(TempInside + 48);
    Serial.println(HumOutside + 48);
    Serial.println(HumInside + 48);
  }
 else{
   digitalWrite(arduinoLED, LOW);
   digitalWrite(greenLED, LOW);
 }
 Serial.flush();
}

Do You have sample in some other language that can read data sent from Arduino? W/e I try can't make it to work :confused:

I know its problem in VB code.

In that case this may not be the best place to get help.

        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()

Opening the serial port resets the Arduino. Sending it data without waiting for the reset to complete is pointless. Closing the serial port resets the Arduino, too.

        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        data_rx = SerialPort1.ReadLine

When serial data arrives, read all of the data. Then call a function with that data, and have that function wait for some more serial data, which will never arrive, because, if it does, the event handler will be dispatched again. Can't you see that the ReadLine call is pointless?

Hi,

I didn't know that. Thank You I modded my VB program now to only open port at begging and I learned from this
http://www.multiwingspan.co.uk/arduino.php?page=vb1

tutorial that I should probably add some Settings tab where I will open connection prior to sending/receiving data from Arduino and close it once I am done with changes.

I am still trying to understand how to read data from Arduino. I think I figured out whats the problem. All applications I build from Visual Studio were only working when sending data, never when receiving data. I am able to turn on/off leds w/o any problems but always freezes or doesn't work at all when receiving data from Arduino.

I followed above mentioned tutorial, like X one in last few days and again it didn't work. I opened Arduino IDE and its Serial Port monitor and sent 1 char of data, again it worked and send it back to monitoring tool. There is some error in my Visual Studio installation and that why nothing worked for me so far...

I reset all, still unable to make it work... I am using this plugin for VS...
http://www.visualmicro.com/page/User-Guide.aspx?doc=Serial-Monitor.html

Doesn't work anymore. I don't remember when it stopped working. My FW and AV is turned off. I tried both Visual Studio 2010 and Visual Studio 2013

I think when trial expired, even though it still says in program 30 days remaining. I just reactivated trial again on same PC, weird... I think I gonna reinstall windows now.

My latest VB code

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class ControlPanel
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim data_rx As String
    Delegate Sub SetTextCallBack(ByVal [text] As String)
    Dim ccg1 As New Sensors()

    Sub DefaultSettings()
        Dim WatteringTime As Date = #3/1/2015 9:00:00 PM#
        ccg1.TempInside = 32
        ccg1.HumInside = 30
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub ControlPanel_load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "COM3"
        SerialPort1.BaudRate = 57600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        DefaultSettings()
        SerialPort1.Open()
    End Sub

    Private Sub GreenLED_On_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_On.Click
        SerialPort1.Write("1")
    End Sub

    Private Sub GreenLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_Off.Click
        SerialPort1.Write("0")
    End Sub

    Private Sub ArdionoLED_On_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_On.Click
        SerialPort1.Write("3")
    End Sub

    Private Sub ArdionoLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_Off.Click
        SerialPort1.Write("4")
    End Sub

    Private Sub MotorOn_Click(sender As System.Object, e As System.EventArgs) Handles MotorOn.Click
        SerialPort1.Write("5")
    End Sub

    Private Sub MotorOff_Click(sender As System.Object, e As System.EventArgs) Handles MotorOff.Click
        SerialPort1.Write("6")
    End Sub

  
    Private Sub Temp_Set_Click(sender As System.Object, e As System.EventArgs) Handles Temp_Set.Click
        Dim array As String
        array = "7" & Chr(tb_TempInside.Text + 48)
        SerialPort1.Write(array)
        ccg1.TempSet = tb_TempInside.Text
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
    End Sub

    Private Sub Humidity_Set_Click(sender As System.Object, e As System.EventArgs) Handles Humidity_Set.Click
        Dim array As String
        array = "8" & Chr(tb_Humidity.Text + 48)
        SerialPort1.Write(array)
        ccg1.HumSet = tb_Humidity.Text
        lb_HumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub bt_Read_Sensors_Click(sender As System.Object, e As System.EventArgs) Handles bt_Read_Sensors.Click
        SerialPort1.Write("9")
        data_rx = ReceiveSerialData()
        'data_rx = SerialPort1.ReadLine
        MsgBox(data_rx)
    End Sub

    Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        data_rx = SerialPort1.ReadLine
        MsgBox(data_rx)
        If Me.tb_Sensors.InvokeRequired Then
            Dim x As New SetTextCallBack(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.tb_Sensors.Text &= [text]
        End If

    End Sub

    Function ReceiveSerialData() As String
        ' Receive strings from a serial port. 
        Dim returnStr As String = ""

        Dim com1 As IO.Ports.SerialPort = Nothing
        Try
            'com1 = My.Computer.Ports.OpenSerialPort("COM1")
            'com1.ReadTimeout = 10000
            Do
                'SerialPort1.Open()
                SerialPort1.ReadTimeout = 1000
                Dim Incoming2 As String = SerialPort1.ReadLine()
                'Dim Incoming As String = com1.ReadLine()
                If Incoming2 Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming2 & vbCrLf
                End If
            Loop
        Catch ex As TimeoutException
            returnStr = "Error: Serial Port read timed out."
        Finally
            If com1 IsNot Nothing Then com1.Close()
        End Try

        Return returnStr
    End Function

End Class

Is this now correct? I opened port only at beginning when form loaded, didn't close it though?

I don't see why its pointless cause I can't get it to work so I can't even learn how to make a simplest version to work... I tried numerous combination, functions and nothing works. I am lost.

I hope when I get fresh windows it will work... Will post update and working code :slight_smile:

Thanks :slight_smile:

Installed fresh windows. Installed VS 2010, Arduino, Atmel Studio & Visual Micro. Still not working. I think my Arduino is broken or cable is f* up.

I am able to send 1 char over VS Serial monitoring tool from Visual Micro and after Arduino stops sending data to my PC, it just receives data after... Which is weird. I am able to send data and control Arduino but Serial monitoring tool in Visual studio doesn't receive data...

It shows up in Device manager but it gets d/c and connects after when I send data to it. After it stops sending data I need to press reset button to make it send and receive data again from Visual studio. From Arduino monitoring tool I can send and receive any amount of data :S

I am having issues similar to these >>
http://forum.arduino.cc/index.php?topic=54796.15

Oh well...

Before it was working with the serial monitor. If that is still the case, the arduino is fine.

An example to get data from VB.Net 2010 (i have write in past) is the bellow:

   Dim Rx As String
   Dim readBuffer As String
   
'   Dim txtIn AS String
'  Delegate Sub myMethodDelegate(ByVal [text] As String)
'  Dim myDelegate As New myMethodDelegate(AddressOf ShowString)

'  Public Sub ShowString(ByVal myString As String)
'      txtIn.AppendText(myString)
'  End Sub


  Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
        If readBuffer.Contains("Exit") Then Exit Sub 'MsgBox("Exit request")

        Rx = readBuffer

        If Rx.Contains("{") Then
		'Do Something
        End If

        If Rx.Contains("}") Then
		'Do Something

        End If

    End Sub


   Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Try
            readBuffer = SerialPort1.ReadLine()
            Me.Invoke(New EventHandler(AddressOf DoUpdate))
        Catch ex As Exception
            MsgBox("read " & ex.Message)
        End Try

    End Sub

And for send something

Dim Complete As String = "Send data to Arduino ..."
If SerialPort1.IsOpen Then
	Complete = Complete + Chr(13)
        SerialPort1.Write(Complete) 
End If

If you use Hardware Serial then you can do (It is a part of a code i use in past)

void serialEvent(){

 	while (Serial.available()){
 		int RxIn = Serial.read();

		if (RxIn == '\n' || RxIn == '\r' ){ // Check for CrLf
			if (Check == true){
				complete = true;
				Check = false;
				Count = 0;
			}


			RxIn = 0;
			Count = 0;
			break;
		}else{
			if (Count == 0){     // Check for sync
				if (RxIn == '@'){
					SoftFunction = true; //Check Software check
					tempCoutn = 0;
				}
			}

			// Check Software option
			if (SoftFunction == true){
				if (tempCoutn == 1){

					if ((RxIn - 48) == 0 ) Serial.println(F("Version 1.0"));
					
				}
				tempCoutn++;
			}

					}
		Count++;
 	}         // End while Serial Available
}

And from vb to send data (for check version)

 Try
            Dim ret As String = "@0" + Chr(13)
            If SerialPort1.IsOpen Then
                SerialPort1.Write(ret)
            End If

        Catch ex As Exception
            MsgBox("Error msg: " & ex.Message.ToString, MsgBoxStyle.Critical, "Error found")
        End Try

Good luck :wink:

if you trying to read serial data from arduino Here is vb code

Use appropriate button Icon & labels here,

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
    Dim myPort As Array  'COM Ports detected on the system will be stored here
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data

    Private Sub frmMain_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600)     'Populate the cmbBaud Combo box to common baud rates used
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)

        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0)    'Set cmbBaud text to the first Baud rate on the list
        'SerialPort1.PortName = cmbPort.Text()
        'SerialPort1.BaudRate = cmbBaud.Text()
        btnDisconnect.Enabled = False           'Initially Disconnect Button is Disabled

    End Sub


    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

        SerialPort1.PortName = cmbPort.Text()


        'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = cmbBaud.Text()
        'Set Baud rate to the selected value on

        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8            'Open our serial port
        SerialPort1.Open()

        btnConnect.Enabled = False          'Disable Connect button
        btnDisconnect.Enabled = True        'and Enable Disconnect button

    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        SerialPort1.Close()             'Close our Serial Port

        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        SerialPort1.Write(txtTransmit.Text & vbCr) 'The text contained in the txtText will be sent to the serial port as ascii
        'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())    'Automatically called every time a data is received at the serialPort
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.Current_Read.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.Current_Read.Text &= [text]
        End If
    End Sub

    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text         'pop a message box to user if he is changing ports
        Else                                                                               'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbBaud.Text         'pop a message box to user if he is changing baud rate
        Else                                                                                'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub



  


End Class

I already wrote a reply, but I lost Internet connection and reply was not saved :cry: Will try to write all I noticed again...

First of all thanks again for answers, I will try all of Your solutions one by one. I have been learning how to implement db and login system. So far so good :slight_smile:

I have tested Your solution AMPS-N
Current_Read is text box with name Current_Read right? Just to be sure cause I am not receiving anything.

My friend dropped by to my house today and tried to figure out the issue. Was unable to do so. He also suggested we should short jump RX and TX on board, pins 0 and 1 saying that way Arduino will send all data to Serial monitoring tool he receives. Sadly that didn't work. Thus I learned one new thing and noticed something interesting. When I send data to my Arduino RX led lights up but TX doesn't. So I opened Arudino.cc Serial Monitoring tool. I sent command 3 & 4 to turn off Arduino LED. It turns the LED on and Off, it lights up both LEDs RX and TX and it prints to Serial monitoring tool "Arduino LED Off", "Arduino LED On". As I expected. Now I opened Visual Micros inside Visual Studio and sent again commands 3 & 4. This time only RX lighted up. TX didn't and nothing was received inside Visual Micro Serial monitoring tool. I also tried to use Brays terminal, Terminal, again only RX LED lighted up. I as well tried my Serial monitoring tool and other applications I coded in last few days, including Yours AMPS-N, TX LED when transiting commands 3 or 4 didn't light up, meaning only that Arduino didn't send any data to COM port. But yeah I can control it np from all apps I build so far and all monitoring tools I tried but in no project or monitoring tool, except Arduino.cc Serial monitoring tool, am I receiving any data from Arduino.

Any thoughts on that? I didn't found any differences between Arduino Leonardo and other boards in this guide

Do I need to send some signal to Arduino from my VB app to enable transmission to PC?

Found a solution.
http://forum.arduino.cc/index.php?topic=151088.0

I need to set DtrEnable to True... Only for Arduino Leonardo afaik?

SerialPort1.DtrEnable = True

Dear God it works now I can continue to work on this project!

Lost few days cause of this -.-" Hope someone help these solutions.

I dont how is ur serial data is. & your arduino code.

The code is below is just to take flaoting point. So if give arduino out put file of serial monitor . i cant what you need to do.Similarly you want data to send data from VB like relay ON & OFF function. You also need to change code In arduino section like receive functions

Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Windows.Forms.DataVisualization.Charting


Public Class Form1
    Dim myPort As Array
    Dim Distance As Integer

    Dim dT As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        PortComboBox.Items.AddRange(myPort)
        BaudComboBox.Items.Add(9600)
        BaudComboBox.Items.Add(19200)
        BaudComboBox.Items.Add(38400)
        BaudComboBox.Items.Add(57600)
        BaudComboBox.Items.Add(115200)
        ConnectButton.Enabled = True
        DisconnectButton.Enabled = False
        Call initializeDataTable()
        Call initializeChart()
        Chart1.DataSource = dT
        Timer1.Interval = 1000
        Timer1.Start()
    End Sub


    

    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
        SerialPort1.PortName = PortComboBox.Text
        SerialPort1.BaudRate = BaudComboBox.Text
        SerialPort1.Open()
        Timer1.Start()

        'lblMessage.Text = PortComboBox.Text & " Connected."
        ConnectButton.Enabled = False
        DisconnectButton.Enabled = True
    End Sub

    Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
        SerialPort1.Close()

        DisconnectButton.Enabled = False
        ConnectButton.Enabled = True
    End Sub
    Dim rnd As New Random

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Static counter As Integer = 0


        Try

            SerialPort1.Write("c")
            System.Threading.Thread.Sleep(250)
            Dim k As Double
            Dim distance As String = SerialPort1.ReadLine()

            ListBoxSensor.Text = distance

          

        Catch ex As Exception

            ' MsgBox("Error in Timer1_Tick: " & ex.Message)
        End Try



    End Sub

    Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
        SerialPort1.Write("1")
    End Sub

    Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
        SerialPort1.Write("0")
    End Sub


    

    Private Sub OK_Pressed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Pressed.Click

    End Sub


End Class

Oki. So far so good. This is first basic version of project I build. I hope someone will find these files & src usefull.

Visual Basic Control Panel Class >>

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class ControlPanel
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim data_rx As String
    Delegate Sub SetTextCallBack(ByVal [text] As String)
    Dim ccg1 As New Sensors()

    Sub DefaultSettings()
        ccg1.TempInside = 0
        ccg1.HumInside = 0
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        lbHumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
    End Sub

    Private Sub GetSerialPortNames()
        For Each serport As String In My.Computer.Ports.SerialPortNames
            cmbPort.Items.Add(serport)
            'MsgBox(sport)
        Next
    End Sub

    Sub ShowString(ByVal myString As String)
        tbIn.AppendText(myString)
    End Sub

    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myDelegate As New myMethodDelegate(AddressOf ShowString)

    Private Sub ControlPanel_load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        DefaultSettings()
        'Enable transmission, for Arduino Leonardo only -.-"
        SerialPort1.DtrEnable = True
        SerialPort1.RtsEnable = True

        Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"}
        cmbBaud.Items.AddRange(BaudRates)
        cmbBaud.SelectedIndex = 4
        Try
            GetSerialPortNames()
            cmbPort.SelectedIndex = 0
        Catch
            MsgBox("No ports connected.")
        End Try

        'Jer uvik sve zaboravim!
        tabControl.SelectTab("tabSettings")

    End Sub

    Private Sub GreenLED_On_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_On.Click
        SerialPort1.Write("1")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub

    Private Sub GreenLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles GreenLED_Off.Click
        SerialPort1.Write("0")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub

    Private Sub ArdionoLED_On_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_On.Click
        SerialPort1.Write("3")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub

    Private Sub ArdionoLED_Off_Click(sender As System.Object, e As System.EventArgs) Handles ArdionoLED_Off.Click
        SerialPort1.Write("4")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub

    Private Sub MotorOn_Click(sender As System.Object, e As System.EventArgs) Handles MotorOn.Click
        SerialPort1.Write("5")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub

    Private Sub MotorOff_Click(sender As System.Object, e As System.EventArgs) Handles MotorOff.Click
        SerialPort1.Write("6")
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)
        MsgBox(data_rx)
    End Sub


    Private Sub Temp_Set_Click(sender As System.Object, e As System.EventArgs) Handles Temp_Set.Click
        Dim array As String
        array = "7" & Chr(tb_TempInside.Text)
        SerialPort1.Write(array)
        ccg1.TempSet = tb_TempInside.Text
        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)

        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)

        'Should be updated every X seconds after it has been set to new value
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        tb_TempInside.Text = ""
    End Sub

    Private Sub Humidity_Set_Click(sender As System.Object, e As System.EventArgs) Handles Humidity_Set.Click
        Dim array As String
        array = "8" & Chr(tb_Humidity.Text)
        SerialPort1.Write(array)
        ccg1.HumSet = tb_Humidity.Text

        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)

        data_rx = SerialPort1.ReadLine
        tbIn.AppendText(data_rx & Environment.NewLine)

        'Should be updated every X seconds after it has been set to new value
        lbHumidityInside.Text = "Humidity Inside " & ccg1.HumInside.ToString & "%"
        tb_Humidity.Text = ""
    End Sub

    Private Sub bt_Read_Sensors_Click(sender As System.Object, e As System.EventArgs) Handles bt_Read_Sensors.Click
        SerialPort1.Write("9")
        tbIn.AppendText("Sensors data" & Environment.NewLine)

        data_rx = SerialPort1.ReadLine
        ccg1.TempOutside = data_rx
        tbIn.AppendText(data_rx & Environment.NewLine)
        lbTempOutside2.Text = "Temperature Outside " & ccg1.TempOutside.ToString & "°C"

        data_rx = SerialPort1.ReadLine
        ccg1.TempInside = data_rx
        tbIn.AppendText(data_rx & Environment.NewLine)
        lbTempInside2.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"
        lbTempInside.Text = "Temperature Inside " & ccg1.TempInside.ToString & "°C"

        data_rx = SerialPort1.ReadLine
        ccg1.HumOutside = data_rx
        tbIn.AppendText(data_rx & Environment.NewLine)
        lbHumOutside2.Text = "Humidity Outside " & ccg1.HumOutside.ToString & "%"

        data_rx = SerialPort1.ReadLine
        ccg1.HumInside = data_rx
        tbIn.AppendText(data_rx & Environment.NewLine)
        lbHumInside2.Text = "Humidity Inside  " & ccg1.HumInside.ToString & "%"
        lbHumidityInside.Text = "Humidity Inside  " & ccg1.HumInside.ToString & "%"

        tb_Sensors.Text = ccg1.TempOutside.ToString & " " & ccg1.TempInside.ToString & " " & ccg1.HumOutside.ToString & " " & ccg1.HumInside.ToString
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
        Try
            SerialPort1.BaudRate = cmbBaud.SelectedItem.ToString
            SerialPort1.PortName = cmbPort.SelectedItem.ToString
            SerialPort1.Open()
            SerialPort1.DtrEnable = True
            SerialPort1.RtsEnable = True

            If SerialPort1.IsOpen Then
                btnConnect.Visible = False
                cmbPort.Enabled = False
                cmbBaud.Enabled = False
                btnDisconnect.Visible = True
            End If
        Catch
            SerialPort1.Close()
        End Try
    End Sub

    Private Sub btnDisconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnDisconnect.Click
        Try
            SerialPort1.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            cmbPort.Enabled = True
            cmbBaud.Enabled = True
            Exit Sub
        Catch
            MessageBox.Show("Sum Ting Wong")
        End Try
    End Sub

    Private Sub ControlPanel_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If SerialPort1.IsOpen() Then
            MessageBox.Show("Disconnect before closing")
            e.Cancel = True
        End If
        End
    End Sub

    Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
        If SerialPort1.IsOpen() Then
            SerialPort1.WriteLine(tbMessage.Text)
            tbIn.AppendText(SerialPort1.ReadLine() & Environment.NewLine)
            tbMessage.Text = ""
        End If
    End Sub

End Class

I attached complete project as well, Arduino code and pictures of panel. Unfortunately I couldn't just paste it here cause it exceeds 9k char limit :cry:

Have a question though... How should I receive multiple lines in VB, should I send actual EOT, ASCII DEC 4 that I will read in VB and recognize as EOT?

What is the best practice?

@PaulS
I still can't see why is ReadLine pointless because that event handler was never dispatched and never worked for me. Can You pin point me error I am making when working with it?

There is probably a much more elegant and better way to code this than I did but as I said I am still in process of learning, so will try to update code later with better versions.

ComputerControledGreenHouse.zip (794 KB)

arduino_src.ino (1.97 KB)

you cant use serial print like as it,

you need to send serial data in CSV format. where you can easily segragate in VB. let me know if you faced any problem

You have to use the following:

ReadExisting

Dim buf As String
buf = port1.ReadExisting

the computer will not freeze.
the other commands like ReadLine or ReadByte or ReadChar ,waits the Data on the port, for this reason the computer freeze.