Résolu visual basic ajouter un voyant

TitoGame

Membre
Inscription
1 Janvier 2013
Messages
723
Réactions
85
Points
13 826
Salut,

Donc voilà je suis en train de coder une petite interface avec Windows form, qui me permettra de communiquer avec mon arduino.
J'ai créer 4 boutons 1 pour me connecter sur le port série un autre pour me déconnecter et 1 ON / OFF,

Je voudrais maintenant rajouter dans mon interface un voyant vert si on est bien connecté sur le port et qui passe au rouge si il est déconnecté,

J'ai essayer un tas de chose mais rien à fonctionner, je patoge un peu un coup de main serait bien venu si possible.

Merci
 

TitoGame

Membre
Inscription
1 Janvier 2013
Messages
723
Réactions
85
Points
13 826
J'ai essayer tout ce qui me semblé logique puis j'ai fait quelque recherche sur Google mes tout ce que je trouve ne correspond pas à mes attentes,

Mais déjà comment ajouter un voyant ? Après lui dire quoi faire et quand c'est pas le problème, mes j'ai lu tellement de chose différente que je me suis perdu,

Je peux mettre le code si tu veux jeter un oeil ?
 

Moncler

Premium
Inscription
23 Avril 2013
Messages
4 759
Réactions
1 246
Points
10 116
J'ai essayer tout ce qui me semblé logique puis j'ai fait quelque recherche sur Google mes tout ce que je trouve ne correspond pas à mes attentes,

Mais déjà comment ajouter un voyant ? Après lui dire quoi faire et quand c'est pas le problème, mes j'ai lu tellement de chose différente que je me suis perdu,

Je peux mettre le code si tu veux jeter un oeil ?
Avec la condition if tu peut faire ça, je connait pas trop le arduino et pour faire un voyant tu me faire un test d'un label qui change de texte et de Color
Exemple :
Si il est connecté avec succès avec la fonction if tu change le texte du label et color, si c'est Non connecté tu le mes en Rouge et Non connecté.
Puis quand ça serait bon tu le design un peut
 

ॐ Devkush ॐ

Premium
Inscription
30 Octobre 2015
Messages
588
Réactions
219
Points
2 176
C#

PHP:
if (this.Check_on.Checked)
            {
                label01.Text = "ON";
                label01.ForeColor = System.Drawing.Color.Green;
            }
            if (!this.Check_on.Checked)
            {
                label01.Text = "OFF";
                label01.ForeColor = System.Drawing.Color.Red;
            }

Et pour le VB

PHP:
If Me.Check_on.Checked Then
    label01.Text = "ON"
    label01.ForeColor = System.Drawing.Color.Green
End If
If Not Me.Check_on.Checked Then
    label01.Text = "OFF"
    label01.ForeColor = System.Drawing.Color.Red
End If
 

TitoGame

Membre
Inscription
1 Janvier 2013
Messages
723
Réactions
85
Points
13 826
bon du coup ça pique un peu les yeux mais en mettant les main dans le "caca" tout fonctionne exactement comme je voulais, Merci pour votre aide en tout cas :)
voici le code:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Text1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Text1.BackColor = Color.Red
        Text1.ForeColor = Color.White
        Text1.TextAlign = HorizontalAlignment.Center
        Text1.Text = "Port Fermé"
        SerialPort1.Close()
        SerialPort2.Close()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
        Text1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        With SerialPort1
            If .PortName = "COM9" Then
                Text1.TextAlign = HorizontalAlignment.Center
                Text1.ForeColor = Color.White
                Text1.Text = "Port Ouvert"
                Text1.BackColor = Color.Green
            Else
                Text1.BackColor = Color.Red
            End If



        End With
        With SerialPort1
            .PortName = "COM9"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = IO.Ports.Parity.None
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
        End With

        Try
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

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

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

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Disconnect.Click
        Text1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        With SerialPort2
            If .PortName = "COM9" Then
                Text1.TextAlign = HorizontalAlignment.Center
                Text1.ForeColor = Color.White
                Text1.Text = "Port Fermé"
                Text1.BackColor = Color.Red
                SerialPort2.Close()

            End If
        End With

        With SerialPort2
            .PortName = "COM9"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = IO.Ports.Parity.None
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
        End With

        Try
            SerialPort1.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub


    Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged

    End Sub

End Class

Pour les curieux voici le code coté arduino, ça suffit pour allumer est éteindre la LED depuis l'interface vb:
Code:
int Led = 2;


void setup() {
  Serial.begin(9600);
  pinMode(Led,OUTPUT);

}

void loop() {

  if (Serial.available())
{
  int led = Serial.read() - 48;
  Serial.println(led);
  if (led == 1)
    digitalWrite(Led,HIGH);
 
  if (led == 0)
    digitalWrite(Led,LOW);
}

 

}
 
Haut