javacomm
please I' m tryng to use the javacomm I need a simple java application that read from the serial ports when I send an 80 to the port.....I'm used the class simpleRead but when I execute it the result is PORT NOT FUOND (I have a serial port)...please help me i have a scholastic examination in this week...please help me fastly
[335 byte] By [
gerlaonea] at [2007-11-27 5:48:20]

hope this helps.
i wrote this eons ago; just modify to suit.
it seems long, but, once you format the text, you will see that it is really sequential.
//
import java.io.*; // if needed
import javax.comm.*; // stored in JavaUtilities Project
//
public class IoCommSerial
{
public IoCommSerial() { super(); }
public static void main(String[] args)
{
new IoCommSerial().methodComReader();
new IoCommSerial().methodComWriter();
}
/*
Note: This is a Serial Reader.
1) the class file must import the package: import javax.comm.*;
2) this required downloading and installing sun's java com packages
3) your classpath must point to the jar file
*/
public void methodComReader()
{
SerialPort serialPort = null;
CommPort commPort = null;;
CommPortIdentifier commPortIdentifier;
String stringName = "COM1";
//
int intBits = 0, ictr = 0;
int intBaud = 9600;
int intDatb = SerialPort.DATAintBits_8;
int intStpb = SerialPort.STOPintBits_1;
int intPrty = SerialPort.PARITY_NONE;
int intTimeout = 2000;
//
try
{
commPortIdentifier = CommPortIdentifier.getPortIdentifier(stringName);
commPort = commPortIdentifier.open("GenericSerialReader", intTimeout);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(intBaud, intDatb, intStpb, intPrty);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveintTimeout(intTimeout);
// "~" terminates the sequence
System.out.println("reading: " + stringName + "\n");
while (intBits < 125)
{
ictr++;
OutputStream outputStream = serialPort.getOutputStream();
InputStream inputStream = serialPort.getInputStream();
intBits = inputStream.read();
if (intBits >= 0) { System.out.print((char) intBits); }
if (ictr > 70)
{ System.out.println(""); ictr = 0; }
}
System.out.println("\n\nDevice Finished");
}
catch (Exception ex) { System.out.println(ex.getMessage() ); }
}
//
// Note: This is a Serial Writer.
public void methodComWriter()
{
String stringName = "COM1", stringTemp = "", stringMessage = "M. George";
SerialPort serialPort = null;
CommPort commPort;
CommPortIdentifier commPortIdentifier;
OutputStream outStream;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//
int intBits = 0, ictr = 0;
int intBaud = 9600;
int intDatb = SerialPort.DATAintBits_8;
int intStpb = SerialPort.STOPintBits_1;
int intPrty = SerialPort.PARITY_NONE;
int intTimeout = 2000;
//
try
{
// Enumeration enumeration = CommPortIdentifier.getPortIdentifiers();
commPortIdentifier = CommPortIdentifier.getPortIdentifier(stringName);
commPort = commPortIdentifier.open("SimpleWriteApp", intTimeout);
serialPort = (SerialPort) commPort;
outStream = serialPort.getOutputStream();
serialPort.setSerialPortParams(intBaud, intDatb, intStpb, intPrty);
// outStream.write(stringMessage.getBytes()); (can replace while)
System.out.println("writing: " + stringName + "\n");
while (stringTemp != null & !stringTemp.startsWith("~"))
{
stringTemp = bufferedReader.readLine();
if (stringTemp != null)
{
outStream.write(stringTemp.getBytes());
outStream.write("\n".getBytes());
}
}
System.out.println("\n\nDevice Finished");
}
catch (Exception ex) { System.out.println(ex.getMessage()); }
}
}
cannot find symbol
symbol : variable DATAintBits_8
location: class javax.comm.SerialPort
int intDatb = SerialPort.DATAintBits_8;
cannot find symbol
symbol : variable STOPintBits_1
location: class javax.comm.SerialPort
int intStpb = SerialPort.STOPintBits_1;
cannot find symbol
symbol : method enableReceiveintTimeout(int)
location: class javax.comm.SerialPort
serialPort.enableReceiveintTimeout(intTimeout);
cannot find symbol
symbol : variable DATAintBits_8
location: class javax.comm.SerialPort
int intDatb = SerialPort.DATAintBits_8;
cannot find symbol
symbol : variable STOPintBits_1
location: class javax.comm.SerialPort
int intStpb = SerialPort.STOPintBits_1;
here are the error when I compile my application
I have an other appliation that has this error
java.lang.UnsatisfiedLinkError: no Serial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Serial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java)
at tesina.Main.listPorts(Main.java:24)
at tesina.Main.main(Main.java:54)
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static EnumerationportList;
InputStreaminputStream;
SerialPortserialPort;
ThreadreadThread;
public static void main(String[] args) {
booleanportFound = false;
StringdefaultPort = "COM1";
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;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
// System.out.println(reader);
}
}
/**
* Constructor declaration
*
*
* @see
*/
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
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) {}
readThread = new Thread(this);
readThread.start();
}
/**
* Method declaration
*
*
* @see
*/
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
/**
* Method declaration
*
*
* @param event
*
* @see
*/
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[50];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
compiling: no error
execution: PORT NOT FOUND
import gnu.io.*;
import java.io.*;
import java.util.*;
public class Main {
public static void listPorts() {
try{
Enumeration thePorts = gnu.io.CommPortIdentifier.getPortIdentifiers();
while (thePorts.hasMoreElements()) {
gnu.io.CommPortIdentifier com = (gnu.io.CommPortIdentifier) thePorts.nextElement();
switch (com.getPortType()) {
case gnu.io.CommPortIdentifier.PORT_SERIAL:
try {
gnu.io.CommPort thePort = com.open("CommUtil", 2000);
System.out.println("Porta seriale: "+thePort.getName());
thePort.close();
} catch (gnu.io.PortInUseException e) {
System.out.println("Porta SERIALE "+com.getName()+", ?in uso.");
} catch (Exception e) {
System.out.println("Failed to open port "+com.getName()+" "+e);
}
}
}
}
catch(Exception ioe){
System.out.println("Errore");
}
}
public static void main(String[] args){
listPorts();
}
}
COMPILING: NO ERRORS
executing :
java.lang.UnsatisfiedLinkError: no Serial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Serial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java)
at tesina.Main.listPorts(Main.java:24)
at tesina.Main.main(Main.java:54)
you don't have the .dll file in the right place.
when I have to put the dll file?you write about what class?
you speaking about rxtxSerial.dll?
whatever came with the java comm package.
where I put the dll file?
it was mentioned in the other thread, reply 19. go back and read it http://forum.java.sun.com/thread.jspa?threadID=619006&tstart=0
java.lang.UnsatisfiedLinkError: no Serial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Serial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java)
at tesina.Main.listPorts(Main.java:24)
at tesina.Main.main(Main.java:54)