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());
        cr=txt;
        output.print(cr);
        output.flush();

        OpenPort.getPort().addDataListener(new SerialPortDataListener() {
               @Override
               public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
               @Override
               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) {


                                txt_.setText(msg);
                                list.add(msg);
                                System.out.println(list);
                                System.out.println(item);


                            }
                            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_;
       }

        @Override
        public void run() {

              sendReceive(txt,txt_);
              try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
        }




    }

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

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

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

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

 executor.shutdown();

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

avatar
  Subscribe  
Notify of