line 14 is not valid java code.

No it does not.. it outputs the first letter of each line instead of the lines with length of one

Please post the code that you are executing that does that.
The code you posted would not compile and can NOT be the code you are executing.

I suggest you make a String with several lines of test data in it and write a small simple program to extract from the String the pieces that you want. Make it a complete self contained program that scans thru the string and parses out the data. For example:
String testData = "First line\nA\nSecond line\nB\n";
Here is a test String that you would want to parse out the A and the B.

Post this complete program when you have problems. It will be something that we can work on for you to get the string parsed.

line 14 is not valid java code.


Please post the code that you are executing that does that.
The code you posted would not compile and can NOT be the code you are executing.

I suggest you make a String with several lines of test data in it and write a small simple program to extract from the String the pieces that you want. Make it a complete self contained program that scans thru the string and parses out the data. For example:
String testData = "First line\nA\nSecond line\nB\n";
Here is a test String that you would want to parse out the A and the B.

Post this complete program when you have problems. It will be something that we can work on for you to get the string parsed.

Unfortunately you will be unable to compile the code as it requires a RXTX library and have the arduino program which is loaded on a microcontroller... and I do have another program that simply takes input by user and works perfectly.. Here's the code.. can also be found on the Arduino website...

import java.awt.List;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;

import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.io.PrintStream;
import javax.swing.JTextField; 

public class SerialTest implements SerialPortEventListener {

	private JFrame frame;
	private JTextArea textArea; 
	private JTextField textField;
	private DataOutputStream dout;
	private DataInputStream din;

	SerialPort serialPort;
	/** The port we're normally going to use. */
	private static final String PORT_NAMES[] = { 
		"/dev/tty.usbmodemfd121" // Mac OS X
		//"/dev/ttyUSB0", // Linux
		//"COM3", // Windows
	};
	/** Buffered input stream from the port */
	private InputStream input;
	/** The output stream to the port */
	private OutputStream output;
	/** Milliseconds to block while waiting for port open */
	private static final int TIME_OUT = 2000;
	/** Default bits per second for COM port. */
	private static final int DATA_RATE = 9600;


	public void initialize() {
		CommPortIdentifier portId = null;
		Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

		// iterate through, looking for the port
		while (portEnum.hasMoreElements()) {
			CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
			for (String portName : PORT_NAMES) {
				if (currPortId.getName().equals(portName)) {
					portId = currPortId;
					break;
				}
			}
		}

		if (portId == null) {
			System.out.println("Could not find MAC OSx USB port.");
			return;
		}


		try {
			// open serial port, and use class name for the appName.
			serialPort = (SerialPort) portId.open(this.getClass().getName(),
					TIME_OUT);

			// set port parameters
			serialPort.setSerialPortParams(DATA_RATE,
					SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);

			// open the streams
			input = serialPort.getInputStream();
			output = serialPort.getOutputStream();

			// add event listeners
			serialPort.addEventListener(this);
			serialPort.notifyOnDataAvailable(true);

			//call gui console.java

			add( new console() );

		} catch (Exception e) {
			System.err.println(e.toString());
		}
	}

	private void add(console console) {
		// TODO Auto-generated method stub

	}

	/**
	 * This should be called when you stop using the port.
	 * This will prevent port locking on platforms like Linux.
	 */
	public synchronized void close() {
		if (serialPort != null) {
			serialPort.removeEventListener();
			serialPort.close();
		}
	}

