Wednesday, May 20, 2015

Serial Transmit & kontrol 3 LED

Assalamu Alaikum Warohmatullahi Wabarokatu, 
ketemu lagi sama saya Septian dalam acara program c# hihihih. baiklah pada kesempatan kali ini saya ingin menjelaskan Serial Port untuk kontrol LED.

Disini kita menggunakan :
1. Virtual Studio C# 2010 Express
2. Configure Virtual Serial Port Driver  
3. Hyperterminal.


Setelah kita memiliki, aplikasi di atas maka kita bisa memulai praktek.


1. kita buat design Form
    ini adalah design Form yg telah saya buat.

disini saya menggunakan serial port, timer, textbox, button, picturebox, label.

ini adalah program untuk menjalankan aplikasi yg ada di atas :

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Image putih = Properties.Resources.putih;
        Image kuning = Properties.Resources.kuning;

        public Form1()
        {
            InitializeComponent();
            pictureBox1.BackgroundImage = putih;
            pictureBox2.BackgroundImage = putih;
            pictureBox3.BackgroundImage = putih;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (serialPort1.BytesToRead != 0)
            {
                textBox2.Text = serialPort1.ReadExisting();
            }

        } 

        private void kirim_Click_1(object sender, EventArgs e)
        {
            serialPort1.Write(textBox1.Text);
        }

        #region tombol on/off LED
        private void on1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B1");
                pictureBox1.BackgroundImage = kuning;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        private void off1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B0");
                pictureBox1.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void on2_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B2");
                pictureBox2.BackgroundImage = kuning;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void off2_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B3");
                pictureBox2.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void on3_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B4");
                pictureBox3.BackgroundImage = kuning;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void off3_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("B5");
                pictureBox3.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

    }
}



2. Kemudian buka aplikasi Configure Virtual Serial Port Driver, selanjutnya :


selanjutnya kita "Add Pair" agar COM1 dan COM2 bisa terhubung.


3. Lalu jalankan aplikasi HTERM seperti ini, lalu setting COM berlawanan dengan COM yang terdapat pada visual studio dan setting baud nya menjadi 9600 lalu kemudian klik connect.


4. Biasanya kalo hyperterminal sudah di buka, maka kita harus merunning program yg telah kita buat agar kita dapat melakukan hubungan antara COM1(aplikasi C#) dengan COM2(HTem). Seperti yg terlihat pada gambar di bawah :



5. Ini adalah contoh program jika menggunakan arduino :



Mungkin cukup sekian ilmu yg dapat saya bagikan, mudah2an bermanfaat bagi kita bersama :)___






No comments:

Post a Comment