Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I admittedly don't have any experience with Arduino, but I have a lot of experience with messing with Java Output/InputStreams. Take this with a grain of salt, as I have no way to actually test this on a real Arduino device, but if I were in your situation, this is what I'd do.</p> <p>I suspect your call to <code>stream.write("This is a very, very long string, which must be made even longer to demonstrate the problem".getBytes());</code> is actually buffering the entire string. When you subsequently call <code>stream.flush()</code>, all of the string data gets "written" to the Arduino device all at once. The reason I suspect this is because you're calling the <code>BufferedOutputStream</code> super constructor with a size of 500 bytes. First, I'd try reducing that to 64-bytes. However, I suspect this might not fix the problem either because <code>BufferedOutputStream</code> isn't going to know when to call <code>ping</code>.</p> <p>I think you're going to want to override both <code>write</code> and <code>flush</code>. The idea behind <code>flush</code>, according to the Java specification, is that <code>flush</code> is supposed to force any unwritten, buffered bytes to get written to the underlying stream:</p> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#flush" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#flush</a>()</p> <p>I'd also probably just extend plain-ole <code>OutputStream</code> and do any buffering on my own. I would override the <code>write</code> method to buffer up to 64-bytes of data at a time. If 64-bytes isn't enough space, call <code>super.write()</code> on your filled 64-byte buffer, then call <code>super.flush()</code> and then <code>ping()</code>, prior to buffering/writing any more data. Repeat this process in a while loop until your buffer has enough space to hold whatever's left of your input data. Then override <code>flush</code> to write any data that didn't completely fill your underlying 64-byte buffer (and thus did not actually get written by previous calls to <code>super.write()</code>).</p> <p>This way, if you call <code>write</code> on too much data, your class will write to the underlying stream until at most 64-bytes remain, and your subsequent call to <code>flush</code> will ensure those last few bytes actually get written to the Arduino. At the end of the day, you also get a fully-Java-compliant <code>OutputStream</code> implementation, which is nice too.</p> <p><em><strong>Edit</em></strong></p> <p>I'm using 64-bytes simply because you stated that your Arduino can handle up to that many at a time. Of course, this can be reduced if necessary, I'm just using "64" for convenience.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload