SELAMAT DATANG DI BLOG SAYA :)

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tampilkan postingan dengan label akuisi data sensor. Tampilkan semua postingan
Tampilkan postingan dengan label akuisi data sensor. Tampilkan semua postingan

Senin, 25 Januari 2016

akuisi data sensor menggunakan C#

halo. .pada kali ini saya akan memberikan tutorial mengenai akuisi data menggunakan C#.
oke langsung saja..
pertama instal microsoft visual studio 2012 atau yang terbaru.
kemudian buat projek baru visual C#
buatlah gambar sebagai berikut :

kemudian pada bagian doc.cs , ketikkan program dibaawah ini.

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 ZedGraph;
using System.Globalization;
using ZedGraph;
//namespace untuk class serial
using System.IO.Ports;
//namespace untuk melihat setting global komputer
using System.Globalization;
namespace serial
{
    public partial class Form1 : Form
    {
        double waktuStart = 1000;
        public Form1()
        {
            InitializeComponent();
        }
            //Inisialisasi Zedgraph
        

        private void Form1_Load(object sender, EventArgs e)
        {
            //—menset event handler untuk DataReceived event—
            serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
            //—menampilkan nama serialport yang tersedia pada
            // komputer—
            string[] portNames = System.IO.Ports.SerialPort.GetPortNames();
            for (int i = 0; i <= portNames.Length - 1; i++)
            {
                cbbCOMPorts.Items.Add(portNames[i]);
            }
            btnDisconnect.Enabled = false;
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {
            //—menutup akses serialport apabila akses serialport terbuka—
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            try
            {
                //—mengatur beberapa parameter untuk koneksi serial
                // port—
                serialPort.PortName = cbbCOMPorts.Text;
                serialPort.BaudRate = int.Parse(comboBox1.Text);
                serialPort.Parity = System.IO.Ports.Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = System.IO.Ports.StopBits.One;
                //—buka serial port—
                serialPort.Open();
                //—menampilkan status dari serial port dan
                // enable/disable -kan tombol—
                lblMessage.Text = cbbCOMPorts.Text + " connected.";

                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
                timer1.Start();
            }


            catch (Exception ex)
            {
                MessageBox.Show("Erorr, Pastikan serial port yang digunakan benar dan telah terhubung!!! cobalah untuk lebih fokus...minum kopi dulu sana :D :p ");
            }
        }


        //event ketika tombol stop diklik
       
           private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                //—tutup serial port—
                serialPort.Close();
                //—menampilkan status dari serial port dan
                // enable/disable -kan tombol—
                lblMessage.Text = serialPort.PortName + " disconnected.";
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
                timer1.Stop();
                //matikan detak
                timer1.Enabled = false;
                //jika komunikasi masih terbuka, tutup komunikasi
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
            }
            catch (Exception gagal)
            {
                //error handling
                //MessageBox.Show(gagal.ToString());
            }
        }
        
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //—menulis tipe data string pada serial port—
                serialPort.Write(txtDataToSend.Text + Environment.NewLine);
                //—menambahkan string yang telah dikirim pada TextBox control—
                txtDataReceived.AppendText(">" + txtDataToSend.Text + Environment.NewLine);
                txtDataReceived.ScrollToCaret();
                //—bersihkanTextBox control—
                txtDataToSend.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //—Event handler untuk DataReceived event—
        private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //—memanggil delegasi untuk mengambil data yang diterima—
            txtDataReceived.BeginInvoke(new myDelegate(updateTextBox));
        }

        //—Delegate and subroutine untuk ditampilkan pada TextBox control—
        public delegate void myDelegate();
        public void updateTextBox()
        {
            //—menambahkan data pada TextBox control—
            txtDataReceived.AppendText(serialPort.ReadExisting());
            txtDataReceived.ScrollToCaret();
        }
      
    }
}
    




oke demikian tutorial kali ini selamat mencoba :)
 
Share:

iklan 1

Diberdayakan oleh Blogger.

Blogger templates