元になる機種を真似るので練習に良いかなと。
片手間に夜とか休日前とかちょこちょこと書いてるので経過のメモを...
モデルは、SharpのEL-566です。8年程使っています。
愛着ある一台
何か色々アレですけど、基本の形は出来てきた?
Mac OS X (x86 and ppc) (there is an Installer with the source)
RXTXcomm.jar goes in /Library/Java/Extensions
librxtxSerial.jnilib goes in /Library/Java/Extensions
Run fixperm.sh thats in the directory. Fix perms is in the Mac_OS_X
subdirectory.
import java.io.IOException;
import java.io.InputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class Sample {
public static final void main(String[] args){
try{
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier("/dev/tty.usbserial-A400FMPJ");
SerialPort port = (SerialPort)portID.open("Sample", 5000); //waiting5000ms
port.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
InputStream is = port.getInputStream();
int c;
while((c = is.read()) != -1){
Calendar cal = Calendar.getInstance();
System.out.print(cal.get(Calendar.SECOND) + "." + cal.get(Calendar.MILLISECOND));
System.out.println("->" + (char)c);
}
is.close();
port.close();
}catch(NoSuchPortException e){
System.err.println("Can Not Find Device");
e.printStackTrace();
}catch(PortInUseException e){
System.err.println("Can Not Open Device");
e.printStackTrace();
}catch(UnsupportedCommOperationException e){
System.err.println("Invalid Parameter");
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier("/dev/tty.usbserial-A400FMPJ");
getPortIdentifierの引数には下の画像にあるように、開きたいデバイスのパスを与えます。
SerialPort port = (SerialPort)portID.open("Sample", 5000);
第一引数にはポートを開くアプリケーション名, 第二引数にはポートが他のアプリケーションに使用されていた場合にポートが解放されるのを待つ待ち時間(ms)です。
port.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);第一引数から、ボーレート, データビット数, ストップビット数, パリティの種類です。
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);フロー制御の設定を行います。
java.lang.UnsatisfiedLinkError: /Library/Java/Extensions/librxtxSerial.jnilib: no suitable image found. Did find: /Library/Java/Extensions/librxtxSerial.jnilib: no matching architecture in universal wrapper thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: /Library/Java/Extensions/librxtxSerial.jnilib: no suitable image found. Did find: /Library/Java/Extensions/librxtxSerial.jnilib: no matching architecture in universal wrapper
gnu.io.PortInUseException: Unknown Application
というエラーが出ました。sudo mkdir /var/lock sudo chmod 777 /var/lock
private int getDay(int year, int month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(checkLeapYear(year))
return 29;
else
return 28;
default:
return -1;
}
}
private boolean checkLeapYear(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
FileReader fr;
BufferedReader br;
fr = new FileReader("Station_a");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
//処理だよー
String tmp[] = line.split("\t");
}
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), "Shift_JIS")));
pw.write(String);
Source source = new Source(new URL(URL)); ListtableList = source.getAllElements(HTMLElementName.TABLE); for(Element table : tableList){ if(tableList.indexOf(table) > 0){ for(Element tbody : table.getChildElements()){ if(table.getChildElements().indexOf(tbody) > 1){ for(Element tr : tbody.getChildElements()){ if(tbody.getChildElements().indexOf(tr) >= 0){//ここが問題 for(Element td : tr.getChildElements()){ if(tr.getChildElements().indexOf(td) > 0){ //処理だよー System.out.println(td.getTextExtractor().toString()); } }//td } }//tr } }//tbody } }//table
String regex = "\\d+"; //ex. 数値
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inStr);
while(matcher.find){
/* 処理 */
}