Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By F.S.

Thread: Serial Communication

  1. #1
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Serial Communication

    This is the first time I have ever tried Serial Communication in Java and I ran into a problem. The idea is that all available COM ports are added in the ComboBox.

    I have added the comm.jar folder in the libraries. The problem is that nothing is added in the ComboBox. When I add "System.out.println(""+portList);". This is what I get "javax.comm.CommPortEnumerator@81572f".

    What am I doing wrong?

    Java Code:
    package sercomsolar;
    /* @author F.S */
    
    import java.io.*;
    import javax.comm.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class PortSelect extends javax.swing.JFrame {
    
        protected HashMap map = new HashMap();
        protected String selectPort;
        protected CommPortIdentifier selectedCOMPort;
               
        public PortSelect() {        
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        //...
    
        private void OK_Button_SerialActionPerformed(java.awt.event.ActionEvent evt) {
            Enumeration portList=CommPortIdentifier.getPortIdentifiers();
            System.out.println(":" + portList);
            while(portList.hasMoreElements()){
                CommPortIdentifier cpi= (CommPortIdentifier)portList.nextElement();
                map.put(cpi.getName(), cpi);            
                ComboBox_Serial.addItem(cpi.getName());            
            }        
        }
       
        
        public static void main(String args[]) {        
            //...
        }
        // Variables declaration - do not modify
        private javax.swing.JComboBox ComboBox_Serial;
        private javax.swing.JLabel Label_Serial;
        private javax.swing.JButton OK_Button_Serial;
        // End of variables declaration
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Serial Communication

    You need to print out each element of the enumeration, as that class doesn't have its own toString() (which is what println calls for non-Strings), resulting in it using the toString() of Object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Serial Communication

    Quote Originally Posted by Tolls View Post
    You need to print out each element of the enumeration, as that class doesn't have its own toString() (which is what println calls for non-Strings), resulting in it using the toString() of Object.
    OK, but the real problem is that nothing is added in the ComboBox using this part of the code:

    Java Code:
    private void OK_Button_SerialActionPerformed(java.awt.event.ActionEvent evt) {
            Enumeration portList=CommPortIdentifier.getPortIdentifiers();
            while(portList.hasMoreElements()){
                CommPortIdentifier cpi= (CommPortIdentifier)portList.nextElement();
                map.put(cpi.getName(), cpi);            
                ComboBox_Serial.addItem(cpi.getName());            
            }        
        }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Serial Communication

    Time to do some debugging. If you add System.out.println(...) statements to that method, do the elements get extracted and do their names make sense? If you change that method to simply add Strings from an array into the combo box, does it work?

  5. #5
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Serial Communication

    Quote Originally Posted by Fubarable View Post
    Time to do some debugging. If you add System.out.println(...) statements to that method, do the elements get extracted and do their names make sense? If you change that method to simply add Strings from an array into the combo box, does it work?
    I've tried System.out.println(...) and it doesn't work. Nothing gets extracted.

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

    Default Re: Serial Communication

    Most likely it's an incorrect installation of that comm package. It surprises me that it still can be downloaded (unless it's for a Linux/Unix box) because it was discontinued for MS Windows boxes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Serial Communication

    Ok then,

    Can anyone suggest me a better library then?

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

    Default Re: Serial Communication

    Google for RXTX, it's a clone of the javacomm package (identical API).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Serial Communication

    Quote Originally Posted by JosAH View Post
    Google for RXTX, it's a clone of the javacomm package (identical API).

    kind regards,

    Jos
    Just to let you know: I got my program working using this library.
    Thx very much
    Fubarable likes this.

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

    Default Re: Serial Communication

    Quote Originally Posted by F.S. View Post
    Just to let you know: I got my program working using this library.
    Thx very much
    Good to read you succeeded. Those RS/232 ports are quite a problem because Microsoft keeps on changing their driver APIs, that Sun javaxcomm package was released without any JSR and RS/232 is not as obsolete as they want to make us believe (usb otoh is a problem too with Java ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with USB/Serial communication
    By Blackmore in forum Advanced Java
    Replies: 0
    Last Post: 08-16-2011, 08:45 PM
  2. Serial communication via ethernet-serial hub
    By Idiodyssey in forum New To Java
    Replies: 0
    Last Post: 05-04-2011, 02:23 PM
  3. Serial port Communication Problem
    By rjagan in forum New To Java
    Replies: 8
    Last Post: 04-20-2011, 12:36 PM
  4. Replies: 1
    Last Post: 07-30-2010, 08:29 AM
  5. serial port communication in java
    By elsanthosh in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 06:33 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
  •