Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: steam229 on Sep 05, 2015, 03:15 pm

Title: Slow Serial Port connection with processing
Post by: steam229 on Sep 05, 2015, 03:15 pm
Suddenly my Serial port connection got very slow, but only when dealing with processing. No problems on arduino console. The delay is huge and sometimes no communication happens even though it is established. This happened after two things that I think are not related: 1. Installed PlatformIO (PlatformIO.org); 2. Installed Processing3 (already tried the same example scripts over and over on different versions);

I'm running it on MacOsX.

Can you help me?

Thanks in Advance,

André
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 05, 2015, 03:26 pm
A specific example on both ends would allow one of us to reproduce, or not, your problem.

What is Processing3?

Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 06, 2015, 08:13 pm
I'm using the common examples that come along with processing and arduino ide.
Processing3 is the newest version.
In any of the examples it happens.
since the problem happens on base code examples that where working a month ago I can extrapolate that it is not a code problem.
Besides that, the delay doesn't happen on terminal view of the serial port.
That led me to conclude that it might be any conflict that derives from any recent installation or process currently running for which I need your guidance, opinions or suggestions to debug

thanks.
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 06, 2015, 08:47 pm
Quote
Processing3 is the newest version.
Noitisnot.

Spaces matter.

I'd try uninstalling PlatformIO.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 06, 2015, 09:27 pm
it does matter, you're totally right,
Thanks

Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 07, 2015, 04:00 pm

Removed PlatformIO and nothing new.

Serial communication works perfectly with all the code examples from arduino and processing for SerialIO on arduino console and work really bad with loads of delay directly with processing on all versions.

Any idea of debug method?


Thanks again!
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 07, 2015, 04:26 pm
Quote
Serial communication works perfectly with all the code examples from arduino and processing for SerialIO on arduino console and work really bad with loads of delay directly with processing on all versions.
Did I misunderstand something there? It seems to me that you said that serial communication between Processing and Arduino is both fast and slow.

I have two versions of Processing, and several different Arduinos, including a Mega and a Due. If you show a SPECIFIC example, and explain what I misunderstood from your (obviously frustrated) statement above, I'd be happy to try to reproduce the problem.

So far, all we've gotten though is a bunch of hand waving.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 07, 2015, 04:56 pm
Thank you very much:

there is no need for a code example since it is not a code problem on any of the sides.

arduino leonardo<-> arduino ide console (works perfectly on both directions)
arduino leonardo <-> processing 3 (huge, almost one minute delay)

the code can be on both cases the arduino and processing code that you load from any of the serialIO native examples.
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 07, 2015, 05:04 pm
Quote
there is no need for a code example since it is not a code problem on any of the sides.
Oh, good. I thought you said that you had a problem. I'm glad that you don't. On the other hand, I'm not all that thrilled that you've been jacking us around.

Quote
arduino leonardo <-> processing 3 (huge, almost one minute delay)
But, since you don't have a problem, I'm forced to assume that the delay is acceptable.

Moving on...
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 07, 2015, 05:50 pm
it is not a programming issue but it is still an issue.
I need help troubleshooting whatever is happening on my computer to cause this.

sorry I just noticed now that I'm on the wrong section... I'm very very very sorry!
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 07, 2015, 09:44 pm
Should I move this topic to the troubleshooting section?
If so, How is that done?

thanks
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 07, 2015, 11:18 pm
Quote
If so, How is that done?
Click any Report to Moderator link, and in the reason field, ask that it be moved. You probably can't report your own posts, but you can click the link in any reply that is not yours.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 12:00 am
Help!
today I tried the communication with a different installation of processing 3 on Raspbian and delay happened again.
Can anyone help me debug this?

mac OsX or Raspbian + Processing 3 + any SerialIO library example code on both arduino and processing = Huge delay on communication

the same doesn't happen if debugging arduino on arduino ide terminal window.




Thanks!
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 09, 2015, 12:22 am
Quote
Can anyone help me debug this?
No. You know what you need to do if you want help.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 12:36 am
Here are the Processing 3 examples, with which it doesn't work on both directions:


Simple Write (arduino side)


// Wiring/Arduino code:
 // Read data from the serial and turn ON or OFF a light depending on the value
 
 char val; // Data received from the serial port
 int ledPin = 4; // Set the pin to digital I/O 4
 
 void setup() {
 pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
 Serial.begin(9600); // Start serial communication at 9600 bps
 }
 
 void loop() {
 if (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }
 if (val == 'H') { // If H was received
 digitalWrite(ledPin, HIGH); // turn the LED on
 } else {
 digitalWrite(ledPin, LOW); // Otherwise turn it OFF
 }
 delay(100); // Wait 100 milliseconds for next reading
 }


