2019年12月19日 星期四

利用VB2010 寫 LED 控制 Arduino WeMos D1 (ESP8266)上 的 LED ON/OFF

利用VB2010 寫  LED 控制 Arduino  WeMos D1 (ESP8266)上 的 LED ON/OFF

1) VB2010 程式
http://www.mediafire.com/file/4mydykxlr418216/RFID_Reader_0.rar/file

2) Arduino 程式
http://www.mediafire.com/file/7cslm8uz8w8c5tn/LED_control.ino/file






==============VB2010========================
Public Class Form1
    Dim WithEvents myComPort As New System.IO.Ports.SerialPort
    Dim data As String
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ComboBox1.Enabled = True
        Button2.Enabled = False
        Button1.Enabled = True
        RadioButton1.Enabled = False
        RadioButton2.Checked = True
        myComPort.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ComboBox1.SelectedItem Is Nothing Then
            If Not myComPort.IsOpen Then
                Try
                    myComPort.BaudRate = 9600
                    myComPort.PortName = ComboBox1.SelectedItem
                    myComPort.Parity = IO.Ports.Parity.None
                    myComPort.DataBits = 8
                    myComPort.StopBits = IO.Ports.StopBits.One
                    myComPort.Handshake = IO.Ports.Handshake.None
                    myComPort.ReadTimeout = 3000
                    myComPort.ReceivedBytesThreshold = 1
                    myComPort.DtrEnable = True

                    myComPort.Open()

                    ComboBox1.Enabled = False
                    Button1.Enabled = False
                    Button2.Enabled = True
                    RadioButton1.Enabled = True
                    RadioButton1.Checked = True

                Catch ex As Exception
                    MsgBox("Error Opening COM Port", MsgBoxStyle.Critical)
                End Try
            End If
        Else
            MsgBox("Select a valid COM Port", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each portName As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(portName)
        Next
        If Not myComPort.IsOpen Then
            Button2.Enabled = False
            ComboBox1.Text = ComboBox1.Items(0)
            RadioButton1.Enabled = False
            RadioButton2.Checked = True
        Else
            Button1.Enabled = False
            ComboBox1.Text = myComPort.PortName
            RadioButton2.Checked = True
        End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        myComPort.DtrEnable = True
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        myComPort.DtrEnable = False
    End Sub

    Public Delegate Sub myDelegate()
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        PictureBox1.Visible = False
        PictureBox2.Visible = True
        myComPort.Write("1")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        PictureBox1.Visible = True
        PictureBox2.Visible = False
        myComPort.Write("0")
    End Sub

End Class

==============Arduino ESP8266========================

 //#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);
      }
    }
  }
}

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...