Ynt: C# İle USB kullanımı

Başlatan Mucit23, 11 Ağustos 2012, 15:34:12

Mucit23

Arkadaşlar tekrar merhaba.

Başka bir uygulamaya bakarak C# da USB cihazı haberleştirmeye çalışıyorum. Fakat anlamadığım bazı problemler var. Sabahtan beri uğraşıyorum gerçekten çözemedim. Yardımınıza ihtiyacım var.

İlk Önce yazdığım kodları vereyim.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsbLibrary;

namespace USB_Test
{
    public partial class Form1 : Form
    {
        byte[] transmit_data = new byte[65];
        byte sayı = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load_1(object sender, EventArgs e) // Form Yüklenirken yapılacaklar.
        {
            toolStripStatusLabel1.ForeColor = Color.Red;
            this.ReadyBox.Items.Add("USB Cihazı Bağlayın");
            toolStripStatusLabel1.Text = "USB Cihaz Bağlı Değil!..";
            this.usbHidPort1.ProductId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            this.usbHidPort1.VendorId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            usbHidPort1.CheckDevicePresent();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            usbHidPort1.RegisterHandle(Handle);   
        }

        protected override void WndProc(ref Message m)
        {
            usbHidPort1.ParseMessages(ref m);
            base.WndProc(ref m);
        }


        private void usbHidPort1_OnspecifiedDeviceArrived(object sender, EventArgs e)
        {
            toolStripStatusLabel1.ForeColor = Color.Green;
            toolStripStatusLabel1.Text = "USB Cihaz Hazır!"; //Cihaz kullanıma Hazır.
            this.ReadyBox.Items.Add("USB Cihaz Kullanıma Hazır");
        }

        private void usbHidPort1_OnSpecifieldDeviceRemoved(object sender, EventArgs e)
        {
            toolStripStatusLabel1.ForeColor = Color.Red;
            toolStripStatusLabel1.Text = "USB Cihaz Çıkartıldı";// Cihaz PC den ayrıldı.
            this.ReadyBox.Items.Add("USB Cihaz Çıkartıldı");
        }

        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("Bir Cihaz Bağlandı");
            MessageBox.Show("Bir Cihaz Bağlandı");
        }

        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("USB Cihaz Çıkartıldı");
        }


        private void Connect_Click_1(object sender, EventArgs e)
        {
        }

        private void Data_Send_Click(object sender, EventArgs e)
        {
        }

    private void usbHidPort1_OnDataRecieved(object sender, DataRecievedEventArgs args)
    {
        if (InvokeRequired)
        {

        }
    }

    }
}


Burada Bazı events ler var.

Örneğin;

        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("Bir Cihaz Bağlandı");
        }

        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("USB Cihaz Çıkartıldı");
        }


Bu eventslerin normalde bilgisayara herhangi bir USB cihaz bağlandığında çalışması gerekir. Mesela Örnek aldığım aşağıdaki programda Aynen öyle oluyor.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsbLibrary;

namespace UsbHid
{
    public partial class Form1 : Form
    {



        byte[] buffergon = new byte[65];
        byte[] buffergel = new byte[65];
        Boolean durum = false;
        Boolean start = false;
        Boolean cihaz_durumu = false;



        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            usb.RegisterHandle(Handle);
        }

        protected override void WndProc(ref Message m)
        {
            usb.ParseMessages(ref m);
            base.WndProc(ref m);	
        }

        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
           
            this.debugTextBox.AppendText("Cihaz geldi");
            this.listBox1.Items.Add("Cihaz geldi");          
            //MessageBox.Show("cihaz geldi");
        }
        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            //MessageBox.Show("cihaz gitti");
            this.listBox1.Items.Add("go go go"); 
            this.debugTextBox.AppendText("Cihaz go go");
        }

        private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            this.debugTextBox.AppendText("Cihaz READY!!!");
            this.listBox1.Items.Add("Cihaz READY!!!!"); 
            
            textdurum.Text = "Cihaz Hazır!";
            textdurum.BackColor = Color.Green;
            cihaz_durumu = true;
             //durum = true;

        }

        private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
        {
           this.debugTextBox.AppendText("Cihaz GOİNG!!!");
           this.listBox1.Items.Add("Cihaz GOİNG!");

           

           textdurum.Text = "Cihaz Ayrıldı!";
           textdurum.BackColor = Color.Red;
           cihaz_durumu = false;
            //durum = false;

        }

        private void cihazı_ac_Click(object sender, EventArgs e)
        {
            if (cihaz_durumu)
            {
                this.debugTextBox.AppendText("Data aktarımı sürüyor");
                this.listBox1.Items.Add("Data aktarımı sürüyor"); 
                textdurum.Text = "Cihaz ile bağlantı açıldı!";
                textdurum.BackColor = Color.Green;
                durum = true;
                start = true;
              DurumKontrol();
            }
            else if(!cihaz_durumu)
            {
                debugTextBox.Text = "Cihaz Bulunamadı''";
                this.listBox1.Items.Add("device not found"); 
                textdurum.Text = "Cihaz Yok!";
                textdurum.BackColor = Color.Red;
                durum = false;
                start = false;
            }
      
        }

        private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
        {
            if (InvokeRequired)
            {
                try
                {
                    Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            //
            else
            {
                byte[] buffergel = new byte[65];
                double sicaklik = 0;
                buffergel = args.data;
                sicaklik = (buffergel[1] + buffergel[2] * 256) * 0.488;
                textBox1.Text = sicaklik.ToString();
                this.listBox1.Items.Add((( buffergel[1] + " " + buffergel[2])));
                this.listBox1.Items.Add(sicaklik).ToString();

                this.listBox1.Items.Add(buffergel[3] +"    "+ buffergel[4]);
               // if(true)
                 //   for (int i = 0; i < 65; i++) this.listBox1.Items.Add(buffergel[i] ).ToString();
                
                
                //goster();
                //i++;
                //this.Text = i.ToString();  
            }
        }
        private void usb_OnDataSend(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //CheckForIllegalCrossThreadCalls = false;
            this.usb.ProductId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            this.usb.VendorId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            usb.CheckDevicePresent();
            //goster();

        }
        public void DurumKontrol()
        {

            byte[] buffergon = new byte[65];

            if (durum & cihaz_durumu & start)
            {
                buffergon[1] = (byte)'Q';
                buffergon[3] = (byte)'a';
                //buffergon[1] = (byte)0x01;        // Convert.ToByte('a');

                usb.SpecifiedDevice.SendData(buffergon);
            }
            else if (!durum & cihaz_durumu)
            {
                //buffergon[20] = Convert.ToByte('a');
                //buffergon[0] = (byte)'a';
                buffergon[1] = 0xFF;
                // buffergon[2] = (byte)'a';
                usb.SpecifiedDevice.SendData(buffergon);
            }
        }
        //this.listBox1.Items.Add("Data aktarıldı");
        private void cihazı_kapat_Click(object sender, EventArgs e)
        {
            if (cihaz_durumu)
            {
                textdurum.Text = "Veri Akışı Kesildi!";
                textdurum.BackColor = Color.Red;
                durum = false;
                DurumKontrol();
            }
            else
            {
                textdurum.Text = "Cihaz Yok!*";
                textdurum.BackColor = Color.Red;
                durum = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] my_buffer = new byte[65];

            my_buffer[2] = (byte)0x01;
           // my_buffer[4] = (byte)'u';
            usb.SpecifiedDevice.SendData(my_buffer);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] my_buffer = new byte[65];

            my_buffer[2] = (byte)0xFF;
            // my_buffer[4] = (byte)'u';
            usb.SpecifiedDevice.SendData(my_buffer);

        }
         
    }
}