	/**
	 * Handle an event on the serial port. Read the data and print it.
	 */
	public synchronized void serialEvent(SerialPortEvent oEvent) {

		try{
			//BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
			//String lineFromInput = in.readLine();
                         
                        //outputs to text file
			//PrintStream out = new PrintStream(new FileOutputStream("BlinkFile.txt"));
			//System.setOut(out);


			if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

				try {

					int available = input.available();
					byte chunk[] = new byte[available];
					input.read(chunk, 0, available);

					String blink = new String(chunk);
					
					ArrayList<String> list = new ArrayList<String>();

					Scanner scanner = new Scanner(blink); //useDelimiter("\\RIGHT blink");
					
					try {
						//System.out.println(blink);
						//Read the line into a String
						BufferedReader reader = new BufferedReader(new StringReader(blink));
						String len = reader.readLine();
						if(blink.matches("A"))
								//length () < 10)
							System.out.println("characters: " +blink);	
						
					}
					finally {

						//scanner.close();
					}
				} catch (Exception e) {
					System.err.println(e.toString());
				}
			}
		}catch(Exception e) {

				System.out.println("Error during reading/writing");
			}
		}

		public static void main(String[] args) throws Exception {
			SerialTest main = new SerialTest();
			main.initialize();

			//have console
			//Console.setVisible(true);

		}


	}

...

Since your problem is with parsing a String and has nothing to do with reading the String,
I suggest you make a String with several lines of test data in it and write a small simple program to extract from the String the pieces that you want. Make it a complete self contained program that scans thru the string and parses out the data. For example:
String testData = "First line\nA\nSecond line\nB\n";
Here is a test String that you would want to parse out the A and the B.

Post this complete program when you have problems. It will be something that we can work on for you to get the string parsed.

Your program would be a little bigger than this:

Scanner scanner = new Scanner(blink); //useDelimiter("\\RIGHT blink");

	//System.out.println(blink);
	//Read the line into a String
	BufferedReader reader = new BufferedReader(new StringReader(blink));
	String len = reader.readLine();
	if(blink.matches("A"))
			//length () < 10)
		System.out.println("characters: " +blink);

Excuse me for butting in, but you are trying to do this by creating a Scanner to analyze the blink String. This is completely unnecessarily, and is making your job much harder than it needs to be. You can just use String methods directly on blink without going via a scanner. Eg

if (blink.length() == 1) 
   System.out.println("Single character input received. Character is " + blink);

I'm still not sure what the blink String contains. A single line, a part of a line or multiple lines.

OK, I got the impression it was a single line - but you're right to question it.

My experience reading from devices has been that sometimes the data can be sent in pieces and the reading program has to buffer it and reassemble the lines before passing them on as lines with a line end character. The post from a ways back:

Letters Spoken are RI
Letters Spoken are GHT blink
...

made me think that.

ok. yes, it definitely needs to be confirmed.

ok. yes, it definitely needs to be confirmed.

To confirm, the blink String outputs a single line repeatedly until the person stops blinking. Thus one line appears at a time in the console.

Let's say the person wants to say 'A' the blink sequence is Left Right Right Left Right..so for Left blink appears as and so on.. where each blink is detected via serial event through eclipse..

Console:
LEFT blink
LEFT ready

I will try the following and get back to you soon..

if (blink.length() == 1) 
   System.out.println("Single character input received. Character is " + blink);

ok. yes, it definitely needs to be confirmed.

So here is my output when I tried what you recommended.. it simply checks each line and returns the first character of the string... instead of check if each line is only one character long..

Console:
Single character input received. Character is R
Single character input received. Character is R
Single character input received. Character is L
Single character input received. Character is L
Single character input received. Character is L
Single character input received. Character is L
Single character input received. Character is R
Single character input received. Character is R
Single character input received. Character is L
Single character input received. Character is L
Single character input received. Character is R
Single character input received. Character is R

Single character input received. Character is R
Single character input received. Character is R
Single character input received. Character is R
Single character input received. Character is R
Single character input received. Character is L

it simply checks each line and returns the first character of the string.

if (blink.length() == 1) 
   System.out.println("Single character input received. Character is " + blink);

There is something you are not explaining or showing.
If the line is one character long, the above prints it.
It does not take the first letter of a line longer than 1 character and strip off the rest of the characters.

Change the test code to this:

if (blink.length() == 1) 
   System.out.println("Single character input received. Character is " + blink + "<");
