serialEvent (Serial myPort)

Bonjour

Pour tracer une courbe sinusoïdale sous Processing, avec les valeurs calculées par Arduino, il est facile de trouver des exemples sur internet. Par exemple celui de la page: http://www.iut-troyes.univ-reims.fr/wikigeii/index.php/Cours:ArduinoProcessing

[u]Code Arduino:[/u]

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

void loop() { static float angle=0.0; float sinus; int i; sinus = (unsigned char) 127.0+127.0*sin(angle); Serial.println(sinus); angle += 0.1; delay(50); }

[u]Code Processing:[/u]

import processing.serial.*;

Serial myPort; int xPos = 1; int lastxPos=1; int lastheight=0;

void setup () { size(600, 400);

println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 115200); myPort.bufferUntil('\n'); background(0); } void draw () {}

void serialEvent (Serial myPort) { String inString = myPort.readStringUntil('\n'); if (inString != null) { inString = trim(inString); float inByte = float(inString); inByte = map(inByte, 0, 1023, 0, height); stroke(127,34,255); strokeWeight(4); line(lastxPos, lastheight, xPos, height - inByte); lastxPos= xPos; lastheight= int(height-inByte);

if (xPos >= width) { xPos = 0; lastxPos= 0; background(0); } else { xPos++; } } }

A la compilation, la fenêtre s'ouvre, de la bonne taille et de la bonne couleur, mais la sinusoïde ne se dessine pas. J'ai bien choisi le bon numéro de port. Par exemple, si après la ligne line(lastxPos, lastheight, xPos, height - inByte); je rajoute println(xPos); je vois bien dans la console série de Processing les valeurs de xPos s'afficher. Cela confirme que mon problème ne vient pas de la liaison série. J'ai alors décidé de supprimer l'appel de la fonction void serialEvent (Serial myPort) et d'intégrer son contenu directement dans la fonction void draw(). Et là ça fonctionne correctement. Je ne comprends pas pourquoi la courbe ne se dessine pas si elle codée dans la fonction serailEvent(), puisque celle ci est bien appelée à chaque fois qu'une donnée est disponible sur le port série, mon test dans la console série de Processing, ayant permis de le vérifier.

Merci d'avance pour votre aide en français si possible, en anglais sinon.

[u]Traduction par Google (without code lines):[/u]

To draw a sinusoidal curve in Processing with the values calculated by Arduino, it is easy to find examples on the internet. Such as the page:

A compilation, the window opens, right size and the right color, but the sinusoid is not drawing. I have chosen the right port number. For example, if after line line (lastxPos, lastheight, xPos, height - inByte); I add println (xPos); I see the console serial Processing xPos the values appear. This confirms my problem does not come from the serial link. I decided to remove the function call void serialEvent (Serial myPort) and integrate the content directly in the function void draw (). And there it is working properly. I do not understand why the curve does not draw if it is encoded in the serailEvent () function, since it is called correctly every time data is available on the serial port, my test in the series of console Processing having allowed the check.

Thank you in advance for your help in French if possible, otherwise in English.

Make up your mind what language you want help in.

  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);

Anonymous printing sucks. Don't do that.

What is actually printed by the println() function? Do you really know?

Is the Arduino actually connected to the first port in the list?

This confirms my problem does not come from the serial link.

What does this mean? It implies that you are getting good data from the serial PORT. Are you really?

I decided to remove the function call void serialEvent

You never call the function, so the statement is patently false. What you might have done is remove the function, which made it impossible for Processing to call it.

But that is NOT the code you posted...

Thank you in advance for your help in French if possible

If you wanted answers in French, you should have posted in the French section.

 sinus = (unsigned char) 127.0+127.0*sin(angle);

Why are you casting 127.0 to unsigned char? That makes no sense.

 angle += 0.1;

Incrementing by 1/10th of a radian doesn't, either.

Your fundamental problem is that you are trying to draw in the serialEven() function, which should ONLY handle getting serial data and doing any necessary parsing. Drawing should be done in the draw() function.