# Serial write fails on the first write

**URL:** <https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 22, 2019, 5:41am UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564 "2019-12-22T05:41:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [December 22, 2019, 5:41am UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564/1 "2019-12-22T05:41:50Z")

</div>

Thank you for reading this. I’ve spent most of the day trying to discover why the serial write function will not send the correct data the first time. If I send the same block of data twice then the correct data is received by the Arduino. If I send the same block once more, without resetting the Arduino, then all is well. The serial write seem to only fail if I write the block once after an Arduino reset.

If I send the same data from the Arduino serial monitor then there isn’t a problem. So, the fault is something that I’m missing from my Processing code, but What?

After a bit more experimentation I’ve discovered that if I send two line feeds before the data block then all is well. Does this mean that I need some sort of marker at the beginning of the block or do I need the reset something in the serial device before sending the data?

import processing.serial.\*;

Serial myPort;

boolean call\_once = true;

void setup()  
{  
myPort = new Serial(this, “/dev/ttyUSB0”, 9600);  
delay(1000); // wait for Arduino to reset  
}

void draw()  
{  
if (call\_once)  
{  
write\_data();  
call\_once = false;  
}  
}

void write\_data()  
{  
myPort.write(‘1’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(‘0’);  
delay(500);  
myPort.write(’\n’);

println(“data written”);  
}

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 22, 2019, 7:06pm UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564/2 "2019-12-22T19:06:23Z")

</div>

Hello,

Please format your code as per “Is your post formatted?”:  
[https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147/4](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147/4)

Otherwise we can’t easily cut and past to test code; quotes are always a problem.

The Processing code posted is fine.  
I can receive the data on a terminal (puTTY) and other Processing application.

Please post your formatted Arduino code (minimal) and I will take a look.

🙂

---

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [December 23, 2019, 4:37am UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564/3 "2019-12-23T04:37:26Z")

</div>

> [@glv](#):
>
> Please post your formatted Arduino code (minimal) and I will take a look.

That’s very kind of you. I’ll post the lot, it’s fairly short.

Edit. I’m not sure that I got the “code goes here working”. I highlighted the code and pressed “\</\>” and the 3 ``` were displayed.

```auto
#include "LedControl.h"
LedControl lc = LedControl (12, 11, 10, 1);

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

int dataNumber = 0;

int grid[] =
{
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
};

void display_grid()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow (0, i, grid[i]);
  }
}
void setup()
{
  Serial.begin (9600);
  // the MAX72XX is in power-saving mode on startup,
  // we have to do a wakeup call
  lc.shutdown (0, false);

  // set the brightness to a medium values
  lc.setIntensity (0, 0); // 0 = low; 8 = high

  // show grid - to demonstrate that the 8x8 array works
  display_grid();
  //delay(1000);

  // clear the display
  lc.clearDisplay (0);
}

void loop()
{
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false)
  {
    rc = Serial.read();

    if (rc != endMarker)
    {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars)
      {
        ndx = numChars - 1;
      }
    }
    else
    {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData()
{
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;

    for (int i = 0; i < 8; i++)
    {
      //grid[i] = atoi(receivedChars[i]);
      grid[i] = receivedChars[i] - '0';
      Serial.println(grid[i]);
    }
    display_grid();
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 23, 2019, 4:51am UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564/4 "2019-12-23T04:51:04Z")

</div>

just ideas:

-a- change from sending chars to sending lines…

-b- already send lots of info back, show it at processing console

-c- handshake: arduino sends a line “READY”  
and processing only starts sending after got that.  
( so it also could help as a kind of see “Arduino REBOOT” logic )

---

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [December 24, 2019, 10:54pm UTC](https://discourse.processing.org/t/serial-write-fails-on-the-first-write/16564/5 "2019-12-24T22:54:15Z")

</div>

> [@kll](#):
>
> handshake: arduino sends a line “READY”

Thank you kll and glv for your time.

It finally dawned on me that Processing is sending data to the Arduino before it’s ready. I simply increased the delay from 1 second to 1.5 seconds after myPort is created. I suppose a more technically correct method would be to have the Arduino signal when it’s ready. I also discovered that the inter-character delay is not necessary.
