Minggu, 31 Mei 2015

Transmiting Data (mematikan dan menghidupkan 3 lampu) menggunakan aplikasi Visual Studio

 Dasar Teori

Pengiriman data melalui serial port sangat sederhana, cukup
menggunakan method Write dengan parameter berupa string yang
ingin dikirim.

pertama buat desigen seperti ini :



lalu buat coding seperti ini :

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;

namespace Kontrol_3_led
{
    public partial class Form1 : Form
    {
        Image biru = Properties.Resources.biru;
        Image putih = Properties.Resources.putih;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();
            comboBox1.Items.Add("COM1");
        }

        private void on1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("1");
                pictureBox1.BackgroundImage = biru;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);      
            }
        }

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

        private void Exit_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Application.Exit();
        }

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

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

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

        private void off2_Click(object sender, EventArgs e)
        {

            try
            {
                serialPort1.Write("3");
                pictureBox2.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

            }
        }

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


Tidak ada komentar:

Posting Komentar