Galera alguem por favor poderia me dizer como faço pra formatar um byte em string.
Estou lendo dados de uma balaça, e os dados via Hyper Terminal vem assim:
-
112 g ...
Na minha aplicação leio os dados e ele vem assim:
-
1
12 g
Ou seja fiz um display para interfacear com o usuário do que so aparece o 12g sendo que são 112g.
Ja fiz de tudo que imaginei e pensei …
segue o codigo:
/*
- HoraData.java
- Created on 6 de Maio de 2006, 00:03
- To change this template, choose Tools | Template Manager
- and open the template in the editor.
*/
package Control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.sql.rowset.serial.SerialArray;
/**
*
-
@author Rogerio
*/
public class CommBalanca implements Runnable, SerialPortEventListener{static CommPortIdentifier portId; static Enumeration portList; private InputStream inputStream; private SerialPort serialPort; private Thread readThread; private byte[] readBuffer;
/** Creates a new instance of HoraData */ public CommBalanca() { detectaRS232();
}
private void detectaRS232(){ portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(“COM1”)) { setConfPorta(); } } } }
private void setConfPorta(){ try { serialPort = (SerialPort) portId.open(“Lendo”, 2000); } catch (PortInUseException e) {} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
} catch (UnsupportedCommOperationException e) {}
}
public void serialEvent(SerialPortEvent serialPortEvent){ switch(serialPortEvent.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: readBuffer = new byte[12];
try { while (inputStream.available() > 0) { int numBytes = inputStream.read(getReadBuffer()); // System.out.println("numBytes: " + numBytes); } System.out.println(new String(readBuffer).trim()); } catch (IOException e) {} break;
}
}public byte[] getReadBuffer() { return readBuffer; } public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) {} }
}
Nada mais eh que o SimpleRead da Sun.
