คัมภีร์การใช้งานภาษา c# : ควบคุม ......visual c#...

Post on 05-Feb-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

คัมภีรการใชงานภาษา C# : ควบคุมการทํางาน Microcontroller, PLC และ IoT

C# : Microcontroller, PLC IoT

Internet of Things (IoT) “ ”

Smart

(Smart Device), (Smart Grid), (Smart Home),

(Smart Network), (Smart Intelligent Transportation)

-

C# Delphi, C++, C++.NET,

VB.NET Java C# Arduino,

C# , C# ,

C# (PLC) C#

IT

“ 4.0” (Industry 4.0)

.

- -

dechritk007@gmail.com

C# : Microcontroller, PLC IoT v

III

1 C# 1

1.1 4

1.1.1 (Data Types) 4

1.1.2 (Variables) 4

1.2 (Operators) 5

1.2.1 (Arithmetic Operators) 6

1.2.2 (Logical Operators) 7

1.2.3 (Compound Assignment Operators) 8

1.2.4 (Comparison) 9

1.3 C# 9

1.3.1 10

1.3.2 C# 12

1.3.3 C# 13

1.4 C# GUI 19

53

54

2 C# Arduino 55

2.1 Arduino IDE 57

2.2 C# Arduino 60

105

106

vi C# : Microcontroller, PLC IoT

3 C# 107

3.1 110

3.1.1 (Temperature Sensor) 110

3.1.2 (Ultrasonic Sensor) 111

3.1.3 (Compass Sensor) 112

3.1.4 (Gyro Sensor) 113

3.1.5 (RFID Sensor) 114

3.1.6 (GPS Sensor) 115

3.1.7 (Webcam Sensor) 116

3.2 C# 117

159

162

4 C# 163

4.1 (Direct Current Motor) 165

4.1.1 166

4.1.2 168

4.2 C# 170

4.3 C# ,

172

216

217

5 C# PLC 219

5.1 ( PLC BECKHOFF ) 221

5.2 C# TwinCAT 223

273

C# : Microcontroller, PLC IoT vii

274

6 C# 275

6.1 C# IoT (Internet of Thing) 278

6.1.1 HTML, CSS JavaScript IoT

285

6.1.2 286

6.1.3 288

6.2 C# (Closed Loop Control) 301

6.2.1 (Proportional Control) 301

6.2.2 (Integral Control) 302

6.2.3 (Derivative Control) 302

6.2.4 PI 303

6.2.5 PD 304

6.2.6 PID 306

6.3 MATLAB 318

6.3.1 Arduino MATLAB 318

337

338

339

1 C#

1.1

1.2 (Operators)

1.3 C#

1.4 C# GUI

บทที่

1การใชภาษา C#

1 C#

.NET Framework

C# C Sharp Delphi, C++

(Object Oriented)

C++ Java

C# Visual C#

Visual Basic Visual C#

Visual C#

C# Java

C# Java

1. C# Java C#

C++.NET C#

Java, C++.NET VB.NET

2. C# Java Garbage Collection

C++ 2 C++

C# Java C#

Reference, Output Object Stack

Versioning Delegate, Properties Operator Overloading

Java

C#

C# : Microcontroller, PLC IoT 1

3. C# Java (Class) C++

1 (Multiple Inheritance) C# Java

(Interface) Multiple Inheritance 2

1.1

1.1.1 (Data Types)

C#

C# (Primitive data type)

4

char string

int long

long int

float double

true

false

C# (Object and

Array Methods) (Library)

1 C#

Byte byte 8 bit 0 255

Char char 16 bit U +0000 U +ffff

SByte sbyte 8 bit -128 127

Int16 short 16 bit 32,768 32,767

Int32 int 32 bit -2,147,483,648 2,147,483,647

Int64 long 64 bit -9,223,372,036,854,775,808

9,223,372,036,854,775,807

Single float 32 bit 3.40282E38 3.40282E38

Double double 64 bit -1.79769E308 1.79769E308

Boolean boolean true, false true, false

String string , text Multiple characters

UInt16 ushort 16 bit 0 65,535

UInt32 unsigned int 32 bit 0 4,294,967,295

UInt64 unsigned long 64 bit 0 18,446,744,073,709,551,615

1.1.2 (Variables)

(Variable)

C#

Boolean, integer, floating, point, string, pointer

C# : Microcontroller, PLC IoT 1

myString ="Hello World";

// string myString Hello World

myInt =1;

// string myInt 1

myDouble =1.0;

// double myDouble 1.0

,

myString1="test1",myString2="test2",myString3="test3",...

C# (case sensitive)

1.2 (Operators)

C#

4

1. (Arithmetic operators)

2. (Logical operators)

3. (Compound assignment operators)

4. (Comparison)

1 C#

1.2.1 (Arithmetic Operators)

C#

1.2

+

-

*

/

%

