Results 1 to 1 of 1
Thread: Serial Port code problem
- 05-08-2011, 11:37 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 10
- Rep Power
- 0
Serial Port code problem
Well, I finally managed to get the RXTX package to work, at least in Windows. I thought I was having trouble with RXTX as I could not get my code to work, but I decided to step back and use a loopback connector and some code I found for loopback on the Internet. Everything worked fine so it is my code that is screwy. I was wondering if someone here on this forum could look at my code and see what I am doing wrong. I am trying to write a command, in this case a "V" to a device and then I am trying to capture what is returned, but nothing comes back and the program terminates.
I am really new to Java so I am fairly sure I am doing something incorrectly, but I have not been able to determine what so far.
:confused:
Java Code:/* * @(#)SimpleRead.java 1.12 98/06/25 SMI * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license * to use, modify and redistribute this software in source and binary * code form, provided that i) this copyright notice and license appear * on all copies of the software; and ii) Licensee does not utilize the * software in a manner which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control * of aircraft, air traffic, aircraft navigation or aircraft * communications; or in the design, construction, operation or * maintenance of any nuclear facility. Licensee represents and * warrants that it will not use or redistribute the Software for such * purposes. */ import java.io.*; import java.util.*; import gnu.io.*; /** * Class declaration * * * @author * @version 1.8, 08/03/00 */ @SuppressWarnings("restriction") public class SerialRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static String messageString = "V"; static OutputStream outputStream; static boolean outputBufferEmptyFlag = false; @SuppressWarnings("rawtypes") static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; /** * Method declaration * * * @param args * * @see */ @SuppressWarnings("unused") public static void main(String[] args) { boolean portFound = false; String defaultPort = "COM9"; if (args.length > 0) { defaultPort = args[0]; } portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(defaultPort)) { System.out.println("Found port: "+defaultPort); portFound = true; SerialRead reader = new SerialRead(); } } } if (!portFound) { System.out.println("port " + defaultPort + " not found."); } } /** * Constructor declaration * * * @see */ public SerialRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {} try { outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); } catch (IOException e) {} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} try { serialPort.notifyOnOutputEmpty(true); } catch (Exception e) { System.out.println("Error setting event notification"); System.out.println(e.toString()); System.exit(-1); } System.out.println("Writing \""+messageString+"\" to " +serialPort.getName().toString()); try { outputStream.write(messageString.getBytes()); outputStream.flush(); } catch (IOException e) {} try { Thread.sleep(2000); // Be sure data is xferred before closing } catch (Exception e) {} readThread = new Thread(this); readThread.start(); serialPort.close(); System.exit(1); } /** * Method declaration * * * @see */ public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {} } /** * Method declaration * * * @param event * * @see */ @SuppressWarnings("unused") public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); } catch (IOException e) {} break; } } }
Similar Threads
-
Serial port Communication Problem
By rjagan in forum New To JavaReplies: 8Last Post: 04-20-2011, 12:36 PM -
Reading from Serial port!
By Dogge in forum New To JavaReplies: 2Last Post: 12-01-2010, 03:21 AM -
serial communication is very slow while reading from serial port
By elsanthosh in forum AWT / SwingReplies: 1Last Post: 07-30-2010, 08:29 AM -
Serial Port
By radhika in forum New To JavaReplies: 5Last Post: 11-06-2009, 10:40 AM -
serial port
By musiigedeo in forum Advanced JavaReplies: 0Last Post: 07-23-2007, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks