Retroceder   Foros del Web > Programación para sitios web > Java y JSP

Respuesta
 
Herramientas Desplegado
Antiguo 27-jun-2006, 11:19   #1 (permalink)
reneac79 está en el buen camino
 
Fecha de Ingreso: abril-2004
Ubicación: Mérida, Yucatán
Mensajes: 35
Molesto Leyendo de bascula

Hola...
Intentare explicarles de la manera mas explicita mi situacion...
Problema: Necesito leer el peso q despliega una bascula conectada al com1, este dato tiene q ser desde un applet, por la sencilla razon q necesito pasar ese dato a una funcion de javascript de una pagina .HTML

Voy por partes... Tengo una aplicacion q lee correctamente el peso de la bascula.. teoricamente solo necesitaria pasarlo a un applet, pero como soy principiante en java, he tenido problemas...

Invetigando, encontre un codigo q lee el puerto serial... de ahi pase a problemas con la seguridad del puerto y un applet... bueno, pues cree el applet firmado y todo ese show... y lo logre... a medias... ya no me marca error de seguridad.. de hecho ejecuto el html, se despliega la firma y todo... pero NO TRAE EL (#/&$%/=( "&%) DATO...

aki les paso los codigos.. el de la aplicacion q funciona correctamente para lo q les pueda ayudar.... y tambien el applet q no me trae el dato de la bascula...


APLICACION:
Código:
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class Bascula implements Runnable, SerialPortEventListener
{
    static String messageString = "P";
    static SerialPort serialPort;
    static OutputStream outputStream;
    static CommPortIdentifier portId;
    static Enumeration portList;
    static InputStream inputStream;
    static Thread readThread;
        String dato;
    public static void main(String[] args) 
    {
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) 
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {   
                if (portId.getName().equals("COM1")) 
                {
                    try 
                    {
                        serialPort = (SerialPort)
                        portId.open("BasculaApp", 2000);
                    } catch (PortInUseException e) {}
                    try 
                    {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try 
                    {
                        serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try 
                    {
                        outputStream.write(messageString.getBytes());
                    } catch (IOException e) {}
                    Bascula reader = new Bascula();
                }
            }
        }

    }
    public Bascula() 
    {
        try 
        {
            serialPort = (SerialPort) portId.open("BasculaApp", 2000);
        } catch (PortInUseException e) {}
        try 
        {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {}
        try 
        {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {}
        serialPort.notifyOnDataAvailable(true);
        try 
        {
            serialPort.setSerialPortParams(9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {}
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() 
    {
        try 
        {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
    }

    public void serialEvent(SerialPortEvent event) 
    {
        switch(event.getEventType()) 
        {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
            case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[10];

            try 
            {
                while (inputStream.available() > 0) 
                {
                    int numBytes = inputStream.read(readBuffer);
                }
                dato=new String(readBuffer);
                System.out.print(dato);
            } catch (IOException e) {}
            break;
        }
    }

}
APPLET:
Código:
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.applet.*;
import netscape.javascript.*;

public class applet extends Applet implements Runnable, SerialPortEventListener 
{
    static String messageString = "P";
    static SerialPort serialPort;
    static OutputStream outputStream;
    static CommPortIdentifier portId;
    static Enumeration portList;
    static InputStream inputStream;
    static Thread readThread;
    String dato;
    private JSObject javaScript;

    public void init() 
    { 
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) 
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {   
                if (portId.getName().equals("COM1")) 
                {
                    try 
                    {
                        serialPort = (SerialPort)
                        portId.open("BasculaApp", 2000);
                    } catch (PortInUseException e) {}
                    try 
                    {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try 
                    {
                        serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try 
                    {
                        outputStream.write(messageString.getBytes());
                    } catch (IOException e) {}
                    applet reader = new applet();
                    try 
                    {
                        serialPort = (SerialPort) portId.open("appletApp", 2000);
                    } catch (PortInUseException e) {}
                    try 
                    {
                        inputStream = serialPort.getInputStream();
                    } catch (IOException e) {}
                    try 
                    {
                        serialPort.addEventListener(this);
                    } catch (TooManyListenersException e) {}
                    serialPort.notifyOnDataAvailable(true);
                    try 
                    {
                        serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    if (readThread == null) 
                    {
                        readThread = new Thread(this);
                    }
                }
            }
        }
    }   
    
    public void start() 
    {
        if (readThread != null) 
        {
            readThread.start();
        }
    }
    
    public void run()
    {
        try 
        {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
    }
    
    public void serialEvent(SerialPortEvent event) 
    {
        switch(event.getEventType()) 
        {
            case SerialPortEvent.BI:
	    case SerialPortEvent.OE:
	    case SerialPortEvent.FE:
	    case SerialPortEvent.PE:
	    case SerialPortEvent.CD:
	    case SerialPortEvent.CTS:
	    case SerialPortEvent.DSR:
	    case SerialPortEvent.RI:
	    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
	    case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[10];
            
            try 
            {
                while (inputStream.available() > 0) 
                {
                    int numBytes = inputStream.read(readBuffer);
                }
                dato=new String(readBuffer);
            } catch (IOException e) {}
            break;
        }
    	javaScript = JSObject.getWindow(this);
        if (javaScript != null)
        {
            Object[] args = new Object[1];
            args[0] = dato;
            javaScript.call("asigna", args);
     	}
    }
    
    public void stop()    
    {
        readThread = null;
    }
    
    public void destroy()    
    {
        serialPort.close();
    }
    
}
Ahora el error q me esta marcando es este:

Caught java.lang.NullPointerException: name can't be null while loading driver com.sun.comm.Win32Driver

Espero q puedan ayudarme.. o darme algun tipo de consejo, se los agradeceria de verdad...
__________________
"La inteligencia consiste no solo en el conocimiento, sino en la destreza de aplicar los conocimientos en la práctica." :pensando: :pensando:

Aristóteles
reneac79 está desconectado   Responder Citando
Respuesta

No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Desactivado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 19:25.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93