Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: jSSC library - for easy work with serial ports

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default jSSC library - for easy work with serial ports

    jSSC (Java Simple Serial Connector) - library for work with serial port from Java. The library was created as a simple and reliable replacement of existing facilities. Briefly about the opportunities it provides. With jSSC you can get the port names, read and write data, control lines RTS and DTR, receive Event 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:

    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:

    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:

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

    Here are a few examples to work with the component. I wish you success. At the moment there is a stable implementation of the libraries under Win32 (Win98-Win7).

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

  2. The Following 2 Users Say Thank You to scream3r For This Useful Post:

    ChristopherLowe (June 6th, 2011), JavaPF (November 24th, 2010)


  3. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: jSSC library - for easy work with serial ports

    jSSC-0.7.1 released. Win64 supports now.

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: jSSC library - for easy work with serial ports

    jSSC-CE released. Now jSSC support WinCE.

  5. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jSSC library - for easy work with serial ports

    Very impresed with Jssc.

    I'm have used it in a terminal program written in netbeans but am having trouble deploying it as NB doesn't create a lib folder in "dist". I followed the guide. I have seen some threads about missing lib folders but found no solutions.

    Has anyone had the same issue ?

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: jSSC library - for easy work with serial ports

    jSSC-0.8 Test Build #2 avaliable for download: http://code.google.com/p/java-simple-serial-connector/

  7. The Following User Says Thank You to scream3r For This Useful Post:

    JavaPF (July 19th, 2011)

Similar Threads

  1. List serial com ports of computer
    By mickey in forum Java Networking
    Replies: 2
    Last Post: August 6th, 2010, 01:44 AM
  2. My Custom Layout (VERY EASY TO USE)
    By aussiemcgr in forum AWT / Java Swing
    Replies: 10
    Last Post: August 5th, 2010, 01:37 PM
  3. A Few Quick, Easy Questions
    By TheAsianMenace in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:47 PM
  4. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM
  5. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM