Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Variables change unpredictable

$
0
0

@midas wrote:

Hello guys, while i have some experience with c++ i am new to of.I have use the ofserial class to communicate with an arduino uno which sends me data with format (a b c \n)
Inside the update function i read the values and i print them. I also have a counter to print the number of the series of data i just received.
The problem is that when i try to close and re-open the serial communication sometimes randomly two of my variables (i & counter )change unpredictably which result in a crash. i tried to declare them as volatile because it changes with a user input but it didn’t work either.

thanks!

void ofApp::printData()
{
	ofLog(OF_LOG_NOTICE, "%d) result:  %f  %f  %f", this->counter, inputs[0], inputs[1], inputs[2]);
}

//--------------------------------------------------------------
void ofApp::setup(){
	mykey = false;
	this->serial_devices = serial.getDeviceList();
	string devie_line;
	counter = 1;
	for (int i = 0;i < this->serial_devices.size(); ++i) {
		devie_line = this->serial_devices[i].getDeviceName();
		ofLog(OF_LOG_NOTICE, ofToString(counter) + ") result: " 
			+ this->serial_devices[i].getDeviceName() + "  "
			+ ofToString(this->serial_devices[i].getDeviceID()) + "  "
			+ this->serial_devices[i].getDevicePath());
	}
	i = 0;
	counter = 0;
	temp = 0;
}

//--------------------------------------------------------------
void ofApp::update(){
	while (mykey) {
		int c = serial.readByte();
		if (c == OF_SERIAL_NO_DATA || c == OF_SERIAL_ERROR || c == 0)
			break;
		if (c == '\n') {
			this->inputs[i] = ofToFloat(str);
			this->printData();
			counter = counter + 1;
			str = "";
			i = 0;
		}
		else if (c == ' ') {
			this->inputs[i] = ofToFloat(str);		
			str = "";
			i++;
		}
		else str.push_back(c);
	}
}

.
.
.

void ofApp::keyPressed(int key){
	if (key == OF_KEY_UP) {
		serial.setup("COM3", 57600); 
		while (!serial.isInitialized()) {
		}
		this->mykey = true;
	}
	if (key == OF_KEY_DOWN) {
		this->mykey = false;
		serial.close();		
		i = 0;
		str = "";
	}
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles