Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Java problems using RXTX and Universal Bit Whacker (UBW)(Serial Port Problems)
icon5.gif  Java problems using RXTX and Universal Bit Whacker (UBW) [message #666943] Sun, 24 April 2011 22:17
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 7
Registered: April 2011
Location: USA
Junior Member
I have a Universal Bit Whacker (UBW) that I bought from http://www.sparkfun.com and I am having a bit of trouble communicating to it over the serial port (virtual serial port via USB).

I wrote a simple program to blink an LED. Works fine with C# in Linux using MonoDevelop. But I need to do this in Java so I installed the RXTX plugin for Eclipse and tried to get it to work. It seems like it works, but the LED never blinks. The only difference in the code for C# has to do with the Serial Port so I am assuming that is my problem, that I am doing something incorrectly. I am still a beginner with Java, but it is close enough to C# that I have not had that many problems until now. I was wondering if anyone has used RXTX and might know what I am doing wrong.

I tried running the program while the C# program was running and did not get any errors which is strange. I should have gotten errors about the port already in use or some kind of exception, but I did not. The application seems to be ignoring anything related to the serial port.

Confused


This is my Java code:

It is a bit long, but eventually I will separate out the serial port code into a different class file, if I ever get it to work.

import java.io.*;
import java.util.*; 
import gnu.io.*;

import java.util.Enumeration;

public class UBW_Console 
{

	/**
	 * @param args
	 */
	
	static SerialPort serialPort = null;
	static OutputStream outStream = null;
	static InputStream inStream = null;
	
	public void portsList(List<String> list)
    {
		
        @SuppressWarnings("rawtypes")
		Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        
                
        String strPorts = "";
        
        Writer writer = null;
        File file = null;
        
        try
        {
        	file = new File("ports.txt");
			writer = new BufferedWriter(new FileWriter(file));
		} 
        catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        
        while(ports.hasMoreElements())
        {
        	strPorts = ((CommPortIdentifier)ports.nextElement()).getName();
            System.out.println(strPorts);
         
            try
	        { 
	            writer.write(strPorts + "\n");
	            list.add(new String(strPorts));
	            
	            
	        } 
	        catch (FileNotFoundException e)
	        {
	            e.printStackTrace();
	        }
	        catch (IOException e)
	        {
	            e.printStackTrace();
	        } 
	          
        }
        
        try
        {
            if (writer != null)
            {
                writer.close();
            }
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }

            
    }
	
	public void Print_List(List<String> list)
	{
		Iterator<String> it=list.iterator();
		
		System.out.println("\nContents of List:");

        while(it.hasNext())
        {
          String value=(String)it.next();

          System.out.println(value);
        }
        
        System.out.println("Size of List: "+list.size());
	}

	public void PopulateListFromFile(List<String> list)
	{
		try
		{
		    // Open the file that is the first 
		    // command line parameter
		    FileInputStream fstream = new FileInputStream("ports.txt");
		    
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)
		    {
		    	list.add(new String(strLine));
		    	// Print the content on the console
		    	System.out.println (strLine);
		    	
		    	strLine = "";
		    }
		    //Close the input stream
		    in.close();
		}
		catch (Exception e)
		{
			//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	public void connect(String portName) throws IOException
	{
		System.out.println("Connecting to "+portName);
		
		try
		{
			// Obtain a CommPortIdentifier object for the port you want to open
		    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);
			 
		    // Get the port's ownership
		    serialPort = (SerialPort)portId.open("UBW_Console", 5000);
			 
		    // Set the parameters of the connection.
		    setSerialPortParameters();
			 
		    // Open the input and output streams for the connection. If they won't
		    // open, close the port before throwing an exception.
		    outStream = serialPort.getOutputStream();
		    inStream = serialPort.getInputStream();
		}
		catch (NoSuchPortException e)
		{
			throw new IOException(e.getMessage());
		}
		catch (PortInUseException e)
		{
			throw new IOException(e.getMessage());
		}
		catch (IOException e) 
		{
			serialPort.close();
		    throw e;
		}
	}
	
	public void setSerialPortParameters()
	{
		//set parameters of serial port except for flow control
		int baudRate = 9600; // 57600bps
		
		try
		{
			// Set serial port to 9600bps-8N1
			serialPort.setSerialPortParams(
		    baudRate,
		    SerialPort.DATABITS_8,
		    SerialPort.STOPBITS_1,
		    SerialPort.PARITY_NONE);
			
			//Set flow control of serial port
			serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
					// OR
					// If CTS/RTS is needed
					//serialPort.setFlowControlMode(
					//SerialPort.FLOWCONTROL_RTSCTS_IN |
					//SerialPort.FLOWCONTROL_RTSCTS_OUT);
			
		}
		catch (UnsupportedCommOperationException ex)
		{
			System.err.println(ex.getMessage());
		}
		
	}
	
	
	public void disconnect()
	{
		if (serialPort != null)
		{
			try
			{
				// close the i/o streams.
		        outStream.close();
		        inStream.close();
			}
			catch (IOException ex)
			{
				// don't care
			}
			finally
			{
				// Close the port.
				System.out.println("\nDisconnecting from Serial Port");
				serialPort.close();
			}
			
		}
	}
	
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		List<String> Ports_List = new ArrayList<String>();
		List<String> Ports_List2 = new ArrayList<String>();
		
		System.out.println("Hello, World!\n");
		
		UBW_Console ubw = new UBW_Console();
		ubw.portsList(Ports_List);
		
		ubw.Print_List(Ports_List);
		System.out.println("\nNewList:");
		ubw.PopulateListFromFile(Ports_List2);
		ubw.Print_List(Ports_List2);
		
		try
		{
			ubw.connect(Ports_List.get(0));
		}
		catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ubw.setSerialPortParameters();
		
		
		//get input and output streams for serial port
		try
		{
			outStream = serialPort.getOutputStream();
			inStream = serialPort.getInputStream();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String serialMessage = "V";
		
		try
		{
			outStream = serialPort.getOutputStream();
			outStream.write(serialMessage.getBytes());
			
			for(int i = 0; i <= 100; i++)
			{				
				serialMessage = "O,0,2,0";
				outStream = serialPort.getOutputStream();
				outStream.write(serialMessage.getBytes());
				
				try
				{
					Thread.sleep(500);
				}
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}					
				
				serialMessage = "O,0,0,0";
				outStream = serialPort.getOutputStream();
				outStream.write(serialMessage.getBytes());
				
				try
				{
					Thread.sleep(500);
				}
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
							
				if((i % 10 == 0 && i != 0) || i == 1)
					System.out.print(i);
				else if(i != 0)
					System.out.print(".");
				//System.out.println("");
										
			}
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
			
		
		ubw.disconnect();
		
		
		
		
	}

}


Previous Topic:[SOLVED] Is it possible to hide projects in the workspace from user view?
Next Topic:IBufferChangedListener vs. IDocumentListener: which was to use for .java files?
Goto Forum:
  


Current Time: Sun Nov 23 21:35:30 GMT 2014

Powered by FUDForum. Page generated in 0.05377 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software