Foros del Web » Programación para mayores de 30 ;) » Java »

detectar cuando una variable a cambiado

Estas en el tema de detectar cuando una variable a cambiado en el foro de Java en Foros del Web. Buenas... recurro a ustedes lo grandes para sacarme de una que no pude jejeje tengo una variable tipo short short estadoPuerto; y necesito saber cuando ...
  #1 (permalink)  
Antiguo 18/11/2012, 21:41
 
Fecha de Ingreso: marzo-2010
Mensajes: 58
Antigüedad: 14 años, 1 mes
Puntos: 1
Pregunta detectar cuando una variable a cambiado

Buenas...

recurro a ustedes lo grandes para sacarme de una que no pude jejeje

tengo una variable tipo short

short estadoPuerto;

y necesito saber cuando la variable a cambiado de valor
necesito estar controlando esta variable SIEMPRE y cuando cambie de valor que llame un método


no se, si con:

Thread
BufferedReader
el metodo run()


se pueda hacer eso :/ pero no se como hacerlo me podrían ayudar.. si tienen alguna idea

lo otro es un bucle while infinito pero jajaja se cuelga el programa
  #2 (permalink)  
Antiguo 18/11/2012, 21:57
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: detectar cuando una variable a cambiado

Bueno la variable no puede cambiar mágicamente, tu eres el responsable de cambiarla durante la ejecución de tu programa, así que una vez aclarado lo anterior puedes utilizar un listener:

Código java:
Ver original
  1. public void setEstadoPuerto(short estadoPuerto) {
  2.       this.estadoPuerto = estadoPuerto;
  3.       myListener.onChangePort(estadoPuerto);
  4. }
  #3 (permalink)  
Antiguo 18/11/2012, 22:02
 
Fecha de Ingreso: marzo-2010
Mensajes: 58
Antigüedad: 14 años, 1 mes
Puntos: 1
Respuesta: detectar cuando una variable a cambiado

lo que pasa es que la variable la controla un puerto con un sensor y ese es el responsable de que esa variable se modifique de un 79 a un 111 entonces necesito monitorearla constantemente
  #4 (permalink)  
Antiguo 18/11/2012, 22:08
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: detectar cuando una variable a cambiado

Tal ves si proporcionas mas información, como las librerías que estas utilizando y el trozo de código donde manejas el sensor sea mas fácil ayudarte.
  #5 (permalink)  
Antiguo 18/11/2012, 22:18
 
Fecha de Ingreso: marzo-2010
Mensajes: 58
Antigüedad: 14 años, 1 mes
Puntos: 1
Respuesta: detectar cuando una variable a cambiado

Asi es como recupero el estado del sensor

short estadoPuerto = (short) lpt1.input((short) 0x379);

es un sensor infrarrojo conectado por puerto paralelo... y pues siempre arroja 79 y 111 dependiendo del estado
  #6 (permalink)  
Antiguo 18/11/2012, 22:23
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: detectar cuando una variable a cambiado

Que tal si pruebas a detener el Thread que verifica el estado cierto tiempo para que no se cuelgue el programa.
http://docs.oracle.com/javase/tutori...ncy/sleep.html
  #7 (permalink)  
Antiguo 18/11/2012, 22:26
 
Fecha de Ingreso: marzo-2010
Mensajes: 58
Antigüedad: 14 años, 1 mes
Puntos: 1
Respuesta: detectar cuando una variable a cambiado

mira masomenos asi esta funcionando
Código Java:
Ver original
  1. private pPort lpt1=new pPort();
  2. short estadoPuerto = (short) lpt1.input((short) 0x379);

y la librería seria esta

