Minggu, 14 Juni 2015

KOMUNIKASI ETHERNET

PRAKTIKUM – KOMUNIKASI ETHERNET



I. Tujuan
 Mahasiswa mempelajari komunikasi Ethernet di C#.
 Mengetahui kegunaan socket di C#.
 Dapat membuat aplikasi windows sederhana untuk mengirim teks.

II. Dasar Teori
Komunikasi Ethernet merupakan salah satu jenis komunikasi yang paling sering ditemui saat ini. Penggunaannya juga beragam, bisa digunakan untuk komunikasi antar PC, PC dengan mikrokontroller, PC dengan PLC, PLC dengan PLC dan sebagainya. Komunikasi Ethernet dapat menggunakan media berupa kabel maupun nirkabel. Media kabel yang digunakan biasanya berupa kabel UTP yang ditiap ujungnya terdapat konektor RJ45, sedangkan yang nirkabel biasanya memanfaatkan router wireless. Untuk mengenali tujuan pengiriman data, komunikasi ini menggunakan IP address dan port. IP Address dianalogikan sebagai kompleks perumahan, dan port dianalogikan sebagai nomor rumah. Jika IP Address dan port yang digunakan asal-asalan, maka paket data yang dikirimkan juga tidak akan pernah sampai ke device tujuan. Pada komunikasi Ethernet terdapat 2 jenis protocol pengiriman data, yaitu TCP dan UDP. Kedua protocol tersebut memiliki kelebihan dan kekurangan masing-masing. Pada praktikum kali ini, kita akan membuat sebuah aplikasi chatting teks sederhana menggunakan protocol UDP.

III. Praktikum
1. Buat project baru, kemudian susun form control seperti pada gambar.
2. Tambahkan library berikut ini.
Library tersebut digunakan untuk mengakses thread, socket dan beberapa method yang dibutuhkan untuk pembuatan aplikasi.

3. Salin dan sesuaikan program dibawah ini ke project anda,




4. Compile dan jalankan aplikasi. 
5. Coba ganti IP address dan port yang digunakan. 
6. Compile aplikasi kemudian amati.

Minggu, 31 Mei 2015

Kontrol 3 Lampu dengan Komunikasi Serial

KOMUNIKASI SERIAL
UNTUK MENGHIDUPKAN DAN MEMATIKAN
3 BUAH LAMPU
DENGAN VISUAL STUDIO C#



LANGKAH-LANGKAH:
1. Buka aplikasi Visual Studio > new project > buat nama project > ok.
2. Buatlah desain seperti contoh.
3. Klik 2x pada tool yang ingin diberi codingan program.
4. Setelah semua selesai, running kan programnya.
5. Inilah hasilnya !!!

Praktikum serial transmit

PRAKTIKUM IX – Serial Transmit




I. Tujuan 
 Mahasiswa mempelajari Serial Port di C# 
 Mengetahui kegunaan Serial Port di SharpDevelop 
 Dapat mengunakan Serial Port dalam aplikasi windows sederhana untuk akuisisi data 


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


III. Praktikum 
1. Tujuan praktikum ini membuat aplikasi chating antara dua komputer menggunakan port serial. 
2. Buat solution baru. Susun form dengan control-control seperti terlihat pada gambar.


3. Komponen Timer ada di Windows Form toolbar. Sedangkan komponen SerialPort ada di Components toolbar. Jangan lupa, set properties Enabled di komponen Timer menjadi true. Kemudian properties Interval di-set menjadi 500 ms. 
4. Double click tombol (button) dengan text “kirim”. Ketikkan statemen di bawah ini di dalam fungsi ButtonClick : 
serialPort1.Write(textBox1.Text);

Kode di atas tujuannya adalah untuk menuliskan isi dari textBox1 ke port serial. Sebelumnya kita harus membuka port dulu dengan statemen :
serialPort1.Open();

Double click untuk timer. Ketikkan statemen di bawah ini di dalam fungsi TimerTick :
if (serialPort1.BytesToRead != 0)
{ textBox2.Text = serialPort1.ReadExisting(); }