Buradaki uygulamayı çalıştırdığımda en basitinden mause çıkarıp taksam bile verdiğim eventler çalışıp mesaj veriyor. Bende ise USB ile ilgili hiçbir tepki alamıyorum. Daha önce hiç C# ile USB kullanmadım. Sorun nerede olabilir. Örnek aldığım kod ile benim yazdıklarımı karşılaştırdım. Sadece veri alma, veri gönderme gibi kısımları yazmışım.
Bilgisi olan arkadaşlar yardımcı olursa sevinirim.

Mucit23


t2

#2
Fare klavye gibi USB aletlerin programla okunması ile, kendi yaptığınız  bir usb cihazın programla okunması farklıdır. Kullandığınız cihaz nedir? 

Öncelikle hazır çalışan örnek proje denemelisiniz. Projeyi beğenmediyseniz uygun göreceğiniz şekilde adım adım değiştşirerek çalışmaya devam etmesini sağlayabilirsiniz.

Mucit23

Hocam şöyle söyliyeyim.

Normalde Aşağıdaki kodlar Bilgisayara herhangi bir USB ile çalışan bir cihaz taktığımda çalışıyor.

        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("Bir Cihaz Bağlandı");
        }

        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            this.ReadyBox.Items.Add("USB Cihaz Çıkartıldı");
        }

Mause, Programlayıcı farketmiyor.

Bu events ler ise Benim cihazım bilgisayara bağlandığında çalışıyor

        private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            this.debugTextBox.AppendText("Cihaz READY!!!");
            this.listBox1.Items.Add("Cihaz READY!!!!"); 
            
            textdurum.Text = "Cihaz Hazır!";
            textdurum.BackColor = Color.Green;
            cihaz_durumu = true;
             //durum = true;

        }

        private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
        {
           this.debugTextBox.AppendText("Cihaz GOİNG!!!");
           this.listBox1.Items.Add("Cihaz GOİNG!");

           

           textdurum.Text = "Cihaz Ayrıldı!";
           textdurum.BackColor = Color.Red;
           cihaz_durumu = false;
            //durum = false;

        }


Cihaz Dediğim ise 18F4550,  Pic Tarafında Problem yok.

Fazla kod kalabalığı yapıyorum ama kodlarında kaynağını bilmiyorum PC de buldum, Bilgisayar tarafında birtek bu program çalışıyor.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsbLibrary;

namespace UsbHid
{
    public partial class Form1 : Form
    {



        byte[] buffergon = new byte[65];
        byte[] buffergel = new byte[65];
        Boolean durum = false;
        Boolean start = false;
        Boolean cihaz_durumu = false;



        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            usb.RegisterHandle(Handle);
        }

        protected override void WndProc(ref Message m)
        {
            usb.ParseMessages(ref m);
            base.WndProc(ref m);	
        }

        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
           
