Serial Programming Logic Error???
Codes for SerialReadByEvents
Code :
/*
* SerialReadByEvents.java
*
* Created on September 22, 2011, 1:21 PM
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
*/
import java.awt.*;
import java.io.*;
import javax.comm.*;
import java.util.*;
/**
* Read from a Serial port, notifying when data arrives.
* Simulation of part of an event-logging service.
*/
public class SerialReadByEvents extends CommPortOpen
implements SerialPortEventListener {
public static void main(String[] argv)
throws IOException, NoSuchPortException, PortInUseException,
UnsupportedCommOperationException {
new SerialReadByEvents(null).converse();
}
/* Constructor */
public SerialReadByEvents(Frame f)
throws IOException, NoSuchPortException, PortInUseException,
UnsupportedCommOperationException {
super(f);
}
protected BufferedReader ifile;
byte ans[] = new byte[255];
/**
* Hold the conversation.
*/
protected void converse() throws IOException {
if (!(thePort instanceof SerialPort)) {
System.err.println("But I wanted a SERIAL port!");
System.exit(1);
}
// Tell the Comm API that we want serial events.
((SerialPort)thePort).notifyOnDataAvailable(true);
try {
((SerialPort)thePort).addEventListener(this);
} catch (TooManyListenersException ev) {
// "CantHappen" error
System.err.println("Too many listeners(!) " + ev);
System.exit(0);
}
// Make a reader for the input file.
ifile = new BufferedReader(new InputStreamReader(is));
//
}
int i=0;
public void serialEvent(SerialPortEvent ev) {
String line;
try {
is = new DataInputStream(thePort.getInputStream());
ifile = new BufferedReader(new InputStreamReader(is));
System.out.println(ifile.read()+" dfvvvvvvft "+ (i++));
} catch (IOException e) {
System.err.println("Read failed");
System.exit(1);
}
catch (IOException ex) {
System.err.println("IO Error " + ex);
}
}
}
my error is the java program will only get the input from the serial port the first time, all subsequent inputs are ignored and was not displayed on the console.. need help on this.. thanks...
Re: Serial Programming Logic Error???
A BufferedReader decouples reads of the source of data from reads by the consumer, so when you .read() on a BufferedReader, it'll return only one byte to you, but may read much more from the source - up to the size of its internal buffer. You create a new BufferedReader on every event, each one of which may read more from the serial port than you ask them to with read()...
Re: Serial Programming Logic Error???
now my code has a new logic error...
Code :
switch (ev.getEventType())
{
case SerialPortEvent.DATA_AVAILABLE:
try {
is = new DataInputStream(thePort.getInputStream());
ifile = new BufferedReader(new InputStreamReader(is));
c= ifile.read();
while (c>=0)
{
test=(char)c;
line =line +test;
System.out.println(line +" dfvvvvvvft "+ (i++));
System.out.println("sux v3");
c=ifile.read();
}
total = total +(Double.parseDouble(line));
System.out.println("Total" +total);
} catch (IOException ex) {
System.err.println("IO Error " + ex);
}
break;
default:
break;
}
it did not go the line the add the total and print out the current total amount.. it just exit the try block and preparing itself for the next input..
Re: Serial Programming Logic Error???
You're not showing us enough code - like where 'c' is declared, for example. I suspect that unless you've properly fixed your initial logic problem that you have a race condition. Try declaring c inside the try block. You don't need it outside, so it's only good practice to limit your variable scope to as small a code block as possible.