Karabük Üniversitesi Teknoloji Fakültesi Mekatronik Mühendisliği Bölümü 2016
C# İLE SERİ PORT, GRAFİK, EXCEL İŞLEMLERİ
Hazırlayan: Arş. Gör. Emel SOYLU
Aşağıdaki kütüphaneleri ekleyiniz.
using System.Data.SqlClient;
using System.IO.Ports;
using System.IO;
using System.Drawing.Imaging;
using System.Data.OleDb;
Solution explorer bölmesinde referanslara sağ tıklayıp Microsoft. Office. Interop. Excel ekleyiniz.
Global değişkenler
double t, sayi;
int sample;
double sample_rate;
DataTable tablo = new DataTable();
string veri = "0";
int str;
Form_Load kodları
t = 0;
sayi = 0;
tablo.Columns.Add("tarih");
tablo.Columns.Add("saat");
tablo.Columns.Add("zaman");
tablo.Columns.Add("veri");
str = 0;
Aşağıdaki gibi bir pencere tasarımı yapınız.
Dosya aç butonu
//excel dosyasını datagride getirme // dosyaya erişim
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.InitialDirectory = @"C:\\";
openDlg.ShowDialog();
string path = openDlg.FileName;
OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM [Sayfa1$]", baglanti);
// verileri datagride ekleme DataTable dt = new DataTable();
dt.Clear();
adp.Fill(dt); dataGridView1.DataSource = dt;
dataGridView1.DataSource = dt;
Grafik butonu
chart1.Series[0].Points.Clear();
int i, k;
k = dataGridView1.RowCount;
double veri;
double zaman;
for (i = 0; i < k-1; i++) {
zaman = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
veri=Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
chart1.Series[0].Points.AddXY(zaman,veri);
}
Kaydet butonu
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.Sheets["Sayfa1"];
worksheet = workbook.ActiveSheet;
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) {
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
for (int j = 0; j < dataGridView1.Columns.Count; j++) {
if (dataGridView1.Rows[i].Cells[j].Value != null) {
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
} else {
worksheet.Cells[i + 2, j + 1] = "";
}}}
Grafiği temizle
chart1.Series[0].Points.Clear();
Tabloyu temizle
dataGridView1.DataSource = null;
str = 0;
Timer-1 aç
sample = 500;
sample_rate = Convert.ToDouble(sample) / 1000;
timer1.Interval = sample;
timer1.Enabled = true;
Timer-1 kapat
timer1.Enabled = false;
Timer-2 aç
sample = 500;
sample_rate = Convert.ToDouble(sample) / 1000;
timer2.Interval = sample;
timer2.Enabled = true;
Timer-2 kapat
timer2.Enabled = false;
seri port aç
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Handshake = Handshake.None;
serialPort1.RtsEnable = true;
serialPort1.DtrEnable = true;
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.Open();
seri port kapat
if (serialPort1.IsOpen == true) {
serialPort1.Close();
}
timer 1
t = t + sample_rate;
Random rasgele = new Random();
sayi= RandomNumber(-100, 100);
chart1.Series[0].Points.AddXY(t, sayi);
tablo.Rows.Add(DateTime.Now.ToLongDateString(),DateTime.Now.ToLongTimeString(),Convert.ToString(t), Convert.ToString(sayi));
dataGridView1.DataSource = tablo;
timer2
t = t + sample_rate;
veri = serialPort1.ReadLine();
chart1.Series[0].Points.AddXY(t, Convert.ToDouble(veri));
dataGridView1.Rows[str].Cells[0].Value = DateTime.Now.ToLongDateString();
dataGridView1.Rows[str].Cells[1].Value = DateTime.Now.ToLongTimeString();
dataGridView1.Rows[str].Cells[2].Value = t.ToString();
dataGridView1.Rows[str].Cells[2].Value = veri;
tablo.Rows.Add(Convert.ToString(t), Convert.ToString(sayi));
dataGridView1.DataSource = tablo;
rasgele sayı fonksiyonu
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max) {
lock (syncLock) { // synchronize
return random.Next(min, max);
} }