
Öncelikle Herkese Merhaba
Yapacağımız bazı Arduino uygulamalarını bilgisayarımızla haberleştirmemiz gerekebiliyor. Bir Windows Form uygulaması ile bunun nasıl yapılacağını anlatmak istedim.
Arduino kısmından bahsetmeyeceğim, C# kısmı şu şekilde;
C# kısmı
Öncelikle kullanacağımız kütüphaneleri ekliyoruz. Kamera görüntüsü için Aforge kütüphanesi, Haberleşme için IO kütüphanesi.
1 2 3 |
using System.IO.Ports; using AForge.Video; using AForge.Video.DirectShow; |
Bluetooth cihazları ve kameralar için birer Global değişken oluşturuyoruz
1 2 |
FilterInfoCollection cihazlar; VideoCaptureDevice kamera; |
Portlardan alınan bilgiler ile bluetooth cihazlarını ve kameralarını combobox’lara dolduruyoruz
1 2 3 4 5 6 7 8 9 10 11 12 |
if (serialPort1.IsOpen) serialPort1.Close(); combohizlar.Items.Clear(); comboportlar.Items.Clear(); combohizlar.Items.Add("9600"); cihazlar = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (string portlar in SerialPort.GetPortNames()) comboportlar.Items.Add(portlar); foreach (FilterInfo cihaz in cihazlar) { cmbKamera.Items.Add(cihaz.Name); } kamera = new VideoCaptureDevice(); |
Şimdi seçilen portlara göre hangi cihaza bağlanacağını ayarlıyoruz. Bağlanma sağlanamazsa ekrana hata mesajı basıyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
private void button6_Click_1(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); // btnbaglan.Text = "Bağlan"; } try { if (!serialPort1.IsOpen) { serialPort1.PortName = comboportlar.SelectedItem.ToString(); serialPort1.BaudRate = Convert.ToInt32(combohizlar.SelectedItem); MessageBox.Show("Bağlandı!"); } } catch { } try { serialPort1.Open(); serialPort1.Write("*"); } catch { MessageBox.Show("Bağlantı Kurulmadı: \n" + serialPort1.PortName.ToString() + " " + serialPort1.BaudRate.ToString()); return; }; } |
Basılan butonlara göre arduinoya gönderilecek char verilerini belirleyip port üzerinden gönderme işlemi yapıyoruz. İşlem başarısız olursa ekrana hata mesajı basıyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
private void button1_Click_1(object sender, EventArgs e) { try { serialPort1.Write('w'.ToString()); lbDurum.Text = "İleri Gidiyor!"; } catch { MessageBox.Show("İleri Gitmiyor!"); } } private void button3_Click_1(object sender, EventArgs e) { try { serialPort1.Write('a'.ToString()); lbDurum.Text = "Sola Gidiyor!"; } catch { MessageBox.Show("Sola Gitmedi"); } } private void button5_Click_1(object sender, EventArgs e) { try { serialPort1.Write('x'.ToString()); lbDurum.Text = "Durdu!"; } catch { MessageBox.Show("Durmadi!"); } } private void button4_Click_1(object sender, EventArgs e) { try { serialPort1.Write('d'.ToString()); lbDurum.Text = "Sağa Gidiyor!"; } catch { MessageBox.Show("Sağa Gitmedi"); } } private void button2_Click_1(object sender, EventArgs e) { try { serialPort1.Write('s'.ToString()); lbDurum.Text = "Geri Gidiyor!"; } catch { MessageBox.Show("Geri gitmedi!"); } } |
Kameraya bağlanma ve görüntü alma işlemlerini yapıyoruz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private void button7_Click_1(object sender, EventArgs e) { if (kamera.IsRunning) { kamera.Stop(); pictureBox2.Image = null; pictureBox2.Invalidate(); } else { kamera = new VideoCaptureDevice(cihazlar[cmbKamera.SelectedIndex].MonikerString); kamera.NewFrame += new NewFrameEventHandler(cam_NewFrame); kamera.Start();//kamerayı başlatıyoruz. } } |
Son olarak Kameradan alınan görüntü için bir bitmap oluşturuyoruz ve görüntüyü buraya atıyoruz
1 2 3 4 5 |
private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap bit = (Bitmap)eventArgs.Frame.Clone(); pictureBox2.Image = bit; } |
Kodun birleştirilmiş hali de şu şekilde;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using AForge.Video; using AForge.Video.DirectShow; namespace TankKontrol { public partial class Form1 : Form { FilterInfoCollection cihazlar; VideoCaptureDevice kamera; public Form1() { InitializeComponent(); } public delegate void AddDataDelegate(String myString); public AddDataDelegate myDelegate; private void Form1_Load(object sender, EventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); combohizlar.Items.Clear(); comboportlar.Items.Clear(); combohizlar.Items.Add("9600"); cihazlar = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (string portlar in SerialPort.GetPortNames()) comboportlar.Items.Add(portlar); foreach (FilterInfo cihaz in cihazlar) { cmbKamera.Items.Add(cihaz.Name); } kamera = new VideoCaptureDevice(); } private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap bit = (Bitmap)eventArgs.Frame.Clone(); pictureBox2.Image = bit; } private void button6_Click_1(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); // btnbaglan.Text = "Bağlan"; } try { if (!serialPort1.IsOpen) { serialPort1.PortName = comboportlar.SelectedItem.ToString(); serialPort1.BaudRate = Convert.ToInt32(combohizlar.SelectedItem); MessageBox.Show("Bağlandı!"); } } catch { } try { serialPort1.Open(); serialPort1.Write("*"); } catch { MessageBox.Show("Bağlantı Kurulmadı: \n" + serialPort1.PortName.ToString() + " " + serialPort1.BaudRate.ToString()); return; }; } private void button1_Click_1(object sender, EventArgs e) { try { serialPort1.Write('w'.ToString()); lbDurum.Text = "İleri Gidiyor!"; } catch { MessageBox.Show("İleri Gitmiyor!"); } } private void button3_Click_1(object sender, EventArgs e) { try { serialPort1.Write('a'.ToString()); lbDurum.Text = "Sola Gidiyor!"; } catch { MessageBox.Show("Sola Gitmedi"); } } private void button5_Click_1(object sender, EventArgs e) { try { serialPort1.Write('x'.ToString()); lbDurum.Text = "Durdu!"; } catch { MessageBox.Show("Durmadi!"); } } private void button4_Click_1(object sender, EventArgs e) { try { serialPort1.Write('d'.ToString()); lbDurum.Text = "Sağa Gidiyor!"; } catch { MessageBox.Show("Sağa Gitmedi"); } } private void button2_Click_1(object sender, EventArgs e) { try { serialPort1.Write('s'.ToString()); lbDurum.Text = "Geri Gidiyor!"; } catch { MessageBox.Show("Geri gitmedi!"); } } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.W) lbDurum.Text = "w"; } private void cmbKamera_SelectedIndexChanged(object sender, EventArgs e) { } private void button7_Click_1(object sender, EventArgs e) { if (kamera.IsRunning) { kamera.Stop(); pictureBox2.Image = null; pictureBox2.Invalidate(); } else { kamera = new VideoCaptureDevice(cihazlar[cmbKamera.SelectedIndex].MonikerString); kamera.NewFrame += new NewFrameEventHandler(cam_NewFrame); kamera.Start();//kamerayı başlatıyoruz. } } private void button8_Click_1(object sender, EventArgs e) { if (kamera.IsRunning) kamera.Stop(); else MessageBox.Show("Zaten çalışmıyor"); } } } |
Görüntüler
Programın Çalışırken ki ekran görüntüsü


