freelanceprogrammers.org Forum Index » Java
Collecting data from another class
Joined: 26 Apr 2004
Posts: 1
Collecting data from another class
Hi all,
I need some help here, I`m trying to take output data from a class using another
class.I`m a little bit confuse how to do it. Maybe someone can help me. I have
attached the source of the class that i want to take the data from.
Many thanks in advance.
Inge.
---------------------------------------------------------------
import javax.comm.*;
import java.io.*;
import java.awt.event.*;
import java.util.TooManyListenersException;
public class ADCConnection implements SerialPortEventListener {
private OutputStream os;
private InputStream is;
private CommPortIdentifier portId;
private SerialPort sPort;
private boolean open;
private StringBuffer inputBuffer;
private int[] analogValues, prevValues;
private int digitalValue;
public String s;
//channelConversion is a crazy little thing used to renumber the analog
values the ADC sends us
private final static int[] channelConversion={0, 2, 4, 6, 1, 3, 5, 7};
final static int jitterThreshhold=2; // used to compensate for noise from
ADC
// these are this things to change if for some reason the ADC changes
public final static String portName="COM1";
public final static int baudRate = 9600;
public final static int flowControlIn = SerialPort.FLOWCONTROL_NONE;
public final static int flowControlOut = SerialPort.FLOWCONTROL_NONE;
public final static int dataBits = SerialPort.DATABITS_8;
public final static int stopBits = SerialPort.STOPBITS_1;
public final static int parity = SerialPort.PARITY_NONE;
public ADCConnection()
{
analogValues=new int[8];
prevValues=new int[8];
for (int i=0; i<analogValues.length; i++) {
analogValues[i]=0;
prevValues[i]=0;
}
digitalValue=0;
inputBuffer=new StringBuffer(); // this is used in the input event handling
open = false;
}
public boolean openConnection() { // returns true on successful opening
// get CommPortIdentifier object for portName
try {
portId = CommPortIdentifier.getPortIdentifier(portName);
} catch (NoSuchPortException e) {
System.err.println(e.toString());
return false;
}
// Open the port -- give it a one second timeout
try {
sPort = (SerialPort)portId.open("SerialDemo", 1000);
} catch (PortInUseException e) {
System.err.println(e.toString());
return false;
}
// Set the parameters of the connection. If they won`t set, close the
// port before throwing an exception.
try {
sPort.setSerialPortParams(baudRate, dataBits, stopBits, parity);
sPort.setFlowControlMode(flowControlIn | flowControlOut);
} catch (UnsupportedCommOperationException e) {
sPort.close();
return false;
}
// Open the input and output streams for the connection. If they won`t
// open, close the port before throwing an exception.
try {
os = sPort.getOutputStream();
is = sPort.getInputStream();
} catch (IOException e) {
sPort.close();
System.err.println(e.toString());
return false;
}
// Add this object as an event listener for the serial port.
try {
sPort.addEventListener(this);
} catch (TooManyListenersException e) {
sPort.close();
System.err.println(e.toString());
return false;
}
// Set notifyOnDataAvailable to true to allow event driven input.
sPort.notifyOnDataAvailable(true);
try {
sPort.enableReceiveTimeout(30);
} catch (UnsupportedCommOperationException e) {
System.err.println(e.toString());
return false;
}
open = true;
return true;
}
public void closeConnection()
{
// If port is alread closed just return
if (!open) {
return;
}
// Check to make sure sPort has reference to avoid a NPE
if (sPort != null) {
try {
// close the i/o streams.
os.close();
is.close();
} catch (IOException e) {
System.err.println(e);
}
sPort.close();
}
open = false;
}
public boolean isOpen() {
return open;
}
public void poll() { // send the analog poll string (do nothing if not open
yet)
if (open)
{
try {
int delay=1; // don`t know why each command must be sent separately, but
that`s how it is
// the "UX" command ask for analog values
os.write("U8 ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("U9 ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UA ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UB ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UC ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UD ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UE ".getBytes());
os.flush();
Thread.sleep(delay);
os.write("UF ".getBytes());
os.flush();
Thread.sleep(delay);
// "I" command gets digital state
os.write("I ".getBytes());
os.flush();
Thread.sleep(delay);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
public int[] getAnalogValues()
{
// fiddle w/ prevValues array to do some anti-jitter work
for (int i=0; i<analogValues.length; i++) {
if (Math.abs(analogValues[i]-prevValues[i])<=jitterThreshhold) {
analogValues[i]=prevValues[i];
} else {
prevValues[i]=analogValues[i];
}
}
return analogValues;
}
public int getDigitalValue()
{
return digitalValue;
}
public void serialEvent(SerialPortEvent e) {
int newData = 0;
if (e.getEventType()== SerialPortEvent.DATA_AVAILABLE) { // this is the only
event type we care about
// Read data until -1 is returned.
while (newData != -1) {
try {
newData = is.read();
if (newData == -1) {
break;
}
if (` ` == (char)newData) { // carriage return signals end of message
s=new String(inputBuffer); //output value ----> this is the output that
I want to take
inputBuffer = new StringBuffer(); // reset this for later
handleMessage(s); // decipher the message
} else { // keep storing incoming characters until
inputBuffer.append((char)newData);
}
} catch (IOException ex) {
System.err.println(ex);
return;
}
}
}
}
private void handleMessage(String s)
{
char command=s.charAt(0);
if (command == `U`)
{
int channel;
try {
channel=channelConversion[Integer.parseInt(s.substring(1,2), 16)-8];
} catch (Exception e) { // could be numberformat or arrayoutofbounds or
whoknows
channel=0;
System.out.println("problem interpretting channel value");
return; // don`t want to actually set any value if the ADC is sending back
junk
}
try
{
analogValues[channel]=Integer.parseInt(s.substring(2,4), 16); // only look
at first two hex digits of the value, the third is too noisy
} catch (NumberFormatException e)
{
System.out.println("trouble parsing analog value from ADC: "+s);
}
} else if (command == `I`)
{
try
{
digitalValue=Integer.parseInt(s.substring(1,3), 16);
} catch (NumberFormatException e)
{
System.out.println("trouble parsing digital value from ADC: "+s);
}
}
}
}
[Non-text portions of this message have been removed]
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







