MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Has anyone got Java to read from Serial Port?

Tue Jun 12, 2012 11:42 pm

Hi All,

Is anyone using Java to read from the serial port. Both RxTx and purejavacomm aren't working at the moment. They both reading in a few packets - the the process hangs and the CPU hits the roof.

If you have I'd be very interested to hear.

Am using OpenJDK 6 at the moment (How do you install the Sun version?)

Many Thanks

Mark

therealeasterbunny
Posts: 77
Joined: Thu Feb 02, 2012 5:53 pm
Contact: Website

Re: Has anyone got Java to read from Serial Port?

Wed Jun 13, 2012 12:21 pm

If you can post sample code you are using into a code window, I can try it with my DEC VT - PL2303 (Serial to USB) - Pi and then let yuo know the outcome of my tests.

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Has anyone got Java to read from Serial Port?

Tue Jun 19, 2012 12:57 pm

You can also use ser2net to simplify your serial port setup.
apt-get install ser2net and then edit the /etc/ser2net.conf
In one of my projects i have used this ser2net tool to decouple my Java applications from the lowlevel serial IO. I only have to create a localhost Java TCP/IP connection to the ser2net tool that in turn know how to talk to the serial port itself.

Cheers
Xerxes
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Thu Jun 21, 2012 7:05 pm

therealeasterbunny