            this.debugTextBox.AppendText("Cihaz geldi");
            this.listBox1.Items.Add("Cihaz geldi");          
            //MessageBox.Show("cihaz geldi");
        }
        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            //MessageBox.Show("cihaz gitti");
            this.listBox1.Items.Add("go go go"); 
            this.debugTextBox.AppendText("Cihaz go go");
        }

        private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            this.debugTextBox.AppendText("Cihaz READY!!!");
            this.listBox1.Items.Add("Cihaz READY!!!!"); 
            
            textdurum.Text = "Cihaz Hazır!";
            textdurum.BackColor = Color.Green;
            cihaz_durumu = true;
             //durum = true;

        }

        private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
        {
           this.debugTextBox.AppendText("Cihaz GOİNG!!!");
           this.listBox1.Items.Add("Cihaz GOİNG!");

           

           textdurum.Text = "Cihaz Ayrıldı!";
           textdurum.BackColor = Color.Red;
           cihaz_durumu = false;
            //durum = false;

        }

        private void cihazı_ac_Click(object sender, EventArgs e)
        {
            if (cihaz_durumu)
            {
                this.debugTextBox.AppendText("Data aktarımı sürüyor");
                this.listBox1.Items.Add("Data aktarımı sürüyor"); 
                textdurum.Text = "Cihaz ile bağlantı açıldı!";
                textdurum.BackColor = Color.Green;
                durum = true;
                start = true;
              DurumKontrol();
            }
            else if(!cihaz_durumu)
            {
                debugTextBox.Text = "Cihaz Bulunamadı''";
                this.listBox1.Items.Add("device not found"); 
                textdurum.Text = "Cihaz Yok!";
                textdurum.BackColor = Color.Red;
                durum = false;
                start = false;
            }
      
        }

        private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
        {
            if (InvokeRequired)
            {
                try
                {
                    Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            //
            else
            {
                byte[] buffergel = new byte[65];
                double sicaklik = 0;
                buffergel = args.data;
                sicaklik = (buffergel[1] + buffergel[2] * 256) * 0.488;
                textBox1.Text = sicaklik.ToString();
                this.listBox1.Items.Add((( buffergel[1] + " " + buffergel[2])));
                this.listBox1.Items.Add(sicaklik).ToString();

                this.listBox1.Items.Add(buffergel[3] +"    "+ buffergel[4]);
               // if(true)
                 //   for (int i = 0; i < 65; i++) this.listBox1.Items.Add(buffergel[i] ).ToString();
                
                
                //goster();
                //i++;
                //this.Text = i.ToString();  
            }
        }
        private void usb_OnDataSend(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //CheckForIllegalCrossThreadCalls = false;
            this.usb.ProductId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            this.usb.VendorId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber);
            usb.CheckDevicePresent();
            //goster();

        }
        public void DurumKontrol()
        {

            byte[] buffergon = new byte[65];

            if (durum & cihaz_durumu & start)
            {
                buffergon[1] = (byte)'Q';
                buffergon[3] = (byte)'a';
                //buffergon[1] = (byte)0x01;        // Convert.ToByte('a');

                usb.SpecifiedDevice.SendData(buffergon);
            }
            else if (!durum & cihaz_durumu)
            {
                //buffergon[20] = Convert.ToByte('a');
                //buffergon[0] = (byte)'a';
                buffergon[1] = 0xFF;
                // buffergon[2] = (byte)'a';
                usb.SpecifiedDevice.SendData(buffergon);
            }
        }
        //this.listBox1.Items.Add("Data aktarıldı");
        private void cihazı_kapat_Click(object sender, EventArgs e)
        {
            if (cihaz_durumu)
            {
                textdurum.Text = "Veri Akışı Kesildi!";
                textdurum.BackColor = Color.Red;
                durum = false;
                DurumKontrol();
            }
            else
            {
                textdurum.Text = "Cihaz Yok!*";
                textdurum.BackColor = Color.Red;
                durum = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] my_buffer = new byte[65];

            my_buffer[2] = (byte)0x01;
           // my_buffer[4] = (byte)'u';
            usb.SpecifiedDevice.SendData(my_buffer);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] my_buffer = new byte[65];

            my_buffer[2] = (byte)0xFF;
            // my_buffer[4] = (byte)'u';
            usb.SpecifiedDevice.SendData(my_buffer);

        }
         
    }
}


Kullanılan kütüphane ise MikroC kitabındada işlenen usbLibrary.dll kütüphanesi.

Sorunum şöyle
Yukarıda en son verdiğim kodlar programın orjinal hali Bu Program ile 18F4550 yi haberleştirebiliyorum. Sıfrıdan kendim bir tasarım yapayım diyorum. Yukarıdaki kodlara bakarak herşeyi aynen yazıyorum. Ama Program hiçbir USB cihazına tepki vermiyor. İşte bu bana kafayı yedirtmek üzere. :S
Mesela yukarıdaki kodlar üzerinde değişiklik yaparak(Taban yine aynı) kendim bir tasarım yapıp kendi cihazımla haberleştirdim. Hiçbir problem olmadı.

İşte bunu çözemeye çalışıyorum.

Sanırım İnternette C# için sadece iki adet usb kütüphanesi var. Biri umng.dll diğeri ise usbLibrary.dll
usblibrary çalışıyor ama diğer kütüphaneyide hiçbir şekilde çalıştıramadım.

