13 Ekim 2013 Pazar

c# seri haberleşme

C# ile Seri Port Kontrolü ve PIC18F2550

Çok bilinen ve çok basit bir işlem olan seri port kontrolünü C# kullanarak basit bir uygulama ile göstermeye çalışacağım. Seri haberleşmenin elektronikte ne kadar önemli bir yere sahip olduğunu anlatmama sanıyorum gerek yoktur. Bu uygulama için hangi PIC modelini kullandığınızın bir önemi yok. Ben elimde olan PIC18F2550 mikrodenetleyicisini kullanmayı tercih ettim.


Rx,Tx pinleri bağlantıları yapıldı ve devremiz çalışmaya hazır.
C# uygulamamıza başlayabiliriz. Form üzerindeki elemanları aşağıdaki gibi yerleştirdikten sonra ilk combobox için isim alanına “baudRate”, ikinci combobox için isim alanına “portName” yazıyoruz. İlk combobox için properties penceresinden items alanına 1200, 2400, 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200 değerlerini alt alta ekliyoruz. İkinci combobox için properties penceresinden items alanına COM1,COM2,COM3,COM4,COM5,COM6 değerlerini alt alta ekliyoruz.

Daha sonra form üzerine bir textbox ve bir buton daha ekliyoruz. Textbox’ın enabled özelliğini ve Port Aç butonu ile Led Toggle butonunun enabled özelliğini false yapıyoruz. Formun yeni görüntüsü aşağıdaki gibi oluyor.

Uygulama kodları :
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
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 System.IO.Ports;
using System.IO;
 
namespace SeriPort
{
    public partial class Form1 : Form
    {
        SerialPort SeriPortum;
        public Form1()
        {
            InitializeComponent();
        }
        private void OkumaFonksiyonum(object s, SerialDataReceivedEventArgs e)
        {
            int bytes = SeriPortum.BytesToRead;
            byte[] buffer = new byte[bytes];
            SeriPortum.Read(buffer, 0, bytes);
            if (buffer[0] == 0xBB)
            {
                textBox1.Text = "LED On";
            }
            else if (buffer[0] == 0xCC)
            {
                textBox1.Text = "LED Off";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            SeriPortum = new SerialPort();
            SeriPortum.DataReceived += new SerialDataReceivedEventHandler(OkumaFonksiyonum);
            baudRate.SelectedItem = "1200";
            portName.SelectedItem = "COM3";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SeriPortum.PortName = portName.Text.ToString();
                SeriPortum.BaudRate = Convert.ToInt32(baudRate.Text.ToString());
                SeriPortum.DataBits = 8;
                SeriPortum.Parity = Parity.None;
                button1.Enabled = false;
                button2.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Seri Port", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 
        int control = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (control == 0)
                {
                    control = 1;
                    if (SeriPortum.IsOpen == false)
                    {
                        SeriPortum.Open();
                        button2.Text = "Port Kapat";
                        button3.Enabled = true;
                        baudRate.Enabled = false;
                        portName.Enabled = false;
                    }
 
                }
                else
                {
                    SeriPortum.Close();
                    control = 0;
                    button2.Text = "Port Aç";
                    button3.Enabled = false;
                    baudRate.Enabled = true;
                    portName.Enabled = true;
                    textBox1.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Seri Port", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 
        private void baudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
        }
 
        private void portName_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            byte[] data = { 0xAA };
            SeriPortum.Write(data, 0, 1);
        }
 
    }
}
PIC programı kodları:
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
#include <18F2550.h>
#fuses HS
#use delay (clock=20000000)
#use rs232 (baud=2400, xmit=pin_c6,rcv=pin_c7, parity=N, stop=1)
 
int data;
int1 control=0;
void main()
{
   while(1)
   {
      data=getch();
      if(data==0xAA)
      {
         control++;
      }
      if(control==1)
      {
         output_high(PIN_B7);
         putc(0xBB);
      }
      else
      {
         output_low(PIN_B7);
         putc(0xCC);
      }
   }
}
 
 
kaynak:http://www.recaisinekli.com/wp/?p=210 

Hiç yorum yok:

Yorum Gönder