Problem trying to send data from Max/MSP to Arduino with Serial.read

Hi there,

I am trying to send 3 pieces of information from Max/MSP using the serial object into Arduino using Serial.read. At the moment I am sending from Max just the numbers 1, 2, and 3 in a single message.

The 3 bytes appear to send from Max (pop-up says write 3) but then serial.read (of which I have 3) seems to be returning -1 for 2 out of every 3 readings as follows.

Reading 1 = 1
Reading 2 = -1
Reading 3 = -1
Reading 1 = 2
Reading 2 = -1
Reading 3 = -1
Reading 1 = 3
Reading 2 = -1
Reading 3 = -1

Here is a copy of the Arduino code

int settings[3];

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
  // put your main code here, to run repeatedly:

settings [0] = Serial.read();
settings [1] = Serial.read();
settings [2] = Serial.read();

  Serial.print("Reading 1 = ");
  Serial.println(settings [0]);
  Serial.print("Reading 2 = ");
  Serial.println(settings [1]);
  Serial.print("Reading 3 = ");
  Serial.println(settings [2]);     

  }
}

And of the Max patch

{
 "boxes" : [ {
 "box" : {
 "maxclass" : "message",
 "text" : "1 2 3",
 "numoutlets" : 1,
 "patching_rect" : [ 194.0, 78.0, 37.0, 22.0 ],
 "id" : "obj-26",
 "outlettype" : [ "" ],
 "style" : "",
 "numinlets" : 2
 }

 }
, {
 "box" : {
 "maxclass" : "message",
 "text" : "print",
 "numoutlets" : 1,
 "patching_rect" : [ 115.0, 115.0, 34.0, 22.0 ],
 "id" : "obj-10",
 "outlettype" : [ "" ],
 "style" : "",
 "numinlets" : 2
 }

 }
, {
 "box" : {
 "maxclass" : "newobj",
 "text" : "print status @popup 1",
 "numoutlets" : 0,
 "patching_rect" : [ 254.0, 218.0, 138.0, 23.0 ],
 "id" : "obj-28",
 "fontname" : "Arial",
 "style" : "",
 "fontsize" : 13.0,
 "numinlets" : 1
 }

 }
, {
 "box" : {
 "maxclass" : "newobj",
 "text" : "serial d 9600",
 "numoutlets" : 2,
 "patching_rect" : [ 194.0, 144.0, 79.0, 22.0 ],
 "id" : "obj-1",
 "outlettype" : [ "int", "" ],
 "style" : "",
 "numinlets" : 1
 }

 }
 ],
 "lines" : [ {
 "patchline" : {
 "source" : [ "obj-26", 0 ],
 "destination" : [ "obj-1", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }

 }
, {
 "patchline" : {
 "source" : [ "obj-10", 0 ],
 "destination" : [ "obj-1", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }

 }
, {
 "patchline" : {
 "source" : [ "obj-1", 1 ],
 "destination" : [ "obj-28", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }

 }
 ],
 "appversion" : {
 "major" : 7,
 "minor" : 3,
 "revision" : 3,
 "architecture" : "x86",
 "modernui" : 1
 }

}

Can anybody help me to get the readings in order?

Thanks in advance!

Asha

Serial.read( ) reads one byte.

Hi ieee488

Serial.read( ) reads one byte.

I am sending 3 bytes from Max and I have 3 Serial.read( ).

I can see that the numbers are being received but the Serial.read is returning a -1 for the 2nd and 3rd Serial.read.

So when I send my message in Max, the 3 bytes are sent but the above 9 readings are printed:

Reading 1 = 1
Reading 2 = -1
Reading 3 = -1
Reading 1 = 2
Reading 2 = -1
Reading 3 = -1
Reading 1 = 3
Reading 2 = -1
Reading 3 = -1

So the 3 numbers I want are there but they are all on reading 1.

If that makes sense. I am probably missing something super noob here though :o

Look at your code again.

ieee488:
Look at your code again.

I have been staring at it for a number of hours hence me coming to the forum XD

What if there is only byte available to be read? ? ?

it works fine if there is only one byte sent from max, as in one number, but if I add more numbers then the issue occurs.

I have been trying to follow this video as this guy appears to do exactly what I am trying to do with seemingly no issue.

ieee488:
What if there is only byte available to be read? ? ?

I see what you are saying now in that although max says its writing 3, its actually writing the 3 values into 1 byte and like you said Serial.read is then reading that one byte and the other two reads are returning nothing.

So I am assuming the vid I linked has something else going on.

I now have the task of splitting the 3 pieces of information back out of the string. I know this is an often asked question so I will go and have a look on the forums.

I have it working! The simplest form is below, I added >=3 after Serial.available and this seems to work nicely.

Max patch

{
	"boxes" : [ 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "10 0 30",
				"patching_rect" : [ 194.0, 78.0, 51.0, 22.0 ],
				"numinlets" : 2,
				"id" : "obj-26",
				"numoutlets" : 1,
				"style" : "",
				"outlettype" : [ "" ]
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "print",
				"patching_rect" : [ 115.0, 115.0, 34.0, 22.0 ],
				"numinlets" : 2,
				"id" : "obj-10",
				"numoutlets" : 1,
				"style" : "",
				"outlettype" : [ "" ]
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "print status @popup 1",
				"patching_rect" : [ 254.0, 218.0, 138.0, 23.0 ],
				"numinlets" : 1,
				"id" : "obj-28",
				"fontname" : "Arial",
				"numoutlets" : 0,
				"style" : "",
				"fontsize" : 13.0
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "serial a 9600",
				"patching_rect" : [ 194.0, 144.0, 79.0, 22.0 ],
				"numinlets" : 1,
				"id" : "obj-1",
				"numoutlets" : 2,
				"style" : "",
				"outlettype" : [ "int", "" ]
			}

		}
 ],
	"lines" : [ 		{
			"patchline" : 			{
				"source" : [ "obj-26", 0 ],
				"destination" : [ "obj-1", 0 ],
				"hidden" : 0,
				"disabled" : 0
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-10", 0 ],
				"destination" : [ "obj-1", 0 ],
				"hidden" : 0,
				"disabled" : 0
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-1", 1 ],
				"destination" : [ "obj-28", 0 ],
				"hidden" : 0,
				"disabled" : 0
			}

		}
 ],
	"appversion" : 	{
		"major" : 7,
		"minor" : 3,
		"revision" : 3,
		"architecture" : "x86",
		"modernui" : 1
	}

}

And Arduino code

int settings[3] = {0, 0, 0};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  if (Serial.available() >= 3) 
  {
  // put your main code here, to run repeatedly:

settings [0] = Serial.read() ;
settings [1] = Serial.read() ;
settings [2] = Serial.read() ;

  Serial.print("Reading 1 = ");
  Serial.println(settings [0]);
  Serial.print("Reading 2 = ");
  Serial.println(settings [1]);
  Serial.print("Reading 3 = ");
  Serial.println(settings [2]);     

  }
}
I am sending 3 bytes from Max and I have 3 Serial.read( ).

So, why are you then storing the bytes in ints?

PaulS:

I am sending 3 bytes from Max and I have 3 Serial.read( ).

So, why are you then storing the bytes in ints?

I think we tend to learn by example, and as so much of the Arduino example material out there uses such from the days of simple blinky sketches, then 3 bytes of wasted memory as in the above case was hardly a concern.
Same as I get irritated by the default.. Serial.begin(9600) when most modern peripherals will run at 115200 quite happily.
You still hardly ever see uint8_t or int32_t and the other variants that I much prefer using myself.
Just me $0.02 :slight_smile:

PaulS:

I am sending 3 bytes from Max and I have 3 Serial.read( ).

So, why are you then storing the bytes in ints?

I have changed the ints to bytes :wink:

indev2:
I think we tend to learn by example, and as so much of the Arduino example material out there uses such from the days of simple blinky sketches, then 3 bytes of wasted memory as in the above case was hardly a concern.

I am a copier not a coder :frowning: