sending commands through RxTx

sending commands through RxTx

i am trying to call lightOn and LightOff method from NewSerialWriter's run() method but not able to make the correct logic. please help me with this. here is my code of different classes.

package newRelay_3088;

import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.InputStream; import java.io.OutputStream; import java.util.*;

public class LightController {

public LightController() throws Exception
{
    super();

    CommPortIdentifier commPortIdentifier = CommPortIdentifier.getPortIdentifier("COM12");

    if(commPortIdentifier.isCurrentlyOwned())
    {
        System.out.println("Error: Port is currently in use");
    }
    else
    {
        CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000);

        if(commPort instanceof SerialPort)
        {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            InputStream in = serialPort.getInputStream();
            OutputStream out = serialPort.getOutputStream();

            (new Thread(new NewSerialReader(in))).start();
            (new Thread(new NewSerialWriter(out))).start();
        }
        else
        {
             System.out.println("Error: Only serial ports are handled by this example.");
        }
    }
}

public static void main(String[] args)
{
    try
    {
        new LightController();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    Lights light_1 = new Lights("00000001");

    Lights light_2 = new Lights("00000002");

    Lights light_3 = new Lights("00000004");

    Lights light_4 = new Lights("00000008");

    Lights light_5 = new Lights("00000016");

    Lights light_6 = new Lights("00000032");

    Lights light_7 = new Lights("00000064");

    Lights light_8 = new Lights("00000128");

    HashMap<Integer, Lights> lightList = new HashMap<Integer, Lights>();

    lightList.put(1, light_1);
    lightList.put(2, light_2);
    lightList.put(3, light_3);
    lightList.put(4, light_4);
    lightList.put(5, light_5);
    lightList.put(6, light_6);
    lightList.put(7, light_7);
    lightList.put(8, light_8);

    lightList.get(1).lightOn(); 
}

}


package newRelay_3088;

import java.io.IOException; import java.io.OutputStream;

public class Lights{ OutputStream out; String str;

public Lights(String str)
{
    this.str = str;
}

public void lightOn()
{
    // code for light on goes here
    System.out.println(str);
    try
    {   
        String hexString = String.format("%02X", Integer.parseInt(str));
        System.out.println("HexSting : " + hexString);
        System.out.println(hexString.length());
        String command = "!012" + hexString + "\r";
        System.out.println(command);
        System.out.println(command.getBytes());
        out.write(command.getBytes());
        System.out.println("-----------------------1-------------------------");
        int c = 0;
        while((c = System.in.read()) > -1)
        {
            System.out.println("-----------------------2-------------------------");
            this.out.write(c);
        }
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    }
}

public void lightOff(int sequenceNo)
{
    // code for light off goes here
    try
    {

        String hexString = String.format("%02X", Integer.parseInt(str));
        System.out.println("HexSting : " + hexString);
        System.out.println(hexString.length());
        String command = "!012" + hexString + "\r";
        System.out.println(command);
        out.write(command.getBytes());
        System.out.println("-----------------------1-------------------------");
        int c = 0;
        while((c = System.in.read()) > -1)
        {
            System.out.println("-----------------------2-------------------------");
            this.out.write(c);
        }
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    } 
}

public void getLightStatus()
{
    // method for getting the current status of all the lights
    // code goes here
}

}


package newRelay_3088;

import java.io.IOException; import java.io.InputStream;

public class NewSerialReader implements Runnable{

InputStream in;

public NewSerialReader(InputStream in)
{
    this.in = in;
}

public void run()
{
    byte[] buffer = new byte[1024];
    int length = -1;

    try
    {
        while((length =this.in.read(buffer)) > -1 )
        {
            System.out.print(new String(buffer, 0, length));
        }
    }
    catch ( IOException e )
    {
       e.printStackTrace();
    } 
}

}


package newRelay_3088;

import java.io.IOException; import java.io.OutputStream;

public class NewSerialWriter implements Runnable{ public OutputStream out;

public NewSerialWriter(OutputStream out) 
{
    this.out = out;
}

public void run() 
{

}

} please help ,e with this. also tell me where else my logic is not right. i sm trying to send 8 different commands to a relay to switch on and off the lights and at the same time continuously checking the status... thanks a ton in advance.

View Answers









Related Tutorials/Questions & Answers:
sending commands through RxTx
sending commands through RxTx  i am trying to call lightOn and LightOff method from NewSerialWriter's run() method but not able to make the correct... me where else my logic is not right. i sm trying to send 8 different commands
Sending images through java mail
Sending images through java mail  Am trying to develop greeting application that having images..... in one jsp page i displayed all images and by clicking one image the control go to mail sending page in that the image should add
Advertisements
sending mails - JSP-Servlet
sending mails  my intension is sending mails through java program with out installing any softwares.what is code
Maven Repository/Dependency: com.github.calimero | calimero-rxtx
Maven Repository/Dependency of Group ID com.github.calimero and Artifact ID calimero-rxtx. Latest version of com.github.calimero:calimero-rxtx dependencies. # Version Release Date 1
sending mail using jsp
sending mail using jsp  please give me the detailed procedure and code for sending mail through jsp program   Please visit the following links: http://www.roseindia.net/ejb/introduction/j2eedemo.shtml http
How to use AT Commands with J2ME?
How to use AT Commands with J2ME?  How to use AT Commands with J2ME? Thank you for your answer
com.github.calimero - calimero-rxtx version 2.3-beta Maven dependency. How to use calimero-rxtx version 2.3-beta in pom.xml?
com.github.calimero  - Version 2.3-beta of calimero-rxtx Maven dependency...-rxtx in pom.xml? How to use calimero-rxtx version 2.3-beta in pom.xml? Learn to use... com.github.calimero  - Version 2.3-beta of calimero-rxtx in project by the help of adding
com.github.calimero - calimero-rxtx version 2.4-rc2 Maven dependency. How to use calimero-rxtx version 2.4-rc2 in pom.xml?
com.github.calimero  - Version 2.4-rc2 of calimero-rxtx Maven dependency? How to use  com.github.calimero  - Version 2.4-rc2 of calimero-rxtx in pom.xml? How to use calimero-rxtx version 2.4-rc2 in pom.xml? Learn to use
Sending hidden values
Sending hidden values  how to send more than one hidden values through javascript function . Main purpose is to remove href link (Query string should not appear in url bar
execute dos commands
execute dos commands  how to execute dos commands using a java program
Servlet os running commands issues - Development process
Servlet os running commands issues  hello, i want the help regarding the running os commands through servlet. so please send it either on my personal email address or send it on the rose india servlet tutorial. thanx n
What is the use of DBCC commands?
What is the use of DBCC commands?  What is the use of DBCC commands?   Hi, DBCC stands for database consistency checker. We use these commands to check the consistency of the databases, i.e., maintenance, validation
Maven dependency for com.github.calimero - calimero-rxtx version 2.5-rc1 is released. Learn to use calimero-rxtx version 2.5-rc1 in Maven based Java projects
of calimero-rxtx released The developers of   com.github.calimero - calimero-rxtx project have released the latest version of this library on 22 Sep 2021, the released version of  com.github.calimero - calimero-rxtx library
Maven dependency for com.github.calimero - calimero-rxtx version 2.5.1 is released. Learn to use calimero-rxtx version 2.5.1 in Maven based Java projects
of calimero-rxtx released The developers of   com.github.calimero - calimero-rxtx project have released the latest version of this library on 13 Dec 2022, the released version of  com.github.calimero - calimero-rxtx library
Maven dependency for com.github.calimero - calimero-rxtx version 2.5 is released. Learn to use calimero-rxtx version 2.5 in Maven based Java projects
-rxtx released The developers of   com.github.calimero - calimero-rxtx project have released the latest version of this library on 23 Nov 2021, the released version of  com.github.calimero - calimero-rxtx library is 2.5.
Maven dependency for com.github.calimero - calimero-rxtx version 2.5-rc2 is released. Learn to use calimero-rxtx version 2.5-rc2 in Maven based Java projects
of calimero-rxtx released The developers of   com.github.calimero - calimero-rxtx project have released the latest version of this library on 31 Oct 2021, the released version of  com.github.calimero - calimero-rxtx library
Maven dependency for com.github.calimero - calimero-rxtx version 2.5-M1 is released. Learn to use calimero-rxtx version 2.5-M1 in Maven based Java projects
of calimero-rxtx released The developers of   com.github.calimero - calimero-rxtx project have released the latest version of this library on 10 Mar 2021, the released version of  com.github.calimero - calimero-rxtx library
java mail sending with images
java mail sending with images  I need to send images through java mail without giving content path(i.e. we don't want hard code the image path)can you tell me the idea?   Please visit the following links: http
Implementing voice commands
Implementing voice commands  Am trying to write a java program to implement voice commands within a windows platform using preferably java (though any language will do). I would be glad for any tips and procedures that would help
MySQL Commands
MySQL Commands       In this section, we are going to read about the mysql commands. Each... of commands. To see list of mysql commands, you type help or \h on the mysql>
sending mail - JSP-Servlet
sending mail  Hi, what is the code for sending mail automatically without user intervention? thanks in advance
ModuleNotFoundError: No module named 'dewi-commands'
ModuleNotFoundError: No module named 'dewi-commands'  Hi, My... 'dewi-commands' How to remove the ModuleNotFoundError: No module named 'dewi-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ssh-commands'
ModuleNotFoundError: No module named 'ssh-commands'  Hi, My Python... 'ssh-commands' How to remove the ModuleNotFoundError: No module named 'ssh-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dewi-commands'
ModuleNotFoundError: No module named 'dewi-commands'  Hi, My... 'dewi-commands' How to remove the ModuleNotFoundError: No module named 'dewi-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'commands-generator'
ModuleNotFoundError: No module named 'commands-generator'  Hi, My... named 'commands-generator' How to remove the ModuleNotFoundError: No module named 'commands-generator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'commands-generator'
ModuleNotFoundError: No module named 'commands-generator'  Hi, My... named 'commands-generator' How to remove the ModuleNotFoundError: No module named 'commands-generator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'reahl-commands'
ModuleNotFoundError: No module named 'reahl-commands'  Hi, My... named 'reahl-commands' How to remove the ModuleNotFoundError: No module named 'reahl-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'robot-commands'
ModuleNotFoundError: No module named 'robot-commands'  Hi, My... named 'robot-commands' How to remove the ModuleNotFoundError: No module named 'robot-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'shell-commands'
ModuleNotFoundError: No module named 'shell-commands'  Hi, My... named 'shell-commands' How to remove the ModuleNotFoundError: No module named 'shell-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'slacktools-commands'
ModuleNotFoundError: No module named 'slacktools-commands'  Hi, My... named 'slacktools-commands' How to remove the ModuleNotFoundError: No module named 'slacktools-commands' error? Thanks   Hi
ModuleNotFoundError: No module named 'virtshell_commands'
ModuleNotFoundError: No module named 'virtshell_commands'  Hi, My... named 'virtshell_commands' How to remove the ModuleNotFoundError: No module named 'virtshell_commands' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'virtualenv-commands'
ModuleNotFoundError: No module named 'virtualenv-commands'  Hi, My... named 'virtualenv-commands' How to remove the ModuleNotFoundError: No module named 'virtualenv-commands' error? Thanks   Hi
ModuleNotFoundError: No module named 'build_commands'
ModuleNotFoundError: No module named 'build_commands'  Hi, My... named 'build_commands' How to remove the ModuleNotFoundError: No module named 'build_commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'commands-generator'
ModuleNotFoundError: No module named 'commands-generator'  Hi, My... named 'commands-generator' How to remove the ModuleNotFoundError: No module named 'commands-generator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-commands'
ModuleNotFoundError: No module named 'django-commands'  Hi, My... named 'django-commands' How to remove the ModuleNotFoundError: No module named 'django-commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dodo_commands'
ModuleNotFoundError: No module named 'dodo_commands'  Hi, My... 'dodo_commands' How to remove the ModuleNotFoundError: No module named 'dodo_commands' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'jupyterlab-commands'
ModuleNotFoundError: No module named 'jupyterlab-commands'  Hi, My... named 'jupyterlab-commands' How to remove the ModuleNotFoundError: No module named 'jupyterlab-commands' error? Thanks   Hi
sending mails - JSP-Servlet
sending mails   sending mail using smtp protocal ,while running,i got error an javax.mail.sendfailed exception. what is this error
sending automatic email - JavaMail
sending automatic email  Dear sir. In my project i need to send an automatic email to the clients when their accounts are going to expire in 30... date and through a trigger it will send a message to the particular client
sending emails - JavaMail
sending emails  what is the code for sending emails in java  Hi Friend, Please visit the following link: http://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks
sending data to facebook
sending data to facebook  how to post data into facebook from a java program
Sending email without authentication
Sending email without authentication  Hi sir, Am doing a project in JSP, in that i want to send mail without any authentication of password so send.../mail/sending-an-email-in-jsp.shtml
Sending File - JSP-Servlet
Sending File  How to use tag in Jsp, & How read a file fom Client  Hi Friend, We used tag to upload a file. Please visit the following links: http://www.roseindia.net/jsp/fileupload.shtml http
Difference between TRUNCATE and DELETE commands
Difference between TRUNCATE and DELETE commands  hii, What is the difference between TRUNCATE and DELETE commands?   hello,ADS_TO_REPLACE_1 TRUNCATE is a DDL command whereas DELETE is a DML command. TRUNCATE is much
sending sms from laptop to mobile
sending sms from laptop to mobile   sending sms from laptop to mobile for multi users
sending sms from laptop to mobile
sending sms from laptop to mobile   sending sms from laptop to mobile for multi users
sending a mail - JSP-Servlet
sending a mail   I m writing a code for send mail in jsp,i sending a mail to specified receiver but if i attach an file and send a mail then i m getting an error. Here Attachment is in String[] Attachment=null so how
for sending project build
for sending project build  hello I want to send my iphone project build to someone else how can i send it...   yes you can send butADS_TO_REPLACE_1 Follow the steps on the portal to creating an ad hoc build
Sending Emails In Java
Sending Emails In Java  I want to send emails from within a java program. I saw some online java programs to do that, but they needed me to enter a smtp server, but i don't know what that should be. Can someone please help
Sending Emails In Java
Sending Emails In Java  I want to send emails from within a java program. I saw some online java programs to do that, but they needed me to enter a smtp server, but i don't know what that should be. Can someone please help

Ads