5. Kode di atas tujuannya adalah untuk membaca data yang ada di buffer serial port. Compile dan jalankan program. 
6. Buka hyperterminal, kemudian amati data yang tampil pada hyperterminal saat anda mengirim data. 
7. Buka Arduino.exe. 
8. Ketikkan program dibawah ini pada Arduino IDE.
9. Koneksikan Arduino ke computer, pilih port dan tipe Arduino terlebih dahulu. Kemudian upload program tersebut dengan cara mengeklik tanda 
10. Tunggu sampai ada tulisan “Done Uploading” pada Arduino IDE. 
11. Jalankan aplikasi anda, dengan mengatur port serial sesuai dengan port Arduino yang terdeteksi pada computer. 
12. Coba kirim data karakter dari aplikasi. 
13. Amati hasilnya pada Arduino.

coding:
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
{
public Form1()
{
InitializeComponent();
}
private void button1_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 Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
}
}
}

Minggu, 03 Mei 2015

TIC TAC TOE

PRAKTIKUM VII – Tic Tac Toe


I. Tujuan
   • Meningkatkan kemampuan berpikir logis
   • Mengetahui kegunaan PictureBox di SharpDevelop
   • Dapat mengunakan PictureBox dalam aplikasi windows sederhana

II. Dasar Teori
           Control PictureBox digunakan untuk menampilkan gambar dan memanipulasinya. PictureBox dapat menangani berbagai macam format file gambar. Dalam praktikum ini kita menggunakan PNG. Untuk mengisi PictureBox dengan gambar, kita tinggal load gambarnya dengan menge-klik Properties Image-nya. Kemudian sesuaikan ukuran Width dan Height nya sehingga gambar tidak terpotong.



III. Praktikum
          Gambar pada PictureBox bisa saling dicopy. Artinya gambar pada instance pictureBox1 dapat kita copy-kan ke instance pictureBox2. Di sini kita akan membuat permainan TicTacToe. Kita susun terlebih dahulu gambar kotak kosong sebanyak 3x3 sebagai papan permainan. Kemudian tambahkan gambar lingkaran dan gambar silang sebagai pemainnya. Gambar ini kita set properties Visible-nya menjadi false. Semua gambar kita letakkan di PictureBox. Inti dari program ini adalah menunggu pemain untuk menempatkan pilihannya. Tiap PictureBox kita tambahkan event onClick. Apabila salah satu di-klik, akan dicek terlebih dahulu, apakah masih kosong atau tidak. Apabila kosong, maka pemain boleh memilih kotak tersebut, artinya kita copy-kan simbol silang ke kotak tersebut. Kemudian tinggal di-cek, apakah sudah ada yang berhasil membuat 3 segaris atau belum. Lalu giliran komputer. Komputer akan memilih secara acak kotak yang kosong, kemudian copy-kan simbol lingkaran ke kotak tersebut. Demikian terus bergantian antara pemain dan komputer sampai semuanya terisi. Tapi apabila ada yang berhasil membuat 3 segaris, maka dia dinyatakan sebagai pemenang.


Langkah kerja:
1. Jalankan SharpDevelop.


2. Buat project baru dengan cara meng-klik tombol "New Solution".


3. Pilih untuk membuat "Windows Application" , beri nama project anda dan klik "Create".


4. Akan muncul dinding baru untuk kita memulai membuat aplikasinya. Pilih tab bertuliskan “Design” untuk membuat desain aplikasi yang diinginkan.


5. Maka akan muncul form template aplikasi windowsnya, desain sesuai keinginan anda, ini contoh desain saya. Praktek kali ini hanya menggunakan 9 PictureBox.




MEMBUAT GAME TIC TAC TOE DI SHARPDEVELOP C#

Haii semuanyaaa !!! :)

Kali ini saya akan memberikan salah satu tutorial penjelasan pemrograman bahasa C.
Berikut merupakan tutorial singkat Pemrograman Bahasa C membuat game TIC TAC TOE dengan Sharpdevelop.
langsung saja di coba yaa...!!

