반응형

// Avr에서 전달 받은 데이터를 이용하여 C#으로 그래프를 그려 보았다.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 SonarGPS
{
    public partial class Form1 : Form
    {
        
        int pointX = 0, pointY = 0, pointSizeX = 20, pointSizeY = 20;
        int boardSize_XY = 340;
        int line1_XY = 0, line2_XY = 0, line3_XY = 0;
 
        private SerialPort SerialPort = new SerialPort();  //시리얼 포트 생성
 
        public Form1()
        {          
            InitializeComponent();
            SerialPort.PortName = "COM3";
            SerialPort.BaudRate = 115200;
            SerialPort.DtrEnable = true;
            SerialPort.Open();
 
        }
 
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
            int Sensor1_x = 0, Sensor1_y = 0, Sensor_sizeX = 20, Sensor_sizeY = 20;
            int Sensor2_x = 0, Sensor2_y = boardSize_XY - Sensor_sizeY;
            int Sensor3_x = boardSize_XY - Sensor_sizeX, Sensor3_y = boardSize_XY - Sensor_sizeY;
 
            // Sensor png 출력
            Image Sensor1 = Bitmap.FromFile("C:\\Sensor.png");
            e.Graphics.DrawImage(Sensor1, Sensor1_x, Sensor1_y, Sensor_sizeX, Sensor_sizeY);
 
            Image Sensor2 = Bitmap.FromFile("C:\\Sensor.png");
            e.Graphics.DrawImage(Sensor2, Sensor2_x, Sensor2_y, Sensor_sizeX, Sensor_sizeY);
 
            Image Sensor3 = Bitmap.FromFile("C:\\Sensor.png");
            e.Graphics.DrawImage(Sensor3, Sensor3_x, Sensor3_y, Sensor_sizeX, Sensor_sizeY);
 
 
            // x,y좌표로 각 센서에서 해당 좌표까지 선 그리기 
            Pen pen1 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(pen1, 2020, line1_XY, line1_XY);
 
            Pen pen2 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(pen2, 20, boardSize_XY - 20, line2_XY, boardSize_XY - line2_XY);
 
            Pen pen3 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(pen3, boardSize_XY - 20, boardSize_XY - 20, boardSize_XY - line3_XY, boardSize_XY - line3_XY);
 
            // draw point
            SolidBrush Point = new SolidBrush(Color.FromArgb(255000));
            e.Graphics.FillEllipse(Point, pointX - pointSizeX / 2, pointY - pointSizeY / 2, pointSizeX, pointSizeY);
 
            // draw angle
            Pen angle1 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(angle1, line1_XY, line1_XY, line2_XY, boardSize_XY - line2_XY);
            Pen angle2 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(angle2, line2_XY, boardSize_XY - line2_XY, boardSize_XY - line3_XY, boardSize_XY - line3_XY);
            Pen angle3 = new Pen(Color.FromArgb(255000));
            e.Graphics.DrawLine(angle3, boardSize_XY - line3_XY, boardSize_XY - line3_XY, line1_XY, line1_XY);
 
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
 
            // 문자열 잘라서 Sensor 값 출력
            string str = SerialPort.ReadLine();
            string[] sensor = str.Split(',');
 
            // senser data strint to double
            double Intsensor1 = Convert.ToDouble(sensor[0]);
            double Intsensor2 = Convert.ToDouble(sensor[1]);
            double Intsensor3 = Convert.ToDouble(sensor[2]);
 
            // Sensor data limit 0 ~ 150
            if ((Intsensor1 > 150& (Intsensor1 < 10000)) { sensor[0= "150"; Intsensor1 = 150; }
            else if (Intsensor1 >= 10000) { sensor[0= "0"; Intsensor1 = 0; }
            if ((Intsensor2 > 150& (Intsensor2 < 10000)) { sensor[1= "150"; Intsensor2 = 150; }
            else if (Intsensor2 >= 10000) { sensor[1= "0"; Intsensor2 = 0; }
            if ((Intsensor3 > 150& (Intsensor3 < 10000)) { sensor[2= "150"; Intsensor3 = 150; }
            else if (Intsensor3 >= 10000) { sensor[2= "0"; Intsensor3 = 0; }
 
            // testbox sensor 값 출력
 
            textBox13.Text = sensor[0];
            textBox14.Text = sensor[1];
            textBox15.Text = sensor[2];
 
            // sensor값으로 x,y좌표 구하기
            // int data = Convert.ToInt32((boardSize_XY / 2 + 10) / 1.4142135623731); //max 150일 때의 좌표
            line1_XY = Convert.ToInt32(Intsensor1 + 20 / 1.4142135623731);
 
            if (line1_XY <= 7)
            {
                line1_XY = (Convert.ToInt32(Intsensor1) + 20);
            }
 
            line2_XY = Convert.ToInt32(Intsensor2 + 20 / 1.4142135623731);
            if (line2_XY <= 7)
            {
                line2_XY = Convert.ToInt32(Intsensor2);
            }
 
            line3_XY = Convert.ToInt32(Intsensor3 + 20 / 1.4142135623731);
            if (line3_XY <= 7)
            {
                line3_XY = Convert.ToInt32(Intsensor3);
            }
            // Point x,y 좌표 data
            textBox1.Text = Convert.ToString((pointX - pointSizeX) + 2);
            textBox2.Text = Convert.ToString(300 - ((pointY - pointSizeY) - 2));
 
            // Point 좌표
            pointX = (line1_XY + line2_XY + (boardSize_XY - line3_XY)) / 3;
            pointY = (line1_XY + (boardSize_XY - line2_XY) + (boardSize_XY - line3_XY)) / 3;
            pictureBox1.Invalidate(); // 화면 갱신
            
        }
    }
}
 
cs



Sonar_GPS.zip


반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

jkit-128-1 추가 기능 구현  (0) 2019.01.22
[j-kit-128] jkit 128 기능구현하기  (0) 2018.12.10
Sonar_GPS  (0) 2018.10.11
Serial 통신_ 문자열 전송  (0) 2018.10.01
Serial 통신_Uart0_문자 1개  (0) 2018.10.01

+ Recent posts