will only return the pointer so an array of unsigned char it will not copy any data or anything.
*holdme gets only the element that the pointer holdme points to.
it would be better to write a method like:
unsigned char holdme[85]; // don't use the pointer sign here!
//--------------------------
void copy_array(unsigned char *arr){
if ( lock() ) {
for(int i = 0; i < 85; i++)
{
arr[i] = holdme[i];
}
unlock(); // don't forget to unlock!
}
}
you’d have to read your bytes like this:
serial.readBytes(&holdme,85);
and copy the bytes in your update method like this:
unsigned char bytesread[85]; // again: no pointer sign
TO.get_array(&bytesread);
this will also use the lock correctly because in your code you were only locking the return of the pointer (maybe not even unlock the mutex again). so copying the bytes would interfere with your thread.
thanx, but now i get a error on compiling about the serial,
D:\ofx\apps\examples\serialExample\src\threadedObject.h|84|error: no matching function for call to `ofSerial::readBytes(unsigned char (*)[85], int)’|
D:\ofx\apps\examples\serialExample\src\testApp.cpp||In member function virtual void testApp::update()':| D:\ofx\apps\examples\serialExample\src\testApp.cpp|63|error: no matching function for call tothreadedObject::get_array(unsigned char (*)[85])’|
D:\ofx\apps\examples\serialExample\src\threadedObject.h|95|note: candidates are: void threadedObject::get_array(unsigned char*)|
D:\ofx\apps\examples\serialExample\src\testApp.cpp||In member function void testApp::AFH(int, int)':| D:\ofx\apps\examples\serialExample\src\testApp.cpp|241|warning: converting toint’ from float'| D:\ofx\apps\examples\serialExample\src\testApp.cpp|258|warning: converting toint’ from `float’|
||=== Build finished: 1 errors, 2 warnings ===|
ok here i the code like it looks like now:
//--------------------------
#ifndef _THREADED_OBJECT
#define _THREADED_OBJECT
#include "ofMain.h"
#define OF_ADDON_USING_OFXTHREAD
#include "ofAddons.h"
// this is not a very exciting example yet
// but ofThread provides the basis for ofNetwork and other
// operations that require threading.
//
// please be careful - threading problems are notoriously hard
// to debug and working with threads can be quite difficult
class threadedObject : public ofxThread{
public:
ofSerial serial;
int count; // threaded fucntions that share data need to use lock (mutex)
// and unlock in order to write to that data
// otherwise it's possible to get crashes.
//
// also no opengl specific stuff will work in a thread...
// threads can't create textures, or draw stuff on the screen
// since opengl is single thread safe
unsigned char holdme[85];
unsigned char bytessend[22];
//--------------------------
threadedObject(){
count = 0;
}
void start(){
bytessend[0]=0x7E ;
bytessend[1]=0x00 ;
bytessend[2]=0x13 ;
bytessend[3]=0x10 ;
bytessend[4]=0x00 ;
bytessend[5]=0x00 ;
bytessend[6]=0x13 ;
bytessend[7]=0xA2 ;
bytessend[8]=0x00 ;
bytessend[9]=0x40 ;
bytessend[10]=0x30 ;
bytessend[11]=0x94 ;
bytessend[12]=0x67 ;
bytessend[13]=0xFF ;
bytessend[14]=0xFE ;
bytessend[15]=0x00 ;
bytessend[16]=0x00 ;
serial.setup("\\\\.\\COM21", 115200);
startThread(true, false); // blocking, verbose
}
void stop(){
stopThread();
}
//--------------------------
void threadedFunction(){
while( isThreadRunning() != 0 ){
if( lock() ){
bytessend[17]=5 ;
bytessend[18]=(unsigned char)ofRandom(0,255) ;
bytessend[19]=(unsigned char)ofRandom(0,255) ;
bytessend[20]=(unsigned char)ofRandom(0,255) ;
bytessend[21]=(unsigned char)ofRandom(0,255) ;
unsigned char sum;
sum = 0xFF - (bytessend[3]+bytessend[4]+bytessend[5]+bytessend[6]+bytessend[7]+bytessend[8]+bytessend[9]+bytessend[10]+bytessend[11]+bytessend[12]+bytessend[13]+bytessend[14]+bytessend[15]+bytessend[16]+bytessend[17]+bytessend[18]+bytessend[19]+bytessend[20]+bytessend[21]) ;
bytessend[22]= sum;
serial.writeBytes(bytessend,23);
ofSleepMillis(5);
if(serial.readByte()==0x7E){
ofSleepMillis(30);
serial.readBytes(holdme,85);
}
unlock();
}
}
}
//--------------------------
void get_array(unsigned char * arr){
if ( lock() ) {
for(int i = 0; i < 85; i++)
{
arr[i] = holdme[i];
}
unlock(); // don't forget to unlock!
}
}
};
#endif