Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. [SOLVED] SerialPort and Thread: can't read from the input stream.

[SOLVED] SerialPort and Thread: can't read from the input stream.

Scheduled Pinned Locked Moved Java
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Francesco Fraccaroli
    wrote on last edited by
    #1

    Hi colleague developers, I've got a problem in communicating with serial port. In my applcation I have to receive some data from the serial port and to manage them for some operation. For now I'm just trying to write out the data received. I've create a test class with main() that manage the connection (open the port, set the parameters) and starts a thread. The thread is started in this manner:

        try {
            (new Thread(new Lettore(port), "Lettore")).start();
        } catch (Exception e) {
            System.err.println("Errore " + e);
        }
    

    where the "port"parameter is the SerialPort object. The "Lettore" class contains the follow:

    SerialPort port;
    
    public Lettore(SerialPort port) throws IOException {
        this.port = port;
    }
    
    @Override
    public void run() {
        byte\[\] buffer = new byte\[1024\];
        int len = -1;
            try {
            while (true) {
                //read, write on terminal, echo.
                len = port.getInputStream().read(buffer);
                System.out.print(new String(buffer, 0, len));
                if (len != 0) {
                    port.getOutputStream().write(buffer, 0, len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }

    In this case, the code in the thread seems to be never executed: no characters are show on the terminal and on the other station of the communication. On the other hand, if I write the same code of the run() function in the main, it works fine. Someone has ever got this problem? Can anyone help me?

    L 1 Reply Last reply
    0
    • F Francesco Fraccaroli

      Hi colleague developers, I've got a problem in communicating with serial port. In my applcation I have to receive some data from the serial port and to manage them for some operation. For now I'm just trying to write out the data received. I've create a test class with main() that manage the connection (open the port, set the parameters) and starts a thread. The thread is started in this manner:

          try {
              (new Thread(new Lettore(port), "Lettore")).start();
          } catch (Exception e) {
              System.err.println("Errore " + e);
          }
      

      where the "port"parameter is the SerialPort object. The "Lettore" class contains the follow:

      SerialPort port;
      
      public Lettore(SerialPort port) throws IOException {
          this.port = port;
      }
      
      @Override
      public void run() {
          byte\[\] buffer = new byte\[1024\];
          int len = -1;
              try {
              while (true) {
                  //read, write on terminal, echo.
                  len = port.getInputStream().read(buffer);
                  System.out.print(new String(buffer, 0, len));
                  if (len != 0) {
                      port.getOutputStream().write(buffer, 0, len);
                  }
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      

      }

      In this case, the code in the thread seems to be never executed: no characters are show on the terminal and on the other station of the communication. On the other hand, if I write the same code of the run() function in the main, it works fine. Someone has ever got this problem? Can anyone help me?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to make your thread runnable, as described in http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html[^].

      F 1 Reply Last reply
      0
      • L Lost User

        You need to make your thread runnable, as described in http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html[^].

        F Offline
        F Offline
        Francesco Fraccaroli
        wrote on last edited by
        #3

        Dear Richard, thanks for the reply. I made the thread runnable by "implements Runnable" on the "Lettore" class definition, now it works. Thank you very much! The final code is this:

        public class Lettore implements Runnable {

        SerialPort port;
        
        public Lettore(SerialPort port) throws IOException {
            this.port = port;
        }
        
        @Override
        public void run() {
            byte\[\] buffer = new byte\[1024\];
            int len = -1;
                try {
                while (true) {
                    //read, write on terminal, echo.
                    len = port.getInputStream().read(buffer);
                    System.out.print(new String(buffer, 0, len));
                    if (len != 0) {
                        port.getOutputStream().write(buffer, 0, len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups