Serial library peculiarity

Hi all,

I am having an issue with the Serial library that comes built into Arduino (from Processing). My project is attempting to communicate with the Arduino from a custom Java application. I have a simple Serial.write( loop going on on the Arduino to test.

I am using the and friends (Exceptions and MessageConsumer) that come packaged with Arduino. I have traced the issue down to the following: it works when debugging only. I have also traced the problem down to the particular line that does not work when running normally. It appears to be the output.write(bytes) line from

  public void write(byte bytes[]) {
    try {
      output.flush();   // hmm, not sure if a good idea

    } catch (Exception e) { // null pointer or serial port dead
      //errorMessage("write", e);

I notice that when I am debugging, my MessageConsumer's message() method gets called as soon as this line executes, but in a normal run that function never gets called. I have already put many hours into this and appear to be at a wall. Any hints/ideas are very appreciated. Thanks, John

hints: hint 1: post the complete code - I don't know what it does, .... hint 2: serial communication takes time, introducing debug statements may provide just the delay needed to work hint 3: describe what you expected, what it did, show output when it worked, and when it didn't

ideas: output.flush(); // hmm, not sure if a good idea If this is the comment, maybe you should try without it? Do you know what it does, exactly?


Thank you for your urging. I still have only a fuzzy grasp of what the original problem was, but you caused me to find a way around it :grin:

It turns out that when constructing a Serial(...) AND calling thatSerial.write() in the same method call, there is a phantom problem somewhere. I even put a Thread.sleep(1000) between the constructor and the write to make sure there was time to initialize everything and create a lock file. But no dice.

Then I put the mySerial.write() call in a separate method, triggered by a separate button, and it works beautifully.

This adds some UI clunk, but I think it's ok. Thank you very much!