0

I've created a java GUI that executes batch files in order to easily and seamlessly command a serial com port device.

try {
Process p = Runtime.getRuntime().exec("toggleLed.bat");
    } catch(Exception e) {
      e.printStackTrace();
    }

This code executes the following .bat file:

@ECHO OFF
ECHO A >COM1
EXIT

This code sends the letter "A" over com port 1 and the device interprets it as a command and turns on the LED, works perfectly. The only problem is, I want to be able to change the COM port via the java GUI. I have a "jSpinner" but I'm not sure how to get this to edit the com port value in the batch file.

Question: How do I get my java GUI to edit a batch file value?

PS: I'm using batch files to communicate over serial because I have tried numerous libraries such as RXTX but none of them work well/are too complicated.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Dragongeek
  • 263
  • 1
  • 5
  • 23
  • 1
    You should generally call `cmd.exe` directly with the needed command. However, I recommend that you take the time to actually use one of the libraries. – nanofarad Aug 14 '13 at 15:40
  • What about the [java-simple-serial-connector](https://code.google.com/p/java-simple-serial-connector/) library? 1+ to @hexafraction's answer below. – Hovercraft Full Of Eels Aug 14 '13 at 15:44
  • I want my application to work cross computer without having to install any things in /jre/bin and so on. It needs to be simple and easy to use. Also I am a novice programmer and I found this to be the most simple solution (so far). I've tried many different libraries and even downloaded demo programs. They all did not work. I may have made stupid mistakes while testing these, but I think that this is the way to go for my application. – Dragongeek Aug 14 '13 at 15:45
  • 1
    "cross computer" Using Windows batch files is *very* cross-compatible. Anyway, don't put the library in /jre/bin, but keep it around with the project itself, and put it on the classpath with -cp – nanofarad Aug 14 '13 at 15:46
  • LOL @ *very* cross-compatible. or where the Class-Path in the Manifest file will find it. – Hovercraft Full Of Eels Aug 14 '13 at 15:48

1 Answers1

3

Don't use a batchfile for a single command:

String command="cmd /C \"ECHO A >COM"+portNum+"\""; 

This translates to cmd /C "ECHO A>COM#"

and call it with:

Process p = Runtime.getRuntime().exec(command);

If you need multiple commands separate them with & inside the command line, such as cmd /C "FOO&BAR --baz"

Note that the quotes that are part of the command line are escaped with a backslash.

However, I strongly recommend you take the time to learn and use a library as it is far more portable.

Libraries can operate on multiple OSs(not just Windows) with some minimal changes, and definitely not a complete rewrite of your application. They can be carried around as jars with your project and put into the classpath, similar to what you're doing with your batch file now, but actually cross-compatible.

Libraries may take a few extra minutes to set up in the project but can be carried around with it and put in the classpath via a custom classloader or, more simply, the -cp parameter to the java command. You can also edit the manifest file to add the jar to the classpath.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Perhaps could also use `"cmd /c start toggleLed.bat 1"` and change the batch file to use `... >com%1`. I have not tried this. – OldCurmudgeon Aug 14 '13 at 15:46
  • Thanks for your answer, but why do you recommend to use a library? So far I've only had problems to even get one to work. – Dragongeek Aug 14 '13 at 15:47
  • 1
    @OldCurmudgeon It still means carrying extra files around. Curiously, the OP wants to carry around bats, not jars that would be even more useful. – nanofarad Aug 14 '13 at 15:47
  • @hexafraction Well, If I use your method of not using batch files, just String command="cmd /C \"ECHO A >COM"+portNum+"\""; I will not need to lug around batch files. – Dragongeek Aug 14 '13 at 15:54
  • @Dragongeek: but hexafraction has stated that his solution (and yours of using batch files) will not work with non-Window's platforms. – Hovercraft Full Of Eels Aug 14 '13 at 15:56
  • @HovercraftFullOfEels I know it's a dead end to use batch files but frankly, my application will only be used on windows machines. I might redo it when I have more experience with Java. – Dragongeek Aug 14 '13 at 15:58
  • @Dragongeek I encourage you to try a simple serial library when you get time to do so. – nanofarad Aug 14 '13 at 16:02
  • @hexafraction If I want to use a serial Library, where would be a good place to start? I've searched for a long time but could not find any (working) tutorials. – Dragongeek Aug 14 '13 at 18:59
  • 1
    @Dragongeek [this one](https://code.google.com/p/java-simple-serial-connector/). IT includes a wiki and some javadocs for specific reference. – nanofarad Aug 14 '13 at 18:59