Java/Comunicazione seriale: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Corretto: "necessario"
Nessun oggetto della modifica
Etichetta: Editor wikitesto 2017
Riga 55:
Il contenuto del file di proprietà è in genere una sola riga, il nome della classe Java con il driver nativo, ad esempio:
 
<sourcesyntaxhighlight lang=java> driver = com.sun.comm.Win32Driver </ source>
 
Il seguente è un hack che permette di distribuire JavaComm via Web Start ignorando che il cervello di proprietà morti file. Si è gravi inconvenienti, e potrebbe non riuscire con le nuove uscite JavaComm - Sun dovrebbe mai venire intorno e fare una nuova versione.
Riga 61:
In primo luogo, disattivare il gestore della sicurezza. Alcuni programmatori Doofus a Sun ha deciso che sarebbe bello nuovo e di nuovo per verificare l'esistenza del temuto''javax.comm.properties''file, anche dopo che è stato caricato inizialmente, per nessun altro motivo apparente, rispetto al controllo per la file.
 
<sourcesyntaxhighlight lang=java> System.setSecurityManager (null); </ source>
 
Poi, quando si inizializza l'API JavaComm, inizializzare il driver manualmente:
 
<sourcesyntaxhighlight lang=java> driverName String = "com.sun.comm.Win32Driver"; / / o ottenere come una proprietà JNLP
 CommDriver CommDriver = (CommDriver) Class.forName (driverName). NewInstance ();
 commDriver.initialize ();</ source>
Riga 115:
 
Enumerating and selecting the desired port identifier is typically done in one loop:
<sourcesyntaxhighlight lang=java>
import javax.comm.*;
import java.util.*;
Riga 156:
//
// Use port identifier for acquiring the port
//</sourcesyntaxhighlight>
...
 
Riga 162:
 
Once a port identifier has been found, it can be used to acquire the desired port:
<sourcesyntaxhighlight lang=java>
//
// Use port identifier for acquiring the port
Riga 179:
// Now we are granted exclusive access to the particular serial
// port. We can configure it and obtain input and output streams.
//</sourcesyntaxhighlight>
...
 
Riga 187:
 
As part of the initialization process the Input and Output streams for communication will be configured in the example.
<sourcesyntaxhighlight lang=java>
import java.io.*;
...
Riga 235:
if (is != null) is.close();
if (os != null) os.close();
if (port != null) port.close();</sourcesyntaxhighlight>
 
=== Simple Data Transfer ===
Riga 250:
* Explain how to mix binary and character I/O over the same stream
* Fix the example to use streams}}
<sourcesyntaxhighlight lang=java>
// Write to the output
os.print("AT");
Riga 257:
is.readLine(); // First read will contain the echoed command you sent to it. In this case: "AT"
is.readLine(); // Second read will remove the extra line feed that AT generates as output
</syntaxhighlight>
</source>
 
==== Simple Reading of Data (Polling) ====
Riga 263:
If you correctly carried out the write operation (see above) then the read operation is as simple as one command:
 
<sourcesyntaxhighlight lang=java> // Read the response
String response = is.readLine(); // if you sent "AT" then response == "OK"</sourcesyntaxhighlight>
 
==== Problems with the simple Reading / Writing ====
Riga 308:
 
==== Setting up a serial Event Handler ====
<sourcesyntaxhighlight lang=java>
import javax.comm.*;
 
Riga 398:
}
}
</syntaxhighlight>
</source>
Once the listener is implemented, it can be used to listen to particular serial port events. To do so, an instance of the listener needs to be added to the serial port. Further, the reception of each event type needs to be requested individually.
<sourcesyntaxhighlight lang=java>
SerialPort port = ...;
...
Riga 433:
port.notifyOnRingIndicator(true);
<- other events not used in this example */
</sourcesyntaxhighlight>
 
 
Riga 453:
 
A ring buffer as such is nothing special, and has no special properties regarding threading. It is just that this simple data structure is used here to provide data buffering. The implementation is done so that access to this data structure has been made thread safe.
<sourcesyntaxhighlight lang=java>
/**
* Synchronized ring buffer.
Riga 607:
}
}
</syntaxhighlight>
</source>
With this ring buffer one can now hand over data from one thread to another in a controlled way. Any other thread-safe, non-blocking mechanism would also do. The key point here is that the write does not block when the buffer is full and also does not block when there is nothing to read.
 
Riga 617:
 
The skeleton event listener proposed a method <code>outputBufferEmpty()</code>, which could be implemented as it follows.
<sourcesyntaxhighlight lang=java>
RingBuffer dataBuffer = ... ;
 
Riga 630:
{{TODO}}
}</sourcesyntaxhighlight>
 
==== Reading of Data ====
 
The following example assumes that the data's destination is some file. Whenever data becomes available it is fetched from the serial port and written to the file. This is an extremely simplified view, because in reality one would need to check the data for an end-of-file indication to, for example, return to the modem command mode.
<sourcesyntaxhighlight lang=java>
import javax.comm.*;
 
Riga 678:
if (is != null) is.close();
if (port != null) port.close();
}</sourcesyntaxhighlight>
 
=== Handling multiple Ports in one Application ===