¡¥·¥ê¥¢¥ëÄÌ¿®¤ò¹Ô¤¤¤¿¤¤Eclipse¤Îpackage²¼¤ËÄɲå饤¥Ö¥é¥êÍѤΥե©¥ë¥À¤òºî¤ë¡Ê̾Á°¤Ï²¿¤Ç¤â¤¤¤¤¤±¤É¼Ì¿¿¤Ç¤Ïlib¡Ë
¢ÄɲÃÍѤΥ饤¥Ö¥é¥ê¤ò¥¤¥ó¥¹¥È¡¼¥ë
¡¡¡¡¤³¤Î¥Õ¥©¥ë¥À¤ò²òÅष¤¿Ãæ¤Î¡¢
¡¡¡¡¡¡rxtxParallel.dll
¡¡¡¡¡¡rxtxSerial.dll
¡¡¡¡¡¡RXTXcomm.jar
¡¡¡¡¤ò¥³¥Ô¡¼¤·¤Æ¡¢¡¤Î¥Õ¥©¥ë¥ÀÆâ¤Ë¥Ú¡¼¥¹¥È
£Eclipse¤ÇLibrary Path¤òÄɲ乤ë
¤¥×¥í¥°¥é¥àºîÀ®
//port check //JDK check import java.io.*; public class CommLight{ static File sharedText; public static void main(String[] args){ SerialComm serialcomm = new SerialComm(); serialcomm.open(); int i = 10; while(i>0){ serialcomm.write((byte)0x34); try{ Thread.sleep(500); }catch(Exception e){ } serialcomm.write((byte)0x20); try{ Thread.sleep(500); }catch(Exception e){ } serialcomm.write((byte)0x78); try{ Thread.sleep(500); }catch(Exception e){ } i--; } System.out.println("Hello, World"); }}
import gnu.io.*; import java.io.*; import java.util.*; public class SerialComm implements SerialPortEventListener{ private SerialPort port; // private static BufferedReader reader; private InputStream in; private OutputStream out; SerialComm(){ super(); } public void open(){ try { // Serial port initialize CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM6"); port = (SerialPort)portId.open("USB Serial Port", 2000); port.setSerialPortParams( 9600, // í êM롯ìx[bps] SerialPort.DATABITS_8, // ÉfÅ[É^ÉrÉbÉgêî SerialPort.STOPBITS_1, // ÉXÉgÉbÉvÉrÉbÉg SerialPort.PARITY_NONE // ÉpÉäÉeÉB ); port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); try{ port.addEventListener(this); port.notifyOnDataAvailable(true); in = port.getInputStream(); out = port.getOutputStream(); }catch(TooManyListenersException ex){ System.out.println("Error1: "+ex); } } catch (Exception e) { System.out.println("Error2: " + e); System.exit(1); } } public void close(){ try{ in.close(); out.close(); port.close(); }catch(Exception e){ System.out.println("close: "+e); } } public void read(){ try{ byte[] buffer; int temp = in.available(); if(temp>0){ System.out.println("temp: "+temp); buffer = new byte[temp]; temp = in.read(buffer); int num = 0; while(num<temp){ System.out.println("b "+num+": "+Integer.toHexString((byte)buffer[num])); num++; } } in.close(); }catch(Exception e){ System.out.println("error4: "+e); } } public static String toHex(byte[] digest) { StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%1$02X", b)); } return sb.toString(); } public void write(byte data){ try{ out.write((byte)data); out.flush(); //ÉXÉgÉäÅ[ÉÄÇ∆COMÉ|Å[ÉgÇï¢ÌÇ¢ßÇÈèàóù }catch(Exception e){ System.out.println("port exception: "+e); } } public void run() { try { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); OutputStream out = port.getOutputStream(); boolean flagQuit = false; while (!flagQuit) { String input = br.readLine(); if (input.length() > 0) { if (input.equals(":quit")) break; input += "\r"; out.write(input.getBytes("US-ASCII")); } } port.close(); } catch (Exception e) { System.out.println("Error3: " + e); } } public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { read(); } } }
¥´°À®