this is the main side of the serial reader code. For now (whilst I've been hacking bits out it's pretty much nothing more than a serial reader.

Cheers

Mark

Code: Select all

package robotDataServer;
/*
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
*/

import purejavacomm.*;

import java.io.*;
//import java.nio.ByteBuffer;
import java.util.*;

public class SerialInterface implements SerialPortEventListener
{
	static CommPortIdentifier portId;
    @SuppressWarnings("rawtypes")
	static Enumeration portList;
    InputStream inputStream;
    OutputStream outputStream;

	SerialPort serialPort;
	byte[] m_ReadBuffer = new byte[253];
	byte[] m_Preamble = new byte[]{(byte) 255, (byte)254, (byte)255};
	byte[] m_LastBuffer = new byte[253];
	byte[] m_CurrentBuffer = new byte[253];
	byte[] m_CorrectedBufferData = new byte[253];
	
	byte[] m_TmpReadBuffer = new byte[253];
	
	int m_BytesRxed =  -1;
	volatile static int m_TotalBytesRead = 0;
	
	
	boolean m_GetHeader = true;
	boolean m_GetTail = false;
	int m_HeaderOffset = 0;
	int m_PacketProcessIndicator = 0;
	
	int m_PacketIDCount = 0;
	int m_LastPacketID = 0;
	
	void openSerialPort(String SerialPortID)
	{
		System.out.println("Attempting to open Serial Port...");
        //portList = CommPortIdentifier.getPortIdentifiers();

        //if (portList == null)
        	//System.out.println("No Serial Ports Found...");
        
        //System.out.println("Attempting to open Serial Port3...");
        //while (portList.hasMoreElements()) 
        {
            //portId = (CommPortIdentifier) portList.nextElement();
            
            //System.out.println("Found System Port: " + portId.getName());
            //if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {
                 //if (portId.getName().equals(SerialPortID)) 
                 {
                	//System.out.println("Found System Port: " + portId.getName());
                	System.setProperty("gnu.io.rxtx.SerialPorts", SerialPortID);
               
                	
                	CommPortIdentifier portId = null;
					try 
					{
						portId = CommPortIdentifier.getPortIdentifier(SerialPortID);
					} 
					catch (NoSuchPortException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				    try 
				    {
				        serialPort = (SerialPort) portId.open("SerialInterface", 5000);
				    } 
				    catch (PortInUseException e) 
				    {
				    	System.out.println(e);
				    }
								    
				    try 
				    {
				        inputStream = serialPort.getInputStream();
				        outputStream = serialPort.getOutputStream();

				    } 
				    catch (IOException e)
				    {
				    	 System.out.println(e);
				    }

				    try 
				    {
				        serialPort.setSerialPortParams(	19200,
											            SerialPort.DATABITS_8,
											            SerialPort.STOPBITS_1,
											            SerialPort.PARITY_NONE);
				        
				        //serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
	                      //        | SerialPort.FLOWCONTROL_RTSCTS_OUT);

				        //serialPort.setRTS(true);
				        
				        
				        serialPort.notifyOnDataAvailable(true);
				        
				        try 
				        {
							serialPort.addEventListener(this);
						} 
				        catch (TooManyListenersException e) 
				        {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

				    } 
				    catch (UnsupportedCommOperationException e)
				    {
				    	 System.out.println(e);
				    }
                 }
            }
		}
    }
	
	public void serialEvent(SerialPortEvent e) 
	{
		int lNumBytes = -1;
		int lBufferPos = 0;
		
		try 
		{
			switch (e.getEventType()) 
			{
				case SerialPortEvent.DATA_AVAILABLE: 
				{
					//Thread.sleep(100);
					//System.out.println("Pre-Read");
					try
					{
						lNumBytes = inputStream.read(m_TmpReadBuffer);
					}
					catch (IOException p)
					{						
						p.printStackTrace();
					}
					
					//System.out.println("Post-Read");
					
					if (lNumBytes != -1) 
					{			
						System.out.println("lNumBytes: " + lNumBytes);
						// If we didn't receive a full packet
						/*if (lNumBytes < 253)
						{
							//m_ReadBuffer
							//System.arraycopy(src, srcPos, dest, destPos, length)''
							if ((m_TotalBytesRead+1 + lNumBytes) < 253)
							{
								System.arraycopy(m_TmpReadBuffer, 0, m_ReadBuffer, m_TotalBytesRead+1, lNumBytes);
								m_TotalBytesRead = m_TotalBytesRead + lNumBytes;
							}
							else
							{
								m_TotalBytesRead = m_TotalBytesRead + lNumBytes;
							}
						}
						else
						{
							m_ReadBuffer = m_TmpReadBuffer;
						}
						*/
						// We either got a full read in one go or built up a full read (m_TotalBytesRead)
						if (lNumBytes >= 253 || m_TotalBytesRead >= 253  )
						{
							m_BytesRxed = lNumBytes;
							m_TotalBytesRead = 0;
								
							if (correctSerialPacketData(m_ReadBuffer, 253) == true)
							{
					    		String lDataString = new String(m_CorrectedBufferData);
					    		StringTokenizer  st = new StringTokenizer(lDataString, ",");
					    		
					    		//System.out.println( m_CorrectedBufferData);
		
					    		int lTolkenCtr = 0;
					    		
					    		try
								{ 
						   			while(st.hasMoreTokens())
						   			{				
						   			  String s = st.nextToken();
						   			  s = s.trim();
						   			  
						   			  if (lTolkenCtr == 2)
						   			  {
						   				  int lPacketID = Integer.parseInt(s);
						   				  
						   				  if (lPacketID != m_LastPacketID+1)
						   				  {
						   					  m_PacketIDCount++;
						   					
						   					  System.out.println("PacketID @ SerialReader: " + m_PacketIDCount);
						   				  }
						    				  
						   				  m_LastPacketID = lPacketID;
						   				  break;
						   			  }
						   			  lTolkenCtr++;	
						   			}
								}
								catch(NumberFormatException ex)
								{
		
								}
							}

							//read data in
						} // end(lNumBytes >= 253)						
					}
					
				} // end case
			} // end switch
		
		}
		catch (Exception p) 
		{
			p.printStackTrace();
		}
	} // end serialEvent
	
	int getBytesReceived()
	{
		int lTmp = m_BytesRxed;
		m_BytesRxed = 0;
		return lTmp;
		
	}
	int findPacketStart(byte[] DataPacket, int packetSize)
	{
		int index = -1;

		for (int i=0; i<packetSize-3; i++)
		{
			if ( (DataPacket[i] == m_Preamble[0]) &&
				 (DataPacket[i+1] == m_Preamble[1]) &&
				 (DataPacket[i+2] == m_Preamble[2]) )
			{
				return i;
			}
		}
		
		return index;
	}
	
	boolean correctSerialPacketData(byte[] DataPacket, int packetSize)
	{
		int lPacketStartPosition = 0;
		
		lPacketStartPosition = findPacketStart(DataPacket, packetSize);
		
		if (lPacketStartPosition == 0)
		{
			m_CorrectedBufferData = Arrays.copyOfRange(DataPacket, lPacketStartPosition, packetSize);
			return true;
		}
		
		// Initialise
		if ( lPacketStartPosition!= -1 && m_PacketProcessIndicator == 0)
		{
			m_LastBuffer = Arrays.copyOfRange(DataPacket, 0, packetSize);
			m_PacketProcessIndicator++;

		}
		else if ( lPacketStartPosition!= -1 )
		{			
			if (m_PacketProcessIndicator == 1)
			{
				m_CurrentBuffer = Arrays.copyOfRange(DataPacket, 0, packetSize);
				
				int lLastPacketStartPosition = findPacketStart(m_LastBuffer, packetSize);		
				int lLastBytesToCopy = (packetSize-1)-lLastPacketStartPosition;
				
				System.arraycopy(m_LastBuffer, lLastPacketStartPosition, m_CorrectedBufferData, 0, lLastBytesToCopy);
				
				int lCurrentPacketStartPosition = findPacketStart(m_CurrentBuffer, packetSize);
				
				int lCurrentBytesToCopy = packetSize-((packetSize-1)-lLastPacketStartPosition);
				
				@SuppressWarnings("unused")
				int lCheckQty = lLastBytesToCopy + lCurrentBytesToCopy;
				int lCurrArrayPos = packetSize-lLastPacketStartPosition;
				
				if ( lCurrentPacketStartPosition > 1)
					System.arraycopy(m_CurrentBuffer, 0, m_CorrectedBufferData, lCurrArrayPos, lCurrentBytesToCopy-1);
				else
					System.arraycopy(m_CurrentBuffer, 0, m_CorrectedBufferData, lCurrArrayPos, 1);
				
				// Make the current buffer now the last buffer
				
				m_LastBuffer = Arrays.copyOfRange(m_CurrentBuffer, 0, packetSize);
				
				return true;
			}			
		}
		
		return false;
	}
	

	void writeSerialData(byte[] DataToWrite, int BytesToWrite) throws IOException
	{		
		DataToWrite[0] = (byte) 255;
		outputStream.write(DataToWrite, 0, BytesToWrite);
		outputStream.flush();
	}
    
    public byte[] getDataBuffer()
    {
   	 byte[] lstr = m_CorrectedBufferData.clone();
   	 return lstr;
    }
}


therealeasterbunny
Posts: 77
Joined: Thu Feb 02, 2012 5:53 pm
Contact: Website

Re: Has anyone got Java to read from Serial Port?

Thu Jun 21, 2012 7:16 pm

thanks MarkyMark

I will be getting the Pi and VT out at the weekend (if not sooner) so will save the code now to my laptop then poke and respond back with my results of the Wigan jury soon.

Cheers

Steve

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Fri Jun 22, 2012 7:10 am

Cheers Steve - what be interested to hear what the Wigan jury has to say on the matter :)

Kemp
Posts: 29
Joined: Tue Jul 03, 2012 12:56 pm

Re: Has anyone got Java to read from Serial Port?

Tue Jul 03, 2012 1:35 pm

I've been having the same problem with the TinyOS serial forwarder program. When I start it the Pi immediately hangs, presumably after doing some operation on /dev/ttyUSB0 (the program does almost nothing other than reading from the "serial port" and passing the data on).

My issue may or may not be related, but it's another datapoint for you possibly?

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Thu Jul 05, 2012 2:44 pm

Soz for the late reply.

For some reason my Java code works perfectly okay if using a USB<->Serial converter but not if using the build in serial port from the chip it's self.

I've disable any console output on that port - so I'm not at all sure what's going on?

Mark

michas
Posts: 32
Joined: Fri Sep 14, 2012 4:19 pm

Re: Has anyone got Java to read from Serial Port?

Fri Sep 14, 2012 4:33 pm

Hi

Did you maybe solved the issue. I have the same problem while trying to use rxtx library
to send data to serial port on Debian RaspBerry.

Thank you

User avatar
jbeale
Posts: 3709
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Has anyone got Java to read from Serial Port?

Fri Sep 14, 2012 4:55 pm

Is there any chance it could be due to the on-chip serial port having just TX and Rx signals, there are no control signals like CTS, DTR, RTS etc.

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Sat Sep 15, 2012 10:37 am

Hi,

Yes I did. I needed to make sure that both /boot/cmdline.txt and /etc/innitab are updated to remove references to the serial port. i.e. prevent the kernel from use it

Example here: http://baldwisdom.com/category/raspberry-pi/

Mark

michas
Posts: 32
Joined: Fri Sep 14, 2012 4:19 pm

Re: Has anyone got Java to read from Serial Port?

Mon Sep 17, 2012 7:00 pm

Hi MarkyMark

Thank you for your response but unfortunately with the two files update I still can't send data to serial
port. Maybe because I want to send data throw the USB port and as I can read in your link, for the
USB port there is no need to disable logging. Am I right?

".... so if you are using an Arduino via USB, it’s not necessary."

I will try to describe the environment on my RaspBerry so maybe you (or maybe someone else) can
still find a solution to this problem.

On the RaspBerry I have:
1) installed (SD card) Raspbian “wheezy” (exactly 2012-08-16-wheezy-raspbian.img)
2) installed JAVA (apt-get install openjdk-7-jdk)
3) installed RXTX library (alt-get install librxtx-java).

