This discussion is archived
1 2 Previous Next 25 Replies Latest reply: Oct 1, 2009 11:54 PM by PhHein RSS

Java Serial Port Program

843807 Newbie
Currently Being Moderated
Hello,

I would like to use COMM API for writing program to access serial port from Java.

I tried to compile SimpleWrite.java & SimpleRead.java
with JD 1.1.6 & jdk1.3.0_02, but it was showing errors.
Which jdk version is best suited for Java COMM API?

Code follows below for SimpleWrite.java & SimpleRead.java

Thanks !!

Mayur


SimpleWrite.java
________________________________________________

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
}
________________________________________________


SimpleRead.java
________________________________________________

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

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();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

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;
}
}
}
  • 1. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    I use recently comm api witk jdk 1.4.1_04 and it works. comm.jar must be into c:\<jdk>\lib win32comm.dll into c:\<jdk>\bin and javax.comm.properties int c:\<jdk>\jre\lib

    it works fine.
  • 2. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Are you using ver 2.0.3 of javax.comm? Thanks in advance
  • 3. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Hi,
    I'm using the Java Comm API, but it doen's find any port.
    I have a bluetooth serial port and a regular paralel por.

    Do you know why ???


    Thanks,
    Marko
  • 4. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    i have read your answers on serial port from java but i have some questions to ask you if you dont mind:
    1 - does this programs work on solaris / sparc machines, cause i guess sparc machine serial port is 32-bit while java os 64-bit so i guess i'll need some sort of emulator in here. if i was write do you suggest any sort of emulators ( library ).
    2 - if u test this program while i dont have a serial device attached to the serial port it will produce the message "No serial port founds " i am just . i just want to know if my serial port is successfully opened or not


    if you dont mind can you answer directly to my mail : r.tarek@egyptianbanks.net


    Thanks alot
    i'l apperciate your help alot
    Rania
  • 5. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    im also having the same problem with the rs232 port, basically im trying to read data fro the rs2322 serial port, i compiled the SimpleRead. java and it worked, but when i tried to run it, i got an error saying exception in thread "main@ java.lang.NoClassDefFoundError: javax/comm/SerialPort EventListener
    at java.lang.ClassLoader.defineClass1(Native Method).
    Any help will really be appreciated
    Shayo
  • 6. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    You may check the readme or the spcific configurations before you run your classes. There are extra files to be copied into the JDK directories. Please make sure you have done it all correctly.
  • 7. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Hi,
    I have been trying to install the java comm api without any success. I installed
    comm.jar into j2sdk1.4.2_04\jre\lib\ext
    javax.comm.properties into j2sdk1.4.2_04\jre\lib
    win32com.dll into j2sdk1.4.2_04\jre\bin
    I've set the CLASSPATH as follows: .;c:\j2sdk1.4.2_04\jre\lib\ext\comm.jar
    I then tried running the SerialDemo provided with commapi bundle but it can't find any serial port.
    Any help on this would really be appreciated. Thanks
    Amish

  • 8. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Try Running the SerialDemo code using the java file in jre\bin

    c:\j2sdk1.4.2_04\jre\bin\java SerialDemo

    Hope this works for you

    -rahul
  • 9. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Hi there,

    my question is much more strange, actually! at first, i can list all the serial port with name "COM1","COM3"... using my java programm. but i don't know for what reason, suddenly i can't find all the communication port including serial & parralle port any longer.

    did somebody have this problem too? how can it be fixed?

    thanks
  • 10. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    throw comm.jar and javax.comm.properties in the same dir where u invoke ur program and make sure win32com.dll copied into windows\system32.

    it works for me..hope i'll work for u too..
  • 11. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Hi!
    I,am Shwetha.M.V. I'am an final year engineering student studying in Mysore.Actually we have the same problem in our project which u had.
    So please help us by giving the solution to the problem if u have already fixed the problem.

    Thanks,
    Shwetha
  • 12. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    hi,

    i had the same problem as you. do u already know how to solve it?

    thanks
  • 13. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    again:

    i had the same problem as marko:

    "I'm using the Java Comm API, but it doen's find any port.
    I have a bluetooth serial port and a regular paralel por."


    does anyone know how to solve it?
  • 14. Re: Java Serial Port Program
    843807 Newbie
    Currently Being Moderated
    Hello,
    I see your code could work on my testing Java computer. Now, do you have any idea about how access paralel port using Java?

    Please...please help me regarding this situation.

    Thanks B4
1 2 Previous Next