title: How to read and download ddd tachograph file with APDU?
body: <p>I'm trying to read data from a tachograph card using <code>javax.smartcardio</code>. The problem is I'm doing most of the things blindly because I can't find a simple tutorial on <code>APDU commands</code>.</p>
  
  <p>Currently I managed to print the card type (<code>card: PC/SC card in HID Global OMNIKEY 3x21 Smart Card Reader 0, protocol T=0, state OK</code>) and read the card identification information (selected file <code>0520</code>).</p>
  
  <p><strong>How do I download the tachograph .ddd file?</strong></p>
  
  <p>Here's the code so far:</p>
  
  <pre><code>public static void main(String[] args) {
  
          try {
              TerminalFactory factory = TerminalFactory.getDefault();
              List&lt;CardTerminal&gt; terminals = factory.terminals().list();
              System.out.println("Terminals: " + terminals);
              // get the first terminal
              CardTerminal terminal = terminals.get(0);
              // establish a connection with the card
              Card card = terminal.connect("T=0");
              System.out.println("card: " + card);
              CardChannel channel = card.getBasicChannel();
  
              byte[] c1 = hexStringToByteArray(stripCommandSpace("00 A4 04 0C 06 FF544143484F"));
              //byte[] c1 = hexStringToByteArray(stripCommandSpace("FF CA 00 00 00"));
              ResponseAPDU r1 = channel.transmit(new CommandAPDU(c1));
              System.out.println("response: " + byteArrayToHexString(r1.getBytes()));
              byte[] c2 = hexStringToByteArray(stripCommandSpace("00 a4 02 0c 02 05 04"));
              ResponseAPDU r2 = channel.transmit(new CommandAPDU(c2));
              System.out.println("response: " + byteArrayToHexString(r2.getBytes()));
              byte[] c3 = hexStringToByteArray(stripCommandSpace("00 B0 00 01 128"));
              ResponseAPDU r3 = channel.transmit(new CommandAPDU(c3));
              System.out.println("response: " + byteArrayToHexString(r3.getBytes()));
              System.out.println("data: " + byteArrayToHexString(r3.getData()));
  
              byte[] ceva = r3.getData();
              String str = new String(ceva);
              System.out.println("Date card: " + str);
  
              String fileName = "D:\\fisier\\test.ddd";
  
              /*BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
          writer.write(str);
  
          writer.close();*/
              FileOutputStream stream = new FileOutputStream(fileName);
              try {
                  stream.write(ceva);
              } finally {
                  stream.close();
              }
  
              // disconnect
              card.disconnect(false);
          } catch (Exception e) {
              System.out.println("Exceptie " + e.getMessage());
          }
      }
  </code></pre>
  
  <p>I managed to identify and read the driver activity file - I had to change the identifier to <code>0504</code> like in the picture below:</p>
  
  <p><a href="https://i.stack.imgur.com/yMLSO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yMLSO.png" alt="enter image description here"></a></p>
  
  <p><strong>Now my problem is how to convert the byte array I get into a .ddd file</strong>.</p>
  
  <p>I tried like in the updated version of my code (see above) but the .ddd reader tells me the file is corrupt (I use a third party app which converts .ddd to .txt - called <strong>readesm</strong>).</p>