# 참고동영상
https://www.youtube.com/watch?v=4yDbBJLhVso

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 System.IO.Ports;

namespace ComPort
{
    public partial class Form1 : Form
    {
        string dataOUT;
        string dataIN;

        public Form1()
        {
            InitializeComponent();            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();            
            cBoxCOMPORT.Items.AddRange(ports);          
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = cBoxCOMPORT.Text;
                serialPort1.BaudRate = Convert.ToInt32(cBoxBaudRate.Text);
                serialPort1.DataBits = Convert.ToInt32(cBoxDataBits.Text);
                serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
                serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cBoxParityBits.Text);
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

                serialPort1.Open();
                progressBar1.Value = 100;
            }

            catch (Exception err)
            {
                MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCLose_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen)
            {
                serialPort1.Close();
                progressBar1.Value = 0;
            }
        }

        private void btnSendData_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                dataOUT = tBoxDataOut.Text;
                serialPort1.Write(dataOUT);                
            }
        }
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {            
            dataIN = serialPort1.ReadExisting();            
            this.Invoke(new EventHandler(ShowData));
        }
        private void ShowData(object sender, EventArgs e)
        {
            tBoxDataIn.Text += dataIN;
        }
    }
}

# 참고동영상
https://www.youtube.com/watch?v=wsMMHaSvvRU

 

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class Form1
    Dim myPort As Array
    Delegate Sub SetTextCallBack(ByVal [text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        Port_ComboBox.Items.AddRange(myPort)
        Write_Button.Enabled = False
    End Sub

    Private Sub init_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles init_Button.Click
        SerialPort1.PortName = Port_ComboBox.Text
        SerialPort1.BaudRate = Board_ComboBox.Text
        SerialPort1.Open()

        init_Button.Enabled = False
        Write_Button.Enabled = True
        Close_Button.Enabled = True
    End Sub

    
    Private Sub Write_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write_Button.Click
        SerialPort1.Write(Input_TextBox.Text & vbCr)

    End Sub

    Private Sub Close_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Close_Button.Click

        SerialPort1.Close()
        init_Button.Enabled = True
        Write_Button.Enabled = False
        Close_Button.Enabled = False
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())

    End Sub
    Private Sub ReceivedText(ByVal text As String)
        If Me.Output_TextBox.InvokeRequired Then
            Dim x As New SetTextCallBack(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.Output_TextBox.Text &= text
            Me.Label3.Text = text
        End If
    End Sub

End Class

# 참고 동영상
https://www.youtube.com/watch?v=8WhQATRycTU

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 RADAR1
{
    public partial class Form1 : Form
    {
        Timer t = new Timer();

        int width = 300, height = 300, Hand = 150;

        int u;
        int cx, cy;
        int x, y;

        int tx, ty, lim = 20;

        Bitmap bmp;
        Pen p;
        Graphics g;

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
                        //create Bitmap
            bmp = new Bitmap(width + 1, height + 1);

            //background color
            this.BackColor = Color.Black;

            //center
            cx = width / 2;
            cy = height / 2;

            //initial degree of HAND
            u = 0;

            //timer            
            t.Interval = 5; //in millisecond
            t.Tick += new EventHandler(this.t_TICK);
            t.Start();
        }
        private void t_TICK(object sender, EventArgs e)
        {
            //pen
            p = new Pen(Color.Green, 1f);

            //graphics
            g = Graphics.FromImage(bmp);

            //calculate x, y coordinate of HAND
            int tu = (u - lim) % 360;

            if (u >= 0 && u <= 180)
            {
                //right half
                //u in degree is converted into radian.

                x = cx + (int)(Hand * Math.Sin(Math.PI * u / 180));
                y = cy - (int)(Hand * Math.Cos(Math.PI * u / 180));
            }
            else
            {
                x = cx - (int)(Hand * -Math.Sin(Math.PI * u / 180));
                y = cy - (int)(Hand * Math.Cos(Math.PI * u / 180));
            }


            if (tu >= 0 && u <= 180)
            {
                tx = cx + (int)(Hand * Math.Sin(Math.PI * tu / 180));
                ty = cy - (int)(Hand * Math.Cos(Math.PI * tu / 180));
            }
            else
            {
                tx = cx - (int)(Hand * -Math.Sin(Math.PI * tu / 180));
                ty = cy - (int)(Hand * Math.Cos(Math.PI * tu / 180));
            }

            // draw circle
            g.DrawEllipse(p, 0, 0, width, height); //bigger circle
            g.DrawEllipse(p, 80, 80, width - 160, height - 160); //smaller circle

            // draw perpendicular line
            g.DrawLine(p, new Point(cx, 0), new Point(cx, height)); // UP-DOWN
            g.DrawLine(p, new Point(cy, 0), new Point(cy, width)); //LEFT-RIGHT

            //draw HAND
            g.DrawLine(new Pen(Color.Black, 1f), new Point(cx, cy), new Point(tx, ty));
            g.DrawLine(p, new Point(cx, cy), new Point(x, y));

            // load bitmap in picturebox1
            pictureBox1.Image = bmp;

            // dispose
            p.Dispose();
            g.Dispose();

            //update
            u++;
            if (u == 360)
                u = 0;
        }
    }
}

+ Recent posts