We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi to all , I'm quite new of Processing and I've a problem with the serial data receiving.
I must receive the below csv string from the serial interface and split character by character into an array ,but all the tentatives to do it have no success . This is the string to be parsed : "$,$,$,z,1,y,2,2,3,0,2,3,4,5,y,y,y,y,y,y,y,5,6,7,8,z,2,y,1,2,1,0,1,2,3,0,y,y,y,y,y,y,y,2,3,2,3,4,4,4,4,4,4,4,4,2,0,1,5,0,8,2,7,1,2,3,3,0"
This is the code:
while (myPort.available() > 0)
{
String inBuffer = myPort.readStringUntil('\n');
if (inBuffer != null)
{println(inBuffer); // till this point all works fine.
String[] to_char = split(inBuffer,',');
print(to_char[1]);
}
}
If instead of print(to_char[1]) i put print(to_char[0]) i see correctly $ otherwise i gote an error that say ArrayIndexOutOfBoundException as just the first element of the array is available.
Could someone give me a good suggestion to solve this problem ? Thank . Roberto
Answers
Really ,any idea ?
http://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
since this in a while loop:
does he do only one loop?
what does he print for inBuffer? the entire line? or only $?
you can't use to_char[1] when it's only 1 element ([0])
maybe use trim() before split
Hi Chrisir, you are right, the problem probably was that I was working inside a loop , while now moving the out into the serialEvent routine an doing some changes It works fine ..
void serialEvent(Serial myPort) { String inBuffer_str = myPort.readStringUntil('\n'); String[] list = split(inBuffer_str, ","); for (int i=0 ;i<list.length;i=++i) { println ("posiz " + i + ": " + list[i] );} }
Thanks again !!