I am having an issue that I believe is simple to fix however I cannot wrap my head around it. I have a java program that connects to an arduino I can issue commands from the java program and the arduino responds accordingly. My issue is when the arduino starts up or resets I believe it is sending a white space or something to the serial port and my java picks that up and is disrupts the program. Arduino code and Java code posted below. I may have to send some sort of message from the arduino to clear the buffers and then start clean. I am not sure. Thanks in advance!
And below is the Java code that is for a button. When the button is pressed it sends a command to turn on an LED and it blinks....the Arduino is suppose to send data back in the form of "OX" when the Java code receives this code "OX" it prints to the console and states: LED BLINKING however it does not get the "OX" because there is something stuck in the buffers.
Sorry. I can't read that stuff. Your code indentation is all over the map, and it's hard to tell what's dependent on what.
It's often the case that communications interfaces get random junk when the device is first powered up. That's usually something that hardware design can help with.
When talking to a PC, though, there can be 2 issues where you have stale data left over from previous connections. First, at the OS level, depending on the TTY characteristics set and what options were applied when the port was opened. And in Java, your choice of what stream/reader you use can further complicate things, because if they are maintaining buffers, those buffers might need to be flushed explictly.
It can be messy and annoying. You might get better results if you use some sort of framing characters around your data and simply discard anything that's not within a frame.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
That's exactly what is happening....the start up and/or reset of the Arduino is throwing junk into the serial stream and that is what needs to be taken care of. How do I go about doing that in getting clean data to read from? I don't have an issue getting feedback from the Arduino on the second or third command issued from my Java code as that flushes the buffers and I get a clean read.
Ok I was able to get it working correcting using the below code. The message is sent from the Arduino to the java program is is printed and acknowledged on the console. Now I am having issues trying to to the same thing each time a button is pressed. How ever the process just hangs. My question is why doesn't it hang in the first instance and hangs in the second instance? Thanks again!
Code that sends the response from the Arduino to the Java program:
Here is the code that is used in the Arduino when a button in Java is pressed and the response back:
My question is why does it work in one section and not in the other as it just hangs.
Hi..as per my understanding there can be 2 issues where you have stale data left over from previous connections. First, at the OS level, depending on the TTY characteristics set and what options were applied when the port was opened. And in Java, your choice of what stream/reader you use can further complicate things
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking