Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   Java (http://www.forosdelweb.com/f45/)
-   -   Leyendo de bascula (http://www.forosdelweb.com/f45/leyendo-bascula-405036/)

reneac79 27/06/2006 11:19

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 zona horaria es GMT -6. Ahora son las 23:32.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.