4) deployed my Java application on the RaspBerry. The application uses rxtx library to send data
to USB port.

5) Connected the external module to USB. The external device has a FTDI chip.


The USB serial port (on which the module has been connected) has been identified as /dev/ttyUSB0.
This was also the name of the serial port I set in my application.

Once I run the application and send data to selected serial port the application simply freeze.
There is no way to stop (just kill the java process).

If you have a suggestion please let me know.

Thank you in advance.
Mitja

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Mon Sep 17, 2012 7:24 pm

Hi Mitja,

Yes you're correct re: the USB port. I was able to use Java on the Pi via a USB<-> Serial adapter no problems.

How my logging do you have in your application? I ended up going down the route of lost of catches for anything that was thrown and quite a few system.put.println()!! - Not elegant I know. But did help be figure our what was and was not working.

How far is your code getting?

Mark

michas
Posts: 32
Joined: Fri Sep 14, 2012 4:19 pm

Re: Has anyone got Java to read from Serial Port?

Mon Sep 17, 2012 8:23 pm

It's very strange. If I add an non existing COM port (example ttyUSB23) my program first
view the list of detected serial ports (one of them is also /dev/ttyUSB0) and after that an
exception is thrown that no data can be sent to selected port.... which is OK because there
is no ttyUSB23 port.

