Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO.Ports;
  11.  
  12. namespace serialPort
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.            
  20.         }
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.              string[] comPorts = SerialPort.GetPortNames();
  25.  
  26.              foreach(string port in comPorts)
  27.              {
  28.                 comboBox1.Items.Add(port);
  29.              }
  30.            
  31.         }
  32.  
  33.         private void connect_Click(object sender, EventArgs e)
  34.         {
  35.             try {
  36.                 serialPort1.PortName = comboBox1.SelectedItem.ToString();
  37.                 serialPort1.Open();
  38.             }
  39.             catch(Exception hata)
  40.             {
  41.                 MessageBox.Show(hata.Message);
  42.             }
  43.         }
  44.  
  45.         private void button1_Click(object sender, EventArgs e)
  46.         {
  47.             try
  48.             {
  49.                 serialPort1.Close();
  50.             }
  51.             catch(Exception hata)
  52.             {
  53.                 MessageBox.Show(hata.Message);
  54.             }
  55.         }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement