Communication Technique สรไกร ไกรปุย หน่วยวิจัยโลจิสติกส์และวิศวกรรมสมองกลฝังตัว มหาวิทยาลัยบูรพา
Prerequisites ★ TCP/IP Concept (Data Communication and Networking) ★ Serial port Interfacing (Microprocessor and Microcontroller Interfacing) ★ Database Connection (Mysql, MicrosoftSQL, PostgreSQL etc) ★ Web Application (PHP, Javascript etc.)
❖ Part I: ✦ TCP/IP Concept (socket programming) ✦ Serial Communication ✦ Database Connection ✦ Multithreading Concept ❖ Part II: ✦ TCP/IP Concept (socket programming) ✦ Serial Communication ✦ Database Connection ✦ Multithreading Concept Contents
❖ Part I: ✦ TCP/IP Concept (socket programming) ✦ Serial Communication ✦ Database Connection ✦ Multithreading Concept ❖ Part II: ✦ System Architecture ✦ C/C++ Language Programming ✦ JAVA Language Programming ✦ Visual C# Language Programming Contents
❖ Part III: ✦ PHP Language Programming ✦ Ajax Technique Contents
Part I Theories and Concepts
TCP/IP Protocol Stack Packet Format
IP ICMP, IGMP ARP TCPUDP Socket FTP, Telnet, etc. NetBIOS Ethernet, Token Ring, etc Port Address
Embedded TCP/IP – Application Areas
Serial Port Connector Serial port คือ เป็นส่วนที่ใช้ในการเชื่อมต่อกับอุปกรณ์ภายนอกแบบอนุกรมตาม มาตรฐานการเชื่อมต่อข้อมูลแบบ RS232 จำกัดความยาวสายไว้ที่ 50 ฟุต ( หรือ ประมาณ 15 เมตร )
Serial Port Configuration Windows-based - COM1, COM2, COM3,… Linux-based (character device) - /dev/ttyS0, /devttyS1,… - /dev/ttyUSB0, /dev/ttyUSB1.... Baud rate ความเร็วในการสื่อสารบิตต่อวินาที ex. 9600, 14400, 19200, Data bit จำนวนบิตใน 1 character Parity bit บิตสำหรับการเช็ค error ของการส่งข้อมูล Stop bit บิตสำหรับ detect end of character Flow control ชนิดการควบคุมการรับ - ส่งข้อมูล
Well-known Database Server Database Server ที่นิยมได้แก่ - Oracle - DB2 (IBM) - Ms SQL Server - MySQL - PostgreSQL - SYBASE Simplified database system environment
DBMS and Database Keywords Database Management System Defining a particular database in terms of its data types, structures, and constraints Constructing or Loading the initial database contents on a secondary storage medium Manipulating the database: Retrieval: Querying, generating reports Modification: Insertions, deletions and updates to its content Accessing the database through Web applications Processing and Sharing by a set of concurrent users and application programs – yet, keeping all data valid and consistent Basics keywords tablefieldrow, columnrecordsetselect, insert, delete, updateprimary key
ฐานข้อมูล (Database) กลุ่มของข้อมูลที่ถูกเก็บรวบรวมไว้ในที่เดียวกันอย่างเป็นระบบ เพื่อให้สามารถ ค้นหา แก้ไข ลบ เพิ่มเติมได้อย่างมีประสิทธิภาพ เทเบิล (Table) เป็นส่วนที่เก็บข้อมูลที่เกี่ยวกับสิ่งใดสิ่งหนึ่งไว้ เรคอร์ด (Record) และ ฟิลด์ (Field) Table จะแบ่งออกเป็น Row และ Column โดยแต่ละแถวคือข้อมูล 1 รายการ หรือ 1 record, ข้อมูลแต่ละส่วนของ record จะถูกแยกเก็บไว้ใน Column ต่างๆ ซึ่งเรียกว่า Field
คีย์หลัก (Primary Key) เป็น Field ที่ใช้แยกแยะ record ต่างๆ ใน Table นั้น เรามักกำหนดให้ Field หนึ่ง ( หรือมากกว่าก็ได้ ) ทำหน้าที่เป็น primary key เพื่อให้ มั่นใจว่าสามารถบ่งชี้ (identify) ได้ว่า record ใดเป็น record ใด ค่า primary key แต่ละ record ห้ามซ้ำกัน คีย์นอก (Foreign Key) เป็น Field ที่เก็บค่า field ที่เป็น Primary Key ของ table อื่นไว้ ใช้เพื่อแสดงการเชื่อมโยงระหว่าง record ใน table ทั้งสอง Field ที่เป็น foreign key สามารถมีซ้ำกันได้ใน record ต่างๆ Schema คือโครงสร้างของ Table ทั้งหมดในฐานข้อมูลนั้นๆ โดยทั่วไปแสดง Table name, Field, Primary Key, Foreign Key
SQL Command INSERT INTO Table1 VALUES(value1,value2,…) SELECT Column1, Column2 FROM Table1 UPDATE Table1 SET Column1 = Value1, Column2 = Value2 WHERE Some_Column = Some_Value DELETE FROM Table1 WHERE Some_Column = Some_Value DDL (Data Definition Language) คำสั่งที่ใช้ในการเตรียมที่เก็บข้อมูล คำสั่งสร้าง / แก้ไข / ลบ - ฐานข้อมูล, Table, Key ฯลฯ DML(Data Manipulation Language) คำสั่งที่ใช้ในการทำงานกับข้อมูล คำสั่งกลุ่ม Insert, Update, Delete, Retrieve(Select)
Multithreading concept Thread คือ หน่วยการทำงานย่อยที่อยู่ใน process (light weight process) Multithreading คือการทำงานของหลายๆ thread ใน 1 process
Thread Libraries - POSIX Thread (pthread) A POSIX standard (IEEE c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) - WIN32 Thread The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) - Java Thread Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface
Part II Programming Techniques
Programming Techniques 3 องค์ประกอบหลัก ในการพัฒนาระบบเพื่อเฝ้าติดตามและแจ้งเตือนของระบบ (Monitoring and Alert System) ส่วนที่ 1 : ( ภายใน Production Line) ✴ การติดต่อระหว่าง STM32 Board และ เครื่อง WorkStation ผ่านพอร์ตอนุกรม RS232 ส่วนที่ 2 : ( การเชื่อมต่อกับระบบฐานข้อมูลกลาง ) ✴ การติดต่อระหว่างโปรแกรมภายในเครื่อง WorkStation และ MySQL Database Server ส่วนที่ 3 : ( การใช้เทคนิค Multi-threading ในโลกความเป็นจริง ) ✴ การใช้เทคนิค Threading เพื่อรับค่าผ่าน Serial Port ที่ถูกเชื่อมต่ออยู่กับ STM32 Board
C/C++ Language Programming Compiler & Library - gcc compiler (current version 4.4.5) - libmysqlclient-dev - libpthread Installation library (linux ubuntu) - sudo apt-get install libmysqlclient-dev
C/C++ serial port Open & initialize
C/C++ serial port Read data from serial port
C/C++ Database Connect to MySQL
C/C++ Database Insert data to MySQL
C/C++ compile & run Command (on linux) compile - gcc myapp.c –o myapp –lpthread –lmysqlclient run -./myapp
Java Language Complier and Library - IDE ที่นิยม Eclipse, Netbeans - JDK 1.5+ (current version 1.6) - Serial port library (RXTXcomm) - MySQL Connector (recommend 5.0.8)
Java Installation Installation of rxtxSerial copy rxtxSerial.dll to [JDKdirectory]\jre\bin\rxtxSerial.dll copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar Installation of mysqlconnector copy mysql-connector-java bin.jar to [JDK-directory]\jre\lib\ext\ mysql-connector-java bin.jar
Java code serial port Identify port name
Java code serial port Initialize serial port
Java code serial port Read data from serial port
Java code Database Connect to MySQL
Java code Database Insert data to MySQL
Java compile & run Compile & Run command javac MyClass.java java MyClass Eclipse IDE press ctrl + F11
.NET Language Compiler and Library -.NET Framework 2.0+(current version3.5) - MySQL Connector (recommend 5.0.9)
.NET Installation Install mysql connector/net Create a new project Add reference to: MySql.Data Add “using MySql.Data.MySqlClient;”
.NET Code serial port Port setting
.NET Code serial port Read data from serial port by Event
.NET Code Database Open connection & insert data
.NET Compile & run Compile & run - press F6 (build) - press F5 (build & run)
Part III Web Application Techniques
Web-based Monitor Why Web-based monitoring ? - สะดวกต่อผู้ใช้ไม่ต้องติดตั้งโปรแกรมในเครื่อง - สะดวกต่อผู้ดูแลระบบในการแก้ไขปรับปรุง - สามารถเฝ้าดูได้จากทุกที่ ที่มี internet - ค่าใช้จ่ายและค่าบำรุงรักษาต่ำลง
PHP & MySQL ขั้นตอนการแสดงผลทาง web browser - request to Web server - PHP connect & query database - response to web browser
PHP Code Connect & query from MySQL
Ajax technique Ajax ( Asynchronous JavaScript And XML) คือ เทคนิค การพัฒนา web application โดยทำงานอยู่เบื้องหลัง เพื่อให้ตอบสนองต่อผู้ใช้ได้ดียิ่งขึ้น
Ajax Technique