Simple Write (Processing 3 side)


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup()
{
  size(200, 200);
  // 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);
}

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(204);                    // change color and
    myPort.write('H');              // send an H to indicate mouse is over square
  }
  else {                        // If mouse is not over square,
    fill(0);                      // change color and
    myPort.write('L');              // send an L otherwise
  }
  rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

Simple Read (Arduino Side)

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}


Simple Read (Processing Side)

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  // 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);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}

Has you can see this are Base well written examples, that per se don't cause delay.
What are the options in this cases, what else might go wrong that motivates this specific problem?

Thanks a lot
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 09, 2015, 01:58 am
The first example has problems. It send a new value each time through draw(), depending on whether or not the mouse is in the square, or not.

Try this code in Processing, instead:
Code: [Select]
import processing.serial.*;

Serial myPort;            // the serial port
int incomingValue =0;     // the value received in the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());

  // Open the appropriate serial port. On my computer, the RF
  // receiver is connected to a USB-to-serial adaptor connected to
  // the first port in the list. It may be on a different port on
  // your machine:

  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw()
{
  myPort.write('H');
//  println("Turn led on");
  delay(10);
  myPort.write('L');
//  println("Turn led off");
  delay(10);
}

When I run this code, the LED turns on and off as I expect.

What does it do for you?
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 11:38 am
it blinks as you expect, but it keeps blinking after for a few more seconds.
anyway, it starts blinking at the same time the program starts.

I used the native example before Processing 3, and I never got any lag problems.
and it worked.

To
(...) send a new value each time through draw(), depending on whether or not the mouse is in the square, or not.


is the idea I want to start from, for example.

Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 09, 2015, 11:46 am
Quote
is the idea I want to start from, for example.
Why? Sending a value ONCE when crossing into, or out of, the box makes sense. Continually spamming "I'm in the box" or "I'm out of the box" does not.

Anyway, the point is to define whether the serial data transfer is really slow, or if the spamming is the problem.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 11:59 am
Also delay() is no longer a processing function (since Processing 2) so even if you "delay(100);" you get the same nervous blink.

and the post lag to turn off is really really strange;

Like I  told you, I think there is need to debug whatever changed on the serialIO relation between Arduino and Processing and not with a specific piece of code.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 12:10 pm
Now it's working and no lag!! can't understand this:

Code: [Select]
import processing.serial.*;

Serial myPort;            // the serial port
int incomingValue =0;     // the value received in the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());

  // Open the appropriate serial port. On my computer, the RF
  // receiver is connected to a USB-to-serial adaptor connected to
  // the first port in the list. It may be on a different port on
  // your machine:

  myPort = new Serial(this, Serial.list()[7], 9600);
}

void draw()
{
  myPort.write('H');
//  println("Turn led on");
  delay(500);
  myPort.write('L');
//  println("Turn led off");
  delay(500);
}

void delay(int delay)
{
  int time = millis();
  while(millis() - time <= delay);
}
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 09, 2015, 12:17 pm
Quote
can't understand this:
At least you have a building block.

delay() being removed was stupid.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 12:28 pm
indeed
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 12:28 pm
Now I tried the opposite way with

Code: [Select]
int sensorPin = A0;
int ledPin = 13;

void setup() {
  pinMode (ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  if (analogRead(sensorPin) > 500) {
    digitalWrite (ledPin, HIGH);
    Serial.println("1");
  } else {
    digitalWrite (ledPin, LOW);
    Serial.println("0");
  }
  delay(100);
}


on arduino

and

Code: [Select]
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  // 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()[7];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();
    println(val);// read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}


on processing


also on arduino side I tried different write to serial port methods:

serial.Write("int");
/
serial.println(int);
/
serial.println("int");

and all gave me different results with the println on the processing side:

for example, the last one:

"
10
48
13
10
49
13
10
49
13
10
49
13
10
49
13
10
49
13
10
48
13
10
48
13
10
48
13
10
48
13
10
49
13
10
49
13
10
49
13
10
"

thanks again!
Title: Re: Slow Serial Port connection with processing
Post by: PaulS on Sep 09, 2015, 12:38 pm
The 13 and 10 are the carriage return and line feed that the ln part of the name is responsible for adding.

The 48 and 49 ('0' and '1') look are like Serial.println(int); was used.
Title: Re: Slow Serial Port connection with processing
Post by: steam229 on Sep 09, 2015, 01:02 pm
Thank you very much for your help:
now i got things to work, it a great sequence of misunderstandings by me:

1. the serial write example on processing is, like you very well detected, wrong;
2. the physical pixel example on arduino ide works perfectly;
3. The Graph example on Arduino ide has something wrong on the processing side.

in the end, through a sequence that I can't figure, it led me to wrong conclusions that without your help were impossible to detect...

Thanks a lot,