Hello, i write a java programm that sends a simple integer (either 0 or 1) to the serial port, to be read by my mega arduino board. The problem currently is the java code :http://pastebin.com/K25NFzGS[/url]...
This is the logs that i obtain after hitting 1 time the Num / key :
Global keyboard hook successfully started !.
Yep ----
Yep -1
Yep #0
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
Yep #2
Yep #3
1
And if i try to hit a seconde time, nothing is printed, not even the "Yep ----". As if the code became impossible to be read by JVM
I hope you could understand me, despite my bad english. Thanks in advance !
A common problem is for a PC program to open the serial port, send a message and then close it. That does not work with an Arduino as it resets when the PC opens the serial port. The PC program should keep the serial port open until it is completely finished with the Arduino.
As well as the Python code in this Python - Arduino demo there is also a JRuby version that may be of interest as JRuby uses the JVM.
The problem is i have to send the value qhen i hit a certain key. And this is handled by a lib in my java program. So i can't use python code if my core code is java coded, can i ?
The only one solution is to call your JRuby compiled class from my java program. I found this topic, is it a good choice to do this method ?
Plaigon:
The problem is i have to send the value qhen i hit a certain key. And this is handled by a lib in my java program. So i can't use python code if my core code is java coded, can i ?
The only one solution is to call your JRuby compiled class from my java program. I found this topic, is it a good choice to do this method ? ruby - Can you call compiled JRuby classes from java? - Stack Overflow
I did not intend you to do either of those things. I just posted the link so you could read and maybe apply the ideas in your own Java code.
Sending a value when you hit a key has nothing to do with the question of whether your Java program keeps the serial port open.
If for some reason you really do need to be able to close and open the serial port there are ways to accommodate that. A simple one is to use a USB-ttl cable connected to the Arduinos Rx, Tx and GND pins rather than the USB connector on the Arduino board.
Am i forced to close each time the serial port after writing value in my programm, or can i just open it on first time after inital launching, and let it open for the next writing ? If i understood correctly the best choice is to let it open, and to handle the code via JRuby, isn't it ? You said "and maybe apply the ideas in your own Java code', it means you intend i call compiled JRuby class, yeah ? Or did you imply another ways to do it ?
Plaigon:
Am i forced to close each time the serial port after writing value in my programm,
Only you have the answer to that question. I have no idea what limitations your project might impose
or can i just open it on first time after inital launching, and let it open for the next writing ?
That is what I recommend - but I have no idea whether that is compatible with other non-Arduino requirements of your project. You have told us nothing about what the project is intended to do.
If i understood correctly the best choice is to let it open,
yes
and to handle the code via JRuby, isn't it ?
That is for you to decide. I don't object if you do that but if you are not familiar with JRuby it might mean a lot of learning. I just thought you might consider writing your own Java code so that it performs the same steps as the JRuby code. They are basically the same steps that the Python example implements.
I am assuming you are a competent Java programmer - I am not, and I intend to keep it that way
Thanks for your answer. I'm going to precise you my project.
I try to connect a dashboard to the game of simulation OMSI. For the moment everything works very, well the final progress is about 70 %. But for the moment I try to switch on the bulb being in the buttons of opening of doors, when these last ones are only opened. The interaction and key binding of this action, is set on Num/ key.
It is thus necessary to understand there that during the first one input key, the value on the serial port is 1, thus the arduino card is supposed to read 1 on the serial port, and thus as a consequence to switch on the bulb via a relay, etc.... When I hit a new time, we change again the integer to 0, and so the arduino card switches off the relay, and thus the bulb. Here is what I have create this topic. It is more clear I think? But maintaining my question stays, with the code Java, is it possible to process orhave i the obligation to use either the python, or either the JRbuy? Thank you in advance =)
Hello I have some news since a moment ago ! First I decided to use a new library: jSerialComm. Here is my code with : package fr.plaigon.djkh;import java.awt.BorderLayout;import java.awt.Color - Pastebin.com
My problem is that if I transfer the code to my arduino card while having the COM 3 port of connected on my software, the following problem happens : arduino logs ndicates a crash saying that the COM 3 port is busy. And if I try to make the opposite, that is to say open the COM 3 port after sending code finished, if I'm going to see content of the serial monitor window, again same crash is visible...
Therefore if anyone has suggestion, please tell me ^^
Sorry but there is no possibility that I am going to spend the time needed to make sense of your very long Java program. Ten or twenty lines, maybe. More than that and Java is a PITA,
In my experience the scatter-gun approach to debugging rarely works. Everytime you bring a new element into play you just add to the confusion.
You have not said how much Java programming experience you have?
I don't really understand why you are asking about the COM port being busy. I suspect you are well aware that only one program can access a COM port at any one time - either your Java program OR the Arduino IDE. You will have to explain the issue in more detail - list the exact steps that you take (a new line for each) to cause the problem.
I suggest that you write a short Java program that sends "<Hello World 1>", "<Hello World 2>" etc once per second to an Arduino program based on the third example in Serial Input Basics and extend the Arduino code to send back "<100>", "<200>" etc.
When you know how to do that you should then know what you need to do with your long program.
Plaigon:
Hello I have some news since a moment ago ! First I decided to use a new library: jSerialComm. Here is my code with : package fr.plaigon.djkh;import java.awt.BorderLayout;import java.awt.Color - Pastebin.com
My problem is that if I transfer the code to my arduino card while having the COM 3 port of connected on my software, the following problem happens : arduino logs ndicates a crash saying that the COM 3 port is busy. And if I try to make the opposite, that is to say open the COM 3 port after sending code finished, if I'm going to see content of the serial monitor window, again same crash is visible...
Therefore if anyone has suggestion, please tell me ^^
You can not have two applications using the serial port at the same time. Further I'm not sure why you open the serial monitor if you want to communicate with your application. Anyway, the Arduino IDE has a special way to handle the serial monitor so it does not interfere with uploads.
So to use your jave application and do an upload
1)
close your java application
2)
upload the code using the IDE; you don't have to close it
3)
start your java application
You seem to have a 5 second delay in both programs. That can't work because the delays will not synchronize. Try the Arduino code in the link I gave you. It does not need any delay().
It will be much easier to help you if you use code I am familiar with.
I think the problem doesn't provide from my arduino code, but rather my java code, because if i try to print the text after writing it with the output object, a blank text appears in the logs (i try to println with that :
System.out.println(new BufferedReader(new InputStreamReader(chosenPort.getInputStream(), "UTF-8")).readLine())
Hello i've got some news since yesterday. First my java code is finally working, arduino receives well messages, but this is my arduinoi script that has problems. If i try to open the relay when informations are on the serila port it doesn't work, but if i try to close it, it perfectly works. Very strange...
The digitalWrite HIGH in the if hasn't any effect despite the clignoting RX led on my arduino board. If i test the opposite, by writing a digitalWrite LOW in the IF, right at this moment it works ! If you have any idea it will be great =)
My arduino script : void setup() { Serial.begin(9600); Serial.setTimeout(50); pinMode(8, - Pastebin.com
I don't understand what you means. The problem is simple and at the same time difficltu (for me anyway) to resolve : i can't open the relay when there's data into serial port while closing it is possible. Where is the mistake ?
This is how you are supposed to post code. If you press the quote link, you can see how it is done.
String text = Serial.readString();
This is a blocking read that will look for data on the serial port.
If data is received, it's stored in str.
2)
Next you check if data is available on the serial port; more than likely not, because you have just read it.
3)
If no data available (as said, very likely), you write the relais pin HIGH.
I don't know if you have mentioned what you exactly write to the Arduino; if it's a single character.
void loop()
{
// if there is data available
if(Serial.available() > 0))
{
// read it
char ch = Serial.read();
// and take action
if(ch == '0')
{
...
...
}
if(ch == '1')
{
...
...
}
}
}
You need to fill in the dots.
You can send the characters '0' and '1' to do something; any other character will be ignored.
PS
get rid of String (capital S); it's dangerous. Use nul terminated character arrays (so called C strings, note the lower case s)