2012-02-12 3 views
0

Linux上で指紋デバイス(tc400)C2デバイスを接続しようとしています。ハードウェアベンダーはLinuxで使用できないDLLファイルを私に提供しているので、ソケットプログラミングを使い、16進コードを使ってデバイスと通信しようとしました。問題は、デバイスがまったく応答しないため、クライアントソケットの入力ストリームを取得しようとするとコードがクラッシュすることです。 これは私のすべてのコードは次のとおりです。JavaとHexを使用してC2デバイスを接続する

import java.io.*; 
import java.net.*; 
public class Client{ 
Socket requestSocket; 
ObjectOutputStream out; 
ObjectInputStream in; 
String message; 
Client(){} 
void run() 
{ 
    try{ 
     //1. creating a socket to connect to the server 
     requestSocket = new Socket("172.16.16.192", 5010); 
     //requestSocket = new Socket("localhost", 5010); 
     System.out.println("Connected to Machine in port 5010"); 
     //2. get Input and Output streams 
     System.out.println("-1"); 
     out = new ObjectOutputStream(requestSocket.getOutputStream()); 
     //out.flush(); 
     System.out.println("D0"); 
     sendMessage("A5\\x00\\x00\\x00\\x00\\x30\\x00\\x00\\x51\\x10"); // getting the device information 
     in = new ObjectInputStream(requestSocket.getInputStream()); 
     //3: Communicating with the server 
     System.out.println("D1"); 
     do{ 
      System.out.println("D2"); 
      try{ 
       System.out.println("D3"); 

       sendMessage("A5\\x00\\x00\\x00\\x00\\x30\\x00\\x00\\x51\\x10"); // getting the device information 
       System.out.println("D5"); 
       byte msg [] = null; 
       System.out.println("D6"); 
       msg = (byte[])in.readObject(); 
       System.out.println("D7"); 
       System.out.println("Machine>" + msg); 

       //sendMessage(message); 
       //message = (String)in.readObject(); 
      } 
      catch(ClassNotFoundException classNot){ 
       System.err.println("data received in unknown format"); 
      } 
     }while(!message.equals("bye")); 
    } 
    catch(UnknownHostException unknownHost){ 
     System.err.println("You are trying to connect to an unknown host!"); 
    } 
    catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
    finally{ 
     //4: Closing connection 
     try{ 
      in.close(); 
      out.close(); 
      requestSocket.close(); 
     } 
     catch(IOException ioException){ 
      ioException.printStackTrace(); 
     } 
    } 
} 
void sendMessage(String msg) 
{ 
    System.out.println("D4"); 

    try{ 
     System.out.println("D41"); 
     out.writeObject(msg.getBytes()); 
     out.flush(); 
     System.out.println("client>" + msg); 
    } 
    catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 
public static void main(String args[]) 
{ 
    Client client = new Client(); 
    client.run(); 
} 
} 

誰もが、私は感謝するでしょうデバイスと通信するために私を助けることができれば。

+0

あなたはこれでどのようになったのですか?私はC3デバイスを持っていて、私はWindows上でのみそれを通信することができます、Linuxの代替を持っていることは素晴らしいです。 – Jeremy

答えて

0

文字列にバイナリデータを格納するのは悪い考えです。特に、StringのgetBytes()メソッドは、システムのデフォルト文字セット(LinuxのほとんどはUTF-8)に依存するバイトを返すため、予期しないバイトシーケンスが生成される可能性があります。

文字列の代わりにbyte[]を使用することをお勧めします。バイト配列はまた、このように定義することができます:

new byte{(byte) 0xA5, (byte) 0x00, (byte) 0x00 (byte) 0x00 (byte) 0x00 (byte) 0x30 (byte) 0x00 (byte) 0x00 (byte) 0x51 (byte) 0x10} 

別の方法としては、バイト配列に進文字列を変換するためのApache common Codec Hexクラスを使用することができます。

第2の間違いは、書き換えられたデータを読み取るためのObjectInputStreamreadObject()の使用です。 Javaのシリアライズされたクラスデータを読み込むために設計されています。 DataInputStreamを使用するより良い使用してこのコード:データは1つのブロックとして転送されない場合には、結果が不完全であってもよいこと

byte msg [] = new byte[1024]; 
System.out.println("D6"); 
int bytesReturned = in.read(msg, 0, msg.length); 

注意。正確な応答msgのサイズを知っている場合は、代わりにreadFull(msg)を使用してください。

+0

本当にこの質問もあなたの答えを見ることができて幸運です、私はここで同じ問題を抱えてくださいくださいstackoverflow.com/questions/23990924/ ... :(あなたが私を助けてください!私は感謝します!@ロバーツ –