# Error with Serial Communication using the call response method

**URL:** <https://discourse.processing.org/t/error-with-serial-communication-using-the-call-response-method/15432>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 12, 2019, 8:37pm UTC](https://discourse.processing.org/t/error-with-serial-communication-using-the-call-response-method/15432 "2019-11-12T20:37:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaltyApe](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@SaltyApe](https://discourse.processing.org/u/SaltyApe)\
**Post date:** [November 12, 2019, 8:37pm UTC](https://discourse.processing.org/t/error-with-serial-communication-using-the-call-response-method/15432/1 "2019-11-12T20:37:19Z")

</div>

Hello! I am a newbie in Arduino and Processing and I am having some issues with receiving data using serial call response. Processing outputs Error Disabling SerialEvent() for COM6 null and does not print the data i send from the Arduino. The code I use is given below:  
Arduino Code:

```auto
int firstSensor = 10; // first analog sensor
int secondSensor = 250; // second analog sensor
int thirdSensor = 255; // digital sensor
float keks=4.23;
int kek100=0;
int mul=0;
int rem=0;
int inByte = 0; // incoming serial byte

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(2, INPUT); // digital sensor is on digital pin 2
  establishContact(); // send a byte to establish contact until receiver responds
}

void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    Serial.write(firstSensor);
    Serial.write(secondSensor);
    Serial.write(thirdSensor);
    delay(10);
   kek100=keks*100;
    mul=ceil(kek100/255);
    rem=((kek100/255)-mul);
    Serial.write(rem);
    Serial.write(mul);
    Serial.println();
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A'); // send a capital A
    delay(300);
  }
}

```

Processing Code:

```auto
import processing.serial.*;

int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
int mul,rem;
float ph;
void setup() {
  size(256, 256); // Stage size
  noStroke(); // No border on the next thing drawn

  // Set the starting position of the ball (middle of the stage)
  xpos = width/2;
  ypos = height/2;

  // Print a list of the serial ports, for debugging purposes:
  printArray(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 // myPort.bufferUntil('B');
 //myPort.bufferUntil('\n');
}

void draw() {
  background(bgcolor);
  fill(fgcolor);
  // Draw the shape
  ellipse(xpos, ypos, 20, 20);
}

void serialEvent(Serial myPort) {
  
  
  
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller. 
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') { 
      myPort.clear(); // clear the serial port buffer
      firstContact = true; // you've had first contact from the microcontroller
      myPort.write('A'); // ask for more
    } 
  } 
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;
    // If we have 3 bytes:
    if (serialCount > 5 ) {
      xpos = serialInArray[0];
      ypos = serialInArray[1];
      fgcolor = serialInArray[2];
   // delay(100);
      rem=serialInArray[3];
      mul=serialInArray[4];
      ph=((4*mul)+rem)/100;

      // print the values (for debugging purposes only):
      println(xpos + "\t" + ypos + "\t" + fgcolor + "\t" + ph);

      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

```

Please don’t pay attention to sensors 1 to 3, they are just for testing right now.

---

<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:** [November 13, 2019, 2:29am UTC](https://discourse.processing.org/t/error-with-serial-communication-using-the-call-response-method/15432/2 "2019-11-13T02:29:47Z")

</div>

if you new to this you might start with a more easy way,

-a- just send from Arduino continuously ( and start with one data sample / sec ` delay(1000);` )  
( if you in processing only need 1 data set per minute you can just use whenever you want  
or do some averaging… meanwhile )

-b- send one line in ASCII what is readable ( and testable from the arduino IDE monitor )  
like CSV style for 3 sensors on Ain:

```auto
0,1023,1023,

```

any formatting / convert 0 … 5V / 0 … 1023 / to  
`engineering units 90 .. 1200 [pound **2/inch** 3]`  
can do at will on processing side for display,  
not inside arduino, data transport , data storage by array or database file  
should stay with int at resolution as long as possible.

* * *

Arduino example:

```auto
**int dt = 1000; // msec between sending

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print( analogRead(0) ); // A0
  Serial.print(",");
  Serial.print( analogRead(1) ); // A1
  Serial.print(",");
  Serial.print( analogRead(2) ); // A2
  Serial.println(",");
  delay(dt); // sample rate
}

/*
// with A0 jumper to 5V see like
  1023,885,788,
  1023,884,786,
  1023,884,786,
  1023,883,786,
  1023,885,788,
*/

```

Processing example:

```auto
// rev 0.1 add a list of records
// rev 0.2 add a plot from string records array
// rev 0.2b add check on 2 vals in split array
// add a scrol in list if longer as 18 HOLD
// rev 0.3 add A03

import processing.serial.*;

Serial myPort; 
String data; // latest arduino line
int[] datos; // as array of integers
StringList list = new StringList(); // stored lines
int listlong = 18; // textlines only

int w= 640, h = 360; // canvas settings

// rev plot
int a=120, d = 5; // scope background area rectangle
int[] datos1, datos2; // as array of integers to make lines
float range = 0.3, basex = 130, basey = h-30; // scope area
float step = ( w-basex-d )/ ( listlong-1 ); // line step calc of array length

boolean diag = true; // print diagnostic info

void setup_serial() { // USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[1]; // adjust 0.. x port
  myPort = new Serial(this, portName, 115200);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) { // handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    if (diag) println(data); // print every GOOD line
    datos = int( split(data, ",") ); // create int array from line to check valid vals
    if ( datos.length >= 2 ) list.append( data ); // good, min 2 vals, store line at String list
    if ( list.size() >= listlong ) list.remove(0); // erase the oldest
  }
}

void settings() {
  size(w, h);
}

void setup() {
  setup_serial();
}

void draw() {
  background(0, 0, 80);
  for ( int i = 0; i < list.size(); i++ ) text( list.get(i), 10, 20+i*20 ); // running list of arduino lines
  plot();
}

void plot() {
  push();
  fill(100);
  rect(a, d, width-a-d, height-2*d);
  for ( int i = 0; i < list.size()-1; i++ ) {
    datos1 = int( split(list.get(i), ",") );
    datos2 = int( split(list.get(i+1), ",") ); 

    stroke(0, 200, 0);
    line(basex+i*step, basey-range*datos1[0], basex+(1+i)*step, basey-range*datos2[0]); // line A0
    stroke(200, 200, 0);
    line(basex+i*step, basey-range*datos1[1], basex+(1+i)*step, basey-range*datos2[1]); // line A1
    stroke(0, 200, 200);
    line(basex+i*step, basey-range*datos1[2], basex+(1+i)*step, basey-range*datos2[2]); // line A2
  }
  pop();
}

/*
 [0] "COM1"
 [1] "COM7"
 try connect to COM7
 1023,880,778,
 1023,880,779,
 */

```

 ![arduinoCSV_3lineplot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1007f5631b316133f26f667bd75d79d2e413658f.png)
