รับ-ส่งข้อมูลผ่าน com port ด้วย java
การส่งข้อมูลผ่าน com port ด้วย java ก่อนอื่นต้องไป download library "RXTXcomm" จากเว็บ //rxtx.qbang.org/wiki/index.php/Download แล้วแตกไฟล์ไปที่ Drive C: (หรือ drive อื่นก็ตามสะดวก) จากนั้นให้ทำขั้นตอนต่อไปนี้
  1. copy file "C:/rxtx-2.1-7-bins-r2/Windows/i368-mingw32/*.dll" ไปเก็บที่ home directory ของ java ยกตัวอย่าง "C:/Program Files/Java/jdk1.7.0_02/bin/"
  2. copy file "C:/rxtx-2.1-7-bins-r2/RXTXComm.jar" เก็บไว้ที่ ext lib ของ java ยกตัวอย่าง  "C:/Program Files/Java/jre7/lib/ext/"
  3. boot เครื่องใหม่
  4. เปิดโปรแกรม Netbeans
  5. สร้าง Project ชื่อ TestComPort
  6. คลิ้กขวาที่ชื่อ Project -> Properties เลือกหัวข้อ Libraries แล้วกดปุ่ม "Add JAR/Folder" เลือกไฟล์ .jar ชื่อ "RXTXComm.jar" ตามรูป

จากนั้นเริ่มเขียน code ตามนี้

package testcomport;

