Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial write from Processing to Arduino sending broken data

I'm new to Processing and serial communication and my problem seems very elementary. I'm trying to send data over from Processing to an Arduino but it seems that something gets lost in translation.

On Arduino I'm running this super simple sketch:

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

void loop()
{
  if (Serial.available() > 0) Serial.println(Serial.read());
}

The intention there is to read a byte from serial and then write it right back so I can see what is going on. Testing this with the included serial monitor behaves as I'd expect: typing in "0" returns "48". So far so good.

Things start to go wrong when I run this Processing sketch:

import processing.serial.*;
Serial myPort;

void setup()
{
  //frameRate(10);
  myPort = new Serial(this, Serial.list()[4], 9600);
}

void draw()
{
  myPort.write("0");
}

I woud expect this code to return an endless stream of "48" with the rate of ten entries per second, since I understand that is the default frame rate. What really happens is something like this:

48
48
488
48
48
48
48
48
48
48
48
48
48

488
48
48
48

It seems like every 10th (give or take a few) byte has a good change of being messed up. Instead of "48" I get back stuff like " ", "488", "4848" or "488". What's even more interesting is that if I uncomment the frameRate(10); line in my Processing script I would expect absolutely nothing to happen, since I'm setting the fps from ten to ten. Instead I start to see stuff like this:

4
44
4848844
444448444844
4
44444444
844
444
844444
8
88
8
4488
84
48
4448444844
444

So basically the numbers make so sense anymore.

It took me quite some time to narrow the problem down to this serial communication and a few hours of Googling around related topics has given me no hints about what might be going on. Any pointers toward further reading or things to try would be greatly appreciated.

I'm using the latest version of Processing downloaded today and my system is a MBP running Mountain Lion with all updates installed.

like image 335
Dasinf Avatar asked Nov 12 '22 17:11

Dasinf


1 Answers

After some further testing, it seems that having the serial monitor on while sending bytes from Processing messes both up for a yet unknown reason. I assume there is some sort of fighting over serial bus priority and the data ends up broken.

Solution: don't try to use multiple programs to read serial data simultaneously.

like image 76
Dasinf Avatar answered Dec 26 '22 14:12

Dasinf