2019年12月22日 星期日

VB2010 Conrol LED ON/OFF

VB2010 Conrol LED ON/OFF 

下載  VB2010 與 Arduino 程式
http://www.mediafire.com/file/8x0q70seete4025/LEDONOFF_VB_COM_PORT.rar/file





============================Arduino============================
 //#define LED     D0        // Led in NodeMCU at pin GPIO16 (D0).
#define LED       2       // Led in ESP32 at pin 2.lolin nodeMCU is 2

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

void loop() {
  int val;
  if(Serial.available()){
    delay(100);
    while(Serial.available() >0){
      val=Serial.read();
      if(val=='1'){digitalWrite(LED,LOW);}
      else if (val=='0') {digitalWrite (LED,HIGH);
      }
    }
  }
}

============================VB2010============================
Imports System.IO
Imports System.IO.Ports

Public Class Form1
    Dim WithEvents sp As New SerialPort

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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        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
    End Sub


    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            sp.BaudRate = cmbBaud.SelectedItem.ToString
            sp.PortName = cmbPort.SelectedItem.ToString
            sp.Open()
            If sp.IsOpen Then
                btnConnect.Visible = False
                cmbPort.Enabled = False
                cmbBaud.Enabled = False
                btnDisconnect.Visible = True
            End If
        Catch
            sp.Close()
        End Try
    End Sub


    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            sp.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            cmbPort.Enabled = True
            cmbBaud.Enabled = True
            Exit Sub
        Catch
            MessageBox.Show("Some kind of problem.")
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        If sp.IsOpen() Then
            MessageBox.Show("Disconnect before closing")
            e.Cancel = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Visible = True
        PictureBox2.Visible = False

        sp.WriteLine("1")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PictureBox2.Visible = True
        PictureBox1.Visible = False
        sp.WriteLine("0")
    End Sub
End Class

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...