else
   System.out.println("Multi-character input received. Characters are " + blink + "<");

There is something you are not explaining or showing.
If the line is one character long, the above prints it.
It does not take the first letter of a line longer than 1 character and strip off the rest of the characters.

Change the test code to this:

if (blink.length() == 1) 
   System.out.println("Single character input received. Character is " + blink + "<");
else
   System.out.println("Multi-character input received. Characters are " + blink + "<");

I tried the test code above and I get the below... Your logic is correct and I understand what you are implying by the test code and it does exactly that but also proves that the serial data coming into eclipse is perhaps lagging or coming in pieces...I am simply confused and not sure what to do now... any other suggestions? I tried doing an if statement : if (blink.equals(list)) where list is an ArrayList with letters ranging from A-Z... But nothing happens

Console:

Multi-character input received. Characters are LEFT<
Multi-character input received. Characters are blink
<
Multi-character input received. Characters are LEF<
Multi-character input received. Characters are T ready
<
Multi-character input received. Characters are RIG<
Multi-character input received. Characters are HT blink
<
Single character input received. Character is R<
Multi-character input received. Characters are IGHT ready
<
Multi-character input received. Characters are RI<
Multi-character input received. Characters are GHT blink
<
Multi-character input received. Characters are LEF<
Multi-character input received. Characters are T blink
<
Multi-character input received. Characters are LEF<
Multi-character input received. Characters are T ready
<
Single character input received. Character is R<
Multi-character input received. Characters are IGHT ready
<
Multi-character input received. Characters are LEFT<
Multi-character input received. Characters are blink
D
<
Multi-character input received. Characters are RIGHT blink
<
Multi-character input received. Characters are RI<
Multi-character input received. Characters are GHT ready

the serial data coming into eclipse is perhaps lagging or coming in pieces

Yes, that is what I thought could be happening.

You need to buffer the input you receive until you get a line end character. Append the Strings that you get to a buffering String until the line end comes, THEN test the length of the buffering String (minus the lineend char).
In the above that you posted, when the < is on the next line that means that the String that you printed out ended with a line end character.

Yes, that is what I thought could be happening.

You need to buffer the input you receive until you get a line end character. Append the Strings that you get to a buffering String until the line end comes, THEN test the length of the buffering String (minus the lineend char).
In the above that you posted, when the < is on the next line that means that the String that you printed out ended with a line end character.

So this is what I came up with but the only thing that outputs is this single line:

Console:
RRIGHT blink

Am I doing this properly? I am unsure to do the first part of receiving until you get a line end character

public synchronized void serialEvent(SerialPortEvent oEvent) {

		if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

			try {

				int available = input.available();
				byte chunk[] = new byte[available];
				input.read(chunk, 0, available);

				String blink = new String(chunk);
				//ArrayList<String> list = new ArrayList<String>();
			
				//buffer the input
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				
					try { 

					System.out.print(blink);
					
					//read line
					String line = in.readLine();

					while (line != null) {
						
						StringBuffer in1 = new StringBuffer();
						in1.append(line);
						System.out.print(in1);
						line = in.readLine();

						//Test the length of buffering String minus
						if(blink.length() == 1)

						       System.out.println("Single " + blink +" \n");
						     //character.append(blink);

						else
							System.out.println("Multi " + blink +" \n");
                                                       //sequence.append(blink);
                                   }
				}
					finally {

						//scanner.close();
					}
				} catch (Exception e) {
					System.err.println(e.toString());
				}
			}

		}
	}

You should wait until you get a lineend character before you build the blink String. Save the bytes read in a buffer until the lineend is received then build the String/line in blink.

You should wait until you get a lineend character before you build the blink String. Save the bytes read in a buffer until the lineend is received then build the String/line in blink.

This is my modified code:

public synchronized void serialEvent(SerialPortEvent oEvent) {

		if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

			try {

				int available = input.available();
				byte chunk[] = new byte[available];
				input.read(chunk, 0, available);

				String blink = new String(chunk);

				try { 

					BufferedReader in4 = new BufferedReader(new StringReader(blink));

					String s = null;
					String[] seq = {" "};
					int i=0;
					String EOL = ",";         //End of line

					//read line
					while((s = in4.readLine()) != null){

						//detect if line has end of line 
						if (s.endsWith(EOL)){

							// Append the Strings that you get to a buffering String until eol
							BufferedReader in5 = new BufferedReader(new StringReader(s));

							String sa = in5.readLine();

							// THEN test the length of the buffering String (minus the lineend char).
							if(sa.replaceAll(",","").length() == 1){

								//use delimiter
								character.append(sa);
								character.setCaretPosition(character.getDocument().getLength());

								//save  s to array for every new letter
								seq[i] = s; 
								//	System.out.print(seq[i++]);
								PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("BlinkFile.txt")));
								out1.print(seq[i++] );
								out1.close();

							}

							//if(s=="SPEAK"){

							//	PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("BlinkFile.txt")));
							//	out1.print(seq[i++] );
							//}
							else

								sequence.append(sa);
								sequence.setCaretPosition(sequence.getDocument().getLength());
						}
					}
				}

				finally {

					//scanner.close();
				}
			} catch (Exception e) {
				System.err.println(e.toString());
			}
		}

	}

And my resulting output is the following but on one single line and no string of one length is detected..

EFT blink,HT blink,T ready,GHT blink,FT ready,LEFT blink, ready,EFT blink,FT ready,FT blink,T ready,T ready,IGHT blink,HT ready,T blink,T blink,GHT ready,IGHT blink, ready,GHT ready,

Where are the missing letters going?

For debugging you show print out the value of blink before scanning it:
println("blink=" + blink +"<");

To debug your scanning code, hardcode a typical value for blink, compile and execute it. Copy the output and paste it here and add comments saying what is wrong with the output and show what the output should look like. Also post the testing code.

Where are the missing letters going?

For debugging you show print out the value of blink before scanning it:
println("blink=" + blink +"<");

To debug your scanning code, hardcode a typical value for blink, compile and execute it. Copy the output and paste it here and add comments saying what is wrong with the output and show what the output should look like. Also post the testing code.

I have no idea where the missing letters are going,

This is my output right now
blink=LEF<
blink=T blink,
<
blink=RIG<
blink=HT blink,
<
blink=LEF<
blink=T ready,
<
blink=L<
blink=EFT blink,
<
blink=R<
blink=IGHT ready,
<
blink=LEFT<
blink= ready,
<
blink=LEF<
blink=T blink,
<
blink=RIGH<
blink=T blink,
E
<
blink=LE<
blink=FT ready,
<
blink=RI<
blink=GHT ready,
<
blink=RIG<
blink=HT blink,
<
blink=R<
blink=IGHT ready,
etc

Before String blink gets buffered..

public synchronized void serialEvent(SerialPortEvent oEvent) {

		if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

			try {

				int available = input.available();
				byte chunk[] = new byte[available];
				input.read(chunk, 0, available);

				String blink = new String(chunk);

				try { 
					
					System.out.println("blink=" + blink +"<");
					
					BufferedReader in4 = new BufferedReader(new StringReader(blink));

					String s = null;
					String[] seq = {" "};
					int i=0;
					String EOL = ",";         //End of line
					
					//read line
					while((s = in4.readLine()) != null){

						//detect if line has end of line 
						if (s.endsWith(EOL)){
							
							// Append the Strings that you get to a buffering String until eol
							BufferedReader in5 = new BufferedReader(new StringReader(s));

							String sa = in5.readLine();

							// THEN test the length of the buffering String (minus the lineend char).
							if(sa.replaceAll(",","").length()==1){

								character.append(sa.replaceAll(",",""));
								character.setCaretPosition(character.getDocument().getLength());

								//save  s to array for every new letter
								seq[i] = s; 
								//	System.out.print(seq[i++]);
								PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("BlinkFile.txt")));
								out1.print(seq[i++] );
								out1.close();
							}
						}
						//if(s=="SPEAK"){

						//	PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("BlinkFile.txt")));
						//	out1.print(seq[i++] );
						//}
						else{

							sequence.append(s.replaceAll(",", ""));
							sequence.setCaretPosition(sequence.getDocument().getLength());
					}
				}
				}
				//	}
				//}

				finally {

					//scanner.close();
				}
			} catch (Exception e) {
				System.err.println(e.toString());
			}
		}

	}