c=a+b

c=a-b

c=a*b

c=a/b

c=a%b

1.2.2 (Logical Operators)

C#

Boolean

!

&&

||

NOT

AND

OR

!(n==1)

a==1&& b==1

a==1|| b==1

C# : Microcontroller, PLC IoT 1

1.2.3 (Compound Assignment Operators)

C#

1.4

+=

-=

*=

/=

%=

>>=

<<=

&=

^=

|=

++

++

--

--

a +=2

a -=2

a *=2

a /=2

a %=2

a >>=2

a <<=2

a &=2

a ^=2

a |=2

++a

a++

--a

a--

a = a+2

a = a-2

a = a*2

a = a/2

a = a%2

a = a>>2

a = a<<2

a = a&2

a = a^2

a = a|2

a = a+1

a = a+1

a = a-1

a = a-1

1 C#

1.2.4 (Comparison)

C#

C# if else,

switch, while, do-while, for

==

!=

<

>

<=

>=

~

a==b

a!=b

a<b

a>b

a<=b

a>=b

a~b

1.3 C#

C# Microsoft Visual C# 2015 Express C++

Java C#

C# namespace, class, method, property,

statement

C# : Microcontroller, PLC IoT 1

1.3.1

C# C#

C# Visual Studio

1. using

using include (Library) C#

Using System include

System namespace

2. Main

Main () C# Main

void, int void

return

3. namespace

namespace C# namespace

Hello, namespace myprogram C# namespace

4. Class Program

Class Program (Template)

(Object) (Attribute)

(Method) Class Program program

5. Comment

Comment C# 2

(1) //

1 C#

// LED ON LED

// LED OFF LED

(2) /* ……………*/

/* print text to the screen

Console.WriteLine(“Hello World”); */

6. Keyword

Keyword C#

C#

abstract as base bool break

byte case catch char checked

class const continue decimal default

delegate do double else enum

event explicit extern false finally

fixed float for foreach goto

if implicit in int interface

internal is lock long namespace

new null object operator out

override params private protected public

readonly ref return sbyte sealed

short sizeof stackalloc static string

struct switch this throw true

try typeof uint ulong unchecked

unsafe ushort using virtual void

volatile while

C# : Microcontroller, PLC IoT 1

1.3.2 C#

C#

C# namespace, class, method, property, statement

C#

using System;

using ……….

Namespace ConsoleApplocation1

{

Class Program

{

Static void Main(string [] args)

{

Console.WriteLine(“Hello World”);

;

…….

…….

}

}

}

(namespace)

(namespace)

(class)

(method)

1 C#

1.3.3 C#

C# Microsoft Visual C# 2015

(Software) (C# Language Software) (C

Language Software) (Visual Basic Software) C#

Visual Studio Community 2015 Files > Project

Project Next

C# : Microcontroller, PLC IoT 1

Visual C# > Windows Form Application Project

name: Project Location: Project

Visual C# > Windows Form Application Project IDE

(Integrated Development Environment)

Build, Debug

1 C#

IDE

Project

(ToolBox) UI (User

Interface)

Solution Explorer Project

(Properties)

UI

Button, Label TextBox

1 (Button) Button Form1

2 (Button Drag & Drop) 3 ( Label)

Label

1 (Button) 2 Properties > Text

Button1 START

C# : Microcontroller, PLC IoT 1

START Form1 Properties > BackColor > Custom

3 4

Form1 C#

namespace, class, Form1 ()

1 C#

string str (string str) START_Click()

Label1

Build > Rebuild Solution Build

Rebuild all: 1 Succeeded, 0 failed, 0 skipped

1

Build

C# : Microcontroller, PLC IoT 1

START START Form1 Label1 “Hello”

Error

Form1

Error 54 Build

Error

1 C#

1.4 C# GUI

C# C#

GUI (Graphical User Interface) Windows Form

C# C C++

C#

(Software Engineering)

Hello World Form1()

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 namespace example111 {12 public partial class Form1 : Form13 {14 public Form1()15 {16 InitializeComponent();17 }18 private void Form1_Load(object sender, EventArgs e)19 {20 string str = "Hello World";21 string str1 = "Hello C#";22 label1.Text = str;23 label2.Text = str1;24 }25 }26 }

C# : Microcontroller, PLC IoT 1

( ) ( ) Build

Hello World Form1()

1-9 namespace

10 Project

12

18-24 Hello World

1 C#

Label Button

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 namespace example211 {12 public partial class Form1 : Form13 {14 public Form1()15 {16 InitializeComponent();17 }18 private void Form1_Load(object sender, EventArgs e)19 {20 string str = "Hello World";21 label1.Text = str;22 }23 private void start_Click(object sender, EventArgs e)24 {25 string str1 = " Hello C#";26 label2.Text = str1;27 }28 }29 }

C# : Microcontroller, PLC IoT 1