import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class TestComPort {

    private InputStream in;
    private OutputStream out;

    public TestComPort() {
    }

    public void run() {
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1"); // ใส่ชื่อ com port ที่ต้องการใช้งาน

            if (portIdentifier.isCurrentlyOwned()) {
                JOptionPane.showMessageDialog(null, "Error: Port COM1  is currently in use", "Notice", JOptionPane.INFORMATION_MESSAGE);

            } else {
                CommPort commPort = portIdentifier(this.getClass().getName(), 2000);

                if (commPort instanceof SerialPort) {
                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // ตรงนี้ต้อง set ให้ตรงกับ com port ที่ติดต่อด้วยที่ความเร็ว และ databits (บางอุปกรณ์เป็น 7)

                    out = serialPort.getOutputStream();
                    in = serialPort.getInputStream();byte[] hello = {'H', 'E', 'L', 'L', 'O', 0x0D, 0x0A};
                    out.write(hello);
                    out.flush(); // ส่ง string "HELLO" ออกไปยัง com port 0x0D, 0x0aA คือตัว Enter หรือ "n" (บางอุปกรณ์ต้องสลับกัน)

                    in = serialPort.getInputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;

                    while ((len = in.read(buffer)) > -1) {
                        System.out.println(new String(buffer, 0, len)); // พิมพ์ค่าที่อ่านจาก com port
                    }

                    in.close();

                } else {
                    JOptionPane.showMessageDialog(null, "Error: Only serial ports COM1 are handled by this example.", "Notice", JOptionPane.INFORMATION_MESSAGE);
                }

                commPort.close();
            }
            /**
             * @param args the command line arguments
             */
        } catch (IOException | UnsupportedCommOperationException | PortInUseException | NoSuchPortException ex) {
            Logger.getLogger(TestComPort.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestComPort prog = new TestComPort();
        prog.run();
    }
}


เมื่อใส่ code เสร็จให้ต่ออุปกรณ์ที่ com port แล้วกด F6 เพื่อ Run โปรแกรมผลจะอยู่ที่ out put pane ด้านล่าง




Create Date : 31 สิงหาคม 2555
Last Update : 31 สิงหาคม 2555 13:23:07 น.
Counter : 2743 Pageviews.

2 comment
ลองศึกษาเรื่อง jQuery
jQuery ตอนแรกป็นอะไรที่รู้สึกว่าไม่จำเป็นต้องใช้เพราะว่าคิดว่าเขียน javascript เป็นอยู่แล้วจะทำอะไรอย่างที่ jQuery มันก็ได้อยู่แล้ว แต่พอตอนหลังมีพัฒนาโปรแกรมบนเว็บมากขึ้นชักขี้เกียจเขียน javascript เองแล้ว ก็เลยมาลองศึกษาดูซะหน่อย

พอคิดได้อย่างนั้นแล้วก็ไปซื้อหนังสือเกี่ยวกับ jQuery มาเล่มนึงเป็นหนังสือแปล (ขอสงวนชื่อหนังสือ) พออ่านแล้วขำเล็กน้อยเพราะคนเขียนเล่นแปลแบบไม่รู้เรื่องเลย แต่ก็เข้าใจยังไงเล่มต่อไปก็พยายามให้ดีกว่านี้หน่อยแล้วกัน ผมก็แทบจะไม่ได้อ่านเลยดูแต่ตัวอย่าง แล้วก็มาเจอใน Tech-Exchange ของพันทิพที่เจอปัญหาว่าเขียนตามหนังสือแล้วใช้งานไม่ได้

ผมก็เลยลองเอา Code ที่อยู่ในหนังสือมาลองเขียนบ้างรายละเอียดอื่นๆกะว่าจะเขียนในอีก blog นึง เอาเป็นว่า code ในหนังสือเป็นประมาณนี้ (ผมตัดบางส่วนเอาไปเอาเฉพาะที่ต้องการอธิบาย)

$(document).ready(function(){
   $('a[href$=.com]').addClass('mailto');
});

สิ่งที่ต้องการใน code นี้คือให้ function ใน jQuery ไปหา tag ที่มีพารามิเตอร์ href ลงท้ายด้วย ".com" เมื่อได้แล้วก็ให้ตั้ง class เป็น "mailto" แต่ถ้าเขียนแบบนี้แล้วปรากฎว่าไม่ทำงาน ผมก็ลองไปค้นจากใน google ดูก็พบว่าส่วน 'a[href$=.com]' มันเป็นข้อความ (เพราะอยู่ใน single quote) ดังนั้นเครื่องหมาย "." ก็ไม่น่าจะใช้ได้ ผมก็เลยแก้ไข Code โดยเพิ่ม "\\" หน้า "." เป็นดังนี้


$(document).ready(function(){
    $('a[href$=\\.com]').addClass('mailto');
});

พอมา run ทดสอบก็ใช้งานได้ปกติครับ 



Create Date : 30 สิงหาคม 2555
Last Update : 30 สิงหาคม 2555 1:16:30 น.
Counter : 1003 Pageviews.

1 comment
การ Connect ฐานข้อมูล MySQL ด้วย java
การ Connection ฐานข้อมูล MySQL เร่ิมต้นจะต้อง download J/Connector มาก่อนโดยไปที่ link นี้ //dev.mysql.com/downloads/ เมื่อติดตั้งเรียบร้อยแล้วไฟล์จะเก็บอยู่ที่ "C:Program FilesMySQLMySQL Connector J" ไฟล์ชื่อ "mysql-connector-java-5.1.20-bin" (อาจจะเปลี่ยนตาม version ของ MySQL)

เมื่อได้ตัว Connector มาแล้วให้เปิดโปรแกรม Netbean สร้าง Project ชื่อ ConnectMySQL ที่ Project Panel ให้คลิ้กขวาที่ชื่อ Project แล้วเลือก Properties -> Libraries กดปุ่ม "Add JAR/Folder"



เลือกไฟล์ .jar ที่ download ไว้ตอนต้น "C:Program FilesMySQLMySQL Connector Jmysql-connector-java-5.1.20-bin.jar" จากนั้นกด OK เข้าหน้าจอพิพม์ Source





จากนั้นให้สร้าง Database ใน MySQL ชื่อ"testdb"   พิมพ์ Code ด้านล่าง แล้วกด F6 เพื่อ Run โปรแกรมทดสอบการทำงาน


package connectmysql;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nattaya
 */
public class ConnectMySQL {

    private Connection conn;
    private String user;
    private String password;

    public ConnectMySQL() {
        user = "root";
        password = "password";
    }

    public void run() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb", user, password);

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ConnectMySQL proj = new ConnectMySQL();
        proj.run();
    }
}


หมายเหตุ
รูปแบบของ url ที่ใช้ใน method getConnection() ดังนี้
jdbc:mysql://hostname/dbname

hostname คือชื่อของ MySQL Server ที่ต้องการ Connect
dbname คือชื่อของ Database







Create Date : 28 สิงหาคม 2555
Last Update : 28 สิงหาคม 2555 16:02:12 น.
Counter : 3874 Pageviews.