I'm lost right now and not entirely sure what to do since String blink comes in so choppy.

You need to redesign the logic.
When is the serialEvent() method called?
When do you have a full line (String with lineend) to scan?
It can take many calls to the method before you get a line.
Don't scan the line until you have all of it.
In some calls, you will only save the data that was read and not do a scan because you do not have a line to scan yet. Wait for the line end before scanning.

String EOL = ","; //End of line

What is this? A comma not a \n???

You need to redesign the logic.
When is the serialEvent() method called?
When do you have a full line (String with lineend) to scan?
It can take many calls to the method before you get a line.
Don't scan the line until you have all of it.
In some calls, you will only save the data that was read and not do a scan because you do not have a line to scan yet. Wait for the line end before scanning.


What is this? A comma not a \n???

After doing more testing, nothing has changed and the incoming data still appears as

blink=RIGH<
blink=LEF<
blink=T blink
<
blink=RIG<
blink=HT blink
<
blink=RIG<
blink=HT ready
<
blink=LEF<
blink=T ready
.
.
.
etc

So after some research I realized I was not the only one with this problem, as I am using an Arduino UNO board and sending data at a baud rate of 9600, there seems to be lagged data. Apparently there is an issue with latency which may have to do with libRXTX, a library required to have a serial communication with Arudino... In other words, when I run the processing code without the serial writing everything is fine and runs at the intended speed. However, when I add the serial writing in, everything becomes slightly slow and choppy...

In that case it may be an idea to run your code in two threads - a high priority thread that just accepts input as it arrives and puts it on a thread-safe FIFO queue (a LinkedBlockingQueue should do perfectly). That should avoid any latency issues. Then have a lower priority thread that takes the data from the queue and processes it without holding up the input?

In that case it may be an idea to run your code in two threads - a high priority thread that just accepts input as it arrives and puts it on a thread-safe FIFO queue (a LinkedBlockingQueue should do perfectly). That should avoid any latency issues. Then have a lower priority thread that takes the data from the queue and processes it without holding up the input?

That sounds like a good idea, but I dont exactly know how to implement such method, I understand the theory but not the actual Java implementation... Also I;m working on other parts of my project and this is the last aspect to do it read the incoming lines properly...

It's nothing too hard - here's the skeleton (omits exception handling and shutdown logic)

// inout that has been recieved and is waiting to be processed...
LinkedBlockingQueue<char[]> inputBuffer = new LinkedBlockingQueue<char[]>();

// callback(?) runs at normal priority...
// does an absolute minimum or work to avoid latency problems
public synchronized void serialEvent(SerialPortEvent oEvent) {
  if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
     try {
        int available = input.available();
        byte chunk[] = new byte[available];
        input.read(chunk, 0, available);
        inputBuffer.put(chunk);  // just save the data and return asap
     } catch ...
     }
  }
}

// all the processing of the input is here, in its own thread...
class inputProcessor implements Runnable {
...
public void run() { 
   while (true) { 
      char[] chunk = inputBuffer.take();  // waits if nothing available
      ... process this chunk at your leisure
   }
}
...
// start input processing to run at a low priority...
Thread processingThread = new Thread(new inputProcessor());
processingThread.setPriority(Thread.MIN_PRIORITY);
processingThread.start();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.