1

I am trying to read and write to a serial port using a combination of shell and java. The goal is to be able to use a PrintWriter and a BufferedReader to send and receive commands from a device connected to a serial port. I know that this can be done in different ways (without using shell) but this is not what I am looking for. I want to be able to do this using shell and java.

Here is my code:

static String port = "/dev/tty.usbmodem411";
static int baudRate = 9600;
private static String command = "screen " + port + " " + baudRate;

public static void main(String[] args) throws Exception {
    System.out.println("Command is " + command);
    Process p = Runtime.getRuntime().exec(command);
    //p.waitFor();

    BufferedReader reader =
            new BufferedReader(new InputStreamReader(
            p.getInputStream()));
    String line = reader.readLine();
    while(true)
    {
        if (line != null) {
            System.out.println(line);

        }
        line = reader.readLine();
    }

}

With this code, I am specifically trying to read from the serial port. I am using java to run a shell command to access a serial port and then read the output from the command.

However, when I run this code I always get a message saying "Must be connected to a terminal." I have also tried changing the line command = "screen " + port + " " + baudRate; to command = "screen -dm" + port + " " + baudRate;, but then I get no output whatsoever. I have consulted several similar questions, Executing screen command from Java and How to open a command terminal in Linux? but I still can't figure out what I should do to fix this problem. I have a feeling that it must be something very simple, but after hours of research I can't figure out what to do.

Community
  • 1
  • 1
Daisy Holton
  • 533
  • 3
  • 17
  • Sometimes we cannot get what we want. Sometimes we can... I would use **kermit** for [this](http://www.columbia.edu/kermit/ckscripts.html). – Elliott Frisch Dec 24 '13 at 22:45
  • I understand that this isn't the best way to do this, but if at all possible I would like to resolve this error. – Daisy Holton Dec 24 '13 at 22:48
  • Shouldn't the command be a string array and each argument a different string... – Aniruddha Sarkar Dec 24 '13 at 22:51
  • I'm fairly sure that it doesn't matter, but I will go ahead and check that. – Daisy Holton Dec 25 '13 at 00:20
  • Update: Changing the command to an array of strings seemed to have no effect on the code. I got exactly the same result. – Daisy Holton Dec 25 '13 at 00:25
  • GNU screen is definitely *not* the best tool for this job; its strengths lie with interactivity. If you really must fork an external utility, at least use [cu(1)](https://www.mirbsd.org/man1/cu) – but you *really* ought to just open the `tty` device directly in Java™. I mean CacheWolf does that in Ewe (an embedded Java™ subset), it's not hard! Your solution *will* have problems. – mirabilos Dec 25 '13 at 00:27
  • +1 for quixote-esque project! Good luck. – shellter Dec 25 '13 at 01:20
  • _Why_ do you need to communicate without using a serial port library? – rm5248 Dec 29 '13 at 02:14
  • I don't, really. I am simply trying to resolve this error. – Daisy Holton Dec 31 '13 at 20:49
  • Funny that nobody mentioned http://www.oracle.com/technetwork/java/index-jsp-141752.html. – Hannes Feb 08 '15 at 14:30

2 Answers2

1

Instead of screen you may use command cu from UUCP package. To install UUCP package sudo apt-get install uucp or sudo yum install uucp.

Then use this command: static String command = "cu -l " + port + " -s " + baudRate;

Some explanation:

Community
  • 1
  • 1
Marek
  • 76
  • 3
0

Your description suggests that you simply need to access the stream received on the serial port. You indicate that you wish to use a shell command to access the port. Why not use cat ... to send the content received from the port to the standard output.

If this works, then why is it not possible to just open the serial port and read from it directly?

Using screen would seem to bring in all the full screen handling provided by this window manager that is unlikely to be handled correctly. For example, what terminal type should screen format the output for?

Pekka
  • 3,529
  • 27
  • 45