Input/output error in writeArray

Hi guys
I am trying to control some impulse relays (Schneider A9C30115 datasheet) via the USB port. I used ULN2803 as a driver and a PC817 optocoupler.
Almost after 40-50 attempts without an error, in the last attempt an error occurred

Error:
java.io.IOException: Input/output error in writeArray
at gnu.io.RXTXPort.writeArray(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1173)
…

N.B:
With only leds connected to my UNO, everything works fine without error
(I did a 24 hours test without error)
The UNO still working fine even after this error, I can control relays by push-buttons, just the USB is affected

…
if (Serial.available() > 0) {
  incomingByte = Serial.read();

if (incomingByte == '1') {
  digitalWrite(6, LOW); 
  delay(200);
  digitalWrite(6, HIGH);  }

if (incomingByte == '2') {
  digitalWrite(7, LOW); 
  delay(200);
  digitalWrite(7, HIGH); }

if (incomingByte == '3') {
  digitalWrite(8, LOW); 
  delay(200);
  digitalWrite(8, HIGH); }
…

Any ideas guys, thanks

Post all your code.
The Arduino IDE serial monitor can become unstable/crash if it gets really long lines of text without terminating CR/LF characters or maybe your sending codes it does not like.

void setup() {

 Serial.begin(9600);
 pinMode(6, OUTPUT);
 digitalWrite(6, HIGH);

 pinMode(7, OUTPUT);
 digitalWrite(7, HIGH);

 pinMode(8, OUTPUT);
 digitalWrite(8, HIGH);
 
 pinMode(9, OUTPUT);
 digitalWrite(9, HIGH);
}

void loop() {
if (Serial.available() > 0) {
 
incomingByte = Serial.read();


if (incomingByte == '1') {

 digitalWrite(6, LOW); 
 delay(200);
 digitalWrite(6, HIGH); 
}

if (incomingByte == '2') {

 digitalWrite(7, LOW); 
 delay(200);
 digitalWrite(7, HIGH); }

if (incomingByte == '3') {

 digitalWrite(8, LOW); 
 delay(200);
 digitalWrite(8, HIGH);}

if (incomingByte == '4') {

 digitalWrite(9, LOW); 
 delay(200);
 digitalWrite(9, HIGH);  }
}

First, make sure you read Nick Gammon's two posts at the top of this Forum. It will help us help you. Second, use Ctrl-T in the IDE to reformat your code in a standard manner. Third, why not simplify your code a bit and save a few bytes in the process:

#define FIRSTPIN  5  // This is your first output pin minus 1

void setup() {

  Serial.begin(9600);
/*                               EDIT:  Save a few more bytes...
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);

  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);

  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);

  pinMode(9, OUTPUT);
  digitalWrite(9, HIGH);
*/
  for (int i = 6; i < 10; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
}

void loop() {
  int incomingByte;

  if (Serial.available() > 0) {

    incomingByte = Serial.read() - '0';                               // Change ASCII character to an int

    if (incomingByte > FIRSTPIN && incomingByte < (FIRSTPIN + 5)) {   // Valid pin number?
      digitalWrite(FIRSTPIN + incomingByte, LOW);
      delay(200);
      digitalWrite(FIRSTPIN + incomingByte, HIGH);
    }
  }
}

It's a java error, it is someything with an error in your computer, not the arduino.

You also seem to be missing } at the end

econjack
1- make sure you read Nick Gammon's two posts at the top of this Forum
I read Nick Gammon's two posts, the main idea was about searching before posting and this is what I did. Anw if I did something wrong, I am sorry :slight_smile:
2- why not simplify your code a bit and save a few bytes in the process
I did the change on my code, your code is great, thank you

michinyon
It's a java error; it is something with an error in your computer, not the arduino.
I did the test on 3 pc and the error persist

I have a feeling that this error is generated by a coil failure, I’ll post my question on the Motors, Mechanics, and Power, maybe someone have an idea

Thanks for all of you