पीसी से एक आर्डिनो को कमांड भेजने के कई तरीके हैं। संदीप बंसिल एक धारावाहिक बंदरगाह को जोड़ने और पढ़ने का एक अच्छा उदाहरण प्रदान करता है।
नीचे एक विंडोज़ फॉर्म पर चेकबॉक्स की स्थिति के आधार पर एक सीरियल पोर्ट को लिखने का एक उदाहरण उदाहरण है, जिसमें arduino पर पीसी से अनुरोध को संसाधित करने का तरीका है।
यह एक वर्बोज़ उदाहरण है, क्लीनर समाधान हैं लेकिन यह स्पष्ट है।
उदाहरण में arduino पीसी से 'ए' या 'बी' के लिए इंतजार कर रहा है। जब चेकबॉक्स चेक किया जाता है तो पीसी 'ए' भेजता है और चेकबॉक्स अनचेक होने पर 'बी' भेजता है। उदाहरण Arduino पर डिजिटल पिन 4 मानता है।
Arduino कोड
#define DIGI_PIN_SOMETHING 4
unit8_t commandIn;
void setup()
{
//create a serial connection at 57500 baud
Serial.begin(57600);
}
void loop()
{
//if we have some incomming serial data then..
if (Serial.available() > 0)
{
//read 1 byte from the data sent by the pc
commandIn = serial.read();
//test if the pc sent an 'a' or 'b'
switch (commandIn)
{
case 'a':
{
//we got an 'a' from the pc so turn on the digital pin
digitalWrite(DIGI_PIN_SOMETHING,HIGH);
break;
}
case 'b':
{
//we got an 'b' from the pc so turn off the digital pin
digitalWrite(DIGI_PIN_SOMETHING,LOW);
break;
}
}
}
}
विंडोज सी #
यह कोड आपके प्रपत्र .cs फ़ाइल में निवास करेंगे। उदाहरण मानता है कि आपने OnOpenForm, OnCloseForm और ऑनक्लिक ईवेंट के लिए चेकबॉक्स में फॉर्म इवेंट संलग्न किए हैं। घटनाओं में से प्रत्येक से आप नीचे दिए गए .... संबंधित तरीकों कॉल कर सकते हैं
using System;
using System.IO.Ports;
class fooForm and normal stuff
{
SerialPort port;
private myFormClose()
{
if (port != null)
port.close();
}
private myFormOpen()
{
port = new SerialPort("COM4", 57600);
try
{
//un-comment this line to cause the arduino to re-boot when the serial connects
//port.DtrEnabled = true;
port.Open();
}
catch (Exception ex)
{
//alert the user that we could not connect to the serial port
}
}
private void myCheckboxClicked()
{
if (myCheckbox.checked)
{
port.Write("a");
}
else
{
port.Write("b");
}
}
}
युक्ति:
आप arduino से एक संदेश को पढ़ने के लिए तो के अंतराल के साथ अपने फार्म के लिए टाइमर जोड़ने चाहते हैं 50
या 100
मिलीसेकंड।
टाइमर का OnTick
घटना में आप निम्नलिखित कोड का उपयोग कर डेटा के लिए जाँच करनी चाहिए:
//this test is used to see if the arduino has sent any data
if (port.BytesToRead > 0)
//On the arduino you can send data like this
Serial.println("Hellow World")
//Then in C# you can use
String myVar = port.ReadLine();
readLine()
का परिणाम होगा कि myVar
Hello World
शामिल हैं।
स्रोत
2012-08-27 22:00:31
, मेरी खोज कभी नहीं मुझे उस पेज मिला है। धन्यवाद। – ikathegreat