0
내 응용 프로그램의 텍스트 필드에서 입력을 읽고 arduino로 보내야합니다. 나는 값을 읽을 수 있지만 arduino에 보내는 방법을 모른다. 직렬 포트에서 읽고 쓰는 Arduino 클래스가 있는데 어떻게 Arduino에 앰프 값을 보낼 수 있습니까?javafx 응용 프로그램에서 Java 용 RXTX를 사용하는 방법
자바 FX 클래스
private String frequenza;
private String ampiezza;
@FXML private TextField amp;
@FXML private TextField freq;
private Stage stageiniziale;
private BorderPane rootlayout;
Arduino a = new Arduino();
@Override
public void start(Stage stageiniziale){
this.stageiniziale = stageiniziale;
this.stageiniziale.setTitle("App Luca");
initRootLayout();
mostraPersona();
}
public void initRootLayout(){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(ArduinoSeriale.class.getResource("Root.fxml"));
rootlayout = (BorderPane) loader.load();
Scene scene= new Scene(rootlayout);
stageiniziale.setScene(scene);
stageiniziale.show();
a.initialize();
}
catch(IOException e){
e.printStackTrace();
}
}
public void mostraPersona() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(ArduinoSeriale.class.getResource("Principale.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootlayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return stageiniziale;
}
public static void main(String[] args) {
launch(args);
}
public void bottoneFrequenza() {
frequenza = getFrequenza();
System.out.println(frequenza);
}
public void bottoneAmpiezza() throws IOException, InterruptedException {
ampiezza = getAmpiezza();
int amp = Integer.parseInt(ampiezza);
System.out.println(amp);
}
public String getFrequenza(){
frequenza = freq.getText();
return frequenza;
}
public String getAmpiezza(){
ampiezza = amp.getText();
return ampiezza;
}
아두 이노 클래스 (RXTX)
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyACM0", // Raspberry Pi
"/dev/ttyUSB0", // Linux
"COM5", // Windows
};
/**
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
*/
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
public void initialize() {
// the next line is for Raspberry Pi and
// gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
// System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
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 {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
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();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
/**
* Handle an event on the serial port. Read the data and print it.
* @param oEvent
*/
@Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
try {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
//serialPort.getOutputStream().write(1);
serialPort.getOutputStream().write(1);
//Thread.sleep(20);
// Ignore all the other eventTypes, but you should consider the other ones.
} catch (IOException ex) {
Logger.getLogger(Arduino.class.getName()).log(Level.SEVERE, null, ex);
}
}
public synchronized void invia(int a) throws IOException, InterruptedException{
serialPort.getOutputStream().write(1);
//Thread.sleep(20);
System.out.println(a);
}
public static void main(String[] args) throws Exception {
Arduino main = new Arduino();
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");
}