But if I add /dev/ttyUSB0 as a selected port to which send data, nothing happened. No message
no System.out.println, neither the list of detected serial ports is shown.

Is maybe something wrong with this serial port name? Should I address it in another way with
another name?

Mitja

MarkyMark
Posts: 23
Joined: Fri Mar 09, 2012 3:50 pm

Re: Has anyone got Java to read from Serial Port?

Mon Sep 17, 2012 9:00 pm

My opening code for the port

Code: Select all

System.setProperty("gnu.io.rxtx.SerialPorts", SerialPortID);
               
                	
                	CommPortIdentifier portId = null;
					try 
					{
						portId = CommPortIdentifier.getPortIdentifier(SerialPortID);
					} 
					catch (NoSuchPortException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

michas
Posts: 32
Joined: Fri Sep 14, 2012 4:19 pm

Re: Has anyone got Java to read from Serial Port?

Tue Sep 18, 2012 9:52 pm

Hi MarkyMark

I took a look at your code posted at Thu Jun 21, 2012 9:05 pm and realized that the problem are the
serial port params with which I open the serial port. If I change my params to 19200 8N1 (as you have)
the serial port on my RaspBerry works perfectly.
But there is another problem because my device attached to the RaspBerry via USB need to work
on/with 38400 baud rate.

Do you know if there is a way or maybe a setting on RaspBerry to make it send data with 38400 baud
rate?

Once again.
If I open the serial port like ......
serialPort.setSerialPortParams(38400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

........ the application freeze.

If I open the serial port like .....
serialPort.setSerialPortParams(19200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

....... I can send data via USB port but my device doesn't work properly.


Any idea?
Mitja

wizarg
Posts: 1
Joined: Thu Dec 05, 2013 12:33 pm

Re: Has anyone got Java to read from Serial Port?

Thu Dec 05, 2013 12:37 pm

Hi Mitja, did you find a way to communicate via serial port at 38400 baud rate with Raspberry?
We are developing a Java application and we have the same poblem.
Best regards,

Wizarg

michas
Posts: 32
Joined: Fri Sep 14, 2012 4:19 pm

Re: Has anyone got Java to read from Serial Port?

Mon Dec 30, 2013 11:01 pm

Hi Wizarg

Yes, I have a working application on Raspberry that sends data via serial port at 38400 baud rate.
What exactly is your problem ?

Mitja

Return to “Troubleshooting”