I’m having trouble using serial. I have read tutorials and examples but it seems that I need to know how many bites it has to read… But I can’t know, it changes each time.
On processing I used readString which was very well working but it doesn’t exist on of…
Do you know a method?
Here is my code:
void testApp::setup(){
// Smooth edges
ofEnableSmoothing();
// Fixed framerate
ofSetFrameRate(30);
mySerial.setup("/dev/cu.usbmodem1411", 19200);
}
//--------------------------------------------------------------
void testApp::update(){
// we want to read 8 bytes
int bytesRequired = (77);
unsigned char bytes[bytesRequired];
int bytesRemaining = bytesRequired;
// loop until we've read everything
while ( bytesRemaining > 0 )
{
// check for data
if ( mySerial.available() )
{
// try to read - note offset into the bytes[] array, this is so
// that we don't overwrite the bytes we already have
int bytesArrayOffset = bytesRequired - bytesRemaining;
int result = mySerial.readBytes( &bytes[bytesArrayOffset],bytesRemaining );
printf("bytes %s \n", bytes);
// check for error code
if ( result == OF_SERIAL_ERROR )
{
// something bad happened
ofLog( OF_LOG_ERROR, "unrecoverable error reading from serial" );
// bail out
break;
}
else if ( result == OF_SERIAL_NO_DATA )
{
// nothing was read, try again
}
else
{
// we read some data!
bytesRemaining -= 1;
}
theBall.moveTo(x_fil,y_fil);
}
}
}
I am front of a new problem which is in the length of the message received.
I receive my message through the addons ofxSimpleSerial. I parse it into dataStrings and then parse it into my variables. But I have a problem because when the message (the firs to be read in general) is not full, the code can’t fill the variable and tell me that there is a mistake.
I try to close and/or flush the serial in different location but don’t resolve it.
Ok I understand.
I have put a line break ("\n") at the end of my serial message (in arduino). But I don’t see where I can “say” to the reader “wait for it to read”.
in the code of the read fuction of the addon, its done by default:
// if we find the splitter we put all the buffered messages
// in the final message, stop listening for more data and
// notify a possible listener
// else we just keep filling the buffer with incoming bytes.
so your onNewMessage fuction will be called when the serial receive the “\n” character, then you sould have your complete message and then you can set message = “”, and requestRead = true if you want to read again
send me the ardu file and the of project and i will take a look (i don’t promisse nothing but i can check).
one rapid thing to check is that if the ardu is always sending message is adding a delay after send println…. i see in your connection 38400 bps its fast, maybe the ardu is slower and can not fill those vars that you are sending at that speed.