Ne kadar çetrefilli bir işmiş arkadaş!!

desman

A freshman at YTU

Mucit23

Evet inceledim. Hatta arkadaşın yazılımını denedim. Ama kendi programıyla bile haberleştiremedim :S

emosamca

PIC i programlarken bir PID ve VID vermişsindir. Bu numaraları girerek aşağıdaki kodu çalıştır.


            usbHidPort1.VendorId = 4660;
            usbHidPort1.ProductId = 4660;
            usbHidPort1.CheckDevicePresent();

Eğer usbHidPort1.SpecifiedDevice null ise senin cihaz takılmamıştır. Null değilse senin cihazdır veya senin cihazın daha önceden takılmıştır. Sonuçta bu VID ve PID numaralarından faydalanacaksın.

muratyamak10

#7
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace led_kontrol
{
    public partial class Form1 : Form
    {
        byte sayi = 0;
        protected override void OnHandleCreated(EventArgs e)
        {

            base.OnHandleCreated(e);
            usbHidPort1.RegisterHandle(Handle);
        }
        protected override void WndProc(ref Message m)
        {
            usbHidPort1.ParseMessages(ref m);
            base.WndProc(ref m);
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void usb_gonder(byte veri)
        {
            byte[] dizi = new byte[usbHidPort1.SpecifiedDevice.OutputReportLength + 1];
            dizi[0] = 0;
            dizi[1] = veri;
            for (int i = 2; i < textBox1.Text.Length; i++) { dizi = 0xFF; }
            if (usbHidPort1.SpecifiedDevice != null)
            {
                usbHidPort1.SpecifiedDevice.SendData(dizi);
            }
            else
            {
                MessageBox.Show("USB cihaz hazır değil,cihazı takınız!.. ");
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Herhangi bir USB Cihaz bağlı değil!..";
            checkBox1.Checked = true;
            checkBox2.Checked = false;
        }
        private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Hikko USB HID (PIC18F4550) Cihazı bağlandı!";
        }
        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Herhangi bir USB Cihaz bağlandı!";
        }
        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Hikko USB HID (PIC18F4550) Cihazı çıkarıldı!";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
               
                usbHidPort1.VendorId = Convert.ToInt32(textBox1.Text);
                usbHidPort1.ProductId = Convert.ToInt32(textBox2.Text);
                if ((usbHidPort1.VendorId == 4660) & (usbHidPort1.ProductId == 4660))
                {
                    usbHidPort1.CheckDevicePresent();
                }
                else
                {
                    MessageBox.Show("Yanlış Vendor ID veya Product ID girdiniz!..");
                }
            }
            catch
            {
                MessageBox.Show("Vendor ID ile Product ID girmelisiniz!..");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox3.Text != "")
                {
                    if (checkBox1.Checked)
                        sayi = Byte.Parse(textBox3.Text, System.Globalization.NumberStyles.HexNumber);
                    if (checkBox2.Checked)
                        sayi = Convert.ToByte(textBox3.Text);
                }
                usb_gonder(10);
            }
            catch
            {
                MessageBox.Show("Hikko USB HID (PIC18F4550) Cihazı bağlı değil!");
                textBox3.Text = "0";
            }
        }
        private void checkBox1_CheckedChanged(object sender,EventArgs e)
        {
            if(checkBox1.Checked)  checkBox2.Checked = false;
            if(checkBox2.Checked)  checkBox1.Checked = false;
            textBox3.MaxLength = 2;
            textBox3.Text = "0";
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked) checkBox1.Checked = false;
            if (checkBox1.Checked) checkBox2.Checked = false;
            textBox3.MaxLength = 3;
        }
    }
}



mesaj birleştirme:: 01 Kasım 2014, 14:35:39

hocam micro c ile 18f4550 kitabından c# arayüz yazmak istedim bir türlü bağlantı kuramadım nerde hata yapıyorum acaba pıd ve vıd leri girdim kodlar bu kadar


