• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Java Arduino Serial Communication

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Group!

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.

 
Bartender
Posts: 20984
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim H.

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.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Its the same code. Or is it? Thanks for the help!
 
Rancher
Posts: 3413
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the process just hangs.


What statement is the execution hanging on?

Is some read statement blocking waiting for input?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!