Serial communication with Max


#1

Sooooo,

I am having a Max program send some data ( a frequency ) to an Arduino. The data is getting through to the arduino but I realized that the Max [Serial] object only really sends one of data. I am wondering how I can send 1. a number larger than 256, and 2. ideally a float. I guess its a formatting problem.
I have been looking into converting it into hex, with [sxformat] but so far no dice.

thanks everyone.


Max Arduino communication
Max Arduino communication
#2

Got any code to share? Also, can you move this to the (newly created) max/msp/jitter sub tools category?


#3

right, code can be found here for the arduino sketch-----

I simplified the Max patch because there was a lot going on in so I just pasted the Serial send part here.

Thanks


----------begin_max5_patcher----------
1683.3oc0ZssiaaCD840eEDB8gV.GCRp6EKJPKJR.5y4khjf.ZIZalJQ5JQ4
raBx+d4EIeakjk850U69fsLEE8YNyLGNC898I24LW7.szA7qfO.t6tuO4t6L
CoG3t5OemSN4gjLRoYZNb5WEy+hyT6sjzGjlgYRAoYv0DYxJFe4mKnIR6Zi7
mAmBbCcm4G5CCcmB7f5Qv3YPvmpeNVpYoTK+ab8ZVrR4iYTy3MivqxY7Lpzf
G2cCJpjMihpG0Nj7w0TKLbXbEf9j9l+XxD8KSGnYmHxyopG9X69upJk.BHiI
UeSfRNa8ZlbF.79UT.KkR.rRfT.REfRQNE7129d.gSxdrTMNgmBJopWjpIun
hmRzeEjLvhB5+VQ4IOBDanE5mWOibVRg3MIBtrPjkoF+qqXIqzeAoErMJ9Vg
ibgTTz.xLFmlHp3FjFzsywEa7EtPO8aH+H8aQgc3YbGnmA0pmAdE496uGjrh
vWRMzyFRVEUyUZJsUJ.2ME36gzFcrqkAr7gaGL.ZLw.4rkqj.Nklps883iO5
j7QGvJZAUEN9yLYoYz0hB4u..5OZmffCxeDn9BVWIUwT1kaNEjxVrP8rbodF
OJpJJakS85ImGEaRwCBsjpI5J.1NohiGMj5bEEp3JahqM+T.HEoULt.7aM+c
tQXn3.CM3FLfPLb3MmMTqvbZQOR3dQd6RQ76U8Nd2xTnzzTwUelxIysFB74X
Y3tD1clZcbWW086u+nfApRzkHApGFnXFFciJsaQgH2jWct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KcsPoEUq2D6uqvYu.yg4sut3UD8iMuKd.3A
e6DN0mO5HRoBCGYtqAxN2nc4Fx1J3aG6fFjTU3sEO3QDdFY6zgviL7fFY3AN
js57FW3Q+S6biTmQCRcN3B4GaSIj0q2PKJqWSCTTsj8EQg9igSmXOQU6GMqn
SAUeH.hceGNjBUiXRUWXUE1FodHx1zsStHkVvqX0mWfhDlT2v0QMBs6.OMc.
9GYUz2T2+uxtWPpxjGRVjjDJWlHxrP6C.3LWTXfWnhLlE36ECw5qh0CgTNLc
0NVieZsOP+ud4w894jq9hXML099ri.36JnT9YiPnFR9t5+zWgBhBvgWev82z
rLwWOazEg8QF5KLBEgzW.8i887qAXKGBh9v2zSBBitVFf47temHK8rgu1SCC
LLbLF4arDDDEGb4te0y7iI+mvbWvy
-----------end_max5_patcher-----------

#4

So if you want to send ASCII encoded floating points from max -> Arduino and back you just have to jump through some hoops that looks like this:

Please see the comments in both programs …

Max MSP program above …

----------begin_max5_patcher----------
1649.3oc0Zs0jZaCE9Y1eEp9k.SoDK4qzLamIISalLsus8kNgLYDXAnDaIFa
4cWRl8+dORxlkkBrFvPR2GrwBoiNmuymNWL62tpiyX48rBGzuh9.pSmucUmN
lgzCzo54NNYz6mjRKLSyYhLKiITN8semhcuxL9qySJ4BI52mLWhdqLgMndJh
xLYoJkoLBvsZzB0xTlYk0yaAUMYNWL6S4rIJqJEDfG31GgiMWs2HtCbQerZM
7DiHji+7u36s1FxE06GtV54rBPuoJtTr1N366pEpWzP8M2pKnOpWzCWck9R+
SDYtQkCVEZb4zor7WMRLRLMURUnrk+g9tYjak7DTASUtnaOz2FIPnW9RzMJZ
tBIKyQ2vx4zTTB6V9D1.8WaGYvX1Ltn6vPW2dfbdXknRkxUR5t47TFpa0Jn2
R4ozwoL3q+MjaO8LLSCglLmli3h2rTwPWWuC4LZR2dFkTOG9TT2po7SWidwn
QhWzy9MUBAUYnne95JY8J6W7f8FKsfswBp.BXOsKcfRZFvrsOQlWiF4LxYkx
rBFV.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7yW6.lbQpcPImMCV8kIiI1EW2ZyFM3r9K886YMS
fnyksQfwFVFI1yz4m8UCryDBm+bg1e+NyqC7jyGp4BMKcX0aBLZ+A7d13clU
YZwaiepXyFqG+oPRgrLeRs1V2aB5wMOgU.MWZx0r1j7exblySRXh06aLgWnI
jI6lQ1X0InApit2h8pOY7DS2wUXhm+ff90c8u1COtMsm960D8O3xgmjlnOdW
N8AnzH7yoOjKq939ii9zH0Ae4TmlDc3BpNM4z0F57YUe7ah9b4Ncgaxocr+g
E8DCYnCL4ogqq8v4H54OTrsl3bO1HC171Pyn2xxKpDoQQfhV9rLW+n4+AFvY
HrOZjnSN6Vd87M0i6PygBLTP0Ek41hStOzVFoSlLgkKJ4U0L.PvUUEwrQsBB
nrx0pQ5Mokr5RO.ydJsLU8TnhNYBzQ5DYpU09.z4oGNJzOR2CZXf+PWh9SC0
CgARyi8k1uxCjCqey5obxfMhWiTq6w1PAeWNiINXMzzfbfm9O8mvgwgjn1W4
9GVZp7t8qcSkvFv+ZEYCCU+ELz0MdEOZKZeLI.af2nXbL1zqevv.+fV0.L+V
BuSllbvfq1S6FZP3gDbfQSgVdFFd7teXMOb0+Bz3ijF.
-----------end_max5_patcher-----------

Arduino Echo Program

String buffer;

float myFloat;

void setup() {
  // Start our Serial device.
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0)
  {
    char inByte = Serial.read(); // Read the incoming byte.

    if (inByte != '\n') // If the byte is NOT an ASCII 10 \n character, add it to the buffer.
    {
      buffer += inByte;
    }
    else // ... otherwise, if it is an ASCII 10 \n character, the buffer is full so decode it.
    {
      myFloat = buffer.toFloat(); // Convert the ASCII string to a floating point number.
      buffer = ""; // Clear the buffer.

      Serial.println(myFloat);  // Send the number back as ASCII encoded along with an ASCII 10 \n character.
    }
  }
}

#5

By the way, you can use the same technique to send a larger integer. You’d just change the .toFloat() to toInt(), etc in the Arduino. Since it’s all sent as ASCII text, it just a matter of how you interpret the text buffer.


#6

Thanks!

This is working super perfectly


#7

Wow. Super perfectly! That is what we all dream of :smile: