2009-08-07 13 views
24

से कैसे पढ़ और लिखना है, मैं बस सीखना शुरू करता हूं कि सी # जीयूआई के माध्यम से अपने हार्डवेयर से डेटा कैसे भेजना और प्राप्त करना है।सीरियल पोर्ट

किसी को भी एक विस्तार करने के लिए सीरियल पोर्ट से डेटा को पढ़ने, लिखने सकते हैं?

+0

[सी # में सीरियल पोर्ट के प्रबंध] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/7275084/managing-serial-ports-in-c-sharp) –

+0

दूसरी तरफ: जुड़ा हुआ पोस्ट इस का एक डुप्लिकेट है। कृपया इस प्रश्न का प्रयोग कैनोनिकल डुप्लिकेट के रूप में करें। – Lundin

उत्तर

57

SerialPort (RS-232 Serial COM Port) in C# .NET
यह लेख .NET में SerialPort वर्ग का उपयोग करने के पढ़ सकते हैं और डेटा लिखने के लिए, यह निर्धारित क्या सीरियल पोर्ट आपकी मशीन पर उपलब्ध हैं, और कैसे फ़ाइलें भेजने के लिए बताते हैं। यह बंदरगाह पर पिन असाइनमेंट भी शामिल है।

उदाहरण कोड:

using System; 
using System.IO.Ports; 
using System.Windows.Forms; 

namespace SerialPortExample 
{ 
    class SerialPortProgram 
    { 
    // Create the serial port with basic settings 
    private SerialPort port = new SerialPort("COM1", 
     9600, Parity.None, 8, StopBits.One); 

    [STAThread] 
    static void Main(string[] args) 
    { 
     // Instatiate this class 
     new SerialPortProgram(); 
    } 

    private SerialPortProgram() 
    { 
     Console.WriteLine("Incoming Data:"); 

     // Attach a method to be called when there 
     // is data waiting in the port's buffer 
     port.DataReceived += new 
     SerialDataReceivedEventHandler(port_DataReceived); 

     // Begin communications 
     port.Open(); 

     // Enter an application loop to keep this thread alive 
     Application.Run(); 
    } 

    private void port_DataReceived(object sender, 
     SerialDataReceivedEventArgs e) 
    { 
     // Show all the incoming data in the port's buffer 
     Console.WriteLine(port.ReadExisting()); 
    } 
    } 
} 
संबंधित मुद्दे