Cara Membuat Permainan Tic Tac Toe dengan menggunakan Sharpdevelop:


1. Jalankan SharpDevelop.


2. Buat project baru dengan cara meng-klik tombol "New Solution".


3. Pilih untuk membuat "Windows Application" , beri nama project anda dan klik "Create".


4. Akan muncul dinding baru untuk kita memulai membuat aplikasinya. Pilih tab bertuliskan “Design” untuk membuat desain aplikasi yang diinginkan.


5. Maka akan muncul form template aplikasi windowsnya, desain sesuai keinginan anda, ini contoh desain saya. Praktek kali ini menggunakan 1 button, 9 picturebox, dan 4 label.



6. Buatlah source code untuk desain yang telah anda buat. Pilih tab bertuliskan “Source”.




*MY SOURCE CODE*




using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace game_tictactoe_devi
{
  
    public partial class MainForm : Form
    {
      
        Image pictureX = game_tictactoe_devi.Resource1.tic1;
        Image pictureO = game_tictactoe_devi.Resource1.tic2;
        Image pctr; int count = 0;
        Boolean checkX = false, checkO = false, easy = false;
        int pict1 = 0, pict5 = 0, pict9 = 0,pict2 = 0, pict6 = 0,pict3 = 0, pict7 = 0,pict4 = 0, pict8 = 0;
      
        public MainForm()
        {
          
            InitializeComponent();
          
        }
      
        private void disablepicturebox()
        {
            List<PictureBox> pctList = new List<PictureBox>();
          
        }
      
        private void wasclickedbefore(int pic)
        {
            switch (pic)
            {
                case 1: { if (pict1 == 1) pict1++; break; }
                case 2: { if (pict2 == 1) pict2++; break; }
                case 3: { if (pict3 == 1) pict3++; break; }
                case 4: { if (pict4 == 1) pict4++; break; }
                case 5: { if (pict5 == 1) pict5++; break; }
                case 6: { if (pict6 == 1) pict6++; break; }
                case 7: { if (pict7 == 1) pict7++; break; }
                case 8: { if (pict8 == 1) pict8++; break; }
                case 9: { if (pict9 == 1) pict9++; break; }
                default: break;
            }

                if (playercpuToolStripMenuItem.Checked == true)
                    easycomputer_play(pic);
                else if (hardToolStripMenuItem.Checked == true && easy==false)
                    hardcomputer_play(pic);
                else if (hardToolStripMenuItem.Checked == true && easy == true)
                    easycomputer_play(pic);
        }
      
        private void easycomputer_play(int pic)
        {
            if (pict1 == 0) { pictureBox1.BackgroundImage = pictureO; pict1 = 2; }
            else if (pict2 == 0) { pictureBox2.BackgroundImage = pictureO; pict2 = 2; }
            else if (pict3 == 0) { pictureBox3.BackgroundImage = pictureO; pict3 = 2; }
            else if (pict4 == 0) { pictureBox4.BackgroundImage = pictureO; pict4 = 2; }
            else if (pict5 == 0) { pictureBox5.BackgroundImage = pictureO; pict5 = 2; }
            else if (pict6 == 0) { pictureBox6.BackgroundImage = pictureO; pict6 = 2; }
            else if (pict7 == 0) { pictureBox7.BackgroundImage = pictureO; pict7 = 2; }
            else if (pict8 == 0) { pictureBox8.BackgroundImage = pictureO; pict8 = 2; }
            else if (pict9 == 0) { pictureBox9.BackgroundImage = pictureO; pict9 = 2; }
        }
      
        private void hardcomputer_play(int pic)
        {
            if ((pict1==2 || pict2==2 || pict3==2 || pict4==2 || pict6==2 || pict7==2 || pict8==2 || pict9==2) && pict5 == 0) { pictureBox5.BackgroundImage = pictureO; pict5 = 2; }
            else if (pict5 == 2 && (pict1 == 0 || pict3 == 0 || pict7 == 0 || pict9 == 0))
            {
                Random Rnd = new Random();
                int rnd = Rnd.Next(1,5);
                switch (rnd)                      
                {
                    case 1: { if (pict1 == 0) pictureBox1.BackgroundImage = pictureO; pict1 = 2; break; }
                    case 2: { if (pict3 == 0) pictureBox3.BackgroundImage = pictureO; pict3 = 2; break; }
                    case 3: { if (pict7 == 0) pictureBox7.BackgroundImage = pictureO; pict7 = 2; break; }
                    case 4: { if (pict9 == 0) pictureBox9.BackgroundImage = pictureO; pict9 = 2; break; }
                    default: break;
                }
                easy = true;
            }
        }
      
        private void result()
        {
            if (pictureBox1.BackgroundImage == pictureX && pictureBox2.BackgroundImage == pictureX && pictureBox3.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox4.BackgroundImage == pictureX && pictureBox5.BackgroundImage == pictureX && pictureBox6.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox7.BackgroundImage == pictureX && pictureBox8.BackgroundImage == pictureX && pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox1.BackgroundImage == pictureX && pictureBox4.BackgroundImage == pictureX && pictureBox7.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox2.BackgroundImage == pictureX && pictureBox5.BackgroundImage == pictureX && pictureBox8.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
          
            else if (pictureBox3.BackgroundImage == pictureX && pictureBox6.BackgroundImage == pictureX && pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox3.BackgroundImage == pictureX && pictureBox5.BackgroundImage == pictureX && pictureBox7.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }

            else if (pictureBox1.BackgroundImage == pictureX && pictureBox5.BackgroundImage == pictureX && pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
          
            resultO();
        }
      
        private void resultO()
        {
            if (pictureBox1.BackgroundImage == pictureO && pictureBox2.BackgroundImage == pictureO && pictureBox3.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox4.BackgroundImage == pictureO && pictureBox5.BackgroundImage == pictureO && pictureBox6.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox7.BackgroundImage == pictureO && pictureBox8.BackgroundImage == pictureO && pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox1.BackgroundImage == pictureO && pictureBox4.BackgroundImage == pictureO && pictureBox7.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox2.BackgroundImage == pictureO && pictureBox5.BackgroundImage == pictureO && pictureBox8.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox3.BackgroundImage == pictureO && pictureBox6.BackgroundImage == pictureO && pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox3.BackgroundImage == pictureO && pictureBox5.BackgroundImage == pictureO && pictureBox7.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO = true;
            }
            else if (pictureBox1.BackgroundImage == pictureO && pictureBox5.BackgroundImage == pictureO && pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (You) has WON !!!", "Congratulation"); checkO = true;
            }
            checkdraw();
        }
      
        private void checkdraw()
        {
            if (pictureBox1.BackgroundImage !=null && pictureBox2.BackgroundImage !=null && pictureBox3.BackgroundImage !=null && pictureBox4.BackgroundImage !=null &&
                pictureBox5.BackgroundImage !=null && pictureBox6.BackgroundImage !=null && pictureBox7.BackgroundImage !=null && pictureBox8.BackgroundImage !=null &&
                pictureBox9.BackgroundImage !=null )
            MessageBox.Show("Game is DRAW !!!", "Congratulation");
        }

        private void score()
        {
            if (checkX)
            {
                disablepicturebox();
                XScore.Text = (int.Parse(XScore.Text) + 1).ToString();
            }
            else if (checkO)
            {
                disablepicturebox();
                OScore.Text = (int.Parse(OScore.Text) + 1).ToString();
            }
        }
      
        private void Picture_Click(object sender, EventArgs e)
        {
            PictureBox piCTure = (PictureBox)sender;
            int pic = Convert.ToInt32((piCTure.Name).Substring(10,1));

            count++;
            // switch kondisi masing-masing picture box
            switch (pic)
            {
                case 1:
                {
                    if (pict1 == 0) { pict1++; pictureBox1.BackgroundImage = pctr; }
                    else if (pict1 == 2) { pictureBox1.Enabled = false; } break;
                }
                case 2:
                {
                    if (pict2 == 0) { pict2++; pictureBox2.BackgroundImage = pctr; }
                    else if (pict2 == 2) { pictureBox2.Enabled = false; } break;
                }
                case 3:
                {
                    if (pict3 == 0) { pict3++; pictureBox3.BackgroundImage = pctr; }
                    else if (pict3 == 2) { pictureBox3.Enabled = false; } break;
                }
                case 4:
                {
                    if (pict4 == 0) { pict4++; pictureBox4.BackgroundImage = pctr; }
                    else if (pict4 == 2) { pictureBox4.Enabled = false; } break;
                }
                case 5:
                {
                    if (pict5 == 0) { pict5++; pictureBox5.BackgroundImage = pctr; }
                    else if (pict5 == 2) { pictureBox5.Enabled = false; } break;
                }
                case 6:
                {
                    if (pict6 == 0) { pict6++; pictureBox6.BackgroundImage = pctr; }
                    else if (pict6 == 2) { pictureBox6.Enabled = false; } break;
                }
                case 7:
                {
                    if (pict7 == 0) { pict7++; pictureBox7.BackgroundImage = pctr; }
                    else if (pict7 == 2) { pictureBox7.Enabled = false; } break;
                }
                case 8:
                {
                    if (pict8 == 0) { pict1++; pictureBox8.BackgroundImage = pctr; }
                    else if (pict8 == 2) { pictureBox8.Enabled = false; } break;
                }
                case 9:
                {
                    if (pict9 == 0) { pict9++; pictureBox9.BackgroundImage = pctr; }
                    else if (pict9 == 2) { pictureBox9.Enabled = false; } break;
                }
                default: break;
            }
            result(); score();
            if (easyToolStripMenuItem.Checked == true || hardToolStripMenuItem.Checked == true)
            {
                pctr = pictureX;
                if (checkX == false && checkO == false) wasclickedbefore(pic);
            }
            else if (player2ToolStripMenuItem.Checked==true)
            {
            if (count % 2 == 0)
            pctr = pictureX;
            else
            pctr = pictureO;
            }
        }

        private void reset()
        {
            pict1 = pict2 = pict3 = pict4 = pict5 = pict6 = pict7 = pict8 = pict9 = count = 0; //mereset nilai int pict1-pict9
            easy = checkX = checkO = false;
            pictureBox1.BackgroundImage = pictureBox2.BackgroundImage = pictureBox3.BackgroundImage = pictureBox4.BackgroundImage =
            pictureBox5.BackgroundImage = pictureBox6.BackgroundImage = pictureBox7.BackgroundImage = pictureBox8.BackgroundImage =
            pictureBox9.BackgroundImage = null;

            List<PictureBox> pctList = new List<PictureBox>();
          
        }

        void Play_Again_Click(object sender, EventArgs e)
        {
            reset();
        }

        void easy_click(object sender, EventArgs e)
        {
            hardToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked = true;
            playercpuToolStripMenuItem.Checked = true; reset(); player2ToolStripMenuItem.Checked = false;
            XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tEasy Level ", "Created by: Devi Tinambunan");
        }

        void hard_click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; hardToolStripMenuItem.Checked = true;
            playercpuToolStripMenuItem.Checked = true; player2ToolStripMenuItem.Checked = false;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tHard Level ", "Created by: Devi Tinambunan");

        }

        void player2_click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; hardToolStripMenuItem.Checked = false;
            playercpuToolStripMenuItem.Checked = false; player2ToolStripMenuItem.Checked = true;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player vs Player ", "Created by: Devi Tinambunan");
        }

        void about_click(object sender, EventArgs e)
        {
            MessageBox.Show("Created by: Devi Tinambunan \n\n Powered by :www.devigusn.blogspot.com", "Warning");
         }
    }
}

7. Jalankan program yang telah anda buat dengan menekan tombol "Run".


inilah hasil dari program yang telah saya buat :



Okayy sekian penjelasannya cara membuat game tic tac toe, maaf ya kalo banyak yang salah :))
Untuk tutorialnya dapat dilihat di: https://www.youtube.com/watch?v=778rMCb6-3Q
Terima kasihh {}