alavi24
24-04-2018, 16:12
سلام خدمت دوستان من به وسیله آردوینو ( Arduino Uno) و ویژال استادیو (#C) یک فرم طراحی کردم با دوتا دکمه که یکی از آن ها برای خاموش کردن LED و دیگری برای روشن کردن LED متصل شده به برد آردوینو استفاده میشه.
این برنامه به صورت کامل کار میکنه و هیچ مشکلی ندارد اما میخوام خاموش و روشن کردن چندتا LED رو انجام بدم که توی این قسمت کار گیر دارم که چه جوری هر دستور را باید از طریق C# برای آردینو ارسال کنم که متوجه بشه که الان کدام LED باید روشن یا خاموش بشه؟؟؟
لطفا اساتید راهنمایی کنند
کد برنامه در #C برای کنترل یک LED
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
port = new SerialPort("COM3", 9600);//Set your board COM
port.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
PortWrite("1");
}
private void button2_Click(object sender, EventArgs e)
{
PortWrite("0");
}
private void PortWrite(string message)
{
port.Write(message);
}
}
}
و کد مربوط به آردوینو برای کنترل یک LED
const int LedPin = 13;
int ledState = 0;
void setup()
{
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char receiveVal;
if(Serial.available() > 0)
{
receiveVal = Serial.read();
if(receiveVal == '1')
ledState = 1;
else
ledState = 0;
}
digitalWrite(LedPin, ledState);
delay(50);
}
این برنامه به صورت کامل کار میکنه و هیچ مشکلی ندارد اما میخوام خاموش و روشن کردن چندتا LED رو انجام بدم که توی این قسمت کار گیر دارم که چه جوری هر دستور را باید از طریق C# برای آردینو ارسال کنم که متوجه بشه که الان کدام LED باید روشن یا خاموش بشه؟؟؟
لطفا اساتید راهنمایی کنند
کد برنامه در #C برای کنترل یک LED
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort port;
public Form1()
{
InitializeComponent();
port = new SerialPort("COM3", 9600);//Set your board COM
port.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
PortWrite("1");
}
private void button2_Click(object sender, EventArgs e)
{
PortWrite("0");
}
private void PortWrite(string message)
{
port.Write(message);
}
}
}
و کد مربوط به آردوینو برای کنترل یک LED
const int LedPin = 13;
int ledState = 0;
void setup()
{
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char receiveVal;
if(Serial.available() > 0)
{
receiveVal = Serial.read();
if(receiveVal == '1')
ledState = 1;
else
ledState = 0;
}
digitalWrite(LedPin, ledState);
delay(50);
}