Advertisement
Guest User

mBus writing reading in Java ME

a guest
May 4th, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. /*
  2.  * * Classname RfSlaveDevice.java
  3.  *
  4.  * Version information
  5.  * Version No. 001
  6.  */
  7.  
  8. package com.rfslave;
  9.  
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13.  
  14. import javax.microedition.io.CommConnection;
  15. import javax.microedition.io.Connector;
  16. import javax.microedition.midlet.MIDlet;
  17. import javax.microedition.midlet.MIDletStateChangeException;
  18.  
  19. public class RfSlaveDevice extends MIDlet {
  20.  
  21.     CommConnection commConn;
  22.     InputStream inStream;
  23.     OutputStream outStream;
  24.  
  25.     /**
  26.      * com.rfslave - default constructor
  27.      */
  28.     public RfSlaveDevice() {
  29.         System.out.println("com.rfslave: Constructor");
  30.         System.out.println("Available COM-Ports: "
  31.                 + System.getProperty("microedition.commports"));
  32.         try {
  33.             String strCOM = "comm:COM0;baudrate=115200;bitsperchar=8;stopbits=1;parity=even";
  34.             commConn = (CommConnection) Connector.open(strCOM);
  35.             System.out.println("CommConnection(" + strCOM + ") opened");
  36.             System.out.println("Real baud rate: " + commConn.getBaudRate());
  37.             outStream = commConn.openOutputStream();
  38.             inStream = commConn.openInputStream();
  39.             System.out.println("InputStream and OutputStream opened");
  40.         } catch (IOException e) {
  41.             System.out.println(e);
  42.             notifyDestroyed();
  43.         }
  44.     }
  45.  
  46.     /**
  47.      * destroyApp()
  48.      *
  49.      * @param cond
  50.      *            true if this is an unconditional destroy false if it is not
  51.      *            currently ignored and treated as true
  52.      */
  53.     public void destroyApp(boolean cond) {
  54.         System.out.println("com.reapptech.cerveros.rfslave: destroyApp(" + cond
  55.                 + ")");
  56.         try {
  57.             inStream.close();
  58.             outStream.close();
  59.             commConn.close();
  60.             System.out.println("Streams and connection closed");
  61.         } catch (IOException e) {
  62.             System.out.println(e);
  63.         }
  64.  
  65.         notifyDestroyed();
  66.     }
  67.  
  68.     /**
  69.      * pauseApp()
  70.      */
  71.     public void pauseApp() {
  72.         System.out.println("com.rfslave: pauseApp()");
  73.     }
  74.  
  75.     /**
  76.      * startApp()
  77.      */
  78.     public void startApp() throws MIDletStateChangeException {
  79.         System.out.println("com.rfslave: startApp");
  80.  
  81.         File dataFile = new File("data.txt", "data/myDir/");
  82.  
  83.         while (1 == 1) {
  84.             // Byte array representation of hexadecimal values 10 5B 01 5C 16 (REQ_UD2 data)
  85.             byte bStart = (byte) Integer.parseInt("000010000", 2);
  86.             byte b2ndDigits = (byte) Integer.parseInt("01011011", 2);
  87.             byte bPrimaryAddr = (byte) Integer.parseInt("00000001", 2);
  88.             byte bCheckSum = (byte) Integer.parseInt("01011100", 2);
  89.             byte bStop = (byte) Integer.parseInt("00010110", 2);
  90.             byte[] bArray = { bStart, b2ndDigits, bPrimaryAddr, bCheckSum,
  91.                     bStop };
  92.  
  93.             int ch;
  94.  
  95.             try {
  96.                 outStream.write(bArray);
  97.                 Thread.sleep(50);
  98.  
  99.                 System.err.println("inStream bytes available: "
  100.                         + inStream.available());
  101.                 if (inStream.available() > 0) {
  102.                     String message = "";
  103.                     while (inStream.available() > 0) {
  104.                         ch = inStream.read(); // Expecting to receive RSP_UD data as per mBus.
  105.                         message += (char) ch;
  106.                     }
  107.                     dataFile.open(true);
  108.                     dataFile.write(message);
  109.                     dataFile.close();
  110.                 }
  111.                 Thread.sleep(50);
  112.             } catch (IOException e) {
  113.                 e.printStackTrace();
  114.             } catch (Exception e) {
  115.                 e.printStackTrace();
  116.             }
  117.  
  118.             destroyApp(true);
  119.         }
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement