
Untuk mulai di Visual Studio pergi ke File -> New Project. Dalam wizard proyek baru pilih Aplikasi Visual C # -> Windows -> Windows Form, Anda harus melihat sesuatu seperti berikut ini

new project wizard
Sekarang buat form seperti form berikut dengan 2 tombol

Arduino LED kontrol
Anda juga perlu menambahkan SerialPort dari kotak peralatan, Anda dapat melihatnya di bawah ini. Klik dua kali untuk menambahkannya
Buka Form1.cs code masukkan source code berikut,
public Form1()
{
InitializeComponent();
//the COM port of my Arduino
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 9600;
}
Pada tools pilih button, double click pada button untuk memasukkan code dari button’s click event. Akan membuka com port, kirim data and lalu closes com port lagi.
private void btnLedOn_Click(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("1");
serialPort1.Close();
}
Same again for the LED off button
private void btnLedOff_Click(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("0");
serialPort1.Close();
}
App Code
Source Code Visual Studio project seperti dibawah ini
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace ArduinoLEDcsharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//the COM port of my Arduino
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 9600;
}
private void btnLedOn_Click(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("1");
serialPort1.Close();
}
private void btnLedOff_Click(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("0");
serialPort1.Close();
}
}
}
Arduino Code
Selanjutnya Arduino IDE and copy source code dibawah ini dan compile lalu upload
#define LEDPin 4
char SerialInput;
void setup()
{
pinMode(LEDPin, OUTPUT);
// serial communication
Serial.begin(9600);
}
void loop()
{
//read from serial port
SerialInput = Serial.read();
//verify incomingOption
switch(SerialInput)
{
case '1':
// Turn ON LED
digitalWrite(LEDPin, HIGH);
break;
case '0':
// Turn OFF LED
digitalWrite(LEDPin, LOW);
break;
}
}
Pak tutorial visual studio sebagai master modbus rtu donk dengan slave arduino
BalasHapusSaya terkendala program di visual studio.y pak