Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * * Classname RfSlaveDevice.java
- *
- * Version information
- * Version No. 001
- */
- package com.rfslave;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.microedition.io.CommConnection;
- import javax.microedition.io.Connector;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class RfSlaveDevice extends MIDlet {
- CommConnection commConn;
- InputStream inStream;
- OutputStream outStream;
- /**
- * com.rfslave - default constructor
- */
- public RfSlaveDevice() {
- System.out.println("com.rfslave: Constructor");
- System.out.println("Available COM-Ports: "
- + System.getProperty("microedition.commports"));
- try {
- String strCOM = "comm:COM0;baudrate=115200;bitsperchar=8;stopbits=1;parity=even";
- commConn = (CommConnection) Connector.open(strCOM);
- System.out.println("CommConnection(" + strCOM + ") opened");
- System.out.println("Real baud rate: " + commConn.getBaudRate());
- outStream = commConn.openOutputStream();
- inStream = commConn.openInputStream();
- System.out.println("InputStream and OutputStream opened");
- } catch (IOException e) {
- System.out.println(e);
- notifyDestroyed();
- }
- }
- /**
- * destroyApp()
- *
- * @param cond
- * true if this is an unconditional destroy false if it is not
- * currently ignored and treated as true
- */
- public void destroyApp(boolean cond) {
- System.out.println("com.reapptech.cerveros.rfslave: destroyApp(" + cond
- + ")");
- try {
- inStream.close();
- outStream.close();
- commConn.close();
- System.out.println("Streams and connection closed");
- } catch (IOException e) {
- System.out.println(e);
- }
- notifyDestroyed();
- }
- /**
- * pauseApp()
- */
- public void pauseApp() {
- System.out.println("com.rfslave: pauseApp()");
- }
- /**
- * startApp()
- */
- public void startApp() throws MIDletStateChangeException {
- System.out.println("com.rfslave: startApp");
- File dataFile = new File("data.txt", "data/myDir/");
- while (1 == 1) {
- // Byte array representation of hexadecimal values 10 5B 01 5C 16 (REQ_UD2 data)
- byte bStart = (byte) Integer.parseInt("000010000", 2);
- byte b2ndDigits = (byte) Integer.parseInt("01011011", 2);
- byte bPrimaryAddr = (byte) Integer.parseInt("00000001", 2);
- byte bCheckSum = (byte) Integer.parseInt("01011100", 2);
- byte bStop = (byte) Integer.parseInt("00010110", 2);
- byte[] bArray = { bStart, b2ndDigits, bPrimaryAddr, bCheckSum,
- bStop };
- int ch;
- try {
- outStream.write(bArray);
- Thread.sleep(50);
- System.err.println("inStream bytes available: "
- + inStream.available());
- if (inStream.available() > 0) {
- String message = "";
- while (inStream.available() > 0) {
- ch = inStream.read(); // Expecting to receive RSP_UD data as per mBus.
- message += (char) ch;
- }
- dataFile.open(true);
- dataFile.write(message);
- dataFile.close();
- }
- Thread.sleep(50);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- destroyApp(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement