Loading ...
Sorry, an error occurred while loading the content.

What could be the cause?

Expand Messages
  • jujames84
    Whenever I close the RoboJDE software, the intellibrain no longer responds to the analog commands. So if something was left in an ON state it remains ON even
    Message 1 of 3 , Jun 27, 2010
    • 0 Attachment
      Whenever I close the RoboJDE software, the intellibrain no longer responds to the analog commands. So if something was left in an ON state it remains ON even if you turn it off. (At the moment you close the RoboJDE). However if you re-open it, the intellibrain starts to respond again.

      [code]
      import com.ridgesoft.intellibrain.IntelliBrain;
      import com.ridgesoft.intellibrain.IntelliBrainAnalogInput;
      import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;
      import javax.comm.SerialPort;
      import java.io.OutputStream;
      import java.io.InputStream;


      public class Main
      {
      /**
      * @param args the command line arguments
      */
      public static void main(String[] args)
      {
      // TODO code application logic here
      try
      {
      IntelliBrainAnalogInput switch1 = IntelliBrain.getAnalogInput(1);
      IntelliBrainAnalogInput switch2 = IntelliBrain.getAnalogInput(2);
      IntelliBrainAnalogInput switch3 = IntelliBrain.getAnalogInput(3);
      IntelliBrainAnalogInput switch4 = IntelliBrain.getAnalogInput(4);
      //IntelliBrainAnalogInput switch5 = IntelliBrain.getAnalogInput(5);
      //IntelliBrainAnalogInput switch6 = IntelliBrain.getAnalogInput(6);
      //IntelliBrainAnalogInput switch7 = IntelliBrain.getAnalogInput(7);
      IntelliBrainDigitalIO led1 = IntelliBrain.getDigitalIO(1);
      IntelliBrainDigitalIO led2 = IntelliBrain.getDigitalIO(2);
      IntelliBrainDigitalIO led3 = IntelliBrain.getDigitalIO(3);
      IntelliBrainDigitalIO led4 = IntelliBrain.getDigitalIO(4);
      //IntelliBrainDigitalIO led5 = IntelliBrain.getDigitalIO(5);
      //IntelliBrainDigitalIO led6 = IntelliBrain.getDigitalIO(6);
      //IntelliBrainDigitalIO led7 = IntelliBrain.getDigitalIO(7);
      led1.setDirection(true);
      led2.setDirection(true);
      led3.setDirection(true);
      led4.setDirection(true);
      //led5.setDirection(true);
      //led6.setDirection(true);
      //led7.setDirection(true);

      SerialPort comPort = IntelliBrain.getCom1();
      InputStream inStream = comPort.getInputStream();
      OutputStream outStream = comPort.getOutputStream();
      int data = 0;

      while((data = inStream.read()) != -1)
      {
      // Switch1 and LED1
      if(switch1.isSet() == true || inStream.read() == 11)
      {
      led1.set();
      outStream.write(1);
      }
      else if(switch1.isSet() == false || inStream.read() == 12)
      {
      led1.clear();
      outStream.write(10);
      }
      // Switch2 and LED2
      if(switch2.isSet() == true || inStream.read() == 21)
      {
      led2.set();
      outStream.write(2);
      }
      else if(switch2.isSet() == false || inStream.read() == 22)
      {
      led2.clear();
      outStream.write(20);
      }
      // Switch3 and LED3
      if(switch3.isSet() == true || inStream.read() == 31)
      {
      led3.set();
      outStream.write(3);
      }
      else if(switch3.isSet() == false || inStream.read() == 32)
      {
      led3.clear();
      outStream.write(30);
      }
      // Switch4 and LED4
      if(switch4.isSet() == true || inStream.read() == 31)
      {
      led4.set();
      outStream.write(4);
      }
      else if(switch4.isSet() == false || inStream.read() == 32)
      {
      led4.clear();
      outStream.write(40);
      }
      }
      }
      catch(Exception error)
      {
      error.printStackTrace();
      }
      }
      }


      [/code]
    • jujames84
      I worked my problem down to the read statement... || inStream.read() == 11 causes my program to no longer respond. Is there anther way to have the
      Message 2 of 3 , Jul 10, 2010
      • 0 Attachment
        I worked my problem down to the read statement...
        "|| inStream.read() == 11" causes my program to no longer respond. Is there anther way to have the intellibrain read com while testing 'If' statements?

        public class Main
        {
        /**
        * @param args the command line arguments
        */
        public static void main(String[] args)
        {
        // TODO code application logic here
        try
        {
        IntelliBrainAnalogInput switch1 = IntelliBrain.getAnalogInput(1);
        IntelliBrainDigitalIO led1 = IntelliBrain.getDigitalIO(1); led1.setDirection(true);

        SerialPort comPort = IntelliBrain.getCom1();
        InputStream inStream = comPort.getInputStream();
        OutputStream outStream = comPort.getOutputStream();

        while(true)
        {
        // Switch1 and LED1
        if(switch1.isSet()==true || inStream.read() == 11)
        {
        led1.set();
        outStream.write(1);
        }
        else if(switch1.isSet() == false || inStream.read() == 12 )
        {
        led1.clear();
        outStream.write(10);
        }
        }
        catch(Exception error)
        {
        error.printStackTrace();
        }
        }

        }
      • Paul King
        Using read() inside if statements in the way you are doing it is problematic. This may be the cause of your problem. read() does not just report the
        Message 3 of 3 , Jul 10, 2010
        • 0 Attachment
           
          Using read() inside "if" statements in the way you are doing it is problematic. This may be the cause of your problem.
           
          read() does not just report the character, it also causes it to be "consumed" from the buffer. And because you are using "||", which is a conditional logic operator, the number of attempted reads is dependent on your switch1 setting.  This block of code can read and consume 0, 1, or (theoretically) 2 characters depending on the switch1 value and the data.
           
          For example, if switch1 is "false" and the input character is "12", your second "if" block will never execute. This is because the first read() will be triggered by the "||" and consume the "12" character before the second "if" can see it.
           
          Also, "if (switch1.isSet()==true)" is redundant.  Just use "if (switch1.isSet())"
           
          Here is a clearer way to write your logic:
           
          int inputChar = inStream.read();
          if (switch1.isSet() || inputChar == 11)
          {
              led1.set();
              outStream.write(1);
          }
          else if (~switch1.isSet() || inputChar == 12 )
          {
              led1.clear();
              outStream.write(10);
          }
          However there is still something malformed about your code fragment. For example, the check for "inputChar == 12" will never actually happen, because it can only be triggered by switch1==true.  However if switch1==true, then first block will execute and prevent the second block from being triggered.
           
          So maybe this is what you intended:
           
          int inputChar = inStream.read();
          if (switch1.isSet() || inputChar == 11)
          {
              led1.set();
              outStream.write(1);
          }
          else
          {
              led1.clear();
              outStream.write(10);
          }
          Also, note that read() will block until there is a character to read.  So if you have nothing on the other side writing characters to the serial cable, it will hang.  If you don't want this behavior, call inStream.available() first, as in:
           
          if (switch1.isSet() || (inStream.available() > 0 && inStream.read() == 11))
          {
              led1.set();
              outStream.write(1);
          }
          else
          {
              led1.clear();
              outStream.write(10);
          }
          Hope this helps and did not misunderstand the problem!
           
          Paul

           
           
          ----- Original Message -----
          From: jujames84
          Sent: Saturday, July 10, 2010 2:24 PM
          Subject: [intellibrain] Re: What could be the cause?

           


          I worked my problem down to the read statement...
          "|| inStream.read() == 11" causes my program to no longer respond. Is there anther way to have the intellibrain read com while testing 'If' statements?

          public class Main
          {
          /**
          * @param args the command line arguments
          */
          public static void main(String[] args)
          {
          // TODO code application logic here
          try
          {
          IntelliBrainAnalogInput switch1 = IntelliBrain.getAnalogInput(1);
          IntelliBrainDigitalIO led1 = IntelliBrain.getDigitalIO(1); led1.setDirection(true);

          SerialPort comPort = IntelliBrain.getCom1();
          InputStream inStream = comPort.getInputStream();
          OutputStream outStream = comPort.getOutputStream();

          while(true)
          {
          // Switch1 and LED1
          if(switch1.isSet()==true || inStream.read() == 11)
          {
          led1.set();
          outStream.write(1);
          }
          else if(switch1.isSet() == false || inStream.read() == 12 )
          {
          led1.clear();
          outStream.write(10);
          }
          }
          catch(Exception error)
          {
          error.printStackTrace();
          }
          }

          }

        Your message has been successfully submitted and would be delivered to recipients shortly.