Arduino和Visual Studio接口

时间:2014-03-13 13:39:13

标签: usb arduino

我喜欢的朋友,当我按下连接到arduino的按钮时,当我按下他在视觉工作室笑脸上显示我写代码但是OvalShape1只显示一次:X

Visual Studio代码:

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代码:

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;
}

0 个答案:

没有答案