Mac OS X pointers problem

Hi,

I think i just found a problem while using arduino in Mac OS X.

Here is the code:

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

void loop() {
  char *buff = (char *)malloc(3 * sizeof(char));
  buff[0] = 'a';
  buff[1] = 'd';
  buff[2] = 0;
  Serial.print(buff);
  free(buff);
}

The problem with this is that when I read the from the serial line it doesn't always outputs "adadadadad"... continuously, instead sometimes some strange characters appear, like "adad? adadadàadad"...

The same happens using this code

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

void loop() {
  char *buff = "ad";
  Serial.print(buff);
}

But when i write something without using a char pointer explicitly, it works well, like in the code below:

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

void loop() {
  Serial.print("ciao");
}

I can't figure out where the problem could be... I can just suppose it's a problem with handling pointers from the Serial but I just did those test until now. At first I thought there could be a problem with allocation using malloc but as you can see from the second code i posted (that is not using malloc) i doesn't work as well.

When i plug the arduino in a linux box all the codes above work well though, meaning that the output is always "adadadadadadadadad"... without any strange character.

Any thoughts? :)

When i plug the arduino in a linux box all the codes above work well though, meaning that the output is always "adadadadadadadadad"... without any strange character.

That's really peculiar! It sort of implies that the Mac serial monitor, or the Mac FTDI driver is at fault. Ah, hang on; if you compile and upload a sketch using char pointers to an Arduino board, then unplug the board from the Mac, and plug it into a Linux box, then it'll still be running the Mac-compiled code, yes? If you then run the serial monitor (without re-compiling or re-uploading), what happens?

I'm trying to work out whether the Mac-hosted compiler is generating bad pointer code, or whether the Mac serial monitor or USB FTDI driver is corrupting the data stream.

if you compile and upload a sketch using char pointers to an Arduino board, then unplug the board from the Mac, and plug it into a Linux box, then it'll still be running the Mac-compiled code, yes? If you then run the serial monitor (without re-compiling or re-uploading), what happens?

I decided to perform some tests on linux and OS X.

Those tests were performed with this code:

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

void loop() {
  char *buff = (char *)malloc(3 * sizeof(char));
  buff[0] = 'a';
  buff[1] = 'd';
  buff[2] = 0;
  Serial.print(buff);
  free(buff);
}

Compilation on linux: [u]Reading on mac/u adadada?adadadada

[u]Reading on linux/u: adadadadadadadadadadad

BUT sometimes there are a lot of 0x00 on the beginning of the read session both on linux and on osx.

Then I found a problem with this code: void setup() { Serial.begin(9600); }

void loop() { char *buff = "ad"; Serial.print(buff); }

Compiling on linux the code above makes the output to be always 0xfe 0xfe 0xfe 0xfe 0xfe ... (both reading on linux and on osx). Also Sometimes an "ad" (only one after a minute of readings) is read though.

Compilation on mac: [u]Reading on mac:[/u] adadadadad?adadadadadadadadadada (wrong) I also found that the "?" character that is printed out is 0xff, 0xfd, 0xfa or others. And I also found that sometimes not all the characters are printed out... like "adaadadad" (notice the double "a"). Sometimes the characters are "substituted", like ada?adad. This happens without need to unplug the usb cable. So the error seems to appear only once for each read session. Usually the ? characters are printed out near the begin of the read session.

[u]Reading on linux:[/u] V?adadadadadadadadadadadadad (wrong) ??adadadadadadadadadadadadad (wrong) Where "??" were 0x58 0x55 or 0x29 0xfd or others... This time the output is wrong only the first time I try to read it because when I try to re-read from serial again the output is correct. To get wrong output again I just have to unplug and replug the usb cable and read.

Those tests were performed in linux using picocom and in osx using cu.

hey I can't belive I'm the only one having those problems in linux and osx... I don't understand why no one is replying to this topic :o am I overlooking something? :)

For just Mac, I’d suggest upgrading to Arduino 0014 - the gcc 4.3.0 in 0013 can generate bad interrupt code. I had similar issues, they all went away with gcc 4.3.2.

You mention similar problems in Linux, which is at odds with the above. Could it be that you’re reading old messed up pending input when repeating your tests after unplug/re-plug? It’s the only explanation I can think of.

Oh, and running at 115200 baud on an 8 MHz Arduino - the generated baud rate is probably too inaccurate in that case. More so with a ceramic resonator, and especially when running off the internal RC clock.

-jcw