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 11 of 11

Thread: X10 Java API

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default X10 Java API

    Hi,

    I'm new to this forum and I wish to post a query that I'm facing now.
    I have an assignment to do and yes I admit I'm very bad in my Java programming.
    My assignment requires me to control CM12U ( similar to CM11A ) protocol and switching on and off
    of the application modules. I'm currently have Jesse Peterson's X10 Java API and an X10.jar API with me

    My burning question is, how do I start off? Could anyone guide me through my programming process
    because to be honest I dislike Java programming but I do wish to learn how I could send commands
    to the CM12U protocol and most importantly on what I should start working on first before taking a step
    forward.

    If possible I would like to overcome this as soon as possible.

    Any help is greatly appreciated.

    Regards,
    Riz


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: X10 Java API

    Hello, X10 ey, how exciting

    What you need to do is read all your requirements for this and then design the program you want to create to support these requirements. Some hints are.

    Is this a GUI app or a command line tool?
    Does the program need to read any user configurable settings from somewhere?

    Once you feel fairly certain on what you need to do to accomplish this you can start the programming. Write some simple tests to figure out how the X10 API works, this will give you a clue of what you need to do to use the API in the best way from your program source.

    I'm sorry if this wasn't really what you were looking for but this is sort of the way I would go about doing this.

    // Json

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: X10 Java API

    Hi Json,

    Thanks for the reply and hints. I guess the only problem I have now is programming. Realizing that the APIs are given to me, I guess I have to start working on it. If there is any example codes that I could refer to on how to send a simple command, I'd greatly appreciate it.

    Will be updating on my progress and hope that the community can guide me along my learning journey to be a better programmer.

    Thank you!

    Regards,
    Riz

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: X10 Java API

    I don't know to be honest, you'd have to look around where you found the API, usually there is some sort of example on how to use it, I'm not too familiar with X10 myself nor the API and I don't the hardware to play with either I'm afraid.

    Any chance you could just google this one?

    Once you get going and you get stuck, show us some code and we'll try and help you figure out whats not working for you.

    // Json

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: X10 Java API

    Hi I'm back. I've found an example on the x10 API.
    Now I guess I have a problem with java.comm. I've been trying to develop a GUI so that when I start the
    x10SerialServer, a GUI comes out to prompt the user to select which com port the x10 protocol is connected
    to.

    Here are my codes:

    x10JavaComm (GUI file to appear when I run x10SerialServer for com port selection) -:
    ----------
    package comm.x10;
     
    /**
     *
     * @author hcdevel
     */
    import javax.swing.*;
    import java.util.Enumeration;
    import javax.comm.CommPortIdentifier;
    import java.awt.*;
    import java.awt.event.*;
     
    public class x10JavaComm extends JFrame{
     
        private Enumeration portList;
        private CommPortIdentifier portID;
        private String portStr = "Serial Ports Found: \n";
        private JComboBox portjcb;
        private final JLabel empty = null;
        private static String portName = new String();
     
        public x10JavaComm() {
     
            this.setTitle("Select Comm Port");
            this.setSize(300,300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            portjcb = new JComboBox();
            JButton selectPortBtn = new JButton("Select Port");
            JTextArea portListJTA = new JTextArea();
            JPanel mainPanel = new JPanel(new GridLayout(2,2));
            add(mainPanel);
     
            portList = CommPortIdentifier.getPortIdentifiers();
     
            while(portList.hasMoreElements()){
     
                portID = (CommPortIdentifier)portList.nextElement();
     
                if(portID.getPortType() == CommPortIdentifier.PORT_SERIAL){
                    portStr += portID.getName() + "\n";
                    portjcb.addItem(portID.getName());
                }
            }
        portListJTA.setText(portStr);
     
        selectPortBtn.addActionListener(new PortSelect());
     
        mainPanel.add(portListJTA);
        mainPanel.add(portjcb);
        mainPanel.add(empty);
        mainPanel.add(selectPortBtn);
     
        this.setVisible(true);
        }
     
        public class PortSelect implements ActionListener{
     
            public void actionPerformed(ActionEvent e){
     
                Object selectedPort = portjcb.getSelectedItem();
                portName = selectedPort.toString();
            }
        }
     
        public String getPort(){
            return portName;
        }
     
    }
    ----------
    x10SerialServer (file to first run to display available ports and prompt user to select com port) -:
    ----------
    import x10.*;
    import x10.net.*;
    import comm.x10.x10JavaComm;
     
    public class X10SerialServer
    {
        static String portID = "";
     
        public static void main(String[] args) throws Exception
        {
            x10JavaComm x10jc = new x10JavaComm();
            x10jc.setVisible(true);
            //Controller controller = new CM11ASerialController(args[0]);
            Controller controller = new CM11ASerialController(x10jc.getPort());
            ControllerServer cs = new ControllerServer(controller, 2400);
            cs.start();
            System.out.println("Threads Started...");
            System.in.read();
        }
    }
    ----------
    My main issue now is I cant call the x10JavaComm GUI out when I run in x10SerialServer. I get the following
    error:
    ----------
    Exception in thread "main" java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1041)
            at java.awt.Container.add(Container.java:365)
            at comm.x10.x10JavaComm.<init>(x10JavaComm.java:56)
            at X10SerialServer.main(X10SerialServer.java:11)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)
    ----------

    Any help or suggestions would be greatly appreciated!!

    Regards
    Riz
    Last edited by Json; May 13th, 2010 at 09:47 AM. Reason: Please use code tags.

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: X10 Java API

    This might be happening because this component is null and you're trying to add it to the frame.

    private final JLabel empty = null;

    // Json

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

    Simple (May 13th, 2010)

  8. #7
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: X10 Java API

    Hi,

    Thanks JSon. I forgot to key in JLabel empty = new JLabel(""); to give the label a null value. Now I guess the only issue I have left is with the X10 API on how do I read values from the serial port (after I send byte values into it) so that it will carry out the neccessary actions.

    Any help or guide is extremely appreciated!

    Regards,
    Riz

  9. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: X10 Java API

    This is just a hunch after looking at the source of CM11ASerialController for 2 minutes I can see there is a method.

    addUnitListener(UnitListener listener)

    So I assume you should add some sort of listener to your controller that will be called upon any incoming signals or any other events for that matter.

    controller.addUnitListener(new UnitListener() {
        public void allUnitsOff(UnitEvent event) {
     
        }
     
        public void allLightsOff(UnitEvent event) {
     
        }
     
        public void allLightsOn(UnitEvent event) {
     
        }
     
        public void unitOn(UnitEvent event) {
     
        }
     
        public void unitOff(UnitEvent event) {
     
        }
     
        public void unitDim(UnitEvent event) {
     
        }
     
        public void unitBright(UnitEvent event) {
     
        }
    });

    And then just add your implementation in there. The above code might not work out of the box as I just scribbled it down rather quick but I hope you get the clue

    Btw, could I ask you what you have plugged into your comm port, I'm fairly interested in this myself but I need to find a way of talking to the power grid, so I'm just curious to what you've got.

    // Json
    Last edited by Json; May 14th, 2010 at 02:21 AM.

  10. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: X10 Java API

    Oh and by the way, I found this web page which was pretty interesting

    Just Development: X10 Home Automation using Java

    // Json

  11. #10
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re: X10 Java API

    Hi,

    I stumbled upon a URL which I did a month back and I didnt realise that it was of use.
    The link is:
    http://www.x10pro.com/pro/pdf/cm11a_protocol.txt


    However, I was told that the x10 API itself has the server being built in. I guess my part now is
    to send bytes to the serial port so that the x10 protocol can recieve it via the serial port.
    My problem now is understanding the URL above.

    It states that I have to send Header:Code;
    Header being the type of code I want to provide, which is either a function code or an address code, and the Code basically being the code.

    The real headache comes in when it says I have to send 2 different bytes for the addition of the 2 bytes being sent and a checkSum will be returned to the PC to indicate if it is valid or unvalid and if it is valid, I will send a byte 0x00 to tell the x10 protocol that transmission should take place.

    Using the API provided, I tried to modify the code of the server and a main program that I created in a way that when I run my main program, the server will run as well and once the server is running, I will just simply type:

    try{
    toSerial.writeByte(0x04);
    toSerial.writeByte(0x66);
    System.out.println("Sent to serial <Header:Code> (Address)");
    //toSerial.writeByte(0x00);
    }
    catch (Exception ex){}

    to send the bytes to the serial port. I tried to do a multithreading in a way that I will run the server first and thereafter my serial port declarations and sending of bytes to the port. I extended the server class as a thread and my main java programming as a Runnable. The server stopped running after I ran my main file and I know that the server stops because the thread has reached its end and will die off.

    How do I keep the server constantly running and how do I go about sending bytes to the serial port for the x10 protocol to take the values and return it back to my program for checking before I continue sending bytes to the x10 protocol?

    Any help or suggestions are greatly appreciated.

    I will provide the codes if necessary.

    Regards,
    Riz

  12. #11
    Junior Member
    Join Date
    May 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: X10 Java API

    Hi Json,

    I plugged a Prolific USB-to-Serial Comm Port into one of my USB ports. The cable is linked from my PC's USB to the x10 protocol which is CM12U. My guess is that CM12U and CM11A are the same because the only difference is their max input voltage due to different power supply to homes in different countries.

    Hope I answered your question

    Regards,
    Riz