Results 1 to 6 of 6
  1. #1
    scream3r is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Serial port communication via jSSC

    jSSC (Java Simple Serial Connector) - is a Free java library for working with serial ports. Current version of library (0.7.1) support Win32 and Win64. Linux version is under construction. jSSC give a simple tool to developer for communicate with different devices via serial port. With jSSC you can get the port names, read and write data, control lines RTS and DTR, receive Events etc. jSSC designed to operate 24/7 multi-threaded systems and is currently successfully used in automation, data collection and recording. Now let me give a few examples of actual work with the component.

    Example 1. Getting the names of serial ports:

    Java Code:
    import jssc.SerialPortList;
    
    public class Main {
    
        public static void main(String[] args) {
            //Method getPortNames() returns an array of strings. Elements of the array is already sorted.
            String[] portNames = SerialPortList.getPortNames();
            for(int i = 0; i < portNames.length; i++){
                System.out.println(portNames[i]);
            }
        }
    }
    Example 2. Read and write data:

    Java Code:
    import jssc.SerialPort;
    import jssc.SerialPortException;
     
    public class Main {
     
        public static void main(String[] args) {
            //In the constructor pass the name of the port with which we work
            SerialPort serialPort = new SerialPort("COM1");
            try {
                //Open port
                serialPort.openPort();
                //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
                serialPort.setParams(SerialPort.BAUDRATE_9600, 
                                     SerialPort.DATABITS_8,
                                     SerialPort.STOPBITS_1,
                                     SerialPort.PARITY_NONE);
                //Writes data to port
                serialPort.writeBytes("Test");
                //Read the data of 10 bytes. Be careful with the method readBytes(), if the number of bytes in the input buffer
                //is less than you need, then the method will wait for the right amount. Better to use it in conjunction with the
                //interface SerialPortEventListener.
                byte[] buffer = serialPort.readBytes(10);
                //Closing the port
                serialPort.closePort();
            }
            catch (SerialPortException ex) {
                System.out.println(ex);
            }
        }
    }
    Example 3. Read and write data using the interface SerialPortEventListener:

    Java Code:
    import jssc.SerialPort;
    import jssc.SerialPortEvent;
    import jssc.SerialPortEventListener;
    import jssc.SerialPortException;
     
    public class Main {
     
        static SerialPort serialPort;
     
        public static void main(String[] args) {
            serialPort = new SerialPort("COM1"); 
            try {
                serialPort.openPort();
                serialPort.setParams(9600, 8, 1, 0);
                //Preparing a mask. In a mask, we need to specify the types of events that we want to track.
                //Well, for example, we need to know what came some data, thus in the mask must have the
                //following value: MASK_RXCHAR. If we, for example, still need to know about changes in states 
                //of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
                int mask = SerialPort.MASK_RXCHAR;
                //Set the prepared mask
                serialPort.setEventsMask(mask);
                //Add an interface through which we will receive information about events
                serialPort.addEventListener(new SerialPortReader());
            }
            catch (SerialPortException ex) {
                System.out.println(ex);
            }
        }
        
        static class SerialPortReader implements SerialPortEventListener {
     
            public void serialEvent(SerialPortEvent event) {
                //Object type SerialPortEvent carries information about which event occurred and a value.
                //For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer.
                if(event.isRXCHAR()){
                    if(event.getEventValue() == 10){
                        try {
                            byte buffer[] = serialPort.readBytes(10);
                        }
                        catch (SerialPortException ex) {
                            System.out.println(ex);
                        }
                    }
                }
                //If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF.
                else if(event.isCTS()){
                    if(event.getEventValue() == 1){
                        System.out.println("CTS - ON");
                    }
                    else {
                        System.out.println("CTS - OFF");
                    }
                }
                else if(event.isDSR()){
                    if(event.getEventValue() == 1){
                        System.out.println("DSR - ON");
                    }
                    else {
                        System.out.println("DSR - OFF");
                    }
                }
            }
        }
    }

    jSSC page on Google Code: java-simple-serial-connector - Project Hosting on Google Code

  2. #2
    spalax is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default Problem with Reading and writing

    hi,

    i tried your last example for reading and writing with event listener nut it's not working with my device.

    i have a vaisala weather station and its paremters are:
    9600,8,1,0

    when i try your code, it's happen nothing.
    but in the same time i can communicate with HyperTerminal.
    but at this moment if i try your example, there is the following message:
    "jssc.SerialPortException: Port name - EMPTY; Method name - setParams(); Exception type - Port not opened."

    why?
    Thank a lot
    SPX

  3. #3
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Dear spalax,

    You can use the javax.comm from Sun. and can use the online codes that are widely available. A example is available with the same settings us you have mentioned.


    Reading and Writing Data from comm port in JAVA

  4. #4
    scream3r is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by spalax View Post
    hi,

    i tried your last example for reading and writing with event listener nut it's not working with my device.

    i have a vaisala weather station and its paremters are:
    9600,8,1,0

    when i try your code, it's happen nothing.
    but in the same time i can communicate with HyperTerminal.
    but at this moment if i try your example, there is the following message:
    "jssc.SerialPortException: Port name - EMPTY; Method name - setParams(); Exception type - Port not opened."

    why?
    Thank a lot
    SPX
    Spalax, you can recieve this exception only if you not opened the port. Please check you source.
    1. Create SerialPort class object: SerialPort serialPort = new SerialPort("COM1");
    2. Open port: serialPort.openPort();
    3. Set serial port settings: serialPort.setParams(9600, 8, 1, 0);

    Check result of openPort() method. This method must return true if you seccesfully opened the port.

    Good luck in your projects.
    Last edited by scream3r; 12-30-2010 at 09:23 PM.

  5. #5
    spalax is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks a lot every one.
    now the code is working
    i will get back to you once i finished the next step

    spalax

  6. #6
    scream3r is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

Similar Threads

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

Posting Permissions

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