Quantcast
Channel: bugs, errors - openFrameworks
Viewing all articles
Browse latest Browse all 636

ofSerial not working?

$
0
0

@phoenixperry wrote:

Hey Folks,

I have a bit of code I've been using since Dec and it's no longer able to connect to serial on a PC in Visual Studio 2015 Community Edition. I've never had this problem in the past.

Does someone know why this might be? Is ofSerial got a bug? Note, I've used a string to pass in the port and I also tried a const string just in case. I am at a loss. I don't have problems using my hardware otherwise on this machine.

Here's my code

#include "SerialReader.h"

SerialReader::SerialReader() {

}

void SerialReader::setup() {
    serial = new ofSerial();
    serial->listDevices();

    if(!serial->setup(0, 9600)) {
        ofLogNotice() << "unable to init serial device";
    }
}

int SerialReader::update() {
    int goal = -1;
    bool on;
    if (serial->available()) {
        // get some data here
        unsigned char bytesReturned[7];
        memset(bytesReturned, 0, sizeof(bytesReturned));
        serial->readBytes(bytesReturned, 6);
        cout << bytesReturned <<endl;
        // the messages from the pollen look like
        // 0:ON or 0:OFF or 3:ON etc.
        // ie, GOAL:STATE
        // we take advantage of the fact that there are < 10 goals,
        // so the 0th char gives us an index
        // and we can just use the 3rd char, 'N' or 'F' to
        // figure out the state
        goal = bytesReturned[0] - '0'; //single character in c is a byte. converting a char to an int
        on = (bytesReturned[3] == 'N');
        ofLogNotice() << goal << ":" << on;
    }
    if (on) {
        return goal;
    } else {
        return -1;
    }
    serial->flush();
}

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 636

Trending Articles