2009-08-07 32 views
24

Donanımımdan C# GUI aracılığıyla veri gönderip nasıl alacağımı öğrenmeye başladım.Seri Bağlantı Noktası Nasıl Okunur ve Yazılır

kimse seri porttan veri okumak nasıl bir ayrıntıyı yazınız miyim?

+0

[C# seri port yönetme] olası çift (http://stackoverflow.com/questions/7275084/managing-serial-ports-in-c-sharp) –

+0

başka bir şekilde: bağlantılı yazı, bunun bir kopyasıdır. Lütfen bu soruyu standart kopya olarak kullanın. – Lundin

cevap

57

SerialPort (RS-232 Serial COM Port) in C# .NET
Bu makale okuma ve veri yazma, seri portlar makinenizde kullanılabilir ve nasıl dosya göndermek belirlemek için .NET SerialPort sınıfını nasıl kullanılacağı açıklanır. Hatta portun kendisinde bulunan pin atamalarını da kapsar.

Örnek Kodu:

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()); 
    } 
    } 
} 
İlgili konular