Ciao a tutti,
sto utilizzando la libreria rxtx.
Ho bisogno semplicemente di mandare un qualsiasi dato ad arduino ma sembra che non ne sono capace.
Peró sono capace di leggere dati da Arduino.
In pratica la comunicazione mi funziona solo da Arduino a Eclipse e non il contrario.
Vi posto il codice. Sto utilizzando l´esempio
TWOWAYSERIALCOMMUNICATION trovato qui:
http://rxtx.qbang.org/wiki/index.php...he_serial_port
Ecco il codice:
Questo é la classe TwoWaySerialComm.java:codice:import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.InputStream; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import gnu.io.CommPort; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.border.EtchedBorder; import processing.serial.*; public class DegreenGUI extends JFrame { private static final int BAUDRATE = 0; private JPanel contentPane; public Serial portToClose; public static void main(String[] args) { System.out.println("Main StartedNEW"); EventQueue.invokeLater(new Runnable() { public void run() { try { //InterfaceOne frame = new InterfaceOne("Button1"); DegreenGUI frame = new DegreenGUI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public DegreenGUI() { setTitle("DEGREEN GUI\n"); //setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0, 0, 900, 1100); // The first 2 numbers set the position of the DEGREEN GUI window on the screen contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); // Button CONNECT JButton btn0 = new JButton("CONNECT"); btn0.setBounds(700, 6, 107, 29); btn0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { actionButtonConnect(); } }); contentPane.setLayout(null); contentPane.add(btn0); } // What is performed when I press button START public void actionButtonConnect(){ String portName="COM3"; try { TwoWaySerialComm serialPort= new TwoWaySerialComm(); (new TwoWaySerialComm()).connect(portName); } catch (Exception e) { e.printStackTrace(); } } // What is performed when I press button WRITE public void actionButtonWrite(){ } }
Io riesco a leggere senza problemi ma non riesco a mandare un messaggio ad Arduino. Questo lo capisco dal fatto che se chiudo Eclipse e provo a mandare un messaggio qualsiasi ad Arduino, mi si accende un Led (ho messo un controllo su Serial.available()>0) e tutto funziona.codice:import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class TwoWaySerialComm { public TwoWaySerialComm() { super(); } void connect ( String portName ) throws Exception { int baudRate=115200; //System.out.println(CommPortIdentifier.getPortIdentifiers()); CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if ( portIdentifier.isCurrentlyOwned() ) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(),baudRate); if ( commPort instanceof SerialPort ) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); OutputStream out = serialPort.getOutputStream(); //System.out.println("Start SerialReader..."); (new Thread(new SerialReader(in))).start(); //System.out.println("Start SerialWriter..."); (new Thread(new SerialWriter(out))).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } } /** */ public static class SerialReader implements Runnable { InputStream in; public SerialReader ( InputStream in ) { this.in = in; } public void run () { byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { System.out.print(new String(buffer,0,len)); } } catch ( IOException e ) { e.printStackTrace(); } } } /** */ public static class SerialWriter implements Runnable { OutputStream out; public SerialWriter ( OutputStream out ) { this.out = out; } public void run () { String sentString=">"; System.out.println(sentString.getBytes()); try { int c = 0; while ( ( c = System.in.read()) > -1 ) { this.out.write(c); this.out.flush(); } } catch ( IOException e ) { e.printStackTrace(); } } } }
Invece se faccio partire questo programma, riesco a ricevere i dati da arduino ma sembra che arduino non si accorge che sto provando a mandare qualcosa con l´istruzione
this.out.write(c);
che dovrebbe mandare il valore di c ad Arduino.
Aiutatemi per favore!!!![]()

Rispondi quotando