Results 1 to 9 of 9
  1. #1
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Smile Serial port Communication Problem

    private void portopenbuttonActionPerformed(java.awt.event.Actio nEvent evt) {
    try
    {
    portconnect("COM7");

    } catch (Exception e)
    { e.printStackTrace(); }
    }


    void portconnect(String portname)throws NoSuchPortException,PortInUseException
    {
    try
    {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portname);
    if(portIdentifier.isCurrentlyOwned())
    { JOptionPane.showMessageDialog(null,"Port in Use!.."); }
    else
    {
    CommPort commPort = portIdentifier.open(this.getClass().getName(),2000 );
    if (commPort instanceof SerialPort )
    {
    SerialPort serialPort = (SerialPort)portIdentifier.open("ListPortclass", 300);
    serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
    JOptionPane.showMessageDialog(null,"Port Opened!..");

    }
    else
    { JOptionPane.showMessageDialog(null,"Error"); }
    }
    }catch(Exception e) { System.out.println(e);}
    }


    Hi friends, this is my code..
    am using netbeans and rxtxcomm.jar for communicate with serial port..

    am not able to access the serial port, the above portparams are given correctly..
    Please check this code friends
    if any mistake help me..

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    are you given any error message?

  3. #3
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Post

    it doesn't give any errors...
    is this step correct or not.. to communicate serial port

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Found this from a google search, apparently it works (you need to install the javax.comm package):

    Java Code:
    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")) {
                        SimpleRead reader = new SimpleRead();
                    }
                }
            }
        }
    
        public void SimpleRead() {
            try {
                serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
            } catch (PortInUseException e) {System.out.println(e);}
            try {
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {System.out.println(e);}
            try {
                serialPort.addEventListener(this);
            } catch (TooManyListenersException e) {System.out.println(e);}
            serialPort.notifyOnDataAvailable(true);
            try {
                serialPort.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {
                System.out.println(e);
            }
            readThread = new Thread(this);
            readThread.start();
        }
    
        public void run() {
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                System.out.println(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) {
                        System.out.println(e);
                    }
                    break;
            }
        }
    }

  5. #5
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    thanks for your searching for me friend..

    am use rxtxcomm.jar package for communication.. am searched on net some one posted, javacomm utility doesnt support for windows.. so i jumped to rxtxpackage..

    can i use the rxtx package otherwise javacomm package..
    which one is suitable to communicate serial port..

    my requirements are :

    access serial port (usb to serial converter)...
    and send some threshold to that port...

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Apparently version 2.0 of javax.comm was compatible with windows...

    Communicating with ports using javax.comm package for Windows ! .. % .. $..*.. / .. @ .. ++++ .. :) … ^

    Version 3.0 has incompatibility issues.

    According to the link above, you can use rxtx to replace javax.comm:
    Not so good news is that Sun no more provides
    support for Java communications API for Windows.Why? Here is the answer.
    But not to worry..
    You can still get the package from www.rxtx.org.
    Changes to be then made include in import statements.
    So instead of import javax.comm it should now be import gnu.io.*

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rjagan View Post
    can i use the rxtx package otherwise javacomm package..
    which one is suitable to communicate serial port..

    my requirements are :

    access serial port (usb to serial converter)...
    and send some threshold to that port...
    Yes, rxtx is a replacement for javax.comm and is functionally identical to Sun's package. You have to change the package name to gnu but that's all. a.a.m.o.f. I find that rxtx works better (faster) than the old javax.comm software on a MS Windows box.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    thanks jos..
    here after i will continue my project with rxtx package..

    the above code using rxtx package only...
    is this code is correct or not to communicate

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rjagan View Post
    thanks jos..
    here after i will continue my project with rxtx package..

    the above code using rxtx package only...
    is this code is correct or not to communicate
    Could be, I didn't look at it; the basic idea is the same always: pick a port you want/need, check if it's available and of the right type; if so set the baud rate, start bits, data bits, and handshake protocol you want and start using it. There are numerous examples available on the internet; it isn't rocket science ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Serial port communication via jSSC
    By scream3r in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 08:12 PM
  2. Replies: 1
    Last Post: 07-30-2010, 08:29 AM
  3. serial port communication in java
    By elsanthosh in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 06:33 PM
  4. Usage of Serial Com Port communication
    By calvinlyp in forum New To Java
    Replies: 0
    Last Post: 02-19-2010, 06:25 AM
  5. Serial Port
    By radhika in forum New To Java
    Replies: 5
    Last Post: 11-06-2009, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •