Paso a explicar la librería java com:
Estos son los servicios basicos que da javacom, hay cosas que no he traducido porque era demasie pal body:
- Enumerar los puertos disponibles CommPortIdentifier.getPortIdentifiers devuelve una lista que contiene un objeto de la clase CommPortIdentifier por cada puerto disponible. Con este objeto se controla el acceso a los puertos.
- Abrir y declarar propietarios de los puertos de comunicaciones en los objetos CommPortIdentifier
- Solucionar el problema de la contención de propiedad de los puertos entre múltiples aplicaciones de Java. Se utilizan eventos para notificar a las aplicaciones interesadas . Por ejemplo, la excepción PortInUseExceptionse lanza cuando una aplicación falla al abrir un puerto.
- Mantener Entradas y Salidas asíncronas y síncronas en los puertos de comunicaciones. Las clases de bajo nivel como SerialPort y ParallelPort, tienen métodos para manejar las Entradas y Salidas en los puertos de comunicación.
- Un gestor de eventos como en los entornos graficos. Por ejemplo, si en el puerto serie cambia el estado del Carrier Detect, Ring Indicator, DTR, etc. el objeto SerialPort envía un enveto de tipo SerialPortEvent que describe los cambios de estado.
Jerarquía de las clases:
- class java.lang.Object
- interface javax.comm. CommDriver
- class javax.comm.CommPort
- class javax.comm.ParallelPort
- class javax.comm.SerialPort
- class javax.comm.CommPortIdentifier
- interface javax.comm.CommPortOwnershipListener (extends java.util.EventListener)
- class java.util.EventObject (implements java.io.Serializable)
- class javax.comm.ParallelPortEvent
- class javax.comm.SerialPortEvent
- interface javax.comm.ParallelPortEventListener (extends java.util.EventListener)
- interface javax.comm.SerialPortEventListener (extends java.util.EventListener)
- class java.lang.Throwable (implements java.io.Serializable)
- class java.lang.Exception
- class javax.comm.NoSuchPortException
- class javax.comm.PortInUseException
- class javax.comm.UnsupportedCommOperationException
- class java.lang.Exception
Las interfacs que vamos a usar:
public interface SerialPortEventListener extends EventListener
Métodos:
public abstract void serialEvent(SerialPortEvent ev)
– Propagates a SerialPortEvent event.
public abstract class CommPort extends Object
Métodos:
public String getName()
devuelve el nombre del puerto.
public String toString()
Devuelve un String representante del puerto
public abstract InputStream getInputStream() throws IOException
Devuelve el String que se encuentra en el buffer de entrada, es la unica manera de recibir del puerto de comunicaciones
public abstract OutputStream getOutputStream() throws IOException
Es la unica manera de mandar por el puerto de comunicaciones
public void close()
Cierra el puerto com.
public static Enumeration getPortIdentifiers()
Obtienes un enumerado con tipos del ojeto CommPortIdentifier por cada puerto.
public synchronized ConnPort open(String appname,int timeout) throws PortInUseException
Opens the communications port. open
obtains exclusive ownership of the port. If the port is owned by some other application, a PORT_OWNERSHIP_REQUESTED event is propagated using the CommPortOwnershipListener
event mechanism. If the application that owns the port calls close
during the event processing, then this open
will succeed. There is one InputStream and one OutputStream associated with each port. After a port is opened with open
, then all calls to getInputStream will return the same stream object until close
is called.
public CommPort open(FileDescriptor fd) throws UnsupportedCommOperationException
abre el puerto de comunicaciones obteniendo un objeto del tipo FileDescriptor que soporta esta tecnica.
public abstract int getBaudRate()
Gets the currently configured baud rate.
public abstract int getDataBits()
Gets the currently configured number of data bits.
– Returns:
integer that can be equal to DATABITS_5, DATABITS_6, DATABITS_7, or DATABITS_8
public abstract int getStopBits()
Gets the currently defined stop bits.
– Returns:
integer that can be equal to STOPBITS_1, STOPBITS_2, or STOPBITS_1_5
public abstract int getParity()
Get the currently configured parity setting.
– Returns:
integer that can be equal to PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK or PARITY_SPACE.
public abstract void sendBreak(int millis)
Sends a break of millis milliseconds duration. Note that it may not be possible to time the duration of the break under certain Operating Systems. Hence this parameter is advisory.
public abstract void addEventListener(SerialPortEventListener lsnr) throws TooManyListenersException
Registers a SerialPortEventListener object to listen for SerialEvents. Interest in specific events may be expressed using the notifyOnXXX calls. The serialEvent method of SerialPortEventListener will be called with a SerialEvent object describing the event.
Only one listener per SerialPort is supported. Calling addEventListener multiple times will simply replace the current SerialPortEventListener object.
All the events received by this listener are generated by one dedicated thread that belongs to the SerialPort object. After the port is closed, no more event will be generated. Another call to open()
of the port’s CommPortIdentifier object will return a new CommPort object, and the lsnr has to be added again to the new CommPort object to receive event from this port.
public abstract void setSerialPortParams(int baudrate,int dataBits,int stopBits,int parity) throws UnsupportedCommOperationException
Sets serial port parameters.
– Parameters:
baudrate – If the baudrate passed in by the application is unsupported by the driver, the driver will throw an UnsupportedCommOperationException
dataBits –
DATABITS_5: 5 bits
DATABITS_6: 6 bits
DATABITS_7: 7 bits
DATABITS_8: 8 bits
stopBits –
STOPBITS_1: 1 stop bit
STOPBITS_2: 2 stop bits
STOPBITS_1_5: 1.5 stop bits
parity –
PARITY_NONE: no parity
PARITY_ODD: odd parity
PARITY_EVEN: even parity
PARITY_MARK: mark parity
PARITY_SPACE: space parity
DEFAULT: 9600 baud, 8 data bits, 1 stop bit, no parity
Importante, comentar que todo esto está en la red, en un documento dentro de proiektua/java/javacomm.
En que jar se encuentra esta clase? Donde lo puedo descargar??
Gracias