문제

Hello I'm creating a program which reads and writes to serial port. At the beginning of the program I'm calling the classes ReadSerialPort , WriteSerialPort and JWakeup .

My problem is : While I'm writing something to the serial port from the JWakeup, I want to wait until the serial Port will response.

public class MainClass{


public static void main(String args[]){

    new ReadSerialPort().OpenSerialPort();
    new WriteSerialPort().OpenSerialPort();

    new JWakeup().setVisible(true);
}}


public class JWakeUp extends JFrame implements ActionListener{


public JWakeUp() {
    super("JWakeUp");

    //Other Code

    new WriteSerialPort().writeDataToSerialPort("WritenString");

    //In this part I want to know when the class ReadSerialPort() 
    // will read the "readenString"

}}
도움이 되었습니까?

해결책

You can create semaphore outside those classes with Semaphore semaphore = new Semaphore(0); and pass it to those classes. After finishing ReadSerialPort use semaphore.release() and inside JWakeUp where you wait for ReadSerialPort use semaphore.acquire()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top