mesaj birleştirme:: 01 Kasım 2014, 00:37:19

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace led_kontrol
{
    public partial class Form1 : Form
    {
        byte sayi = 0;
        protected override void OnHandleCreated(EventArgs e)
        {

            base.OnHandleCreated(e);
            usbHidPort1.RegisterHandle(Handle);
        }
        protected override void WndProc(ref Message m)
        {
            usbHidPort1.ParseMessages(ref m);
            base.WndProc(ref m);
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void usb_gonder(byte veri)
        {
            byte[] dizi = new byte[usbHidPort1.SpecifiedDevice.OutputReportLength + 1];
            dizi[0] = 0;
            dizi[1] = veri;
            for (int i = 2; i < textBox1.Text.Length; i++) { dizi = 0xFF; }
            if (usbHidPort1.SpecifiedDevice != null)
            {
                usbHidPort1.SpecifiedDevice.SendData(dizi);
            }
            else
            {
                MessageBox.Show("USB cihaz hazır değil,cihazı takınız!.. ");
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Herhangi bir USB Cihaz bağlı değil!..";
            checkBox1.Checked = true;
            checkBox2.Checked = false;
        }
        private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Hikko USB HID (PIC18F4550) Cihazı bağlandı!";
        }
        private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Herhangi bir USB Cihaz bağlandı!";
        }
        private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = " Hikko USB HID (PIC18F4550) Cihazı çıkarıldı!";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
               
                usbHidPort1.VendorId = Convert.ToInt32(textBox1.Text);
                usbHidPort1.ProductId = Convert.ToInt32(textBox2.Text);
                if ((usbHidPort1.VendorId == 4660) & (usbHidPort1.ProductId == 4660))
                {
                    usbHidPort1.CheckDevicePresent();
                }
                else
                {
                    MessageBox.Show("Yanlış Vendor ID veya Product ID girdiniz!..");
                }
            }
            catch
            {
                MessageBox.Show("Vendor ID ile Product ID girmelisiniz!..");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox3.Text != "")
                {
                    if (checkBox1.Checked)
                        sayi = Byte.Parse(textBox3.Text, System.Globalization.NumberStyles.HexNumber);
                    if (checkBox2.Checked)
                        sayi = Convert.ToByte(textBox3.Text);
                }
                usb_gonder(sayi);
            }
            catch
            {
                MessageBox.Show("Hikko USB HID (PIC18F4550) Cihazı bağlı değil!");
                textBox3.Text = "0";
            }
        }
        private void checkBox1_CheckedChanged(object sender,EventArgs e)
        {
            if(checkBox1.Checked)  checkBox2.Checked = false;
            if(checkBox2.Checked)  checkBox1.Checked = false;
            textBox3.MaxLength = 2;
            textBox3.Text = "0";
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked) checkBox1.Checked = false;
            if (checkBox1.Checked) checkBox2.Checked = false;
            textBox3.MaxLength = 3;
        }
    }
}


trinity

adam ilk önce bir usbLibrary.dll kütüphanesi yazmış, çünkü c# dan direk usb cihaza bağlanılamıyor, daha alt seviyeye inmeniz gerekiyor. bu kütüphanenin fonksiyon yapısını bilmeden kendiniz kod geliştiremezsiniz. adamın kütüphanesini kullanacaksanız gene onun verdiği çalışan örnek kodlara ihtiyacınız var.

ha ben zehir gibi programcıyım bir gecede 50 sayfadan aşağı kod yazmam diyorsan, usb kütüphanenizi kendiniz yazarsınız, adamın örnek kodlarına da ihtiyacınız kalmaz.

bazı eski kütüphaneler 64bit çalışmıyor, buldunuz kütüphane güncel olmalı.

kemalserkan

işletim sisteminiz 32-bit mi yoksa 64-bit mi? doğru dll'i kullanmalısınız

MCansız

Zorunlu olarak C# mı kullanıyorsunuz Delphi kullanacaksanız yardımcı olurum