So I've been looking around for a way to write from an Arduino directly onto a file on the PC, and basically I've found out there's no native way to do so. I wanted to do this in order to then read the file from a C++/Java program, and use the information in it. I also wanted to do this in real-time at some point, so it would be kind of like sending information from the Arduino over to the Java/C++ program for processing.
However, I've seen multiple people state on other forums that you can link the Serial output to some program running on the PC, and then use that program to write the output to a file. However, each time, they neglect to write out how exactly to do this.
The main purpose I wanted to write from the Arduino directly to a file was to read this file from another (Java/C++) program, so the above would be great for me. So how can I get the Serial output into a Java (much more preferably, as I might want to use Swing later) or C++ program, to then use this information in the program itself, or write it to a file? Real-time sending would be a great help.
If the above isn't possible, MATLAB might do, but to be clear, I would much rather be able to interface with Java/C++. Or both Java and MATLAB.
EDIT: To be more specific about what exactly I'd like to do, it is to sort of 'trigger' the Java program to read from the Serial output when a new line has been written (so it reads each line separately) and store it in a string in the Java program, then process it, all at once, and then sleep until another new line is written to the Serial port.
The following Links show you, how you can implement the serial communication in Java and C++.
Java :
http://playground.arduino.cc/interfacing/java
C++ :
http://playground.arduino.cc/Interfacing/CPPWindows
If you want to write the data stream to a local file, you can do that, for instance, with ofstream (C++) or PrintWriter (Java).
Furthermore, there are a several additional libraries for other programming languages such as c# (cmdMessenger).
Just in case anyone was looking for the easiest way to do this, it is hands down to ignore C++ and Java and use MATLAB.
There's a great short tutorial at AllAboutEE that I used and it solved most of my problems. Instead of plotting the data at the end, just use fprintf in MATLAB to output the data to a file.
Related
In my Ruby on Rails app I'm having a routine that writes to a file (through a java application) and then reads the written file.
write_to_file(file.path, data)
read_file(file.path)
Most of the time this works just fine. But some times it looks like the file write had not happened but there were no errors either. And when I retry the routine with the same data it has worked each time.
I have begun to think if the file write happens asynchronously and the file is actually read before the data is written to the disk. Would this be possible?
write_to_file calls a java application through a socket connection that takes care of the writing. Java application returns a simple json string back to Rails.
This question is really "what does the Java code do?" and is not really a Ruby question. It's not even really a Java question, because the Java language allows (of course) any kind of implementation.
The Java code could certainly be returning before the file is available for reading. We have no idea. It could be posting a request to a queue, and then returning, for example.
The Java code is what you need to look at. If you don't want to bother with that, you could always do something like this:
sleep 0.01 until File.readable?(file.path)
This is a bit crude and there are more elegant ways to do this, but it would work.
You might be experiencing file buffering where small amounts of data aren't written to the file unless it's flushed. I'm not sure what interface you're using here, but the flush method is intended to deal with this exact situation.
I'd like to use the filesystem to pass information between a java process and a native process. I would like the java process to open the file and wait until a certain word is written into the file. My specific need is for Android, but I'm guessing any answer will work of other versions of Java.
I imagine I can open the file again and again, looking for the word that the java process expects to be written into the file, and sleeping between opens. But it feels like there might be a more effective way.
I prefer to use file system because sockets (which are far more natural for Java) can be a little complicated to use from native code in my set up. I'm also okay with working out something using pipes, if it makes a difference.
One more important note: the Java process is a simple command line process, not an APK app.
What is the best way to do this?
The StdIn library is one of the easiest ways to accomplish this, here are the docs. You could run the program with:
java File < input.txt
And write something like:
String word = "x" // whatever String you're looking for
while (!StdIn.isEmpty()) {
String test = StdIn.readString();
if (test.equals(word))
// do something
}
I want to know what is the easiest way to pass a String from my Java program to my Python program. The reason is that I use boilerpipe to extract some text from the web, then analyse it through my Java program, but I also have to make some semantic searchs using pattern.en, which is only available for python.
I don't need to get results back from my python program, just that my Python program can get the String.
I first thought about letting my python program listen to a .txt file, while java feeds it with the String, but I think it'll be too hard to make.
I'm on Windows 7, and my Python version is 2.7.9.
EDIT : I think I haven't been very clear actually. Ideally, I'd want my java code to run my python program which would have recieved the string.
Graciela Runolfsdottir's 2nd answer would maybe do the trick, if I can make something like that :
String s = "string to analyse"
FileUtils.writeStringToFile(new File("test.txt"), s);
Then execute it with : python analyzer.py test.txt, but I don't know how to call this command from java. Is it possible ?
The easiest way to accomplish this (and historically the "canonical" way) would be to write the string to stdout with the source program (the Java program in your case) and read from stdin with the sink program (the python program).
To link the input/output, you could use "Command Redirection", specifically the pipe:
java -jar source_program.jar | python sink_program.py
You can pass String through:
command line argument (it should not to be long and must be escaped)
file (just save string to file and pass it file name to Python application)
STDIN (output your string to STDIN of your Python program)
I think the last way is simple in implementation and you should choose it.
Different approach: try using jython. That might you to actually run python code "within" the context of your JVM.
Or if you are really concerned about having a "solid" solution based on a profound architecture; consider using a message bus; like ActiveMQ which has clients for both Java and Python.
I am writing a console application in Java. It is similar to a chat client: Input and output are asynchronously made. The problem is that if some output is made while the user is in the middle of typing, the lines will get mixed up on the screen.
I am looking for a solution which allows me to have a input area separate from the output area. At the moment I am using an extra thread which polls a BufferedReader on System.in.
The program needs to run on a Linux server and be accessed via an ssh session. So any hints that only work in this environment are fine.
Are there any high level libraries which can do this? Or is there a smart trick using terminal / ANSI codes? The ANSI codes s (save cursor) and r (restore cursor) might be helpful but how do i know where to jump to do the output and how do i handle scrolling?
I recall a long time ago working with similar things but in C++. I was using the ncurses library then. Check out javacurses which seems to be a Java implementation of something like ncurses.
Sounds like you need to use Curses. JCurses is a Java implementation of the Curses library and will give you control of the terminal to allow scrolling, positioning etc.
I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:
=====================
You just need to make sure that stdin for kgsGtp it connected to
the engine's output and stdout for kgsGtp is connected to the engine's
input. Usually, the easiest way to do this is by forking and execing
kgsGtp from within your engine.
=====================
Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.
I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?
That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.
Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.
Edit: more complete example is here: http://support.microsoft.com/kb/190351
What you need is equivalent of POSIX dup() on windows may be this