2016-08-06 7 views
0

COMポートの一覧を表示するコードを作成した後、そのうちの1つを選択したいと思います。 私が得るリストは文字列の配列なので、setterメソッドのためのキャストを作成する必要がありますが、キャストは機能しません。どうして?事前に文字列からCommPortIdentifierへのキャスト

おかげ

package tests; 

import gnu.io.*; 

import java.util.*; import java.io.InputStream; 

public class connectnow_array implements Runnable, SerialPortEventListener { 

static CommPortIdentifier portId; 
static Enumeration portList; 

InputStream inputStream; 
SerialPort serialPort; 
Thread readThread; 
byte[] readBuffer; 


public static String[] listSerialPorts() { 

    Enumeration ports = CommPortIdentifier.getPortIdentifiers(); 
    ArrayList portList = new ArrayList(); 
    String portArray[] = null; 
    while (ports.hasMoreElements()) { 
     CommPortIdentifier currPortId = (CommPortIdentifier) ports.nextElement(); 
     if (currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 
      portList.add(currPortId.getName()); 
     } 
    } 
    portArray = (String[]) portList.toArray(new String[0]); 
    System.out.println("portList=" + portList); 

    return portArray; 
} 

public static void main(String[] args) { 

    String[] lista_de_puertos=null; 

    connectnow_array main = new connectnow_array(); 
    lista_de_puertos=main.listSerialPorts(); 

    //System.out.println(Arrays.toString(lista_de_puertos)); 
    System.out.println(lista_de_puertos[0]); 

System.out.println(getPortId()); 

setPortId((CommPortIdentifier)lista_de_puertos[0]); //PROBLEM HERE** 


} 

public static CommPortIdentifier getPortId() { 
    return portId; 
} 

public static void setPortId(CommPortIdentifier portId) { 
    connectnow_array.portId = portId; 
} 

}

+0

あなたは、最小完全、かつ検証例を含むようにあなたの質問を編集してくださいもらえますか?参照:http://stackoverflow.com/help/mcve – johnnyaug

答えて

0

は次のようになります。

portArray = (String[]) portList.toArray(new String[portList.size()]); 
+0

スレッド "main"の例外java.lang.Error:未解決のコンパイルの問題: \t型の不一致:文字列[]からCommPortIdentifier []に変換できません – Condor

関連する問題