İçeriğe geç
ShiftDelete.Net
Haber ara/ SDN'ye SorGiriş
Haber Akışı SDNTV İnceleme Forum Yapay Zeka Akıllı Telefon Elektrikli Otomobil Oyun ShiftDelete.Net Global Teknoloji Haberleri Mobil Otomobil Donanım Savunma Bilim Sosyal Medya İnternet Giyilebilir Teknoloji Rehber Ürünler Sinema Dizi Akıllı Ev Motosiklet Mikromobilite Kripto Para Sağlık Teknolojileri Son dakika Yazarlar
Mustafa Kemal Atatürk

Cumhuriyetimizin kurucusu Gazi Mustafa Kemal Atatürk'ü vefatının 88. yılında saygı ve özlemle anıyoruz.Gazi Mustafa Kemal Atatürk'ü saygı ve özlemle anıyoruz.Atatürk'ü saygı ve özlemle anıyoruz.

  1. Ana sayfa
  2. SDN Forum
  3. Programlama
  4. Programlama Dilleri
Programlama Dilleri

C# Serialport Veri Okuma

Konu bilgisi

3cevap
9.425görüntüleme
14.08.2013açıldı
14.08.2013son cevap

#1
Merhaba saygı değer abilerim ve arkadaşlar ,

C# ta uygulama yazmaktayım. Program içerisinde Comporttan data okuma var ve benim yazdığım kodlar hatalı, daha doğrusu kantardan gelen datayı okuyorum ama metnin başına ve sonuna arakterler atıyor. Porttan veri okuma konusunda elinde bilgi olan arkadaşlar ya da herhangi başka bir şekilde yardımcı olabilir misiniz ?

İyi Günler,İyi Çalışmalar.
Cevaplar
#2
Bu örneği bir dener misin?

[COLOR=blue]using[/COLOR] System;
  [COLOR=blue]using[/COLOR] System.Collections.Generic;
  [COLOR=blue]using[/COLOR] System.ComponentModel;
  [COLOR=blue]using[/COLOR] System.Data;
  [COLOR=blue]using[/COLOR] System.Drawing;
  [COLOR=blue]using[/COLOR] System.Text;
  [COLOR=blue]using[/COLOR] System.Windows.Forms;
  
  [COLOR=blue]namespace[/COLOR] SimpleSerial
  {
      [COLOR=blue]public partial class[/COLOR] Form1 : Form
      {
          [COLOR=green][I]// Add this variable[/I][/COLOR]

          [COLOR=blue]string[/COLOR] RxString;
          
          [COLOR=blue]public[/COLOR] Form1()
          {
              InitializeComponent();
          }
  
          [COLOR=blue]private void[/COLOR] buttonStart_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              serialPort1.PortName = "COM1";
              serialPort1.BaudRate = 9600;
  
              serialPort1.Open();
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
              {
                  buttonStart.Enabled = [COLOR=blue]false[/COLOR];
                  buttonStop.Enabled = [COLOR=blue]true[/COLOR];
                  textBox1.ReadOnly = [COLOR=blue]false[/COLOR];
              }
          }
  
          [COLOR=blue]private void[/COLOR] buttonStop_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
              {
                  serialPort1.Close();
                  buttonStart.Enabled = [COLOR=blue]true[/COLOR];
                  buttonStop.Enabled = [COLOR=blue]false[/COLOR];
                  textBox1.ReadOnly = [COLOR=blue]true[/COLOR];
              }
  
          }
  
          [COLOR=blue]private[/COLOR] void Form1_FormClosing([COLOR=blue]object[/COLOR] sender, FormClosingEventArgs e)
          {
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen) serialPort1.Close();
          }
  
          [COLOR=blue]private[/COLOR] void textBox1_KeyPress([COLOR=blue]object[/COLOR] sender, KeyPressEventArgs e)
          {
              [COLOR=green][I]// If the port is closed, don't try to send a character.[/I][/COLOR]

              [COLOR=blue]if[/COLOR](!serialPort1.IsOpen) [COLOR=blue]return[/COLOR];
  
              [COLOR=green][I]// If the port is Open, declare a char[] array with one element.[/I][/COLOR]
              [COLOR=blue]char[/COLOR][] buff = [COLOR=blue]new[/COLOR] char[1];
              
              [COLOR=green][I]// Load element 0 with the key character.[/I][/COLOR]

              buff[0] = e.KeyChar;
  
              [COLOR=green][I]// Send the one character buffer.[/I][/COLOR]
              serialPort1.Write(buff, 0, 1);
  
              [COLOR=green][I]// Set the KeyPress event as handled so the character won't
              // display locally. If you want it to display, omit the next line.[/I][/COLOR]
              e.Handled = [COLOR=blue]true[/COLOR];
          }
  
          [COLOR=blue]private void[/COLOR] DisplayText([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              textBox1.AppendText(RxString);
          }
  
          [COLOR=blue]private void[/COLOR] serialPort1_DataReceived
            ([COLOR=blue]object[/COLOR] sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              RxString = serialPort1.ReadExisting();
              [COLOR=blue]this[/COLOR].Invoke([COLOR=blue]new[/COLOR] EventHandler(DisplayText));
          }
      }
  }

