下載 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);
}
}
}
}
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
沒有留言:
張貼留言