title: How to write and send an ASTM frame to medical equipment
body: <p>I am currently working on the ASTM protocol to send orders tests request to medical instrument. But I cannot send a message to the equipment correctly. To be more explicit, I want for example to send these frames:</p>
  
  <pre><code>        String h1, s2, s3, s4, s5, s6 = "";
          h1 = "H|@^\\|ODM-IdfDGIWA-36|||GeneXpert PC^GeneXpert^4.8|||||LIS||P|1394-97|20070521100245";
          s2 = "P|1";
          s3 = "O|1|SID-818||^^^TestId-12|S|20070812140500|||||A||||ORH||||||||||Q";
          s4 = "O|2|SID-818||^^^TestId-14|S|20070812140600|||||A||||ORH||||||||||Q";
          s5 = "O|3|SID-818||^^^TestId-16|S|20070812140700|||||A||||ORH||||||||||Q";
          s6 = "L|1|F";
  </code></pre>
  
  <p>and here is how I am doing now:</p>
  
  <pre><code>    writeMeBytes(outToServer, h1.getBytes());
      writeMeBytes(outToServer, s2.getBytes());
      writeMeBytes(outToServer, s3.getBytes());
      writeMeBytes(outToServer, s4.getBytes());
      writeMeBytes(outToServer, s5.getBytes());
      writeMeBytes(outToServer, s6.getBytes());
  
  
  public static void writeMeBytes(DataOutputStream dos, byte [] b){
      if (b.length &gt;0){
          int j = 0;
          while (j &lt;= b.length-1) {
              try {
                  dos.write(b[j++]);
              } catch (IOException ex) {
                  Logger.getLogger(SimpleServer.class.getName()).log(Level.SEVERE, null, ex);
              }
  
          }
      }
  }
  </code></pre>
  
  <p>I turn it into byte and then send byte after byte.</p>
  
  <p>Except that I do not see any change on the receiver side.</p>
  
  <p><strong>Update according to @Muhammad Answer</strong></p>
  
  <p>This is what I did to send order to GeneXpert DX System</p>
  
  <pre><code>public class SimpleServer {
  
  private static ServerSocket server;
  private static Socket connection;
  
  public static void main(String args[]) throws IOException, InterruptedException {
      server = new ServerSocket(12221);
      boolean stopped = false;
  
      System.out.println(" start... ");
      connection = server.accept();
      System.out.println("wait for connection");
      BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      DataOutputStream outToClient = new DataOutputStream(connection.getOutputStream());
      String currentMsg = "";
      int clientIntMessage;
  
      String h1, s2, s3, s4, s5, s6 = "";
      h1 = "1H|@^\\|ODM-IdfDGIWA-36|||GeneXpert PC^GeneXpert^4.8|||||LIS||P|1394-97|20070521100245" + ProtocolASCII.LF
              + "P|1" + ProtocolASCII.LF
              + "O|1|SID-818||^^^TestId-12|S|20070812140500|||||A||||ORH||||||||||Q" + ProtocolASCII.LF
              + "L|1|F" + ProtocolASCII.LF;
      s2 = "P|1";
      s3 = "O|1|SID-818||^^^TestId-12|S|20070812140500|||||A||||ORH||||||||||Q";
      //s4 = "O|2|SID-818||^^^TestId-14|S|20070812140600|||||A||||ORH||||||||||Q";
      //s5 = "O|3|SID-818||^^^TestId-16|S|20070812140700|||||A||||ORH||||||||||Q";
      s6 = "L|1|F";
  
      String retmsg = h1;
      //logException("OrderMessae   :" + retmsg);
      retmsg = ProtocolASCII.STX + retmsg + ProtocolASCII.CR + ProtocolASCII.ETX + ProtocolMessage.getCheckSum(retmsg) + ProtocolASCII.CR + ProtocolASCII.LF;
  
      clientIntMessage = inFromClient.read();
      //while (clientIntMessage != ProtocolASCII.EOT) {
      while (true) {
          currentMsg += String.valueOf(Character.toChars(clientIntMessage));
          if (clientIntMessage == ProtocolASCII.ENQ) {
              outToClient.writeBytes("" + ProtocolASCII.ACK);
              System.out.println(" &lt;--- LIS [ACK] on DX [ENQ]");
          } else if (clientIntMessage == ProtocolASCII.ACK) {
              System.out.println(" ---&gt; DX [ACK]");
              // Send your order message here
              outToClient.writeBytes(retmsg);
          } else if (clientIntMessage == ProtocolASCII.CR) {
              System.out.println(currentMsg);
              outToClient.writeBytes("" + ProtocolASCII.ACK);
          } else if (clientIntMessage == ProtocolASCII.NAK) {
              System.out.println(" ---&gt; DX sent [NAK] ");
              System.out.println(" --- LIS now wait 10 sec... ");
              Thread.sleep(10000);
              outToClient.writeBytes("" + ProtocolASCII.ENQ);
              System.out.println(" &lt;--- LIS [ENQ] ");
          } else if (clientIntMessage == ProtocolASCII.EOT) {
              System.out.println(" ---&gt; DX END OF TRANSMISSION");
              outToClient.writeBytes("" + ProtocolASCII.ENQ);
              System.out.println(" &lt;--- LIS [ENQ] ");
          }
  
          if (stopped) {
              break;
          }
          clientIntMessage = inFromClient.read();
      }
      connection.close();
      stopped = true;
  }}
  </code></pre>
  
  <p>And this is the result I get from the console:</p>
  
  <pre><code>start... 
  wait for connection
   &lt;--- LIS [ACK] on DX [ENQ]
  1H|@^\|ODM-rQTcjIWA-66||GeneXpert PC^GeneXpert^4.8|||||LIS||P|1394-97|20180314003724
  Q|1|ALL||||||||||O@N
  L|1|N
  B5
   ---&gt; DX [EOT] 
   &lt;--- LIS [ENQ] 
   ---&gt; DX [ACK]
   ---&gt; DX sent [NAK] 
   --- LIS now wait 10 sec... 
  </code></pre>
  
  <p>DX is the machine software and LIS is the host. 
  Whenever I try to send the ENQ, the machine answers me with NAK. </p>
  
  <p><strong>UPDATE 2</strong><br>
  It seem to be working. But now windows events shows me an error about why my records orders don't appear in the GeneXpert DX host record list. The header record have been sent first. </p>
  
  <p><a href="https://i.stack.imgur.com/nMWfh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nMWfh.png" alt="Windows event logs after following @Muhammad *UPDATE 2**"></a></p>
  
  <p><a href="https://i.stack.imgur.com/eZV4n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eZV4n.png" alt="GeneXpert DX STATUS"></a></p>