hbektas Konuyu açan
#3
eSa dedi: ↑
Bu örneği bir dener misin?

[COLOR=blue]using[/COLOR] System;
  [COLOR=blue]using[/COLOR] System.Collections.Generic;
  [COLOR=blue]using[/COLOR] System.ComponentModel;
  [COLOR=blue]using[/COLOR] System.Data;
  [COLOR=blue]using[/COLOR] System.Drawing;
  [COLOR=blue]using[/COLOR] System.Text;
  [COLOR=blue]using[/COLOR] System.Windows.Forms;
  
  [COLOR=blue]namespace[/COLOR] SimpleSerial
  {
      [COLOR=blue]public partial class[/COLOR] Form1 : Form
      {
          [COLOR=green][I]// Add this variable[/I][/COLOR]

          [COLOR=blue]string[/COLOR] RxString;
          
          [COLOR=blue]public[/COLOR] Form1()
          {
              InitializeComponent();
          }
  
          [COLOR=blue]private void[/COLOR] buttonStart_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              serialPort1.PortName = "COM1";
              serialPort1.BaudRate = 9600;
  
              serialPort1.Open();
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
              {
                  buttonStart.Enabled = [COLOR=blue]false[/COLOR];
                  buttonStop.Enabled = [COLOR=blue]true[/COLOR];
                  textBox1.ReadOnly = [COLOR=blue]false[/COLOR];
              }
          }
  
          [COLOR=blue]private void[/COLOR] buttonStop_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
              {
                  serialPort1.Close();
                  buttonStart.Enabled = [COLOR=blue]true[/COLOR];
                  buttonStop.Enabled = [COLOR=blue]false[/COLOR];
                  textBox1.ReadOnly = [COLOR=blue]true[/COLOR];
              }
  
          }
  
          [COLOR=blue]private[/COLOR] void Form1_FormClosing([COLOR=blue]object[/COLOR] sender, FormClosingEventArgs e)
          {
              [COLOR=blue]if[/COLOR] (serialPort1.IsOpen) serialPort1.Close();
          }
  
          [COLOR=blue]private[/COLOR] void textBox1_KeyPress([COLOR=blue]object[/COLOR] sender, KeyPressEventArgs e)
          {
              [COLOR=green][I]// If the port is closed, don't try to send a character.[/I][/COLOR]

              [COLOR=blue]if[/COLOR](!serialPort1.IsOpen) [COLOR=blue]return[/COLOR];
  
              [COLOR=green][I]// If the port is Open, declare a char[] array with one element.[/I][/COLOR]
              [COLOR=blue]char[/COLOR][] buff = [COLOR=blue]new[/COLOR] char[1];
              
              [COLOR=green][I]// Load element 0 with the key character.[/I][/COLOR]

              buff[0] = e.KeyChar;
  
              [COLOR=green][I]// Send the one character buffer.[/I][/COLOR]
              serialPort1.Write(buff, 0, 1);
  
              [COLOR=green][I]// Set the KeyPress event as handled so the character won't
              // display locally. If you want it to display, omit the next line.[/I][/COLOR]
              e.Handled = [COLOR=blue]true[/COLOR];
          }
  
          [COLOR=blue]private void[/COLOR] DisplayText([COLOR=blue]object[/COLOR] sender, EventArgs e)
          {
              textBox1.AppendText(RxString);
          }
  
          [COLOR=blue]private void[/COLOR] serialPort1_DataReceived
            ([COLOR=blue]object[/COLOR] sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              RxString = serialPort1.ReadExisting();
              [COLOR=blue]this[/COLOR].Invoke([COLOR=blue]new[/COLOR] EventHandler(DisplayText));
          }
      }
  }

Teşekkür ederim Hemen deniyorum.
#4
serialPort1.Read şeklinde okuma yaparsan gelen verileri byte dizisi şeklinde okursun.
serialPort1.ReadExisting şeklinde okuma yaparsan gelen verileri string şeklinde okursun.
serialPort1.ReadLine şeklinde okuma yaparsan gelen verileri satırlar halinde okursun.

Genelde bu biçimlerde kullanılmakta gelen veri hangi formatta ise bunlardan uygun olanını seçersin.

Cevabını yaz

Cevap yazmak için giriş yap ya da üye ol. Eski forum kullanıcı adın ve parolanla girebilirsin.

Reklam