( ) ( ) Build

Label Button

1-9 namespace

10 Project

12

18-22 Form1 string str Hello World Label1

23-27 start Hello C# Label2

1 C#

Label TextBox Button

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 namespace example311 {12 public partial class Form1 : Form13 {14 public Form1()15 {16 InitializeComponent();17 }18 private void Form1_Load(object sender, EventArgs e)19 {20 string str = "Hello World";21 label1.Text = str;22 }23 private void start_Click(object sender, EventArgs e)24 {25 textBox1.Text = "Hello C#";26 }27 }28 }

C# : Microcontroller, PLC IoT 1

( ) ( ) Build

Label TextBox Button

1-9 namespace

10 Project

12

18-22 Form1 string str Hello World Label1

23-26 start Hello C# TextBox1

1 C#

Radiobutton Men Women Button

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 namespace example411 {12 public partial class Form1 : Form13 {14 public Form1()15 {16 InitializeComponent();17 }18 private void start_Click(object sender, EventArgs e)19 {20 if (radioButton1.Checked == true)21 {22 MessageBox.Show("You are men");23 label1.Text = "BOY";24 }25 if (radioButton2.Checked == true)26 {27 MessageBox.Show("You are women");28 label1.Text = "GIRL";29 }30 }31 private void Form1_Load(object sender, EventArgs e)32 {3334 }35 }36 }

C# : Microcontroller, PLC IoT 1

( ) ( ) Build

Radiobutton Button

1-9 namespace

10 Project

12

20-24 Radiobutton1 MessageBox Label1

25-29 Radiobutton2 MessageBox Label1

1 C#

Button Textbox

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 namespace example511 {12 public partial class Number1 : Form13 {14 public Number1()15 {16 InitializeComponent();17 }18 private void button1_Click(object sender, EventArgs e)19 {20 if (textBox1.Text =="0" && textBox1.Text !=null)21 {22 textBox1.Text = "1";23 }24 else25 {26 textBox1.Text = textBox1.Text + "1";27 }28 }29 private void botton2_Click(object sender, EventArgs e)30 {31 if (textBox1.Text == "0" && textBox1.Text != null)32 {33 textBox1.Text = "2";34 }35 else36 {37 textBox1.Text = textBox1.Text + "2";38 }

C# : Microcontroller, PLC IoT 1

39 }40 private void botton3_Click(object sender, EventArgs e)41 {42 if (textBox1.Text == "0" && textBox1.Text != null)43 {44 textBox1.Text = "3";45 }46 else47 {48 textBox1.Text = textBox1.Text + "3";49 }50 }51 private void button4_Click(object sender, EventArgs e)52 {53 if (textBox1.Text == "0" && textBox1.Text != null)54 {55 textBox1.Text = "4";56 }57 else58 {59 textBox1.Text = textBox1.Text + "4";60 }61 }62 private void button5_Click(object sender, EventArgs e)63 {64 if (textBox1.Text == "0" && textBox1.Text != null)65 {66 textBox1.Text = "5";67 }68 else69 {70 textBox1.Text = textBox1.Text + "5";71 }72 }73 private void button6_Click(object sender, EventArgs e)74 {75 if (textBox1.Text == "0" && textBox1.Text != null)76 {77 textBox1.Text = "6";78 }79 else

1 C#

Project

80 {81 textBox1.Text = textBox1.Text + "6";82 }83 }84 private void button7_Click(object sender, EventArgs e)85 {86 if (textBox1.Text == "0" && textBox1.Text != null)87 {88 textBox1.Text = "7";89 }90 else91 {92 textBox1.Text = textBox1.Text + "7";93 }94 }95 private void button8_Click(object sender, EventArgs e)96 {97 if (textBox1.Text == "0" && textBox1.Text != null)98 {99 textBox1.Text = "8";100 }101 else102 {103 textBox1.Text = textBox1.Text + "8";104 }105 }106 private void button9_Click(object sender, EventArgs e)107 {108 if (textBox1.Text == "0" && textBox1.Text != null)109 {110 textBox1.Text = "9";111 }112 else113 {114 textBox1.Text = textBox1.Text + "9";115 }116 }117 private void button10_Click(object sender, EventArgs e)118 {119 textBox1.Text = textBox1.Text + "0";120 }

C# : Microcontroller, PLC IoT 1

( ) ( ) Build

Button Textbox

1-9 namespace

10 Project

12

18-28 button1 TextBox 1

29-39 button2 TextBox 2

40-50 button3 TextBox 3

51-61 button4 TextBox 4

121 private void Number1_Load(object sender, EventArgs e)122 {123 }124 }125 }

1 C#

62-72 button5 TextBox 5

73-83 button6 TextBox 6

84-94 button7 TextBox 7

95-105 button8 TextBox 8

106-116 button9 TextBox 9

117-120 button10 TextBox 0

top related