1 comment
สร้าง Frame ด้วย Netbeans
การเขียนโปรแกรมแบบ Windows application ด้วย Java จะเริ่มต้นด้วยการสร้าง Frame ขึ้นมาก่อน เพื่อเป็นที่รองรับของ Component อื่นๆในภายหลัง ในโปรแกรม Netbean มี tool ที่ช่วยให้เราสร้าง Frame ได้ง่ายขึ้น ไม่จำเป็นต้องเขียนด้วย code java ทั้งหมดทำให้ง่ายต่อการพัฒนาโปรแกรม

เริ่มต้นด้วยการเปิดโปรแกรม Netbeans สร้าง Project ใหม่ เมื่อขึ้นหน้าจอของ Project ในตัวอย่างนี้ใช้ชื่อ "FirstApp" แล้วให้คลิ้กขวาที่ชื่อ Package เลือก New->JFrame  Form ดังรูป




จากนั้นให้ใส่ชื่อของ Frame ในตัวอย่างนี้ใช้ชื่อ "MainFrame"



โปรแกรม Netbeans จะแสดง Class ของ JFrame ชื่อ "MainFrame" ดังรูป



จากนั้นมี 2 วิธีที่จะทำให้ Frame แสดงเมื่อ Run โปรแกรม แบบแรกคือ copy code ส่วน Main class ของ MainFrame.java มาแปะบน FirstApp.java โดยการคลิ้กปุ่ม "Source" ข้างๆปุ่ม "Design" หน้าจอ Design จะเปลี่ยนเป็นหน้าจอสำหรับพิมพ์ Source Code ตามรูป




ให้ copy code ส่วนนี้ไปแปะใน method main() ของ FirstApp.java จากนั้นทดสอบโดยการกดปุ่ม F6 เพื่อ Run โปรแกรม

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });



อีกวิธีให้คลิ้กขวาที่ชื่อ Project เลือก Properties เลือกหัวข้อ "Run" แล้วเปลี่ยน Main Class เป็น firstapp.MainFrame โดยเลือกจากการกดปุ่ม Browse



เมื่อแก้ไขเสร็จแล้วให้กดปุ่ม F6 เพื่อ Run โปรแกรม



Create Date : 23 สิงหาคม 2555
Last Update : 23 สิงหาคม 2555 15:41:37 น.
Counter : 2071 Pageviews.

0 comment
Hello World เริ่มต้นเขียนโปรแกรมด้วยจาวา
ประเดิม Blog แรกของจาวาโปรแกรมมิ่งด้วย การเขียน Hello World ด้วยจาวาตามธรรมเนียมก่อน สำหรับ Tool ที่ใช้สำหรับเขียนโปรแกรมผมใช้ Netbeans IDE ซึ่ง Download ได้จากเว็บ //www.oracle.com/technetwork/java/javase/downloads/index.html ขั้นตอนการติดตั้งของข้ามไปเพราะไม่มีอะไร Run โปรแกรมที่ Download เสร็จกด Next ไปอย่างเดียวไม่ได้ซับซ้อน

เมื่อติดตั้ง Netbeans เรียบร้อยแล้วให้เปิดโปรแกรมขึ้นมาแล้วเลือกเมนู File->New Project จะขึ้นหน้าจอดังรูป



เลือก Categories = Java, Project = Java Application จากนั้นกดปุ่ม Next จะขึ้นหน้าจอ



ช่อง Project Name พิมพ์คำว่า "HelloWorld" จะขึ้นหน้าจอดังรูป




แก้ไข Source code ที่ไฟล์ HelloWorld.java ดังนี้

package helloworld;

/**
 *
 * @author nattaya
 */
public class HelloWorld {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

เมื่อแก้ไขเสร็จแล้วให้กดปุ่ม F6 เพื่อ Run โปรแกรม ผลของโปรแกรมจะแสดงอยู่ทางด้านล่าง



Create Date : 23 สิงหาคม 2555
Last Update : 23 สิงหาคม 2555 15:41:53 น.
Counter : 1851 Pageviews.

2 comment

vrod
Location :
  

[ดู Profile ทั้งหมด]
 ฝากข้อความหลังไมค์
 Rss Feed
 Smember
 ผู้ติดตามบล็อก : 1 คน [?]



ทำงานด้านคอมพิวเตอร์ เข้ามาในพันทิพส่วนมากเข้าห้องศาสนาและหว้ากอ อาจจะเห็นชื่อนี้อยู่ในห้องชานเรือนบ้างแต่นั้นเป็นเพราะแฟนผมขอพ่วงใช้ด้วยเพราะไม่อยากตั้งชื่อใหม่ครับ




New Comments