Writing to and reading from the Same Serial Port
              in 
             Core Library Questions 
              •  
              2 years ago    
            
 
           
             Hey,
            
i have following problem and I hope one can help me:
            
1) I created a virtual serial port with: http://www.eterlogic.com/Products.VSPE.html
2) my Java Project consists of two classes: one establishs the connection to the Serial port and is waiting for receiving bytes, the second one take the same serial port and writes a byte to it.
            
Problem:
I never receive data the second class sends. If I call available() i always got 0 as return... so no data are in the serial buffer to read.
            
            
            
Code:
            
SerialReceive Class
            
            
            
 
           
 
            
           i have following problem and I hope one can help me:
1) I created a virtual serial port with: http://www.eterlogic.com/Products.VSPE.html
2) my Java Project consists of two classes: one establishs the connection to the Serial port and is waiting for receiving bytes, the second one take the same serial port and writes a byte to it.
Problem:
I never receive data the second class sends. If I call available() i always got 0 as return... so no data are in the serial buffer to read.
Code:
SerialReceive Class
- public class SerialReceive extends Thread{
public Serial serial;
byte[] inBuffer;
public SerialReceive(PApplet parent) {
serial = new Serial(parent, Serial.list()[0], 115200);
inBuffer = new byte[1];
}
public void run(){
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SerialReceive.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(serial.available());
if(serial.available() != 0){
serial.readBytes(inBuffer);
System.out.println(inBuffer[0]);
}
}
} 
- public class SerialSend extends Thread {
Serial serial;
byte[] inBuffer;
public SerialSend(PApplet parent, Serial serialDest) {
serial = serialDest;
inBuffer = new byte[1];
}
public void run() {
serial.write(65); // Sending a Testbyte
System.out.println(serial.available());
}
} 
- public void setup(){
serialreceive = new SerialReceive(this);
serialreceive.start();
serialsend = new SerialSend(this, serialreceive.serial);
serialsend.start();
} 
 
              
              1  
            
 
            