salve a tutti...![]()
Sono settimane che non riesco a risolvere questo problema...
Sto cercando di realizzare un' applicazione di streaming che usi la porta seriale....lato server, e quindi in scrittura sembra che non ci siano problemi, lato client però, le cose non vanno,non riesco a leggere in maniera corretta, la classe che implementa il thread di lettura della porta seriale è la seguente:
class SerialInputStream extends Thread implements PushSourceStream ,SerialPortEventListener{
boolean done = false;
boolean dataRead = false;
Serial port;
InputStream inStream;
int offset, length;
byte[] buffer;
int num;
SourceTransferHandler sth= null;
ObjectOutputStream dout;
int ByteRead;
int off;
public SerialInputStream (Serial port) {
this.port = port;
try {
FileOutputStream file =new FileOutputStream("testWrite.txt");
dout = new ObjectOutputStream(file); } catch (Exception e) {}
ByteRead=0;
off=0;
}
public int read(byte readBuffer[], int offset, int length) {
port.open();
//System.err.println("Try to read\n");
try {
port.serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
int numBytes;
try {
//port.serialPort.getInputStream();
port.serialPort.notifyOnDataAvailable(true);
if ( port.inputStream.available() > 0) {
//System.err.println("Dati disponibili\n");
numBytes = port.inputStream.read(readBuffer, offset,length);
ByteRead = ByteRead+numBytes;
System.err.println("Totale byte letti :"+ ByteRead+"\n");
// off= off+length;
// dout.write(readBuffer, off ,length);
//dout.close();
}
else
{
System.out.println("Non ci sono dati disponibili\n");
//port.close();
return -1;
}
synchronized (this) {
dataRead = true;
notifyAll();
}
}
catch (IOException e) {
//port.close();
return -1;}
//port.close();
return numBytes;
}
public synchronized void start() {
super.start();
if (sth != null) {
dataRead = true;
notifyAll();
}
}
public synchronized void kill() {
done = true;
port.close();
notifyAll();
}
public int getMinimumTransferSize() {
return 2*1024; // twice the MTU size, just to be safe.
}
public synchronized void setTransferHandler(SourceTransferHandler sth) {
this.sth = sth;
//sth = setTransferHandler( new SerialTransferHandler(this));
dataRead= true;
}
// Not applicable.
public ContentDescriptor getContentDescriptor() {
return null;
}
// Not applicable.
public long getContentLength() {
//return LENGTH_UNKNOWN;
return -1;
}
// Not applicable.
public boolean endOfStream() {
return false;
}
// Not applicable.
public Object[] getControls() {
return new Object[0];
}
// Not applicable.
public Object getControl(String type) {
return null;
}
/**
* Loop and notify the transfer handler of new data.
*/
public void run() {
while (!done) {
synchronized (this) {
while (!dataRead && !done) {
try {
wait();
System.err.println("Waiting\n");
}
catch (InterruptedException e) { }
}
dataRead = false;
}
if (sth != null && !done) {
sth.transferData(this);
//Questo punta a null e quindi non trasmette niente
}
}
}
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:
break;
}
}
}//end class SerialInputStream
L'apertura della porta seriale viene anche fatta nel costruttore della mia classe che estende serialPort....ora perchè se tolgo la open() all'interno della read , il player del Client non legge più alcun dato da seriale???![]()
Lasciando la open mi legge dei dati ma la riproduzione del file è pessima...![]()
![]()
![]()
HELP ME PLEASE!!!!!!!

Rispondi quotando