java -> Arduino Mega
Arduino -> java の双方向のシリアル通信をしたいのですが、うまくいきません。
どうすればよいでしょうか?
java
1import java.io.*; 2import gnu.io.*; 3 4public class Serialcommnucation { 5 6 public static void main(String[] args) { 7 try { 8 CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier("C:/Program Files (x86)/Java/jdk1.6.0_45/jre/lib/ext/RXTXcomm.jar"); 9 // 第一パラメータはArdinoのスケッチ名 10 SerialPort port = (SerialPort) portID.open("tusin", 3000); // 失敗したら5秒待つ 11 12 // 第一引数のポートはスケッチファイルで指定した数字 13 port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 14 port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 15 16 InputStream is = port.getInputStream(); 17 int c; // arduinoからの出力読み込み用 18 System.out.println("start reading"); 19 c = is.read(); 20 String str1 = String.valueOf(c); 21 if (c != -1) { 22 //System.out.println(c); // デバッグ用 23 if(c == 65){ 24 System.out.println("A"); 25 }else if(c == 66){ 26 System.out.println("B"); 27 }else if(c == 67){ 28 System.out.println("C"); 29 }else if(c == 68){ 30 System.out.println("D"); 31 }else if(c == 69){ 32 System.out.println("E"); 33 }else if(c == 70){ 34 System.out.println("F"); 35 }else if(c == 71){ 36 System.out.println("G"); 37 }else if(c == 72){ 38 System.out.println("H"); 39 }else if(c == 73){ 40 System.out.println("I"); 41 }else if(c == 74){ 42 System.out.println("J"); 43 }else if(c == 75){ 44 System.out.println("K"); 45 }else if(c == 76){ 46 System.out.println("L"); 47 }else if(c == 77){ 48 System.out.println("M"); 49 }else if(c == 78){ 50 System.out.println("N"); 51 }else if(c == 79){ 52 System.out.println("O"); 53 }else if(c == 80){ 54 System.out.println("P"); 55 }else if(c == 81){ 56 System.out.println("Q"); 57 }else if(c == 82){ 58 System.out.println("R"); 59 }else if(c == 83){ 60 System.out.println("S"); 61 }else if(c == 84){ 62 System.out.println("T"); 63 }else if(c == 85){ 64 System.out.println("U"); 65 }else if(c == 86){ 66 System.out.println("V"); 67 }else if(c == 87){ 68 System.out.println("W"); 69 }else if(c == 88){ 70 System.out.println("X"); 71 }else if(c == 89){ 72 System.out.println("Y"); 73 }else if(c == 90){ 74 System.out.println("Z"); 75 }else if(c == 97){ 76 System.out.println("a"); 77 }else if(c == 98){ 78 System.out.println("b"); 79 }else if(c == 99){ 80 System.out.println("c"); 81 }else if(c == 100){ 82 System.out.println("d"); 83 }else if(c == 101){ 84 System.out.println("e"); 85 }else if(c == 102){ 86 System.out.println("f"); 87 }else if(c == 103){ 88 System.out.println("g"); 89 }else if(c == 104){ 90 System.out.println("h"); 91 }else if(c == 105){ 92 System.out.println("i"); 93 }else if(c == 106){ 94 System.out.println("j"); 95 }else if(c == 107){ 96 System.out.println("k"); 97 }else if(c == 108){ 98 System.out.println("l"); 99 }else if(c == 109){ 100 System.out.println("m"); 101 }else if(c == 110){ 102 System.out.println("n"); 103 }else if(c == 111){ 104 System.out.println("o"); 105 }else if(c == 112){ 106 System.out.println("p"); 107 }else if(c == 113){ 108 System.out.println("q"); 109 }else if(c == 114){ 110 System.out.println("r"); 111 }else if(c == 115){ 112 System.out.println("s"); 113 }else if(c == 116){ 114 System.out.println("t"); 115 }else if(c == 117){ 116 System.out.println("u"); 117 }else if(c == 118){ 118 System.out.println("v"); 119 }else if(c == 119){ 120 System.out.println("w"); 121 }else if(c == 120){ 122 System.out.println("x"); 123 }else if(c == 121){ 124 System.out.println("y"); 125 }else if(c == 122){ 126 System.out.println("z"); 127 } 128 OutputStream out = port.getOutputStream(); 129 out.write(c); 130 } 131 else { 132 } 133 is.close(); 134 port.close(); 135 System.out.println("finished reading"); 136 } catch (NoSuchPortException e) { 137 System.err.println("Can Not Find Device"); 138 e.printStackTrace(); 139 } catch (PortInUseException e) { 140 System.err.println("Can Not Open Device"); 141 e.printStackTrace(); 142 } catch (UnsupportedCommOperationException e) { 143 System.err.println("Invalid Parameter"); 144 e.printStackTrace(); 145 } catch (IOException e) { 146 e.printStackTrace(); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 } 150 } 151 152} 153 154[Arduino] 155#define LED 13 156 157void setup() { 158 // put your setup code here, to run once: 159 pinMode(LED, OUTPUT); 160 Serial.begin(9600); 161 162 //Serial.println("Test"); 163} 164 165void loop() { 166 167 int input; 168 Serial.write("a"); 169 170 input = Serial.read(); 171 Serial.println(input); 172 173 if(input != -1 ){ 174 Serial.print("LED ON\n"); 175 digitalWrite(LED, HIGH); 176 }else { 177 } 178} 179
そして、実行すると、Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
at Serialcommnucation.main(Serialcommnucation.java:8)
とエラーが出ます。どうすればよいでしょうか?
回答1件
あなたの回答
tips
プレビュー