Ciao watermark, grazie per l'info !!!
Utilizzo pure quella libreria, la RxTx.
Ho provato di nuovo a smanettare il codice, non vuole saperne proprio nulla di leggere il peso con Jbutton. La cosa strana è che con la JTextArea funziona correttamente.
Ve lo posto, magari riuscite ad individuare il problema anche perchè non vorrei iniziare tutto daccapo visto che per far funzionare questo ho dovuto "sbattere" la testa ovunque.
Se è proprio necessario tenterò la strada di utilizzare due thread appositi, per la lettura e scrittura.
codice:
import javax.swing.*;
import gnu.io.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("serial")
public class MainProva extends JFrame implements SerialPortEventListener
{
/*** My Variables ***********************************************************/
GUIProva gui; //class defining the graphical user interface
SerialPort serialPort;
InputStream inputStream;
OutputStream outputStream;
CommPortIdentifier portIdentifier;
boolean debug; //true to print exception reports, false for release
static String[] portNames;
static boolean portOpen;
static int baud,databits,parity,stopbits;
/*** Constructor ************************************************************/
public MainProva()
{
debug = false; //Activates exception reports used for debugging
portOpen = false; //flags whether a port is currently open
portNames = new String[10]; //ports found on this computer, 10 maximum
baud = 9600; //default baud setting
databits = 8; //default databits setting
parity = 0; //default parity setting
stopbits = 1; //default stopbits setting
findPorts(); //this method populates the string[] portNames
}
/*** main method ************************************************************/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainProva main=new MainProva();
main.init();
}
});
}
/*** called by main() *******************************************************/
private void init() {
gui=new GUIProva(this);
gui.setVisible(true);
initwritetoport();
}
private void findPorts() {
Enumeration<CommPortIdentifier> portEnum =
CommPortIdentifier.getPortIdentifiers();
int portCount = 0;
while ( portEnum.hasMoreElements() ) {
portIdentifier = portEnum.nextElement();
// populate the portNames array for display in gui.jcbPort
//if the port is a serial port
if(portCount < portNames.length &&
portIdentifier.getPortType() == portIdentifier.PORT_SERIAL)
{
//put it in the array
portNames[portCount] = portIdentifier.getName();
portCount++;
}
}
}
public void RS232Setup(String portName) {
Enumeration<CommPortIdentifier> portEnum =
CommPortIdentifier.getPortIdentifiers();
if(portOpen) { //close any currently open port
try {
inputStream.close();
}
catch(IOException e) {exceptionReport(e);}
serialPort.notifyOnDataAvailable(false);
serialPort.close();
serialPort.removeEventListener();
}
portOpen = false; //clear the portOpen flag
while ( portEnum.hasMoreElements() ) {
portIdentifier = portEnum.nextElement();
if(portIdentifier.getName().equals(portName)
&& portIdentifier.getPortType() == portIdentifier.PORT_SERIAL)
{
gui.setjlStatus(portName + " found.");
try { //try to open the port
serialPort = (SerialPort)portIdentifier.open("CommUtil", 2000);
portOpen = true; //if successful, update the portOpen flag
break;
}
catch (PortInUseException e) {
gui.setjlStatus("Port " + portIdentifier.getName() + " is in use.");
}
catch (Exception e) {
gui.setjlStatus("Failed to open port " + portIdentifier.getName());
}
}
} //end of while loop
//if we succeeded in opening the port, set it up for operation
if(portOpen) {
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {exceptionReport(e);}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {exceptionReport(e);}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try {
//set the port parameters
serialPort.setSerialPortParams(baud, databits, stopbits, parity);
//update to gui to display current status
gui.setjlStatus(portName,
serialPort.getBaudRate(),
serialPort.getParity(),
serialPort.getDataBits(),
serialPort.getStopBits(),
portOpen);
//update the gui open/close port button
gui.setjbOpenClose(portOpen);
}
catch (UnsupportedCommOperationException e) {exceptionReport(e);}
initwritetoport();
}
} //end of RS232Setup() method
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[64];
try {
// read data
int numBytes = inputStream.read(readBuffer);
inputStream.close();
//send the received data to the GUI
String result = new String(readBuffer,0,numBytes);
gui.setjtaReceived(result);
System.out.println(result);
}
catch (IOException e) {exceptionReport(e);}
break;
}
} //end of serialEvent() method
public void initwritetoport() {
if(portOpen) {
try {
// get the outputstream
outputStream = serialPort.getOutputStream();
} catch (IOException e) {exceptionReport(e);}
try {
// activate the OUTPUT_BUFFER_EMPTY notifier
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
exceptionReport(e);
System.exit(-1);
}
}
} //end of initwritetoport() method
public void writetoport(String outString) {
try { if(portOpen)
outputStream.write(outString.getBytes());
System.out.println(outString.getBytes());
}
catch (IOException e) {exceptionReport(e);}
}
public void exceptionReport(Exception e) {
if(debug) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}