galera, to com um problema com comunicacao serial do pacote java.comm…
Estou me comunicando com um pic e mando comando do computador pro pic, e o pic envia respostas…
O programa do pic está funcionando perfeitamente… e os dados estão sendo enviado para o pic corretamente
pelo computador! Mas quando o pic responde, e vai ser lancado um evento dizendo que tem dados no buffer,
ocorre o seguinte erro:
java.lang.IllegalAccessError:
testeserial.PIC.serialEvent(Ljavax/comm/SerialPortEvent;)V
at
com.sun.comm.Win32SerialPort.sendDataAvailEvent(Win32Serial
Port.java:649)
at
com.sun.comm.NotificationThread.run(Win32SerialPort.java:87
8)
alguém pode me ajudar a entender pq isso aconteceu e como solucionar o problema? note que na hora q eu mando
uma mensagem para o pic, eu uso um timer de 1 segundo (já tentei até com 20 segs). segue aih fragmentos do codigo…
valeu
private void obterControlePortaSerial() {
boolean portFound = false;
portList = CommPortIdentifier.getPortIdentifiers();
while( portList.hasMoreElements() && portFound == false ) {
portId = (CommPortIdentifier) portList.nextElement();
if( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
portFound = true;
}
}
if( portFound ) {
try {
serialPort = (SerialPort) portId.open( "fdas", 2000 );
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.addEventListener( this );
serialPort.notifyOnDataAvailable( true );
haveSerialControl = true;
}
catch ( Exception exc) {
exc.printStackTrace();
haveSerialControl = false;
}
}
else {
haveSerialControl = false;
}
}
public String enviarMensagemPIC(String commandString) throws Exception {
readBuffer = null;
if( haveSerialControl ) {
for(int count = 0; (count < 5) && (readBuffer == null); count++ ) {
try { outputStream.write( commandString.getBytes() ); } catch( IOException exc ) {}
java.util.Timer timer = new java.util.Timer();
timerEstourado = false;
timer.schedule(new TimerOut(), 1000);
while(!timerEstourado) { }
timerEstourado = false;
}
}
if( readBuffer == null ){
throw new Exception();
}
returnString = readBuffer.toString();
return returnString;
}
class TimerOut extends java.util.TimerTask {
public void run() {
timerEstourado = true;
}
}
// soh que ele nem chega a entrar nesse serialEvent, eu já testei
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:
readBuffer = new byte[20];
JOptionPane.showMessageDialog(null, "Marca 1");
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read( readBuffer );
JOptionPane.showMessageDialog(null, "Marca 2");
}
JOptionPane.showMessageDialog(null, "Marca 3");
System.out.print( new String( readBuffer ) );
JOptionPane.showMessageDialog(null, "Marca 4");
} catch ( IOException e ) {}
JOptionPane.showMessageDialog(null, "Marca 5");
break;
}
}