การเขียน รับ/ส่ง ข้อมูล ไปยัง Serial Port โดยตรง

Post Reply
brid.surapol
Posts: 163
Joined: 11 Apr 2013, 11:43

การเขียน รับ/ส่ง ข้อมูล ไปยัง Serial Port โดยตรง

Post by brid.surapol »

Code: Select all

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SOtest {

public static void main(String[] args) {
    SOtest sot = new SOtest();
    sot.rx();    // or sot.tx() for the transmit side
}

public void tx()  {
    FileOutputStream nmoutfile;

    try {
        nmoutfile = new FileOutputStream("/dev/ttyS0");
        nmoutfile.write(49);  //  ascii value 10 still needed...?
        nmoutfile.close();    //  doesn't force value 49 to send

    } catch (Exception ex) {
        System.out.println(ex.getMessage().toString());
    }
}

public void rx()  {
    FileInputStream nminfile; 

    try {
        nminfile = new FileInputStream("/dev/ttyS0");

        while (true) {
            System.out.println(nminfile.read());
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage().toString());
    } 
}
}
Post Reply

Return to “การใช้งาน Java และ JVM”