.轡螢▲訥命を行いたい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(); } } }
ゴ粟