2012-02-12 3 views
0

Linux에서 지문 장치 (tc400) C2 장치를 연결하려고합니다. 하드웨어 벤더가 리눅스에서 사용할 수없는 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} 

은 또한 당신은 바이트 배열 16 진수 문자열을 변환 Apache common Codec Hex 클래스를 사용할 수 있습니다.

두 번째 실수는 재발행 된 데이터를 읽는 데 ObjectInputStreamreadObject()의 사용법입니다. Java 직렬화 클래스 데이터를 읽도록 설계되었습니다. DataInputStream를 사용하여보다 효율적으로 사용하고이 코드 : 데이터가 하나 개의 블록으로 옮겨진되지 않는 경우에는 결과가 불완전 할 수 있다는

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/...(: 당신이 나를 도울 수 있겠습니까! 나는 감사 할거야! @ 로버트 –