certo, chiedo scusa. Allora il codice trovato è il seguente:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Scanner;
import gnu.io.*;
public class Prova implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
// "/dev/tty.usbserial-A9007UX1", // Mac OS X
// "/dev/ttyUSB0", // Linux
"COM3", // Windows
};
private BufferedReader input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine = null;
if (input.ready()) {
inputLine = input.readLine();
System.out.println(inputLine);
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other
// ones.
}
public static void main(String[] args) throws Exception {
Prova main = new Prova();
main.initialize();
Thread t = new Thread() {
public void run() {
// the following line will keep this app alive for 1000 seconds,
// waiting for events to occur and responding to them (printing
// incoming messages to console).
try {
Thread.sleep(1000000);
} catch (InterruptedException ie) {
}
}
};
t.start();
System.out.println("Started");
}
}
a console viene stampato questo (variabili configurate in arduino per la scrittura del codice di funzionamento delle fotoresistenze):
Started
Raw Sensor Values Red: 919 Green: 913 Blue: 881
Mapped Sensor Values Red: 229/t Green: 228 Blue: 220Raw Sensor Values Red: 919 Green: 914 Blue: 882
Mapped Sensor Values Red: 229/t Green: 228 Blue: 220Raw Sensor Values Red: 919 Green: 913 Blue: 882
Mapped Sensor Values Red: 229/t Green: 228 Blue: 220Raw Sensor Values Red: 920 Green: 914 Blue: 882
Mapped Sensor Values Red: 230/t Green: 228 Blue: 220Raw Sensor Values Red: 920 Green: 913 Blue: 881
Mapped Sensor Values Red: 230/t Green: 228 Blue: 220Raw Sensor Values Red: 919 Green: 914 Blue: 881
Mapped Sensor Values Red: 229/t Green: 228 Blue: 220Raw Sensor Values Red: 921 Green: 914 Blue: 882
Mapped Sensor Values Red: 230/t Green: 228 Blue: 220Raw Sensor Values Red: 918 Green: 913 Blue: 880
Mapped Sensor Values Red: 229/t Green: 228 Blue: 220Raw Sensor Values Red: 920 Green: 914 Blue: 881
Mapped Sensor Values Red: 230/t Green: 228 Blue: 220Raw Sensor Values Red: 922 Green: 914 Blue: 883
.................................................. .................................................. .................................................. ................
continua a stampare valori di luci delle fotoresistenze fin quando non stoppo l'esecuzione. Io vorrei rendere questi valori (ad esempio mapped sensor values) come variabili da poter utilizzare stesso in java, ad esempio "if (mapped sensor value red) {...}
puoi aiutarmi?non so se sono stato chiaro