Arduino with Visual Studio Problem

Hi friends !

I want to make when i press button who connected to arduino and when i press him to show it on visual studio :slight_smile: i write an code but the OvalShape1 is show only one time :X

Visual Studio code :

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

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com4" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

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

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim dataRecive As String = SerialPort1.ReadLine()

        If (dataRecive = 1) Then
            OvalShape1.Visible = True
        ElseIf (dataRecive = 0) Then
            OvalShape1.Visible = False
        End If
    End Sub

    
End Class

Arduino Code :

int button = 7;
int buttonState = 0; 
int lastButtonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT);
}

void loop()
{
  buttonState = digitalRead(button);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      Serial.println("1");
    }
    else {
      Serial.println("0");
    }
  }
  lastButtonState = buttonState;
}

Please any help

        If (dataRecive = 1) Then
            OvalShape1.Visible = True
        ElseIf (dataRecive = 0) Then
            OvalShape1.Visible = False
        End If

I'd be real surprised if the string that the Arduino sent was 0 or 1. "0" or "1" seem a lot more likely.

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

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com4" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

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

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim dataRecive As Integer = Val(ChrW(SerialPort1.ReadChar()))
        If (dataRecive = 0) Then
            MessageBox.Show("The Button Is ON")
        ElseIf (dataRecive = 1) Then
            MessageBox.Show("The Button Is OFF")
            SerialPort1.Close()
        End If
    End Sub

End Class

I edit the code and now it work... But work with messagebox :X i want when this but with an shape or pictue :slight_smile: Please any help with this. I realy happy if in this forum any help...

Why are you closing the serial port as soon as data arrives?

Good Afternoon,
my advise is initially doesn't put a condition inside arduino sketch try only to print data over the serial and retrive from visual studio side ...

in this way you can check the communication between both ... after that you can add condition on that,

thanks
gnux

I write some code and works but work with messabebox :X i want with an shape or image.

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

Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)       Handles MyBase.Load
    SerialPort1.Close()
    SerialPort1.PortName = "com4" 'change com port to match your Arduino port
    SerialPort1.BaudRate = 9600
    SerialPort1.DataBits = 8
    SerialPort1.Parity = Parity.None
    SerialPort1.StopBits = StopBits.One
    SerialPort1.Handshake = Handshake.None
    SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub

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

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim dataRecive As String = SerialPort1.ReadLine()

    If (dataRecive = 1) Then
        OvalShape1.Visible = True
    ElseIf (dataRecive = 0) Then
        OvalShape1.Visible = False
    End If
End Sub
End Class

Any help for this problem ? The problem is, when i press the button from arduino, in visual studio show the messagebox but when i write the code with an shape example with OvalShape not work...

Did you read Paul's replies?