Código JAva:
Ver original
  1. package hardware.jnpout32;
  2.  
  3. // ** Derived from information provided on the web by Dr. Kenneth G. Schweller,
  4. // ** ( http://web.bvu.edu/faculty/schweller/ ) and his Mars Rover project page.
  5.  
  6. public class pPort
  7. {
  8.    ioPort pp;                   // wrapper class for 'Jnpout32.dll'
  9.                                // with methods:
  10.                                //    int Out32(int port, int value);
  11.                                //    int Inp32(int port);
  12.    short portAddress;            // address of data port
  13.    short currentVal;             // current value of port bits
  14.  
  15.    public pPort()
  16.    {
  17.       pp = new ioPort();
  18.       portAddress = (short)0x378;     // Hex Address of Data Byte of PC Parallel Port
  19.       setAllDataBits((short)0);       // initialize port bits to 0
  20.       currentVal = 0x00;
  21.    }
  22.  
  23.    // wrap ParallelPort output method
  24.    public void output(short port, short value)
  25.    {
  26.       pp.Out32(port, value);
  27.    }
  28.  
  29.    // wrap ParallelPort input method
  30.    public short input(short port)
  31.    {
  32.       return pp.Inp32(port);
  33.    }
  34.  
  35.     // output to default Data port
  36.     public void output(short value)
  37.     {
  38.       pp.Out32(portAddress, value);
  39.     }
  40.  
  41.     // input from default Data port
  42.     public short input()
  43.     {
  44.       return pp.Inp32(portAddress);
  45.     }
  46.  
  47.  
  48.   /**
  49.    * set all bits on Data port to zero
  50.    **/
  51.    public void setAllDataBits(short value)
  52.    {
  53.       pp.Out32(portAddress, value);
  54.       currentVal = value;
  55.    }
  56.  
  57.  
  58.    // For users who prefer dealing with Pin numbers
  59.    //    Set Pin <pin> to <value>
  60.    public void setPin(short pin, short value)
  61.    {
  62.       if (pin >= 2 && pin <= 9)
  63.          // just set the corresponding Data bit to indicted value
  64.          setDataBit((short)(pin-2), value);
  65.    }
  66.  
  67.  
  68.    /**
  69.     * Set Data Bit at selected index to a value of 1 or 0
  70.     * while preserving current values of all other Data bits
  71.     **/
  72.    void setDataBit(short index, short value)
  73.    {
  74.       switch(index)
  75.       {
  76.          case 0:
  77.            if (value==0)                        //  Set Data[0] to 0
  78.  
  79.               currentVal = (short) (currentVal & 0xFE);
  80.                                                        //      aaaa aaaa  currentVal
  81.                                                 //  AND 1111 1110  mask
  82.                                                 //      =========
  83.                                                 //      aaaa aaa0  new currentVal
  84.  
  85.            else                                 //  Set Data[0] to 1
  86.  
  87.               currentVal = (short) (currentVal | 0x01);
  88.                                                         //      aaaa aaaa   currentVal
  89.                                                 //  OR  0000 0001   mask
  90.                                                 //      =========
  91.                                                 //      aaaa aaa1   new currentVal
  92.            break;
  93.          case 1:
  94.            if (value==0)
  95.               currentVal = (short) (currentVal & 0xFD);
  96.                                                         //  currentVal = aaaa aa0a
  97.            else
  98.               currentVal = (short) (currentVal | 0x02);
  99.                                                         //  currentVal = aaaa aa1a
  100.            break;
  101.          case 2:
  102.            if (value==0)
  103.               currentVal = (short) (currentVal & 0xFB);
  104.                                                         //  currentVal = aaaa a0aa
  105.            else
  106.               currentVal = (short) (currentVal | 0x04);
  107.                                                         //  currentVal = aaaa a1aa
  108.            break;
  109.          case 3:
  110.            if (value==1)
  111.               currentVal = (short) (currentVal & 0xF7);
  112.                                                         //  currentVal = aaaa 0aaa
  113.            else
  114.               currentVal = (short) (currentVal | 0x08);   //  currentVal = aaaa 1aaa
  115.            break;
  116.          case 4:
  117.            if (value==0)
  118.               currentVal = (short) (currentVal & 0xEF);
  119.                                                         //  currentVal = aaa0 aaaa
  120.            else
  121.               currentVal = (short) (currentVal | 0x10);   //  currentVal = aaa1 aaaa
  122.            break;
  123.          case 5:
  124.            if (value==0)
  125.               currentVal = (short) (currentVal & 0xDF);
  126.                                                         //  currentVal = aa0a aaaa
  127.            else
  128.               currentVal = (short) (currentVal | 0x20);   //  currentVal = aa1a aaaa
  129.            break;
  130.          case 6:
  131.            if (value==0)
  132.               currentVal = (short) (currentVal & 0xBF);
  133.                                                         //  currentVal = a0aa aaaa
  134.            else
  135.               currentVal = (short) (currentVal | 0x40);   //  currentVal = a1aa aaaa
  136.            break;
  137.              case 7:
  138.            if (value==0)
  139.               currentVal = (short) (currentVal &  0x7F);
  140.                                                         //  currentVal = 0aaa aaaa
  141.            else
  142.               currentVal = (short) (currentVal | 0x80);   //  currentVal = 1aaa aaaa
  143.            break;
  144.  
  145.          default:
  146.            System.out.println("index must be 0 - 7");
  147.       }
  148.       pp.Out32(portAddress, currentVal);
  149.    }
  150.  
  151.     public short input(short s, short s0) {
  152.         throw new UnsupportedOperationException("Not yet implemented");
  153.     }
  154.  
  155.  
  156. }
  #8 (permalink)  
Antiguo 18/11/2012, 22:37
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: detectar cuando una variable a cambiado

Suponiendo que lo estés corriendo en un Thread:
(Recuerda capturar la excepciones yo no las capture por ser solo un ejemplo)

Código java:
Ver original
  1. while(condicion){
  2.      short estadoPuerto = (short) lpt1.input((short) 0x379);
  3.      myListener.onChangePort(estadoPuerto);
  4.      Thread.sleep(4000);
  5. }

Etiquetas: variables, cambios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:17.