Synchronizing threads to send and receive data from arduino

I’m creating a method to send and receive data from arduino . After I call this method in a thread. I use ThreadPoolExecutor to execute this thread 3 times.Because I have 3 JtestField and I need in every one show a received data. when I execute them only one which ise shown. I was trying to use differe t methods to synchronize these threads but no result. Thank you for your help.

This is the method:

  synchronized  void sendReceive(String txt,JTextField txt_){
        PrintWriter output = new PrintWriter(OpenPort.getPort().getOutputStream());

        OpenPort.getPort().addDataListener(new SerialPortDataListener() {
               public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
               public void serialEvent(SerialPortEvent event)

                    Scanner data=new Scanner(OpenPort.getPort().getInputStream());

                    while(data.hasNextLine()) {
                            String msg = null;
                            Object item=null;
                            try{msg= data.nextLine();}catch(Exception e){}

                            if(msg != null) {


                            msg = null;




The thread in which I call it:

      class ThreadDemo extends Thread {
    String cr=null;
    JTextField txt_;
    String txt;
    private Thread t;
    ThreadDemo(String txt,JTextField txt_) {
          this.txt = txt;
          this.txt_ = txt_;

        public void run() {

              try {Thread.sleep(1000000);} catch (InterruptedException ie) {}


and after I use the ThreadPoolExecutor to execte threads in the constructor of my first class:

 executor.execute(new ThreadDemo("ee_get:100",SCAN.ipAddress_txt));

 executor.execute(new ThreadDemo("ee_get:160",SCAN.port_txt));

 executor.execute(new ThreadDemo("ee_get:60",SCAN.intervale_txt));


please any help I was spending a lot of time to resolve this problem and no result.

Normally I must send 3 commands to arduino and receive 3 times data and every time this data must be shown in a JTextField. However I see one received data in the JTextfield

Leave a Reply

Notify of